test_bug669671.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=669671
  5. -->
  6. <head>
  7. <title>Test for Bug 669671</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. </head>
  11. <body>
  12. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=669671">Mozilla Bug 669671</a>
  13. <p id="display"></p>
  14. <div id="content" style="display: none">
  15. </div>
  16. <pre id="test">
  17. <script type="application/javascript;version=1.7">
  18. /**
  19. * Test for Bug 669671.
  20. *
  21. * This is a bit complicated. We have a script, file_bug669671.sjs, which counts
  22. * how many times it's loaded and returns that count in the body of an HTML
  23. * document. For brevity, call this page X.
  24. *
  25. * X is sent with Cache-Control: max-age=0 and can't be bfcached (it has an
  26. * onunload handler). Our test does the following in a popup:
  27. *
  28. * 1) Load X?pushed, to prime the cache.
  29. * 2) Navigate to X.
  30. * 3) Call pushState and navigate from X to X?pushed.
  31. * 4) Navigate to X?navigated.
  32. * 5) Go back (to X?pushed).
  33. *
  34. * We do all this work so we can check that in step 5, we fetch X?pushed from
  35. * the network -- we shouldn't use our cached copy, because of the
  36. * cache-control header X sends.
  37. *
  38. * Then we go back and repeat the whole process but call history.replaceState
  39. * instead of pushState. And for good measure, we test once more, this time
  40. * modifying only the hash of the URI using replaceState. In this case, we
  41. * *should* load from the cache.
  42. *
  43. **/
  44. SimpleTest.requestLongerTimeout(2);
  45. SimpleTest.waitForExplicitFinish();
  46. function onChildLoad()
  47. {
  48. SimpleTest.executeSoon(function() { gGen.next() });
  49. }
  50. var _loadCount = 0;
  51. function checkPopupLoadCount()
  52. {
  53. is(popup.document.body.innerHTML, _loadCount + '', 'Load count');
  54. // We normally want to increment _loadCount here. But if the test fails
  55. // because we didn't do a load we should have, let's not cause a cascade of
  56. // failures by incrementing _loadCount.
  57. var origCount = _loadCount;
  58. if (popup.document.body.innerHTML >= _loadCount + '')
  59. _loadCount++;
  60. return origCount;
  61. }
  62. function test()
  63. {
  64. // Step 0 - Make sure the count is reset to 0 in case of reload
  65. popup.location = 'file_bug669671.sjs?countreset';
  66. yield;
  67. is(popup.document.body.innerHTML, '0',
  68. 'Load count should be reset to 0');
  69. // Step 1 - The popup's body counts how many times we've requested the
  70. // resource. This is the first time we've requested it, so it should be '0'.
  71. checkPopupLoadCount();
  72. // Step 2 - We'll get another onChildLoad when this finishes.
  73. popup.location = 'file_bug669671.sjs';
  74. yield undefined;
  75. // Step 3 - Call pushState and change the URI back to ?pushed.
  76. checkPopupLoadCount();
  77. popup.history.pushState('', '', '?pushed');
  78. // Step 4 - Navigate away. This should trigger another onChildLoad.
  79. popup.location = 'file_bug669671.sjs?navigated-1';
  80. yield undefined;
  81. // Step 5 - Go back. This should result in another onload (because the file is
  82. // not in bfcache) and should be the fourth time we've requested the sjs file.
  83. checkPopupLoadCount();
  84. SpecialPowers.wrap(popup).back();
  85. yield undefined;
  86. // This is the check which was failing before we fixed the bug.
  87. checkPopupLoadCount();
  88. popup.close();
  89. // Do the whole thing again, but with replaceState.
  90. popup = window.open('file_bug669671.sjs?replaced');
  91. yield undefined;
  92. checkPopupLoadCount();
  93. popup.location = 'file_bug669671.sjs';
  94. yield undefined;
  95. checkPopupLoadCount();
  96. popup.history.replaceState('', '', '?replaced');
  97. popup.location = 'file_bug669671.sjs?navigated-2';
  98. yield undefined;
  99. checkPopupLoadCount();
  100. SpecialPowers.wrap(popup).back();
  101. yield undefined;
  102. checkPopupLoadCount();
  103. popup.close();
  104. // Once more, with feeling. Notice that we don't have to prime the cache
  105. // with an extra load here, because X and X#hash share the same cache entry.
  106. popup = window.open('file_bug669671.sjs?hash-test');
  107. yield undefined;
  108. var initialCount = checkPopupLoadCount();
  109. popup.history.replaceState('', '', '#hash');
  110. popup.location = 'file_bug669671.sjs?navigated-3';
  111. yield undefined;
  112. checkPopupLoadCount();
  113. SpecialPowers.wrap(popup).back();
  114. yield undefined;
  115. is(popup.document.body.innerHTML, initialCount + '',
  116. 'Load count (should be cached)');
  117. popup.close();
  118. SimpleTest.finish();
  119. yield undefined;
  120. }
  121. // This will call into onChildLoad once it loads.
  122. var popup = window.open('file_bug669671.sjs?pushed');
  123. var gGen = test();
  124. </script>
  125. </pre>
  126. </body>
  127. </html>