PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/image/test/browser/browser_image.js

http://github.com/zpao/v8monkey
JavaScript | 193 lines | 147 code | 24 blank | 22 comment | 20 complexity | c3d334627a4a6a777e9d472bb4205a00 MD5 | raw file
  1waitForExplicitFinish();
  2requestLongerTimeout(2); // see bug 660123 -- this test is slow on Mac.
  3
  4// A hold on the current timer, so it doens't get GCed out from
  5// under us
  6var gTimer;
  7
  8// Browsing to a new URL - pushing us into the bfcache - should cause
  9// animations to stop, and resume when we return
 10function testBFCache() {
 11  function theTest() {
 12    var abort = false;
 13    var chances, gImage, gFrames;
 14    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
 15    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
 16      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
 17      var window = gBrowser.contentWindow;
 18      // If false, we are in an optimized build, and we abort this and
 19      // all further tests
 20      if (!actOnMozImage(window.document, "img1", function(image) {
 21        gImage = image;
 22        gFrames = gImage.framesNotified;
 23      })) {
 24        gBrowser.removeCurrentTab();
 25        abort = true;
 26      }
 27      goer.next();
 28    }, true);
 29    yield;
 30    if (abort) {
 31      finish();
 32      yield; // optimized build
 33    }
 34
 35    // Let animation run for a bit
 36    chances = 120;
 37    do {
 38      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 39      gTimer.initWithCallback(function() {
 40        if (gImage.framesNotified >= 20) {
 41          goer.send(true);
 42        } else {
 43          chances--;
 44          goer.send(chances == 0); // maybe if we wait a bit, it will happen
 45        }
 46      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
 47    } while (!(yield));
 48    is(chances > 0, true, "Must have animated a few frames so far");
 49
 50    // Browse elsewhere; push our animating page into the bfcache
 51    gBrowser.loadURI("about:blank");
 52
 53    // Wait a bit for page to fully load, then wait a while and
 54    // see that no animation occurs.
 55    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 56    gTimer.initWithCallback(function() {
 57      gFrames = gImage.framesNotified;
 58      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 59      gTimer.initWithCallback(function() {
 60        // Might have a few stray frames, until other page totally loads
 61        is(gImage.framesNotified == gFrames, true, "Must have not animated in bfcache!");
 62        goer.next();
 63      }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); // 4 seconds - expect 40 frames
 64    }, 0, Ci.nsITimer.TYPE_ONE_SHOT); // delay of 0 - wait for next event loop
 65    yield;
 66
 67    // Go back
 68    gBrowser.goBack();
 69
 70    chances = 120;
 71    do {
 72      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 73      gTimer.initWithCallback(function() {
 74        if (gImage.framesNotified - gFrames >= 20) {
 75          goer.send(true);
 76        } else {
 77          chances--;
 78          goer.send(chances == 0); // maybe if we wait a bit, it will happen
 79        }
 80      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
 81    } while (!(yield));
 82    is(chances > 0, true, "Must have animated once out of bfcache!");
 83
 84    // Finally, check that the css background image has essentially the same
 85    // # of frames, implying that it animated at the same times as the regular
 86    // image. We can easily retrieve regular images through their HTML image
 87    // elements, which is what we did before. For the background image, we
 88    // create a regular image now, and read the current frame count.
 89    var doc = gBrowser.selectedBrowser.contentWindow.document;
 90    var div = doc.getElementById("background_div");
 91    div.innerHTML += '<img src="animated2.gif" id="img3">';
 92    actOnMozImage(doc, "img3", function(image) {
 93      is(Math.abs(image.framesNotified - gImage.framesNotified)/gImage.framesNotified < 0.5, true,
 94         "Must have also animated the background image, and essentially the same # of frames");
 95    });
 96
 97    gBrowser.removeCurrentTab();
 98
 99    nextTest();
100  }
101
102  var goer = theTest();
103  goer.next();
104}
105
106// Check that imgContainers are shared on the same page and
107// between tabs
108function testSharedContainers() {
109  function theTest() {
110    var gImages = [];
111    var gFrames;
112
113    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
114    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
115      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
116      actOnMozImage(gBrowser.contentDocument, "img1", function(image) {
117        gImages[0] = image;
118        gFrames = image.framesNotified; // May in theory have frames from last test
119                                        // in this counter - so subtract them out
120      });
121      goer.next();
122    }, true);
123    yield;
124
125    // Load next tab somewhat later
126    gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
127    gTimer.initWithCallback(function() {
128      goer.next();
129    }, 1500, Ci.nsITimer.TYPE_ONE_SHOT);
130    yield;
131
132    gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "imageX2.html");
133    gBrowser.selectedBrowser.addEventListener("pageshow", function () {
134      gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
135      [1,2].forEach(function(i) {
136        actOnMozImage(gBrowser.contentDocument, "img"+i, function(image) {
137          gImages[i] = image;
138        });
139      });
140      goer.next();
141    }, true);
142    yield;
143
144    var chances = 120;
145    do {
146      gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
147      gTimer.initWithCallback(function() {
148        if (gImages[0].framesNotified - gFrames >= 10) {
149          goer.send(true);
150        } else {
151          chances--;
152          goer.send(chances == 0); // maybe if we wait a bit, it will happen
153        }
154      }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
155    } while (!(yield));
156    is(chances > 0, true, "Must have been animating while showing several images");
157
158    // Check they all have the same frame counts
159    var theFrames = null;
160    [0,1,2].forEach(function(i) {
161      var frames = gImages[i].framesNotified;
162      if (theFrames == null) {
163        theFrames = frames;
164      } else {
165        is(theFrames, frames, "Sharing the same imgContainer means *exactly* the same frame counts!");
166      }
167    });
168
169    gBrowser.removeCurrentTab();
170    gBrowser.removeCurrentTab();
171
172    nextTest();
173  }
174
175  var goer = theTest();
176  goer.next();
177}
178
179var tests = [testBFCache, testSharedContainers];
180
181function nextTest() {
182  if (tests.length == 0) {
183    finish();
184    return;
185  }
186  tests.shift()();
187}
188
189function test() {
190  ignoreAllUncaughtExceptions();
191  nextTest();
192}
193