browser_image.js 6.5 KB

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