summaryrefslogtreecommitdiffstats
path: root/docshell/test/mochitest/test_bug1121701.html
blob: cd0b2529d453fe3d1b22456ceb8d437a5706ff31 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1121701
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1121701</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 1121701 */
  SimpleTest.waitForExplicitFinish();

  var testUrl1 = "file_bug1121701_1.html";
  var testUrl2 = "file_bug1121701_2.html";

  var page1LoadCount = 0;
  let page1Done = {};
  page1Done.promise = new Promise(resolve => {
    page1Done.resolve = resolve;
  });
  let page2Done = {};
  page2Done.promise = new Promise(resolve => {
    page2Done.resolve = resolve;
  });

  addLoadEvent(async function() {

    // 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.
    if (isXOrigin) {
      await SpecialPowers.pushPrefEnv({
        set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
      });
      SpecialPowers.wrap(document).notifyUserGestureActivation();
      await SpecialPowers.addPermission("storageAccessAPI", true, window.location.href);
      await SpecialPowers.wrap(document).requestStorageAccess();
    }

    var bc = new BroadcastChannel("file_bug1121701_1");
    var bc2 = new BroadcastChannel("file_bug1121701_2");

    async function scheduleFinish() {
      await Promise.all([page1Done.promise, page2Done.promise]);
      bc2.close();
      bc.close();
      SimpleTest.finish();
    }

    bc.onmessage = (msgEvent) => {
      var msg = msgEvent.data;
      var command = msg.command;
      if (command == "child1PageShow") {
        ++page1LoadCount;
        var persisted = msg.persisted;
        var pageHideAsserts = msg.pageHideAsserts;
        if (pageHideAsserts) {
          ok(pageHideAsserts.persisted, "onpagehide: test page 1 should get persisted");
          is(pageHideAsserts.innerHTML, "modified",  "onpagehide: innerHTML text is 'modified'");
        }
        if (page1LoadCount == 1) {
          SimpleTest.executeSoon(function() {
            is(persisted, false, "Initial page load shouldn't be persisted.");
            bc.postMessage({command: "setInnerHTML", testUrl2});
          });
        } else if (page1LoadCount == 2) {
          is(persisted, true, "Page load from bfcache should be persisted.");
          is(msg.innerHTML, "modified", "innerHTML text is 'modified'");
          bc.postMessage({command: "close"});
        }
      } else if (command == "closed") {
        page1Done.resolve();
      }
    }
    bc2.onmessage = (msgEvent) => {
      var msg = msgEvent.data;
      var command = msg.command;
      if (command == "child2PageShow") {
        bc2.postMessage({command: "setInnerHTML", location: location.href});
      } else if (command == "onmessage") {
        page2Done.resolve();
      }
    }

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

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1121701">Mozilla Bug 1121701</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>