test_removal_onload.html (3751B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=841579 5 --> 6 <head> 7 <title>Test for Bug 841579</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="/tests/SimpleTest/WindowSnapshot.js"></script> 10 <script type="application/javascript" src="imgutils.js"></script> 11 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 12 </head> 13 <body> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=841579">Mozilla Bug 841579</a> 15 <p id="display"></p> 16 <div id="content"> 17 </div> 18 <pre id="test"> 19 <script type="application/javascript"> 20 /** Test for Bug 841579*/ 21 22 SimpleTest.requestFlakyTimeout("Early failure timeout"); 23 SimpleTest.waitForExplicitFinish(); 24 25 const FAILURE_TIMEOUT = 120000; // Fail early after 120 seconds (2 minutes) 26 27 const Cc = SpecialPowers.Cc; 28 const Ci = SpecialPowers.Ci; 29 const gContent = document.getElementById("content"); 30 31 var gImg; 32 var gMyDecoderObserver; 33 var gIsTestFinished = false; 34 var gFiles; 35 var gNotifications = 0; 36 var gLoads = 0; 37 var gRemovals = 0; 38 var gExpected = 5; 39 40 function* fileToLoad() { 41 yield "red.png"; 42 yield "invalid.jpg"; 43 yield "lime100x100.svg"; 44 yield "bad.jpg"; 45 yield "rillybad.jpg"; 46 } 47 48 function onSizeAvailable() { 49 ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src); 50 } 51 function onLoadComplete() { 52 ok(gExpected > gLoads, "AfterLoad.onLoadComplete called for " + gImg.src); 53 gLoads++; 54 SimpleTest.executeSoon(function() { 55 try { 56 gContent.removeChild(gImg); 57 } 58 catch (e) {} 59 gRemovals++; 60 maybeAdvance(); 61 }); 62 } 63 function onDecodeComplete() { 64 ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src); 65 } 66 67 function failTest() { 68 ok(false, "timing out after " + FAILURE_TIMEOUT + "ms. " + 69 "currently displaying " + gImg.src); 70 cleanUpAndFinish(); 71 } 72 73 function onNotification() 74 { 75 gNotifications++; 76 maybeAdvance(); 77 } 78 79 function maybeAdvance() 80 { 81 if (gRemovals != gNotifications) { 82 return; 83 } 84 85 let {done, value} = gFiles.next(); 86 if (done) { 87 cleanUpAndFinish(); 88 return; 89 } 90 gImg.src = value; 91 gContent.appendChild(gImg); 92 } 93 94 function cleanUpAndFinish() { 95 // On the off chance that failTest and myOnStopFrame are triggered 96 // back-to-back, use a flag to prevent multiple calls to SimpleTest.finish. 97 if (gIsTestFinished) { 98 return; 99 } 100 let imgLoadingContent = SpecialPowers.wrap(gImg); 101 imgLoadingContent.removeObserver(gMyDecoderObserver); 102 // TODO: this isn't the case until post-bug 716140's refactorings 103 // ok(gNotifications == gLoads, "Should be notified the same number of times as loads"); 104 SimpleTest.finish(); 105 gIsTestFinished = true; 106 } 107 108 function main() { 109 gFiles = fileToLoad(); 110 gImg = new Image(); 111 gImg.onload = onNotification; 112 gImg.onerror = onNotification; 113 114 // Create, customize & attach decoder observer 115 var observer = new ImageDecoderObserverStub(); 116 observer.sizeAvailable = onSizeAvailable; 117 observer.loadComplete = onLoadComplete; 118 observer.decodeComplete = onDecodeComplete; 119 gMyDecoderObserver = 120 Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools) 121 .createScriptedObserver(SpecialPowers.wrapCallbackObject(observer)); 122 let imgLoadingContent = SpecialPowers.wrap(gImg); 123 imgLoadingContent.addObserver(gMyDecoderObserver); 124 125 // We want to test the cold loading behavior, so clear cache in case an 126 // earlier test got our image in there already. 127 clearAllImageCaches(); 128 129 // kick off image-loading! myOnStopFrame handles the rest. 130 gImg.setAttribute("src", gFiles.next()); 131 132 // In case something goes wrong, fail earlier than mochitest timeout, 133 // and with more information. 134 setTimeout(failTest, FAILURE_TIMEOUT); 135 } 136 137 window.onload = main; 138 139 </script> 140 </pre> 141 </body> 142 </html>