summaryrefslogtreecommitdiffstats
path: root/dom/broadcastchannel/tests/test_bfcache.html
blob: 1c6155e6f173ec17ba4dba0a17cc4b9dbd4e5391 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for bfcache and BroadcastChannel</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
  <script type="application/javascript">

  /*
   * The test opens a new window. Then a message 'load' is sent there to load
   * another page. If expectedPersisted is false, a dummy message is sent
   * through a BroadcastChannel which the first has registered to use.
   * That should evict the page from bfcache.
   * 'back' message is sent to call history.back().
   * The page which is loaded from session history should be persisted if
   * expectedPersisted is true.
   */

  SimpleTest.waitForExplicitFinish();
  var testUrl1 = "testUrl1_bfcache.html";


  function executeTest() {
    var bc1 = new BroadcastChannel("testUrl1_bfcache");
    var bc2 = new BroadcastChannel("testUrl2_bfcache");
    bc1.onmessage = function(event) {
      if (event.data == "closed") {
        info("Closed");
        runTest();
        return;
      }
      page1Shown(event.data);
    };
    bc2.onmessage = function(event) { page2Shown(event.data); };

    var counter = 0;
    var expectedPersisted = false;
    var bc = new BroadcastChannel("a");

    function page1Shown(e) {
      if (counter == 0) {
        ok(!e.persisted, "test page should have been persisted initially");
        bc1.postMessage("load");
      } else {
        is(e.persisted, expectedPersisted, "test page should have been persisted in pageshow");
        bc1.postMessage("close");
      }

      counter++;
    }

    function page2Shown() {
      if (!expectedPersisted) {
        SimpleTest.executeSoon(function() {
          info("Posting a message.");
          bc.postMessage(42);
        });
      }

      SimpleTest.executeSoon(function() {
        info("Going back");
        bc2.postMessage("back");
      });
    }

    var tests = [
      { expectedPersisted: true },
      { expectedPersisted: false },
    ];

    function runTest() {
      if (!tests.length) {
        bc.close();
        bc1.close();
        bc2.close();
        SimpleTest.finish();
        return;
      }

      var test = tests.shift();

      counter = 0;
      expectedPersisted = test.expectedPersisted;
      window.open(testUrl1, "", "noopener");
    }


    // If Fission is disabled, the pref is no-op.
    SpecialPowers.pushPrefEnv({set: [["fission.bfcacheInParent", true]]}, () => {
      runTest();
    });

  }

  if (isXOrigin) {
    // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
    // Acquire storage access permission here so that the BroadcastChannel used to
    // communicate with the opened windows works in xorigin tests. Otherwise,
    // the iframe containing this page is isolated from first-party storage access,
    // which isolates BroadcastChannel communication.
    SpecialPowers.wrap(document).notifyUserGestureActivation();
    SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
      SpecialPowers.wrap(document).requestStorageAccess().then(() => {
        SpecialPowers.pushPrefEnv({
          set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]]
        }).then(() => {
          executeTest();
        });
      });
    });
  } else {
    executeTest();
  }

  </script>
</body>
</html>