tor-browser

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

test_bug1729662.html (2108B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <meta charset="utf-8">
      5  <title>Test back/forward after pushState</title>
      6  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      7  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
      8  <script>
      9    SimpleTest.waitForExplicitFinish();
     10    SimpleTest.requestFlakyTimeout("Need to wait to make sure an event does not fire");
     11 
     12    function waitForEvent(target, event) {
     13      return new Promise(resolve =>
     14        target.addEventListener(event, resolve, { once: true }));
     15    }
     16 
     17    async function runTest() {
     18      // Target page needs to be the initial load so that the synchronously
     19      // available window is reused and we can attach popstate listeners.
     20      const win = window.open('file_bug1729662.html');
     21 
     22      let timeoutID;
     23      let loadCount = 0;
     24      let bc = new BroadcastChannel("bug1729662");
     25      bc.onmessage = ({ data }) => {
     26        if (data != 'load') return;
     27        if (++loadCount > 1) {
     28          // We should only get one load event in win
     29          clearTimeout(timeoutID);
     30        }
     31      };
     32 
     33      // We should only go back and forward once
     34      // The popstate for back will have state == null, see file_bug1729662.html
     35      const state1 = await waitForEvent(win, 'popstate');
     36      is(state1.state, null, 'got expected state for history.back');
     37 
     38      const state2 = await waitForEvent(win, 'popstate');
     39      is(state2.state, 1, 'got expected state for history.forward');
     40 
     41      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     42      const timeout = new Promise(resolve => timeoutID = setTimeout(resolve, 1000, 'timeout'));
     43      const result = await Promise.race([
     44        waitForEvent(win, 'popstate'),
     45        timeout
     46      ]);
     47      is(result, 'timeout', 'Got no more popstate');
     48 
     49      is(loadCount, isXOrigin ? 0 : 1, 'Got exactly one (zero for xorigin) load events in win');
     50 
     51      clearTimeout(timeoutID);
     52      win.close();
     53 
     54      SimpleTest.finish();
     55    }
     56  </script>
     57 </head>
     58 <body onload="runTest();">
     59 <p id="display"></p>
     60 <div id="content" style="display: none"></div>
     61 <pre id="test"></pre>
     62 </body>
     63 </html>