summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/session-history-initiator.https.html
blob: f6d5eb555ccdf312d9431594141fb4793e27f5de (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
<!DOCTYPE html>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<script src="session-history-harness.js"></script>
<body>
  <script>
    const urlParams = new URLSearchParams(window.location.search);
    const prerender = urlParams.get("prerender");
    const testName = urlParams.get("testName");
    const uid = urlParams.get("uid");

    const prerenderChannel = new PrerenderChannel(
      `prerender-channel-${testName}`, uid
    );
    const testChannel = new PrerenderChannel(`test-channel-${testName}`, uid);

    // Activate when a test sends a "activate" message.
    testChannel.addEventListener("message", (e) => {
      assert(e.data === "activate");
      window.location.href = `${prerender}?testName=${testName}&uid=${uid}`;
    });

    // Runs before and after the history manipulation in the prerender page to confirm
    // that the session history of the initiator page is not affected by any history
    // changes in the prerender page.
    function assertInitialHistoryState() {
      assert(history.length == 1, "Initial history length");
      assert(!history.state, "Initial history state");
    }

    async function startPrerenderingAndWaitTestResult() {
      const message = new Promise((resolve) => {
        prerenderChannel.addEventListener(
          "message",
          (e) => {
            resolve(e.data);
          },
          { once: true }
        );
      });

      assertInitialHistoryState();

      startPrerendering(`${prerender}?testName=${testName}&uid=${uid}`);
      const testResult = await message;

      assertInitialHistoryState();

      return testResult;
    }

    (async () => {
      try {
        testChannel.postMessage(await startPrerenderingAndWaitTestResult());
      } catch (e) {
        testChannel.postMessage("Failed: " + e.name + ": " + e.message);
      } finally {
        prerenderChannel.close();
      }
    })();
  </script>
</body>