From 0b991cefdd3ad79484e2a103c5850416d66da484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 27 Jan 2016 10:45:39 +0100 Subject: [PATCH] Only mark an image as loaded if it actually has src set and is complete. --- src/item/Raster.js | 3 +- test/js/helpers.js | 82 ++++++++++++++++++++++---------------------- test/tests/Raster.js | 14 ++++---- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/src/item/Raster.js b/src/item/Raster.js index 3f3e90d7..1070310a 100644 --- a/src/item/Raster.js +++ b/src/item/Raster.js @@ -245,7 +245,6 @@ var Raster = Item.extend(/** @lends Raster# */{ // Trigger the load event on the image once it's loaded DomEvent.add(image, { load: function(event) { - that._loaded = true; that._setImage(image); emit(event); }, @@ -272,7 +271,7 @@ var Raster = Item.extend(/** @lends Raster# */{ // A Image object this._image = image; this._canvas = null; - this._loaded = image && image.complete; + this._loaded = !!(image && image.src && image.complete); } // Both canvas and image have width / height attributes. Due to IE, // naturalWidth / Height needs to be checked for a swell, because it diff --git a/test/js/helpers.js b/test/js/helpers.js index 488a3402..181ecf93 100644 --- a/test/js/helpers.js +++ b/test/js/helpers.js @@ -37,6 +37,47 @@ console.error = function() { errorHandler.apply(this, arguments); }; +// Override equals to convert functions to message and execute them as tests() +function equals(actual, expected, message, options) { + // Allow the use of functions for actual, which will get called and their + // source content extracted for readable reports. + if (typeof actual === 'function') { + if (!message) + message = getFunctionMessage(actual); + actual = actual(); + } + // Get the comparator based on the expected value's type only and ignore the + // actual value's type. + var type = typeof expected, + cls; + type = expected === null && 'Null' + || type === 'number' && 'Number' + || type === 'boolean' && 'Boolean' + || type === 'undefined' && 'Undefined' + || Array.isArray(expected) && 'Array' + || expected instanceof Element && 'Element' // handle DOM Elements + || (cls = expected && expected._class) // check _class 2nd last + || type === 'object' && 'Object'; // Object as catch-all + var comparator = type && comparators[type]; + if (!message) + message = type ? type.toLowerCase() : 'value'; + if (comparator) { + comparator(actual, expected, message, options); + } else if (expected && expected.equals) { + // Fall back to equals + QUnit.push(expected.equals(actual), actual, expected, message); + } else { + // Finally perform a strict compare + QUnit.push(actual === expected, actual, expected, message); + } + if (options && options.cloned && cls) { + var identical = identicalAfterCloning[cls]; + QUnit.push(identical ? actual === expected : actual !== expected, + actual, identical ? expected : 'not ' + expected, + message + ': identical after cloning'); + } +} + // Register a jsDump parser for Base. QUnit.jsDump.setParser('Base', function (obj, stack) { // Just compare the string representation of classes inheriting from Base, @@ -338,47 +379,6 @@ function getFunctionMessage(func) { return message; } -// Override equals to convert functions to message and execute them as tests() -function equals(actual, expected, message, options) { - // Allow the use of functions for actual, which will get called and their - // source content extracted for readable reports. - if (typeof actual === 'function') { - if (!message) - message = getFunctionMessage(actual); - actual = actual(); - } - // Get the comparator based on the expected value's type only and ignore the - // actual value's type. - var type = typeof expected, - cls; - type = expected === null && 'Null' - || type === 'number' && 'Number' - || type === 'boolean' && 'Boolean' - || type === 'undefined' && 'Undefined' - || Array.isArray(expected) && 'Array' - || expected instanceof Element && 'Element' // handle DOM Elements - || (cls = expected && expected._class) // check _class 2nd last - || type === 'object' && 'Object'; // Object as catch-all - var comparator = type && comparators[type]; - if (!message) - message = type ? type.toLowerCase() : 'value'; - if (comparator) { - comparator(actual, expected, message, options); - } else if (expected && expected.equals) { - // Fall back to equals - QUnit.push(expected.equals(actual), actual, expected, message); - } else { - // Finally perform a strict compare - QUnit.push(actual === expected, actual, expected, message); - } - if (options && options.cloned && cls) { - var identical = identicalAfterCloning[cls]; - QUnit.push(identical ? actual === expected : actual !== expected, - actual, identical ? expected : 'not ' + expected, - message + ': identical after cloning'); - } -} - function test(testName, expected) { return QUnit.test(testName, function() { var project = new Project(); diff --git a/test/tests/Raster.js b/test/tests/Raster.js index 34b668cc..5cdeaa71 100644 --- a/test/tests/Raster.js +++ b/test/tests/Raster.js @@ -14,19 +14,19 @@ module('Raster'); test('Create a raster without a source and check its size', function() { var raster = new Raster(); - equals(raster.size.toString(), new Size(0, 0).toString(), true); + equals(raster.size, new Size(0, 0), true); }); test('Create a raster without a source and set its size', function() { var raster = new Raster(); raster.size = [640, 480]; - equals(raster.size.toString(), new Size(640, 480).toString(), true); + equals(raster.size, new Size(640, 480), true); }); asyncTest('Create a raster from a url', function(callback) { var raster = new Raster('assets/paper-js.gif'); raster.onLoad = function() { - equals(raster.size.toString(), new Size(146, 146).toString(), true); + equals(raster.size, new Size(146, 146), true); callback(); }; }); @@ -34,7 +34,7 @@ asyncTest('Create a raster from a url', function(callback) { asyncTest('Create a raster from a data url', function(callback) { var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII='); raster.onLoad = function() { - equals(raster.size.toString(), new Size(2, 2).toString(), true); + equals(raster.size, new Size(2, 2), true); callback(); }; }); @@ -46,7 +46,7 @@ asyncTest('Create a raster from a dom image', function(callback) { DomEvent.add(img, { load: function() { var raster = new Raster(img); - equals(raster.size.toString(), new Size(146, 146).toString(), true); + equals(raster.size, new Size(146, 146), true); document.body.removeChild(img); callback(); } @@ -56,7 +56,7 @@ asyncTest('Create a raster from a dom image', function(callback) { test('Create a raster from a canvas', function(callback) { var canvas = CanvasProvider.getCanvas(30, 20); var raster = new Raster(canvas); - equals(raster.size.toString(), new Size(30, 20).toString(), true); + equals(raster.size, new Size(30, 20), true); CanvasProvider.release(canvas); }); @@ -68,7 +68,7 @@ asyncTest('Create a raster from a dom id', function(callback) { DomEvent.add(img, { load: function() { var raster = new Raster('testimage'); - equals(raster.size.toString(), new Size(146, 146).toString(), true); + equals(raster.size, new Size(146, 146), true); document.body.removeChild(img); callback(); }