test_bfcache_plus_hash.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=646641
  5. -->
  6. <head>
  7. <title>Test for Bug 646641</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <script type="text/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=646641">Mozilla Bug 646641</a>
  14. <p id="display"></p>
  15. <div id="content" style="display: none">
  16. </div>
  17. <pre id="test">
  18. <script type="application/javascript;version=1.7">
  19. /** Test for Bug 646641 **/
  20. /*
  21. * In a popup (because navigating the main frame confuses Mochitest), do the
  22. * following:
  23. *
  24. * * Call history.pushState().
  25. * * Navigate to a new page.
  26. * * Go back two history entries.
  27. *
  28. * Check that we go back, we retrieve the document from bfcache.
  29. */
  30. SimpleTest.waitForExplicitFinish();
  31. function debug(msg) {
  32. // Wrap dump so we can turn debug messages on and off easily.
  33. dump(msg + '\n');
  34. }
  35. var expectedLoadNum = -1;
  36. function childLoad(n) {
  37. if (n == expectedLoadNum) {
  38. debug('Got load ' + n);
  39. expectedLoadNum = -1;
  40. // Spin the event loop before calling gGen.next() so the generator runs
  41. // outside the onload handler. This prevents us from encountering all
  42. // sorts of docshell quirks.
  43. //
  44. // (I don't know why I need to wrap gGen.next() in a function, but it
  45. // throws an error otherwise.)
  46. setTimeout(function() { gGen.next() }, 0);
  47. }
  48. else {
  49. debug('Got unexpected load ' + n);
  50. ok(false, 'Got unexpected load ' + n);
  51. }
  52. }
  53. var expectedPageshowNum = -1;
  54. function childPageshow(n) {
  55. if (n == expectedPageshowNum) {
  56. debug('Got expected pageshow ' + n);
  57. expectedPageshowNum = -1;
  58. ok(true, 'Got expected pageshow ' + n);
  59. setTimeout(function() { gGen.next() }, 0);
  60. }
  61. else {
  62. debug('Got pageshow ' + n);
  63. }
  64. // Since a pageshow comes along with an onload, don't fail the test if we get
  65. // an unexpected pageshow.
  66. }
  67. function waitForLoad(n) {
  68. debug('Waiting for load ' + n);
  69. expectedLoadNum = n;
  70. }
  71. function waitForShow(n) {
  72. debug('Waiting for show ' + n);
  73. expectedPageshowNum = n;
  74. }
  75. function test() {
  76. var popup = window.open('data:text/html,' +
  77. '<html><body onload="opener.childLoad(1)" ' +
  78. 'onpageshow="opener.childPageshow(1)">' +
  79. 'Popup 1' +
  80. '</body></html>');
  81. waitForLoad(1);
  82. yield undefined;
  83. popup.history.pushState('', '', '');
  84. popup.location = 'data:text/html,<html><body onload="opener.childLoad(2)">Popup 2</body></html>';
  85. waitForLoad(2);
  86. yield undefined;
  87. // Now go back 2. The first page should be retrieved from bfcache.
  88. popup.history.go(-2);
  89. waitForShow(1);
  90. yield undefined;
  91. popup.close();
  92. SimpleTest.finish();
  93. // Yield once more so we don't throw a StopIteration exception.
  94. yield undefined;
  95. }
  96. var gGen = test();
  97. gGen.next();
  98. </script>
  99. </pre>
  100. </body>
  101. </html>