summaryrefslogtreecommitdiffstats
path: root/docshell/test/test_bfcache_plus_hash.html
blob: 30c0b6b50291d671eaa07c3c3df75486895f24b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=646641
-->
<head>
  <title>Test for Bug 646641</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/WindowSnapshot.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=646641">Mozilla Bug 646641</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="application/javascript;version=1.7">

/** Test for Bug 646641 **/

/* 
 * In a popup (because navigating the main frame confuses Mochitest), do the
 * following:
 *
 *  * Call history.pushState().
 *  * Navigate to a new page.
 *  * Go back two history entries.
 *
 * Check that we go back, we retrieve the document from bfcache.
 */

SimpleTest.waitForExplicitFinish();

function debug(msg) {
  // Wrap dump so we can turn debug messages on and off easily.
  dump(msg + '\n');
}

var expectedLoadNum = -1;
function childLoad(n) {
  if (n == expectedLoadNum) {
    debug('Got load ' + n);
    expectedLoadNum = -1;

    // Spin the event loop before calling gGen.next() so the generator runs
    // outside the onload handler.  This prevents us from encountering all
    // sorts of docshell quirks.
    //
    // (I don't know why I need to wrap gGen.next() in a function, but it
    // throws an error otherwise.)
    setTimeout(function() { gGen.next() }, 0);
  }
  else {
    debug('Got unexpected load ' + n);
    ok(false, 'Got unexpected load ' + n);
  }
}

var expectedPageshowNum = -1;
function childPageshow(n) {
  if (n == expectedPageshowNum) {
    debug('Got expected pageshow ' + n);
    expectedPageshowNum = -1;
    ok(true, 'Got expected pageshow ' + n);
    setTimeout(function() { gGen.next() }, 0);
  }
  else {
    debug('Got pageshow ' + n);
  }

  // Since a pageshow comes along with an onload, don't fail the test if we get
  // an unexpected pageshow.
}

function waitForLoad(n) {
  debug('Waiting for load ' + n);
  expectedLoadNum = n;
}

function waitForShow(n) {
  debug('Waiting for show ' + n);
  expectedPageshowNum = n;
}

function test() {
  var popup = window.open('data:text/html,' +
                          '<html><body onload="opener.childLoad(1)" ' +
                                      'onpageshow="opener.childPageshow(1)">' +
                                'Popup 1' +
                                '</body></html>');
  waitForLoad(1);
  yield undefined;

  popup.history.pushState('', '', '');

  popup.location = 'data:text/html,<html><body onload="opener.childLoad(2)">Popup 2</body></html>';
  waitForLoad(2);
  yield undefined;

  // Now go back 2.  The first page should be retrieved from bfcache.
  popup.history.go(-2);
  waitForShow(1);
  yield undefined;

  popup.close();
  SimpleTest.finish();
  
  // Yield once more so we don't throw a StopIteration exception.
  yield undefined;
}

var gGen = test();
gGen.next();

</script>
</pre>
</body>
</html>