summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_window_close.html
blob: 5b0d7e6fa695673b18fbfa30fa14056943f9fa7c (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>test window.close()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.waitForExplicitFinish();
    var b = new BroadcastChannel("windowclose");

    const link = "link";
    const windowopen = "window.open()";
    var tests = [
        {
        type: windowopen,
        noopener: true,
        shouldCloseWithoutHistory: true,
        shouldCloseWithHistory: false
      },
      {
        type: windowopen,
        noopener: false,
        shouldCloseWithoutHistory: true,
        shouldCloseWithHistory: true
      },
      {
        type: link,
        noopener: true,
        shouldCloseWithoutHistory: true,
        shouldCloseWithHistory: false
      },
      {
        type: link,
        noopener: false,
        shouldCloseWithoutHistory: true,
        shouldCloseWithHistory: true
      }
    ];

    var loadTypes = ["withouthistory", "withhistory"];

    async function start() {
      // If Fission is disabled, the pref is no-op.
      await SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]});

      for (let test of tests) {
        await SpecialPowers.pushPrefEnv({ set: [["dom.allow_scripts_to_close_windows", false]]});
        if (test.type == windowopen) {
          for (let loadType of loadTypes) {
            var features = test.noopener ? "noopener" : "";
            window.open("file_window_close.html?" + loadType, "", features);
            await new Promise(function(r) {
              b.onmessage = function(e) {
                var expectedClose = loadType == "withouthistory" ?
                  test.shouldCloseWithoutHistory : test.shouldCloseWithHistory;
                is(e.data, expectedClose ? "closed" : "blocked",
                   "Expected close on " + loadType + ": " + expectedClose);
                r();
              }
            });
          }
        } else if (test.type == link) {
          var rel = test.noopener ? "rel='noopener'" : "";
          for (let loadType of loadTypes) {
            document.getElementById("content").innerHTML =
              "<a href='file_window_close.html?" +  loadType + "'" +
                " target='foo' " + rel + "'>link</a>";
            var p = new Promise(function(r) {
              b.onmessage = function(e) {
                var expectedClose = loadType == "withouthistory" ?
                  test.shouldCloseWithoutHistory : test.shouldCloseWithHistory;
                is(e.data, expectedClose ? "closed" : "blocked",
                   "Expected close on " + loadType + ": " + expectedClose);
                r();
              }
            });
            document.getElementById("content").firstChild.click();
            await p;
          }
        }
      }
      SimpleTest.finish();
    }

  </script>
</head>
<body onload="setTimeout(start)">
<p id="display"></p>
<div id="content"></div>
<pre id="test"></pre>
</body>
</html>