tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_beforeunload_and_bfcache.html (3428B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Loading a page from BFCache and firing beforeunload on the current page</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
      8  <script>
      9  SimpleTest.waitForExplicitFinish();
     10 
     11  /*
     12   * This is a simple test to ensure beforeunload is fired on the current page
     13   * when restoring a page from bfcache.
     14   * (1) The controller page opens a new window. Another page is loaded there
     15   *     and session history is navigated back to check whether bfcache is
     16   *     enabled. If not, close message is sent and the opened window closes
     17   *     and the test ends.
     18   * (2) beforeunload event listener is added to the page and history.forward()
     19   *     is called. The event listener should send a message to the controller
     20   *     page.
     21   * (3) Close message is sent to close the opened window and the test finishes.
     22   */
     23 
     24  var pageshowCount = 0;
     25  var gotBeforeUnload = false;
     26  var bfcacheDisabled = false;
     27 
     28 
     29  function executeTest() {
     30    var bc = new BroadcastChannel("beforeunload_and_bfcache");
     31    bc.onmessage = function(event) {
     32      if (event.data.type == "pageshow") {
     33        ++pageshowCount;
     34        if (pageshowCount == 1) {
     35          bc.postMessage("nextpage");
     36        } else if (pageshowCount == 2) {
     37          bc.postMessage("back");
     38        } else if (pageshowCount == 3) {
     39          if (!event.data.persisted) {
     40            ok(true, "BFCache not enabled");
     41            bfcacheDisabled = true;
     42            bc.postMessage("close");
     43            return;
     44          }
     45          bc.postMessage("forward");
     46        } else if (pageshowCount == 4) {
     47          ok(event.data.persisted, "Should have loaded a page from bfcache.");
     48          bc.postMessage("close");
     49        }
     50      } else if (event.data == "beforeunload") {
     51        gotBeforeUnload = true;
     52      } else if (event.data == "closed") {
     53        isnot(bfcacheDisabled, gotBeforeUnload,
     54             "Either BFCache shouldn't be enabled or a beforeunload event should have been fired.");
     55        bc.close();
     56        SimpleTest.finish();
     57      }
     58    };
     59 
     60    function runTest() {
     61     SpecialPowers.pushPrefEnv({"set": [["docshell.shistory.bfcache.allow_unload_listeners", false]]},
     62       function() {
     63         window.open("file_beforeunload_and_bfcache.html", "", "noopener");
     64       }
     65     );
     66    }
     67    runTest();
     68  }
     69 
     70  if (isXOrigin) {
     71    // Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
     72    // Acquire storage access permission here so that the BroadcastChannel used to
     73    // communicate with the opened windows works in xorigin tests. Otherwise,
     74    // the iframe containing this page is isolated from first-party storage access,
     75    // which isolates BroadcastChannel communication.
     76    SpecialPowers.wrap(document).notifyUserGestureActivation();
     77    SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
     78      SpecialPowers.wrap(document).requestStorageAccess().then(() => {
     79        SpecialPowers.pushPrefEnv({
     80          set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]]
     81        }).then(() => {
     82          executeTest();
     83        });
     84      });
     85    });
     86  } else {
     87    executeTest();
     88  }
     89 
     90  </script>
     91 </head>
     92 <body>
     93 <p id="display"></p>
     94 <div id="content" style="display: none"></div>
     95 <pre id="test"></pre>
     96 </body>
     97 </html>