Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 waitForExplicitFinish();
2 requestLongerTimeout(2); // see bug 660123 -- this test is slow on Mac.
4 // A hold on the current timer, so it doens't get GCed out from
5 // under us
6 var gTimer;
8 // Browsing to a new URL - pushing us into the bfcache - should cause
9 // animations to stop, and resume when we return
10 function 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 }
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");
50 // Browse elsewhere; push our animating page into the bfcache
51 gBrowser.loadURI("about:blank");
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 var additionalFrames = gImage.framesNotified - gFrames;
62 is(additionalFrames == 0, true, "Must have not animated in bfcache! Got " + additionalFrames + " additional frames");
63 goer.next();
64 }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); // 4 seconds - expect 40 frames
65 }, 0, Ci.nsITimer.TYPE_ONE_SHOT); // delay of 0 - wait for next event loop
66 yield;
68 // Go back
69 gBrowser.goBack();
71 chances = 120;
72 do {
73 gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
74 gTimer.initWithCallback(function() {
75 if (gImage.framesNotified - gFrames >= 20) {
76 goer.send(true);
77 } else {
78 chances--;
79 goer.send(chances == 0); // maybe if we wait a bit, it will happen
80 }
81 }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
82 } while (!(yield));
83 is(chances > 0, true, "Must have animated once out of bfcache!");
85 // Finally, check that the css background image has essentially the same
86 // # of frames, implying that it animated at the same times as the regular
87 // image. We can easily retrieve regular images through their HTML image
88 // elements, which is what we did before. For the background image, we
89 // create a regular image now, and read the current frame count.
90 var doc = gBrowser.selectedBrowser.contentWindow.document;
91 var div = doc.getElementById("background_div");
92 div.innerHTML += '<img src="animated2.gif" id="img3">';
93 actOnMozImage(doc, "img3", function(image) {
94 is(Math.abs(image.framesNotified - gImage.framesNotified)/gImage.framesNotified < 0.5, true,
95 "Must have also animated the background image, and essentially the same # of frames. " +
96 "Regular image got " + gImage.framesNotified + " frames but background image got " + image.framesNotified);
97 });
99 gBrowser.removeCurrentTab();
101 nextTest();
102 }
104 var goer = theTest();
105 goer.next();
106 }
108 // Check that imgContainers are shared on the same page and
109 // between tabs
110 function testSharedContainers() {
111 function theTest() {
112 var gImages = [];
113 var gFrames;
115 gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
116 gBrowser.selectedBrowser.addEventListener("pageshow", function () {
117 gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
118 actOnMozImage(gBrowser.contentDocument, "img1", function(image) {
119 gImages[0] = image;
120 gFrames = image.framesNotified; // May in theory have frames from last test
121 // in this counter - so subtract them out
122 });
123 goer.next();
124 }, true);
125 yield;
127 // Load next tab somewhat later
128 gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
129 gTimer.initWithCallback(function() {
130 goer.next();
131 }, 1500, Ci.nsITimer.TYPE_ONE_SHOT);
132 yield;
134 gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "imageX2.html");
135 gBrowser.selectedBrowser.addEventListener("pageshow", function () {
136 gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
137 [1,2].forEach(function(i) {
138 actOnMozImage(gBrowser.contentDocument, "img"+i, function(image) {
139 gImages[i] = image;
140 });
141 });
142 goer.next();
143 }, true);
144 yield;
146 var chances = 120;
147 do {
148 gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
149 gTimer.initWithCallback(function() {
150 if (gImages[0].framesNotified - gFrames >= 10) {
151 goer.send(true);
152 } else {
153 chances--;
154 goer.send(chances == 0); // maybe if we wait a bit, it will happen
155 }
156 }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
157 } while (!(yield));
158 is(chances > 0, true, "Must have been animating while showing several images");
160 // Check they all have the same frame counts
161 var theFrames = null;
162 [0,1,2].forEach(function(i) {
163 var frames = gImages[i].framesNotified;
164 if (theFrames == null) {
165 theFrames = frames;
166 } else {
167 is(theFrames, frames, "Sharing the same imgContainer means *exactly* the same frame counts!");
168 }
169 });
171 gBrowser.removeCurrentTab();
172 gBrowser.removeCurrentTab();
174 nextTest();
175 }
177 var goer = theTest();
178 goer.next();
179 }
181 var tests = [testBFCache, testSharedContainers];
183 function nextTest() {
184 if (tests.length == 0) {
185 finish();
186 return;
187 }
188 tests.shift()();
189 }
191 function test() {
192 ignoreAllUncaughtExceptions();
193 nextTest();
194 }