image/test/browser/browser_image.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/test/browser/browser_image.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +waitForExplicitFinish();
     1.5 +requestLongerTimeout(2); // see bug 660123 -- this test is slow on Mac.
     1.6 +
     1.7 +// A hold on the current timer, so it doens't get GCed out from
     1.8 +// under us
     1.9 +var gTimer;
    1.10 +
    1.11 +// Browsing to a new URL - pushing us into the bfcache - should cause
    1.12 +// animations to stop, and resume when we return
    1.13 +function testBFCache() {
    1.14 +  function theTest() {
    1.15 +    var abort = false;
    1.16 +    var chances, gImage, gFrames;
    1.17 +    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
    1.18 +    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
    1.19 +      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
    1.20 +      var window = gBrowser.contentWindow;
    1.21 +      // If false, we are in an optimized build, and we abort this and
    1.22 +      // all further tests
    1.23 +      if (!actOnMozImage(window.document, "img1", function(image) {
    1.24 +        gImage = image;
    1.25 +        gFrames = gImage.framesNotified;
    1.26 +      })) {
    1.27 +        gBrowser.removeCurrentTab();
    1.28 +        abort = true;
    1.29 +      }
    1.30 +      goer.next();
    1.31 +    }, true);
    1.32 +    yield;
    1.33 +    if (abort) {
    1.34 +      finish();
    1.35 +      yield; // optimized build
    1.36 +    }
    1.37 +
    1.38 +    // Let animation run for a bit
    1.39 +    chances = 120;
    1.40 +    do {
    1.41 +      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.42 +      gTimer.initWithCallback(function() {
    1.43 +        if (gImage.framesNotified >= 20) {
    1.44 +          goer.send(true);
    1.45 +        } else {
    1.46 +          chances--;
    1.47 +          goer.send(chances == 0); // maybe if we wait a bit, it will happen
    1.48 +        }
    1.49 +      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    1.50 +    } while (!(yield));
    1.51 +    is(chances > 0, true, "Must have animated a few frames so far");
    1.52 +
    1.53 +    // Browse elsewhere; push our animating page into the bfcache
    1.54 +    gBrowser.loadURI("about:blank");
    1.55 +
    1.56 +    // Wait a bit for page to fully load, then wait a while and
    1.57 +    // see that no animation occurs.
    1.58 +    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.59 +    gTimer.initWithCallback(function() {
    1.60 +      gFrames = gImage.framesNotified;
    1.61 +      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.62 +      gTimer.initWithCallback(function() {
    1.63 +        // Might have a few stray frames, until other page totally loads
    1.64 +        var additionalFrames = gImage.framesNotified  - gFrames;
    1.65 +        is(additionalFrames == 0, true, "Must have not animated in bfcache! Got " + additionalFrames + " additional frames");
    1.66 +        goer.next();
    1.67 +      }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); // 4 seconds - expect 40 frames
    1.68 +    }, 0, Ci.nsITimer.TYPE_ONE_SHOT); // delay of 0 - wait for next event loop
    1.69 +    yield;
    1.70 +
    1.71 +    // Go back
    1.72 +    gBrowser.goBack();
    1.73 +
    1.74 +    chances = 120;
    1.75 +    do {
    1.76 +      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.77 +      gTimer.initWithCallback(function() {
    1.78 +        if (gImage.framesNotified - gFrames >= 20) {
    1.79 +          goer.send(true);
    1.80 +        } else {
    1.81 +          chances--;
    1.82 +          goer.send(chances == 0); // maybe if we wait a bit, it will happen
    1.83 +        }
    1.84 +      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
    1.85 +    } while (!(yield));
    1.86 +    is(chances > 0, true, "Must have animated once out of bfcache!");
    1.87 +
    1.88 +    // Finally, check that the css background image has essentially the same
    1.89 +    // # of frames, implying that it animated at the same times as the regular
    1.90 +    // image. We can easily retrieve regular images through their HTML image
    1.91 +    // elements, which is what we did before. For the background image, we
    1.92 +    // create a regular image now, and read the current frame count.
    1.93 +    var doc = gBrowser.selectedBrowser.contentWindow.document;
    1.94 +    var div = doc.getElementById("background_div");
    1.95 +    div.innerHTML += '<img src="animated2.gif" id="img3">';
    1.96 +    actOnMozImage(doc, "img3", function(image) {
    1.97 +      is(Math.abs(image.framesNotified - gImage.framesNotified)/gImage.framesNotified < 0.5, true,
    1.98 +         "Must have also animated the background image, and essentially the same # of frames. " +
    1.99 +         "Regular image got " + gImage.framesNotified + " frames but background image got " + image.framesNotified);
   1.100 +    });
   1.101 +
   1.102 +    gBrowser.removeCurrentTab();
   1.103 +
   1.104 +    nextTest();
   1.105 +  }
   1.106 +
   1.107 +  var goer = theTest();
   1.108 +  goer.next();
   1.109 +}
   1.110 +
   1.111 +// Check that imgContainers are shared on the same page and
   1.112 +// between tabs
   1.113 +function testSharedContainers() {
   1.114 +  function theTest() {
   1.115 +    var gImages = [];
   1.116 +    var gFrames;
   1.117 +
   1.118 +    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
   1.119 +    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
   1.120 +      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
   1.121 +      actOnMozImage(gBrowser.contentDocument, "img1", function(image) {
   1.122 +        gImages[0] = image;
   1.123 +        gFrames = image.framesNotified; // May in theory have frames from last test
   1.124 +                                        // in this counter - so subtract them out
   1.125 +      });
   1.126 +      goer.next();
   1.127 +    }, true);
   1.128 +    yield;
   1.129 +
   1.130 +    // Load next tab somewhat later
   1.131 +    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
   1.132 +    gTimer.initWithCallback(function() {
   1.133 +      goer.next();
   1.134 +    }, 1500, Ci.nsITimer.TYPE_ONE_SHOT);
   1.135 +    yield;
   1.136 +
   1.137 +    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "imageX2.html");
   1.138 +    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
   1.139 +      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
   1.140 +      [1,2].forEach(function(i) {
   1.141 +        actOnMozImage(gBrowser.contentDocument, "img"+i, function(image) {
   1.142 +          gImages[i] = image;
   1.143 +        });
   1.144 +      });
   1.145 +      goer.next();
   1.146 +    }, true);
   1.147 +    yield;
   1.148 +
   1.149 +    var chances = 120;
   1.150 +    do {
   1.151 +      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
   1.152 +      gTimer.initWithCallback(function() {
   1.153 +        if (gImages[0].framesNotified - gFrames >= 10) {
   1.154 +          goer.send(true);
   1.155 +        } else {
   1.156 +          chances--;
   1.157 +          goer.send(chances == 0); // maybe if we wait a bit, it will happen
   1.158 +        }
   1.159 +      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
   1.160 +    } while (!(yield));
   1.161 +    is(chances > 0, true, "Must have been animating while showing several images");
   1.162 +
   1.163 +    // Check they all have the same frame counts
   1.164 +    var theFrames = null;
   1.165 +    [0,1,2].forEach(function(i) {
   1.166 +      var frames = gImages[i].framesNotified;
   1.167 +      if (theFrames == null) {
   1.168 +        theFrames = frames;
   1.169 +      } else {
   1.170 +        is(theFrames, frames, "Sharing the same imgContainer means *exactly* the same frame counts!");
   1.171 +      }
   1.172 +    });
   1.173 +
   1.174 +    gBrowser.removeCurrentTab();
   1.175 +    gBrowser.removeCurrentTab();
   1.176 +
   1.177 +    nextTest();
   1.178 +  }
   1.179 +
   1.180 +  var goer = theTest();
   1.181 +  goer.next();
   1.182 +}
   1.183 +
   1.184 +var tests = [testBFCache, testSharedContainers];
   1.185 +
   1.186 +function nextTest() {
   1.187 +  if (tests.length == 0) {
   1.188 +    finish();
   1.189 +    return;
   1.190 +  }
   1.191 +  tests.shift()();
   1.192 +}
   1.193 +
   1.194 +function test() {
   1.195 +  ignoreAllUncaughtExceptions();
   1.196 +  nextTest();
   1.197 +}
   1.198 +

mercurial