/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

  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. is(gImage.framesNotified == gFrames, true, "Must have not animated in bfcache!");
  57. goer.next();
  58. }, 4000, Ci.nsITimer.TYPE_ONE_SHOT); // 4 seconds - expect 40 frames
  59. }, 0, Ci.nsITimer.TYPE_ONE_SHOT); // delay of 0 - wait for next event loop
  60. yield;
  61. // Go back
  62. gBrowser.goBack();
  63. chances = 120;
  64. do {
  65. gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  66. gTimer.initWithCallback(function() {
  67. if (gImage.framesNotified - gFrames >= 20) {
  68. goer.send(true);
  69. } else {
  70. chances--;
  71. goer.send(chances == 0); // maybe if we wait a bit, it will happen
  72. }
  73. }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
  74. } while (!(yield));
  75. is(chances > 0, true, "Must have animated once out of bfcache!");
  76. // Finally, check that the css background image has essentially the same
  77. // # of frames, implying that it animated at the same times as the regular
  78. // image. We can easily retrieve regular images through their HTML image
  79. // elements, which is what we did before. For the background image, we
  80. // create a regular image now, and read the current frame count.
  81. var doc = gBrowser.selectedBrowser.contentWindow.document;
  82. var div = doc.getElementById("background_div");
  83. div.innerHTML += '<img src="animated2.gif" id="img3">';
  84. actOnMozImage(doc, "img3", function(image) {
  85. is(Math.abs(image.framesNotified - gImage.framesNotified)/gImage.framesNotified < 0.5, true,
  86. "Must have also animated the background image, and essentially the same # of frames");
  87. });
  88. gBrowser.removeCurrentTab();
  89. nextTest();
  90. }
  91. var goer = theTest();
  92. goer.next();
  93. }
  94. // Check that imgContainers are shared on the same page and
  95. // between tabs
  96. function testSharedContainers() {
  97. function theTest() {
  98. var gImages = [];
  99. var gFrames;
  100. gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "image.html");
  101. gBrowser.selectedBrowser.addEventListener("pageshow", function () {
  102. gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
  103. actOnMozImage(gBrowser.contentDocument, "img1", function(image) {
  104. gImages[0] = image;
  105. gFrames = image.framesNotified; // May in theory have frames from last test
  106. // in this counter - so subtract them out
  107. });
  108. goer.next();
  109. }, true);
  110. yield;
  111. // Load next tab somewhat later
  112. gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  113. gTimer.initWithCallback(function() {
  114. goer.next();
  115. }, 1500, Ci.nsITimer.TYPE_ONE_SHOT);
  116. yield;
  117. gBrowser.selectedTab = gBrowser.addTab(TESTROOT + "imageX2.html");
  118. gBrowser.selectedBrowser.addEventListener("pageshow", function () {
  119. gBrowser.selectedBrowser.removeEventListener("pageshow", arguments.callee, true);
  120. [1,2].forEach(function(i) {
  121. actOnMozImage(gBrowser.contentDocument, "img"+i, function(image) {
  122. gImages[i] = image;
  123. });
  124. });
  125. goer.next();
  126. }, true);
  127. yield;
  128. var chances = 120;
  129. do {
  130. gTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
  131. gTimer.initWithCallback(function() {
  132. if (gImages[0].framesNotified - gFrames >= 10) {
  133. goer.send(true);
  134. } else {
  135. chances--;
  136. goer.send(chances == 0); // maybe if we wait a bit, it will happen
  137. }
  138. }, 500, Ci.nsITimer.TYPE_ONE_SHOT);
  139. } while (!(yield));
  140. is(chances > 0, true, "Must have been animating while showing several images");
  141. // Check they all have the same frame counts
  142. var theFrames = null;
  143. [0,1,2].forEach(function(i) {
  144. var frames = gImages[i].framesNotified;
  145. if (theFrames == null) {
  146. theFrames = frames;
  147. } else {
  148. is(theFrames, frames, "Sharing the same imgContainer means *exactly* the same frame counts!");
  149. }
  150. });
  151. gBrowser.removeCurrentTab();
  152. gBrowser.removeCurrentTab();
  153. nextTest();
  154. }
  155. var goer = theTest();
  156. goer.next();
  157. }
  158. var tests = [testBFCache, testSharedContainers];
  159. function nextTest() {
  160. if (tests.length == 0) {
  161. finish();
  162. return;
  163. }
  164. tests.shift()();
  165. }
  166. function test() {
  167. ignoreAllUncaughtExceptions();
  168. nextTest();
  169. }