Bug 1182545 - Make browser_bug666317.js wait for the discard event instead of expecting it at a precise time. r=tn
authorSeth Fowler <mark.seth.fowler@gmail.com>
Fri, 10 Jul 2015 16:37:03 -0700
changeset 278011 07bcf36f5ab25e4cc58f33e9732246c252762dcd
parent 278010 567ba16066941fb41d14a68dcb077125f5df91d3
child 278012 0fc4dec6cd81c056e05b3fd2661f5ef80ef2f0b0
push id3422
push usercykesiopka.bmo@gmail.com
push dateSun, 12 Jul 2015 16:26:48 +0000
reviewerstn
bugs1182545, 666317
milestone42.0a1
Bug 1182545 - Make browser_bug666317.js wait for the discard event instead of expecting it at a precise time. r=tn
image/test/browser/browser_bug666317.js
--- a/image/test/browser/browser_bug666317.js
+++ b/image/test/browser/browser_bug666317.js
@@ -5,21 +5,29 @@ let pageSource =
     '<img id="testImg" src="' + TESTROOT + 'big.png">' +
   '</body></html>';
 
 let oldDiscardingPref, oldTab, newTab;
 let prefBranch = Cc["@mozilla.org/preferences-service;1"]
                    .getService(Ci.nsIPrefService)
                    .getBranch('image.mem.');
 
-function ImageDiscardObserver(result) {
+var gWaitingForDiscard = false;
+var gScriptedObserver;
+var gClonedRequest;
+
+function ImageDiscardObserver(callback) {
   this.discard = function onDiscard(request)
   {
-    result.wasDiscarded = true;
+    if (!gWaitingForDiscard) {
+      return;
+    }
+
     this.synchronous = false;
+    callback();
   }
 
   this.synchronous = true;
 }
 
 function currentRequest() {
   let img = gBrowser.getBrowserForTab(newTab).contentWindow
             .document.getElementById('testImg');
@@ -65,57 +73,63 @@ function test() {
 
   // Create and focus a new tab.
   oldTab = gBrowser.selectedTab;
   newTab = gBrowser.addTab('data:text/html,' + pageSource);
   gBrowser.selectedTab = newTab;
 
   // Run step2 after the tab loads.
   gBrowser.getBrowserForTab(newTab)
-          .addEventListener("pageshow", step2 );
+          .addEventListener("pageshow", step2);
 }
 
 function step2() {
-  // Create a place to hold the result.
-  var result = { wasDiscarded: false };
-
   // Create the discard observer.
-  var observer = new ImageDiscardObserver(result);
-  var scriptedObserver = Cc["@mozilla.org/image/tools;1"]
-                           .getService(Ci.imgITools)
-                           .createScriptedObserver(observer);
+  var observer = new ImageDiscardObserver(() => runAfterAsyncEvents(step5));
+  gScriptedObserver = Cc["@mozilla.org/image/tools;1"]
+                        .getService(Ci.imgITools)
+                        .createScriptedObserver(observer);
 
   // Clone the current imgIRequest with our new observer.
   var request = currentRequest();
-  var clonedRequest = request.clone(scriptedObserver);
+  gClonedRequest = request.clone(gScriptedObserver);
 
   // Check that the image is decoded.
   forceDecodeImg();
 
   // The FRAME_COMPLETE notification is delivered asynchronously, so continue
   // after we're sure it has been delivered.
-  runAfterAsyncEvents(() => step3(result, scriptedObserver, clonedRequest));
+  runAfterAsyncEvents(step3);
 }
 
-function step3(result, scriptedObserver, clonedRequest) {
+function step3() {
   ok(isImgDecoded(), 'Image should initially be decoded.');
 
   // Focus the old tab, then fire a memory-pressure notification.  This should
   // cause the decoded image in the new tab to be discarded.
   gBrowser.selectedTab = oldTab;
+
+  // Allow time to process the tab change.
+  runAfterAsyncEvents(step4);
+}
+
+function step4() {
+  gWaitingForDiscard = true;
+
   var os = Cc["@mozilla.org/observer-service;1"]
              .getService(Ci.nsIObserverService);
   os.notifyObservers(null, 'memory-pressure', 'heap-minimize');
 
-  // The DISCARD notification is delivered asynchronously, so continue after
-  // we're sure it has been delivered.
-  runAfterAsyncEvents(() => step4(result, scriptedObserver, clonedRequest));
+  // The DISCARD notification is delivered asynchronously. ImageDiscardObserver
+  // will eventually call step5. (Or else, sadly, the test will time out.)
 }
 
-function step4(result, scriptedObserver, clonedRequest) {
-  ok(result.wasDiscarded, 'Image should be discarded.');
+function step5() {
+  ok(true, 'Image should be discarded.');
 
   // And we're done.
   gBrowser.removeTab(newTab);
   prefBranch.setBoolPref('discardable', oldDiscardingPref);
-  clonedRequest.cancelAndForgetObserver(0);
+
+  gClonedRequest.cancelAndForgetObserver(0);
+
   finish();
 }