summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/session-history-prerender.https.html
blob: b02865c1bc7aa88d70c83dbd4ee66e938754baa7 (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
<!DOCTYPE html>
<!--
 "Activation" suffix in these test names communicates to the test harness that
 this part of the test is run post-activation.
-->
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<script src="session-history-harness.js"></script>
<script src="session-history-test-util.js"></script>
<body>
  <script>
    function testHistoryPushStateInPrerender() {
      assert(history.length == 1, "Initial history length");
      assert(!history.state, "Initial history state");

      history.pushState("teststate", null, null);

      assert(history.length == 1, "History length unchanged");
      assert(history.state == "teststate", "Update state");
    }

    function testHistoryReplaceStateInPrerender() {
      assert(history.length == 1, "Initial history length");
      assert(!history.state, "Initial history state");

      history.replaceState("teststate", null, null);

      assert(history.length == 1, "History length unchanged");
      assert(history.state == "teststate", "Update state");
    }

    function testLocationAssignInPrerender() {
      assert(history.length == 1, "Initial history length");
      const initialLocation = location.href;
      location.assign("#test");

      assert(history.length == 1, "History length unchanged");
      assert(location.href != initialLocation, "Update location");
    }

    function testLocationReplaceInPrerender() {
      assert(history.length == 1, "Initial history length");
      const initialLocation = location.href;
      location.replace("#test");

      assert(history.length == 1, "History length unchanged");
      assert(location.href != initialLocation, "Update location");
    }

    function testSetLocationHrefInPrerender() {
      assert(history.length == 1, "Initial history length");
      const initialLocation = location.href;
      location.href = "#test";

      assert(history.length == 1, "History length unchanged");
      assert(location.href != initialLocation, "Update location");
    }

    function testSyntheticAnchorClickInPrerender() {
      assert(history.length == 1, "Initial history length");
      const initialLocation = location.href;

      const anchor = document.createElement("a");
      anchor.href = "#test";
      document.body.appendChild(anchor);

      anchor.click();

      assert(history.length == 1, "History length unchanged");
      assert(location.href != initialLocation, "Update location");
    }

    function testHistoryLengthInPrerender() {
      assert(history.length == 1, "Initial history length");
    }

    function testHistoryLengthInPrerenderActivation() {
      assert(history.length == 2, "History length after activation");

      // TODO(http://crbug.com/1220992): Test whether calling history.back()
      // after activation should go back to the initiator page correctly.
      // We might need a non-trivial refactoring to test this scenario correctly.
    }

    // This test runs testSubfrarmeNavigationInPrerenderInSubframe() in a
    // subframe, and waits for a message from a navigated subframe.
    async function testSubframeNavigationInPrerender() {
      assert(window.parent == window, "not the top frame");
      const params = new URLSearchParams(window.location.search);
      const testName = params.get("testName");
      const uid = params.get("uid");
      const resultPromise = waitChannelMessage(
          `prerender-channel-${testName}InSubframeAfterNavigation`, uid);

      params.set("testName", testName + "InSubframe");
      const frame = document.createElement("iframe");
      const url = location.pathname + "?" + params.toString();
      frame.src = url;
      document.body.appendChild(frame);
      const result = await resultPromise;
      assert(result == "Passed", result);
    }

    function testSubframeNavigationInPrerenderInSubframe() {
      assert(window.parent != window, "not in a subframe");
      assert(window.parent == window.top, "the direct parent isn't the top");
      assert(history.length == 1, "Initial history length");

      const params = new URLSearchParams(window.location.search);
      const testName = params.get("testName");
      params.set("testName", testName + "AfterNavigation");
      location.href = location.pathname + "?" + params.toString();
    }

    function testSubframeNavigationInPrerenderInSubframeAfterNavigation() {
      assert(window.parent != window, "not in a subframe");
      assert(window.parent == window.top, "the direct parent isn't the top");
      assert(history.length == 1, "History length after subframe navigation");
    }

    // This test runs testSubframeReloadInPrerenderInSubframe() in a
    // subframe, and waits for a message from a navigated subframe.
    async function testSubframeReloadInPrerender() {
      assert(window.parent == window, "not the top frame");
      const params = new URLSearchParams(window.location.search);
      const testName = params.get("testName");
      const uid = params.get("uid");
      const resultPromise = waitChannelMessage(
          `prerender-channel-${testName}InSubframe`, uid);

      params.set("testName", testName + "InSubframe");
      const frame = document.createElement("iframe");
      const url = location.pathname + "?" + params.toString();
      frame.src = url;
      document.body.appendChild(frame);
      const result = await resultPromise;
      assert(result == "Passed", result);
      const second_result = await waitChannelMessage(
          `prerender-channel-${testName}InSubframe`, uid);
      assert(second_result == "Passed", second_result);
    }

    function testSubframeReloadInPrerenderInSubframe() {
      assert(window.parent != window, "not in a subframe");
      assert(window.parent == window.top, "the direct parent isn't the top");
      assert(history.length == 1, "Initial history length");
      window.location.reload();
    }
  </script>
</body>