michael@0: waitForExplicitFinish(); michael@0: requestLongerTimeout(2); // see bug 660123 -- this test is slow on Mac. michael@0: michael@0: // A hold on the current timer, so it doens't get GCed out from michael@0: // under us michael@0: var gTimer; michael@0: michael@0: // Browsing to a new URL - pushing us into the bfcache - should cause michael@0: // animations to stop, and resume when we return michael@0: function testBFCache() { michael@0: function theTest() { michael@0: var abort = false; michael@0: var chances, gImage, gFrames; michael@0: gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html"); michael@0: gBrowser.selectedBrowser.addEventListener("pageshow", function () { michael@0: gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true); michael@0: var window = gBrowser.contentWindow; michael@0: // If false, we are in an optimized build, and we abort this and michael@0: // all further tests michael@0: if (!actOnMozImage(window.document, "img1", function(image) { michael@0: gImage = image; michael@0: gFrames = gImage.framesNotified; michael@0: })) { michael@0: gBrowser.removeCurrentTab(); michael@0: abort = true; michael@0: } michael@0: goer.next(); michael@0: }, true); michael@0: yield; michael@0: if (abort) { michael@0: finish(); michael@0: yield; // optimized build michael@0: } michael@0: michael@0: // Let animation run for a bit michael@0: chances = 120; michael@0: do { michael@0: gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: gTimer.initWithCallback(function() { michael@0: if (gImage.framesNotified >= 20) { michael@0: goer.send(true); michael@0: } else { michael@0: chances--; michael@0: goer.send(chances == 0); // maybe if we wait a bit, it will happen michael@0: } michael@0: }, 500, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: } while (!(yield)); michael@0: is(chances > 0, true, "Must have animated a few frames so far"); michael@0: michael@0: // Browse elsewhere; push our animating page into the bfcache michael@0: gBrowser.loadURI("about:blank"); michael@0: michael@0: // Wait a bit for page to fully load, then wait a while and michael@0: // see that no animation occurs. michael@0: gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: gTimer.initWithCallback(function() { michael@0: gFrames = gImage.framesNotified; michael@0: gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: gTimer.initWithCallback(function() { michael@0: // Might have a few stray frames, until other page totally loads michael@0: var additionalFrames = gImage.framesNotified - gFrames; michael@0: is(additionalFrames == 0, true, "Must have not animated in bfcache! Got " + additionalFrames + " additional frames"); michael@0: goer.next(); michael@0: }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); // 4 seconds - expect 40 frames michael@0: }, 0, Ci.nsITimer.TYPE_ONE_SHOT); // delay of 0 - wait for next event loop michael@0: yield; michael@0: michael@0: // Go back michael@0: gBrowser.goBack(); michael@0: michael@0: chances = 120; michael@0: do { michael@0: gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: gTimer.initWithCallback(function() { michael@0: if (gImage.framesNotified - gFrames >= 20) { michael@0: goer.send(true); michael@0: } else { michael@0: chances--; michael@0: goer.send(chances == 0); // maybe if we wait a bit, it will happen michael@0: } michael@0: }, 500, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: } while (!(yield)); michael@0: is(chances > 0, true, "Must have animated once out of bfcache!"); michael@0: michael@0: // Finally, check that the css background image has essentially the same michael@0: // # of frames, implying that it animated at the same times as the regular michael@0: // image. We can easily retrieve regular images through their HTML image michael@0: // elements, which is what we did before. For the background image, we michael@0: // create a regular image now, and read the current frame count. michael@0: var doc = gBrowser.selectedBrowser.contentWindow.document; michael@0: var div = doc.getElementById("background_div"); michael@0: div.innerHTML += ''; michael@0: actOnMozImage(doc, "img3", function(image) { michael@0: is(Math.abs(image.framesNotified - gImage.framesNotified)/gImage.framesNotified < 0.5, true, michael@0: "Must have also animated the background image, and essentially the same # of frames. " + michael@0: "Regular image got " + gImage.framesNotified + " frames but background image got " + image.framesNotified); michael@0: }); michael@0: michael@0: gBrowser.removeCurrentTab(); michael@0: michael@0: nextTest(); michael@0: } michael@0: michael@0: var goer = theTest(); michael@0: goer.next(); michael@0: } michael@0: michael@0: // Check that imgContainers are shared on the same page and michael@0: // between tabs michael@0: function testSharedContainers() { michael@0: function theTest() { michael@0: var gImages = []; michael@0: var gFrames; michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html"); michael@0: gBrowser.selectedBrowser.addEventListener("pageshow", function () { michael@0: gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true); michael@0: actOnMozImage(gBrowser.contentDocument, "img1", function(image) { michael@0: gImages[0] = image; michael@0: gFrames = image.framesNotified; // May in theory have frames from last test michael@0: // in this counter - so subtract them out michael@0: }); michael@0: goer.next(); michael@0: }, true); michael@0: yield; michael@0: michael@0: // Load next tab somewhat later michael@0: gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: gTimer.initWithCallback(function() { michael@0: goer.next(); michael@0: }, 1500, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: yield; michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "imageX2.html"); michael@0: gBrowser.selectedBrowser.addEventListener("pageshow", function () { michael@0: gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true); michael@0: [1,2].forEach(function(i) { michael@0: actOnMozImage(gBrowser.contentDocument, "img"+i, function(image) { michael@0: gImages[i] = image; michael@0: }); michael@0: }); michael@0: goer.next(); michael@0: }, true); michael@0: yield; michael@0: michael@0: var chances = 120; michael@0: do { michael@0: gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); michael@0: gTimer.initWithCallback(function() { michael@0: if (gImages[0].framesNotified - gFrames >= 10) { michael@0: goer.send(true); michael@0: } else { michael@0: chances--; michael@0: goer.send(chances == 0); // maybe if we wait a bit, it will happen michael@0: } michael@0: }, 500, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: } while (!(yield)); michael@0: is(chances > 0, true, "Must have been animating while showing several images"); michael@0: michael@0: // Check they all have the same frame counts michael@0: var theFrames = null; michael@0: [0,1,2].forEach(function(i) { michael@0: var frames = gImages[i].framesNotified; michael@0: if (theFrames == null) { michael@0: theFrames = frames; michael@0: } else { michael@0: is(theFrames, frames, "Sharing the same imgContainer means *exactly* the same frame counts!"); michael@0: } michael@0: }); michael@0: michael@0: gBrowser.removeCurrentTab(); michael@0: gBrowser.removeCurrentTab(); michael@0: michael@0: nextTest(); michael@0: } michael@0: michael@0: var goer = theTest(); michael@0: goer.next(); michael@0: } michael@0: michael@0: var tests = [testBFCache, testSharedContainers]; michael@0: michael@0: function nextTest() { michael@0: if (tests.length == 0) { michael@0: finish(); michael@0: return; michael@0: } michael@0: tests.shift()(); michael@0: } michael@0: michael@0: function test() { michael@0: ignoreAllUncaughtExceptions(); michael@0: nextTest(); michael@0: } michael@0: