tor-browser

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

browser_scriptCache_clear.js (4591B)


      1 requestLongerTimeout(2);
      2 
      3 const TEST_URL = "https://example.com/browser/dom/tests/browser/dummy.html";
      4 const SCRIPT_NAME = "counter_server.sjs";
      5 const TEST_SCRIPT_URL =
      6  "https://example.com/browser/dom/tests/browser/" + SCRIPT_NAME;
      7 
      8 function getCounter(tab, type) {
      9  const browser = tab.linkedBrowser;
     10  return SpecialPowers.spawn(
     11    browser,
     12    [SCRIPT_NAME, type],
     13    async (scriptName, type) => {
     14      const { promise, resolve } = Promise.withResolvers();
     15 
     16      const script = content.document.createElement("script");
     17      switch (type) {
     18        case "script":
     19          script.addEventListener("load", resolve);
     20          script.src = scriptName;
     21          break;
     22        case "module-top-level":
     23          script.addEventListener("load", resolve);
     24          script.type = "module";
     25          script.src = scriptName;
     26          break;
     27        case "module-static":
     28          content.document.addEventListener("module-loaded", resolve);
     29          script.type = "module";
     30          script.textContent = `
     31 import "./${scriptName}";
     32 document.dispatchEvent(new CustomEvent("module-loaded"));
     33 `;
     34          break;
     35        case "module-dynamic":
     36          content.document.addEventListener("module-loaded", resolve);
     37          script.type = "module";
     38          script.textContent = `
     39 await import("./${scriptName}");
     40 document.dispatchEvent(new CustomEvent("module-loaded"));
     41 `;
     42          break;
     43      }
     44      content.document.body.appendChild(script);
     45 
     46      await promise;
     47 
     48      return parseInt(content.document.body.getAttribute("counter"));
     49    }
     50  );
     51 }
     52 
     53 async function reloadAndGetCounter(tab, type) {
     54  await BrowserTestUtils.reloadTab(tab);
     55 
     56  return getCounter(tab, type);
     57 }
     58 
     59 async function doTest(useNavigationCache, type) {
     60  await SpecialPowers.pushPrefEnv({
     61    set: [
     62      ["dom.script_loader.experimental.navigation_cache", useNavigationCache],
     63    ],
     64  });
     65  registerCleanupFunction(() => SpecialPowers.popPrefEnv());
     66 
     67  ChromeUtils.clearResourceCache();
     68  Services.cache2.clear();
     69 
     70  const resetResponse = await fetch(TEST_SCRIPT_URL + "?reset");
     71  is(await resetResponse.text(), "reset", "Server state should be reset");
     72 
     73  const tab = await BrowserTestUtils.openNewForegroundTab({
     74    gBrowser,
     75    url: TEST_URL,
     76  });
     77 
     78  is(await getCounter(tab, type), 0, "counter should be 0 for the first load.");
     79 
     80  is(
     81    await reloadAndGetCounter(tab, type),
     82    0,
     83    "cache should be used for subsequent load."
     84  );
     85 
     86  ChromeUtils.clearResourceCache();
     87  Services.cache2.clear();
     88  is(
     89    await reloadAndGetCounter(tab, type),
     90    1,
     91    "request should reach the server after removing all cache."
     92  );
     93  is(
     94    await reloadAndGetCounter(tab, type),
     95    1,
     96    "cache should be used for subsequent load."
     97  );
     98 
     99  ChromeUtils.clearResourceCache();
    100  is(await reloadAndGetCounter(tab, type), 1, "network cache should be used.");
    101 
    102  Services.cache2.clear();
    103  if (!useNavigationCache) {
    104    is(
    105      await reloadAndGetCounter(tab, type),
    106      2,
    107      "request should reach the server after network cache is cleared."
    108    );
    109  } else {
    110    // The above reload loads from the network cache, and the JS cache is
    111    // re-created.
    112 
    113    is(await reloadAndGetCounter(tab, type), 1, "JS cache should be used.");
    114  }
    115 
    116  ChromeUtils.clearResourceCache();
    117  if (!useNavigationCache) {
    118    is(
    119      await reloadAndGetCounter(tab, type),
    120      2,
    121      "network cache should be used."
    122    );
    123  } else {
    124    // The above reload loads from the JS cache.
    125    // Currently, network cache is not re-created from the JS cache.
    126    is(
    127      await reloadAndGetCounter(tab, type),
    128      2,
    129      "request should reach the server after both cache is cleared."
    130    );
    131  }
    132 
    133  BrowserTestUtils.removeTab(tab);
    134 }
    135 
    136 add_task(async function test_scriptWithoutNavigationCache() {
    137  await doTest(false, "script");
    138 });
    139 
    140 add_task(async function test_scriptWithNavigationCache() {
    141  await doTest(true, "script");
    142 });
    143 
    144 add_task(async function test_moduleTopLevelWithoutNavigationCache() {
    145  await doTest(false, "module-top-level");
    146 });
    147 
    148 add_task(async function test_moduleTopLevelWithNavigationCache() {
    149  await doTest(true, "module-top-level");
    150 });
    151 
    152 add_task(async function test_moduleStaticWithoutNavigationCache() {
    153  await doTest(false, "module-static");
    154 });
    155 
    156 add_task(async function test_moduleStaticWithNavigationCache() {
    157  await doTest(true, "module-static");
    158 });
    159 
    160 add_task(async function test_moduleDynamicWithoutNavigationCache() {
    161  await doTest(false, "module-dynamic");
    162 });
    163 
    164 add_task(async function test_moduleDynamicWithNavigationCache() {
    165  await doTest(true, "module-dynamic");
    166 });