Bug 1359833 - Part 9. Fix image mochitests to not assume a particular order of events. r=tnikkel
authorAndrew Osmond <aosmond@mozilla.com>
Wed, 19 Jul 2017 14:15:12 -0400
changeset 418442 b44a7fd7a4175d0e705245ec74267e2d16d8cc6f
parent 418441 b63d906a88b55c62d0fe3eafc09a5a0c1da3bc03
child 418443 09deb3ef1f63354a1d4716826c4ccbec1d730db4
push id7566
push usermtabara@mozilla.com
push dateWed, 02 Aug 2017 08:25:16 +0000
treeherdermozilla-beta@86913f512c3c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstnikkel
bugs1359833
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1359833 - Part 9. Fix image mochitests to not assume a particular order of events. r=tnikkel
image/test/mochitest/test_removal_ondecode.html
image/test/mochitest/test_removal_onload.html
--- a/image/test/mochitest/test_removal_ondecode.html
+++ b/image/test/mochitest/test_removal_ondecode.html
@@ -29,51 +29,81 @@ const Ci = SpecialPowers.Ci;
 const gContent = document.getElementById("content");
 
 var gImg;
 var gMyDecoderObserver;
 var gIsTestFinished = false;
 var gFiles;
 var gNotifications = 0;
 var gLoads = 0;
+var gRemovals = 0;
+var gExpected = 5;
 
 function* fileToLoad() {
   yield "red.png";
   yield "invalid.jpg";
   yield "lime100x100.svg";
   yield "bad.jpg";
   yield "rillybad.jpg";
 }
 
 function onSizeAvailable(aRequest) {
   ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src);
 }
 function onLoadComplete(aRequest) {
-  ok(true, "AfterLoad.onLoadComplete called for " + gImg.src);
+  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.
+  var hasSize = false;
+  try {
+    hasSize = aRequest.image.width > 0 && aRequest.image.height > 0;
+  } catch(e) {}
+
+  if (hasSize) {
+    maybeAdvance();
+  } else {
+    onDecodeComplete(aRequest);
+  }
 }
+
 function onDecodeComplete(aRequest) {
-  ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src);
+  ok(gExpected > gRemovals, "AfterLoad.onDecodeComplete called for " + gImg.src);
   SimpleTest.executeSoon(function() {
     try {
       gContent.removeChild(gImg);
     } 
     catch (e) {} 
+    gRemovals++;
+    maybeAdvance();
   });
 }
 
 function failTest() {
   ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  " +
             "currently displaying " + gImg.src);
   cleanUpAndFinish();
 }
 
 function onNotification()
 {
+  ok(gExpected > gNotifications, "AfterLoad.onNotification called for " + gImg.src);
   gNotifications++;
+  maybeAdvance();
+}
+
+function maybeAdvance()
+{
+  if (gNotifications != gLoads || gNotifications != gRemovals) {
+    return;
+  }
+
   let {done, value} = gFiles.next();
   if (done) {
     cleanUpAndFinish();
     return;
   }
   gImg.src = value;
   gContent.appendChild(gImg);
 }
@@ -109,17 +139,18 @@ function main() {
   let imgLoadingContent = SpecialPowers.wrap(gImg).QueryInterface(Ci.nsIImageLoadingContent);
   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());
+  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;
 
--- a/image/test/mochitest/test_removal_onload.html
+++ b/image/test/mochitest/test_removal_onload.html
@@ -29,51 +29,64 @@ const Ci = SpecialPowers.Ci;
 const gContent = document.getElementById("content");
 
 var gImg;
 var gMyDecoderObserver;
 var gIsTestFinished = false;
 var gFiles;
 var gNotifications = 0;
 var gLoads = 0;
+var gRemovals = 0;
+var gExpected = 5;
 
 function* fileToLoad() {
   yield "red.png";
   yield "invalid.jpg";
   yield "lime100x100.svg";
   yield "bad.jpg";
   yield "rillybad.jpg";
 }
 
 function onSizeAvailable(aRequest) {
   ok(true, "AfterLoad.onSizeAvailable called for " + gImg.src);
 }
 function onLoadComplete(aRequest) {
-  ok(true, "AfterLoad.onLoadComplete called for " + gImg.src);
+  ok(gExpected > gLoads, "AfterLoad.onLoadComplete called for " + gImg.src);
   gLoads++;
   SimpleTest.executeSoon(function() {
     try {
       gContent.removeChild(gImg);
     } 
-    catch (e) {} 
+    catch (e) {}
+    gRemovals++;
+    maybeAdvance();
   });
 }
 function onDecodeComplete(aRequest) {
   ok(true, "AfterLoad.onDecodeComplete called for " + gImg.src);
 }
 
 function failTest() {
   ok(false, "timing out after " + FAILURE_TIMEOUT + "ms.  " +
             "currently displaying " + gImg.src);
   cleanUpAndFinish();
 }
 
 function onNotification()
 {
   gNotifications++;
+  maybeAdvance();
+}
+
+function maybeAdvance()
+{
+  if (gRemovals != gNotifications) {
+    return;
+  }
+
   let {done, value} = gFiles.next();
   if (done) {
     cleanUpAndFinish();
     return;
   }
   gImg.src = value;
   gContent.appendChild(gImg);
 }