summaryrefslogtreecommitdiffstats
path: root/docshell/test/navigation/test_beforeunload_and_bfcache.html
blob: 6bb958c6c6877ddbf1aeb5942178af5303d72a17 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Loading a page from BFCache and firing beforeunload on the current page</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
  SimpleTest.waitForExplicitFinish();

  /*
   * This is a simple test to ensure beforeunload is fired on the current page
   * when restoring a page from bfcache.
   * (1) The controller page opens a new window. Another page is loaded there
   *     and session history is navigated back to check whether bfcache is
   *     enabled. If not, close message is sent and the opened window closes
   *     and the test ends.
   * (2) beforeunload event listener is added to the page and history.forward()
   *     is called. The event listener should send a message to the controller
   *     page.
   * (3) Close message is sent to close the opened window and the test finishes.
   */

  var pageshowCount = 0;
  var gotBeforeUnload = false;
  var bfcacheDisabled = false;


  function executeTest() {
    var bc = new BroadcastChannel("beforeunload_and_bfcache");
    bc.onmessage = function(event) {
      if (event.data.type == "pageshow") {
        ++pageshowCount;
        if (pageshowCount == 1) {
          bc.postMessage("nextpage");
        } else if (pageshowCount == 2) {
          bc.postMessage("back");
        } else if (pageshowCount == 3) {
          if (!event.data.persisted) {
            ok(true, "BFCache not enabled");
            bfcacheDisabled = true;
            bc.postMessage("close");
            return;
          }
          bc.postMessage("forward");
        } else if (pageshowCount == 4) {
          ok(event.data.persisted, "Should have loaded a page from bfcache.");
          bc.postMessage("close");
        }
      } else if (event.data == "beforeunload") {
        gotBeforeUnload = true;
      } else if (event.data == "closed") {
        isnot(bfcacheDisabled, gotBeforeUnload,
             "Either BFCache shouldn't be enabled or a beforeunload event should have been fired.");
        bc.close();
        SimpleTest.finish();
      }
    };

    function runTest() {
     SpecialPowers.pushPrefEnv({"set": [["docshell.shistory.bfcache.allow_unload_listeners", false]]},
       function() {
         window.open("file_beforeunload_and_bfcache.html", "", "noopener");
       }
     );
    }
    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>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>