tor-browser

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

session-history-harness.js (2188B)


      1 // We don't have the test harness in this context, so we roll our own
      2 // which communicates with our initiator which is actually running the tests.
      3 
      4 function assert(condition, message) {
      5  if (!condition) {
      6    throw new Error("Assertion failed: " + message);
      7  }
      8 }
      9 
     10 // Run a test after activation.
     11 document.addEventListener("prerenderingchange", async (_) => {
     12  // history.length is racy on activation. Wait *100ms* as a workaround.
     13  // See crbug.com/1222893.
     14  await new Promise((resolve) => {
     15    window.setTimeout(resolve, 100);
     16  });
     17 
     18  const urlParams = new URLSearchParams(window.location.search);
     19  const testName = urlParams.get("testName");
     20  const uid = urlParams.get("uid");
     21  const testChannel = new PrerenderChannel(
     22    `test-channel-${testName}`, uid
     23  );
     24 
     25  try {
     26    const activationTestFn = testName + "Activation";
     27    const testFn = window[activationTestFn];
     28    if (!testFn) {
     29      testChannel.postMessage("Missing test: " + testName);
     30      return;
     31    }
     32    testFn();
     33    testChannel.postMessage("Passed");
     34  } catch (e) {
     35    testChannel.postMessage(
     36      "Failed: " + e.name + ": " + e.message,
     37    );
     38  } finally {
     39    testChannel.close();
     40  }
     41 })
     42 
     43 if (document.prerendering) {
     44  window.onload = async () => {
     45    const urlParams = new URLSearchParams(window.location.search);
     46    const testName = urlParams.get("testName");
     47    const uid = urlParams.get("uid");
     48    const prerenderChannel = new PrerenderChannel(
     49      `prerender-channel-${testName}`, uid
     50    );
     51 
     52    // The document load event is not finished at this point, so navigations
     53    // would be done with replacement. This interferes with our tests. We wait
     54    // for the next task before navigating to avoid this.
     55    await new Promise((resolve) => {
     56      window.setTimeout(resolve);
     57    });
     58 
     59    try {
     60      let testFn = window[testName];
     61      if (!testFn) {
     62        prerenderChannel.postMessage("Missing test: " + testName);
     63        return;
     64      }
     65      await testFn();
     66      prerenderChannel.postMessage("Passed");
     67    } catch (e) {
     68      prerenderChannel.postMessage(
     69        "Failed: " + e.name + ": " + e.message,
     70      );
     71    } finally {
     72      prerenderChannel.close();
     73    }
     74  };
     75 }