summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_bug1850335.html
blob: 546aab59c8f1d739876b26df278fa9de9b15ab69 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test referrer with going back</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();

    function waitForMessage(bc) {
      return new Promise(resolve => {
        bc.addEventListener("message", ({ data }) => { resolve(data); }, { once: true });
      });
    }

    async function runTest() {
      let bc = new BroadcastChannel("bug1850335");

      // Load the first page.
      let waitForPage1 = waitForMessage(bc);
      window.open("file_bug1850335_1.html", "_blank", "noopener");
      let { persisted, value: initial } = await waitForPage1;

      ok(!persisted, "Loaded first page");
      is(initial, "", "Initial value is empty string");

      // Set the value of the input element in the first page.
      let waitForValue = waitForMessage(bc);
      bc.postMessage({ cmd: "setValue", arg: "ok" });
      let { value: expected }= await waitForValue;
      is(expected, "ok", "Loaded first page");

      // Load the second page (same origin with the first page).
      let waitForPage2 = waitForMessage(bc);
      bc.postMessage({ cmd: "load", arg: "file_bug1850335_2.html" });
      ({ persisted } = await waitForPage2);

      ok(!persisted, "Loaded second page (same-origin)");

      // Load the third page (cross origin with the first and second pages). The
      // third page will immediately do |history.back()| to go back to the
      // second page.
      waitForPage2 = waitForMessage(bc);
      const crossOrigin = new URL("file_bug1850335_3.html", `https://example.com${location.pathname}`);
      bc.postMessage({ cmd: "load", arg: crossOrigin.href });
      ({ persisted } = await waitForPage2);

      ok(!persisted, "Second page should not be in the BFCache");

      // Go back to the first page.
      waitForPage1 = waitForMessage(bc);
      bc.postMessage({ cmd: "back" });
      let { persisted: fromBFCache, value: result } = await waitForPage1;

      ok(fromBFCache, "Page came from BFCache");
      is(result, expected, "Value wasn't cleared");

      bc.postMessage({ cmd: "close" });

      bc.close();

      SimpleTest.finish();
    }
  </script>
</head>
<body onload="runTest();">
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>