tor-browser

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

test_ship_beforeunload_fired_2.html (2573B)


      1 <html>
      2  <head>
      3    <title>
      4      Test that ensures beforeunload is fired when session-history-in-parent is enabled
      5    </title>
      6    <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7    <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
      8  </head>
      9  <script>
     10    SimpleTest.waitForExplicitFinish();
     11 
     12    /*
     13     * This test ensures beforeunload is fired on the current page
     14     * when it is entering BFCache, and the next page is not coming from
     15     * session history and also not coming out from BFCache.
     16     *
     17     * (1) The controller page opens a new window, and page A is loaded there.
     18     * (2) Page A then navigates to page B, and a beforeunload event
     19     *     listener is registered on page B.
     20     * (3) Page B then navigates to page C, and the beforeunload handler
     21     *     should send a message to the controller page.
     22     * (4) Page C then navigates back to page B to check if page B has
     23     *     been successfully added to BFCache.
     24     */
     25 
     26    var bc = SpecialPowers.wrap(BroadcastChannel).unpartitionedTestingChannel("ship_beforeunload");
     27    var pageshowCount = 0;
     28 
     29    var beforeUnloadFired = false;
     30    bc.onmessage = function(event) {
     31      if (event.data.type == "pageshow") {
     32        ++pageshowCount;
     33        if (pageshowCount == 1) {
     34          bc.postMessage({action: "navigate_to_page_b"});
     35        } else if (pageshowCount == 2) {
     36          ok(!event.data.persisted, "?page B shouldn't in BFCache because it's the first navigation");
     37          bc.postMessage({action: "register_beforeunload",
     38            loadNextPageFromSessionHistory: false});
     39        } else if (pageshowCount == 3) {
     40          ok(!event.data.persisted, "navigated to page C that was a new page");
     41          ok(beforeUnloadFired, "beforeUnload should be fired on page B");
     42          bc.postMessage({action: "back_to_page_b", forwardNavigateToPageB: false});
     43        } else if (pageshowCount == 4) {
     44          ok(event.data.persisted, "page B has been successfully added to BFCache");
     45          bc.postMessage({action: "close"});
     46          SimpleTest.finish();
     47        }
     48      } else if (event.data == "beforeunload_fired") {
     49        beforeUnloadFired = true;
     50      }
     51    }
     52 
     53    function runTest() {
     54      SpecialPowers.pushPrefEnv({"set": [
     55          ["fission.bfcacheInParent", true],
     56          ["docshell.shistory.bfcache.ship_allow_beforeunload_listeners", true]
     57        ]},
     58        function() {
     59          window.open("file_ship_beforeunload_fired.html", "", "noopener");
     60        }
     61      );
     62    }
     63  </script>
     64  <body onload="runTest()"></body>
     65 </html>