<!DOCTYPE HTML><html><!--https://bugzilla.mozilla.org/show_bug.cgi?id=841579--><head><title>Test for Bug 841579</title><scriptsrc="/tests/SimpleTest/SimpleTest.js"></script><scriptsrc="/tests/SimpleTest/WindowSnapshot.js"></script><scripttype="application/javascript"src="imgutils.js"></script><linkrel="stylesheet"type="text/css"href="/tests/SimpleTest/test.css"/></head><body><atarget="_blank"href="https://bugzilla.mozilla.org/show_bug.cgi?id=841579">Mozilla Bug 841579</a><pid="display"></p><divid="content"></div><preid="test"><scripttype="application/javascript">/** Test for Bug 841579**/SimpleTest.requestFlakyTimeout("Early failure timeout");SimpleTest.waitForExplicitFinish();constFAILURE_TIMEOUT=120000;// Fail early after 120 seconds (2 minutes)constCc=SpecialPowers.Cc;constCi=SpecialPowers.Ci;constgContent=document.getElementById("content");vargImg;vargMyDecoderObserver;vargIsTestFinished=false;vargFiles;vargNotifications=0;vargLoads=0;vargRemovals=0;vargExpected=5;function*fileToLoad(){yield"red.png";yield"invalid.jpg";yield"lime100x100.svg";yield"bad.jpg";yield"rillybad.jpg";}functiononSizeAvailable(){ok(true,"AfterLoad.onSizeAvailable called for "+gImg.src);}functiononLoadComplete(aRequest){ok(gExpected>gLoads,"AfterLoad.onLoadComplete called for "+gImg.src);gLoads++;// We aren't going to get a decode complete event if the metadata decoding// failed (i.e. for invalid.jpg). By definition we should have the size or// an error once we get a load complete event, so check if the size is valid// and if not, trigger a decode complete event manually.varhasSize=false;try{hasSize=aRequest.image.width>0&&aRequest.image.height>0;}catch(e){}if(hasSize){maybeAdvance();}else{onDecodeComplete(aRequest);}}functiononDecodeComplete(){ok(gExpected>gRemovals,"AfterLoad.onDecodeComplete called for "+gImg.src);SimpleTest.executeSoon(function(){try{gContent.removeChild(gImg);}catch(e){}gRemovals++;maybeAdvance();});}functionfailTest(){ok(false,"timing out after "+FAILURE_TIMEOUT+"ms. "+"currently displaying "+gImg.src);cleanUpAndFinish();}functiononNotification(){ok(gExpected>gNotifications,"AfterLoad.onNotification called for "+gImg.src);gNotifications++;maybeAdvance();}functionmaybeAdvance(){if(gNotifications!=gLoads||gNotifications!=gRemovals){return;}let{done,value}=gFiles.next();if(done){cleanUpAndFinish();return;}gImg.src=value;gContent.appendChild(gImg);}functioncleanUpAndFinish(){// On the off chance that failTest and myOnStopFrame are triggered// back-to-back, use a flag to prevent multiple calls to SimpleTest.finish.if(gIsTestFinished){return;}letimgLoadingContent=SpecialPowers.wrap(gImg);imgLoadingContent.removeObserver(gMyDecoderObserver);// TODO - this isn't the case until post-bug 716140's refactorings// ok(gNotifications == gLoads, "Should be notified the same number of times as loads");SimpleTest.finish();gIsTestFinished=true;}functionmain(){gFiles=fileToLoad();gImg=newImage();gImg.onload=onNotification;gImg.onerror=onNotification;// Create, customize & attach decoder observervarobserver=newImageDecoderObserverStub();observer.sizeAvailable=onSizeAvailable;observer.loadComplete=onLoadComplete;observer.decodeComplete=onDecodeComplete;gMyDecoderObserver=Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools).createScriptedObserver(SpecialPowers.wrapCallbackObject(observer));letimgLoadingContent=SpecialPowers.wrap(gImg);imgLoadingContent.addObserver(gMyDecoderObserver);// We want to test the cold loading behavior, so clear cache in case an// earlier test got our image in there already.clearAllImageCaches();// kick off image-loading! myOnStopFrame handles the rest.gImg.setAttribute("src",gFiles.next().value);gContent.appendChild(gImg);// In case something goes wrong, fail earlier than mochitest timeout,// and with more information.setTimeout(failTest,FAILURE_TIMEOUT);}window.onload=main;</script></pre></body></html>