no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / docshell / test / mochitest / test_bfcache_plus_hash.html
blobcb5bc06f2189e91e89369881c540bbd1b09d17e5
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 src="/tests/SimpleTest/SimpleTest.js"></script>
9 <script 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">
17 </div>
18 <pre id="test">
19 <script type="application/javascript">
21 /** Test for Bug 646641 */
23 /**
24 * Steps:
25 * - Main page (this one) opens file_bfcache_plus_hash_1.html (subpage 1)
26 * - subpage 1 sends msg { "childLoad", 1 }
27 * - subpage 1 sends msg { "childPageshow", 1 }
28 * - main page sends message "pushState"
29 * - subpage 1 does pushState()
30 * - subpage 1 navigates to file_bfcache_plus_hash_2.html (subpage 2)
31 * - subpage 2 sends msg { "childLoad", 2 }
32 * - subpage 2 sends msg { "childPageshow", 2 }
33 * - main page sends msg "go-2"
34 * - subpage 2 goes back two history entries
35 * - subpage 1 sends msg { "childPageshow", 1 }
36 * - Receiving only this msg shows we have retrieved the document from bfcache
37 * - main page sends msg "close"
38 * - subpage 1 sends msg "closed"
40 SimpleTest.waitForExplicitFinish();
42 function debug(msg) {
43 // Wrap dump so we can turn debug messages on and off easily.
44 dump(msg + "\n");
47 var expectedLoadNum = -1;
48 var expectedPageshowNum = -1;
50 function waitForLoad(n) {
51 debug("Waiting for load " + n);
52 expectedLoadNum = n;
55 function waitForShow(n) {
56 debug("Waiting for show " + n);
57 expectedPageshowNum = n;
62 function executeTest() {
63 function* test() {
64 window.open("file_bfcache_plus_hash_1.html", "", "noopener");
65 waitForLoad(1);
66 waitForShow(1);
67 yield undefined;
68 yield undefined;
70 bc1.postMessage("pushState");
72 waitForLoad(2);
73 waitForShow(2);
74 yield undefined;
75 yield undefined;
77 // Now go back 2. The first page should be retrieved from bfcache.
78 bc2.postMessage("go-2");
79 waitForShow(1);
80 yield undefined;
82 bc1.postMessage("close");
85 var bc1 = new BroadcastChannel("bug646641_1");
86 var bc2 = new BroadcastChannel("bug646641_2");
87 bc1.onmessage = (msgEvent) => {
88 var msg = msgEvent.data.message;
89 var n = msgEvent.data.num;
90 if (msg == "childLoad") {
91 if (n == expectedLoadNum) {
92 debug("Got load " + n);
93 expectedLoadNum = -1;
95 // Spin the event loop before calling gGen.next() so the generator runs
96 // outside the onload handler. This prevents us from encountering all
97 // sorts of docshell quirks.
98 setTimeout(function() { gGen.next(); }, 0);
99 } else {
100 debug("Got unexpected load " + n);
101 ok(false, "Got unexpected load " + n);
103 } else if (msg == "childPageshow") {
104 if (n == expectedPageshowNum) {
105 debug("Got expected pageshow " + n);
106 expectedPageshowNum = -1;
107 ok(true, "Got expected pageshow " + n);
108 setTimeout(function() { gGen.next(); }, 0);
109 } else {
110 debug("Got unexpected pageshow " + n);
111 ok(false, "Got unexpected pageshow " + n);
113 } else if (msg == "closed") {
114 bc1.close();
115 bc2.close();
116 SimpleTest.finish();
120 bc2.onmessage = bc1.onmessage;
122 var gGen = test();
124 // If Fission is disabled, the pref is no-op.
125 SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
126 gGen.next();
129 if (isXOrigin) {
130 // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
131 // Acquire storage access permission here so that the BroadcastChannel used to
132 // communicate with the opened windows works in xorigin tests. Otherwise,
133 // the iframe containing this page is isolated from first-party storage access,
134 // which isolates BroadcastChannel communication.
135 SpecialPowers.wrap(document).notifyUserGestureActivation();
136 SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
137 SpecialPowers.wrap(document).requestStorageAccess().then(() => {
138 SpecialPowers.pushPrefEnv({
139 set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
140 }).then(() => {
141 executeTest();
145 } else {
146 executeTest();
150 </script>
151 </pre>
152 </body>
153 </html>