summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_bug1729662.html
blob: ec4350849461027ff1cbe3bafc13a95907900ba9 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test back/forward after pushState</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();
    SimpleTest.requestFlakyTimeout("Need to wait to make sure an event does not fire");

    async function runTest() {
      let win = window.open();
      let goneBackAndForwardOnce = new Promise((resolve) => {
        let timeoutID;

        // We should only get one load event in win.
        let bc = new BroadcastChannel("bug1729662");
        bc.addEventListener("message", () => {
          bc.addEventListener("message", () => {
            clearTimeout(timeoutID);
            resolve(false);
          });
        }, { once: true });

        let goneBack = false, goneForward = false;
        win.addEventListener("popstate", ({ state }) => {
          // We should only go back and forward once, if we get another
          // popstate after that then we should fall through to the
          // failure case below.
          if (!(goneBack && goneForward)) {
            // Check if this is the popstate for the forward (the one for
            // back will have state == undefined).
            if (state == 1) {
              ok(goneBack, "We should have gone back before going forward");

              goneForward = true;

              // Wait a bit to make sure there are no more popstate events.
              // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
              timeoutID = setTimeout(resolve, 1000, true);

              return;
            }

            // Check if we've gone back once before, if we get another
            // popstate after that then we should fall through to the
            // failure case below.
            if (!goneBack) {
              goneBack = true;

              return;
            }
          }

          clearTimeout(timeoutID);
          resolve(false);
        });
      });

      win.location = "file_bug1729662.html";

      ok(await goneBackAndForwardOnce, "Stopped navigating history");

      win.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>