tor-browser

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

unload-helper.js (1913B)


      1 // Code used by controlling frame of the unload policy tests.
      2 
      3 const MAIN_FRAME = 'main';
      4 const SUBFRAME = 'sub';
      5 
      6 async function isUnloadAllowed(remoteContextWrapper) {
      7  return remoteContextWrapper.executeScript(() => {
      8    return document.featurePolicy.allowsFeature('unload');
      9  });
     10 }
     11 
     12 // Checks whether a frame allows running unload handlers by checking the policy.
     13 async function assertWindowAllowsUnload(
     14    remoteContextWrapper, name, {shouldRunUnload}) {
     15  const maybeNot = shouldRunUnload ? '' : 'not ';
     16  assert_equals(
     17      await isUnloadAllowed(remoteContextWrapper), shouldRunUnload,
     18      `${name}: unload in ${name} should ${maybeNot}be allowed`);
     19 }
     20 
     21 // Checks whether a frame runs unload handlers.
     22 // This checks the policy directly and also installs an unload handler and
     23 // navigates the frame checking that the handler ran.
     24 async function assertWindowRunsUnload(
     25    remoteContextWrapper, name, {shouldRunUnload}) {
     26  await assertWindowAllowsUnload(remoteContextWrapper, name, {shouldRunUnload});
     27  const maybeNot = shouldRunUnload ? '' : 'not ';
     28 
     29  // Set up recording of whether unload handler ran.
     30  await remoteContextWrapper.executeScript((name) => {
     31    localStorage.setItem(name, 'did not run');
     32    addEventListener('unload', () => localStorage.setItem(name, 'did run'));
     33  }, [name]);
     34 
     35  // Navigate away and then back.
     36  const second = await remoteContextWrapper.navigateToNew();
     37  // Navigating back ensures that the unload has completed.
     38  // Also if the navigation is cross-site then we have to return
     39  // to the original origin in order to read the recorded unload.
     40  second.historyBack();
     41 
     42  // Check that unload handlers ran as expected.
     43  const recordedUnload = await remoteContextWrapper.executeScript(
     44      (name) => localStorage.getItem(name), [name]);
     45  assert_equals(
     46      recordedUnload, `did ${maybeNot}run`,
     47      `${name}: unload should ${maybeNot}have run`);
     48 }