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