diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index b6f375c378..594f7b5e2d 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -104,6 +104,7 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) { e => { if (typeof failureCallback === 'function') { failureCallback(e); + self._decrementPreload(); } else { console.error(e); } @@ -130,6 +131,7 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) { p5._friendlyFileLoadError(0, img.src); if (typeof failureCallback === 'function') { failureCallback(e); + self._decrementPreload(); } else { console.error(e); } @@ -152,6 +154,7 @@ p5.prototype.loadImage = function(path, successCallback, failureCallback) { p5._friendlyFileLoadError(0, path); if (typeof failureCallback === 'function') { failureCallback(e); + self._decrementPreload(); } else { console.error(e); } diff --git a/test/unit/image/loading.js b/test/unit/image/loading.js index b56c578784..4bef74e167 100644 --- a/test/unit/image/loading.js +++ b/test/unit/image/loading.js @@ -221,6 +221,49 @@ suite('loading images', function() { }; }; new p5(mySketch, null, false); + + // Test loading image failure in preload() without failure callback + mySketch = function(this_p5) { + this_p5.preload = function() { + this_p5.loadImage('', function() { + throw new Error('Should not be called'); + }); + }; + + this_p5.setup = function() { + throw new Error('Should not be called'); + }; + }; + new p5(mySketch, null, false); + + // Test loading image failure in preload() with failure callback + mySketch = function(this_p5) { + var myImage; + this_p5.preload = function() { + suite('Test loading image failure in preload() with failure callback', function() { + test('Load fail and use failure callback', function(done) { + myImage = this_p5.loadImage('', function() { + assert.fail(); + done(); + }, function() { + assert.ok(myImage); + done(); + }); + }); + }); + }; + + this_p5.setup = function() { + suite('setup() after preload() failure with failure callback', function() { + test('should be loaded now preload() finished', function(done) { + assert.isTrue(myImage instanceof p5.Image); + assert.isTrue(myImage.width === 1 && myImage.height === 1); + done(); + }); + }); + }; + }; + new p5(mySketch, null, false); }); suite('loading animated gif images', function() {