summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_bfcache_plus_hash.html
blob: 66871fcbbfcd716c60493a65b236bfb2d75006e7 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=646641
-->
<head>
  <title>Test for Bug 646641</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script 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">

/** Test for Bug 646641 **/

/**
 * Steps:
 * - Main page (this one) opens file_bfcache_plus_hash_1.html (subpage 1)
 * - subpage 1 sends msg { "childLoad", 1 }
 * - subpage 1 sends msg { "childPageshow", 1 }
 * - main page sends message "pushState"
 * - subpage 1 does pushState()
 * - subpage 1 navigates to file_bfcache_plus_hash_2.html (subpage 2)
 * - subpage 2 sends msg { "childLoad", 2 }
 * - subpage 2 sends msg { "childPageshow", 2 }
 * - main page sends msg "go-2"
 * - subpage 2 goes back two history entries
 * - subpage 1 sends msg  { "childPageshow", 1 }
 *  - Receiving only this msg shows we have retrieved the document from bfcache
 * - main page sends msg "close"
 * - subpage 1 sends msg "closed"
 */
SimpleTest.waitForExplicitFinish();

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

var expectedLoadNum = -1;
var expectedPageshowNum = -1;

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

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



function executeTest() {
  function* test() {
    window.open("file_bfcache_plus_hash_1.html", "", "noopener");
    waitForLoad(1);
    waitForShow(1);
    yield undefined;
    yield undefined;

    bc1.postMessage("pushState");

    waitForLoad(2);
    waitForShow(2);
    yield undefined;
    yield undefined;

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

    bc1.postMessage("close");
  }

  var bc1 = new BroadcastChannel("bug646641_1");
  var bc2 = new BroadcastChannel("bug646641_2");
  bc1.onmessage = (msgEvent) => {
    var msg = msgEvent.data.message;
    var n = msgEvent.data.num;
    if (msg == "childLoad") {
      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.
        setTimeout(function() { gGen.next(); }, 0);
      } else {
        debug("Got unexpected load " + n);
        ok(false, "Got unexpected load " + n);
      }
    } else if (msg == "childPageshow") {
      if (n == expectedPageshowNum) {
        debug("Got expected pageshow " + n);
        expectedPageshowNum = -1;
        ok(true, "Got expected pageshow " + n);
        setTimeout(function() { gGen.next(); }, 0);
      } else {
        debug("Got unexpected pageshow " + n);
        ok(false, "Got unexpected pageshow " + n);
      }
    } else if (msg == "closed") {
      bc1.close();
      bc2.close();
      SimpleTest.finish();
    }
  }

  bc2.onmessage = bc1.onmessage;

  var gGen = test();

  // If Fission is disabled, the pref is no-op.
  SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
    gGen.next();
  });
}
if (isXOrigin) {
  // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
  // Acquire storage access permission here so that the BroadcastChannel used to
  // communicate with the opened windows works in xorigin tests. Otherwise,
  // the iframe containing this page is isolated from first-party storage access,
  // which isolates BroadcastChannel communication.
  SpecialPowers.wrap(document).notifyUserGestureActivation();
  SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
    SpecialPowers.wrap(document).requestStorageAccess().then(() => {
      SpecialPowers.pushPrefEnv({
        set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
      }).then(() => {
        executeTest();
      });
    });
  });
} else {
  executeTest();
}


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