tor-browser

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

helper_disable_cache.js (3416B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // This file assumes we have head.js globals for the scope where this is loaded.
      7 /* import-globals-from head.js */
      8 
      9 /* exported initTab, checkCacheStateForAllTabs, setDisableCacheCheckboxChecked,
     10            finishUp */
     11 
     12 // Common code shared by browser_toolbox_options_disable_cache-*.js
     13 const TEST_URI = URL_ROOT + "browser_toolbox_options_disable_cache.sjs";
     14 var tabs = [
     15  {
     16    title: "Tab 0",
     17    desc: "Toggles cache on.",
     18    startToolbox: true,
     19  },
     20  {
     21    title: "Tab 1",
     22    desc: "Toolbox open before Tab 1 toggles cache.",
     23    startToolbox: true,
     24  },
     25  {
     26    title: "Tab 2",
     27    desc: "Opens toolbox after Tab 1 has toggled cache. Also closes and opens.",
     28    startToolbox: false,
     29  },
     30  {
     31    title: "Tab 3",
     32    desc: "No toolbox",
     33    startToolbox: false,
     34  },
     35 ];
     36 
     37 async function initTab(tabX, startToolbox) {
     38  tabX.tab = await addTab(TEST_URI);
     39 
     40  if (startToolbox) {
     41    tabX.toolbox = await gDevTools.showToolboxForTab(tabX.tab, {
     42      toolId: "options",
     43    });
     44  }
     45 }
     46 
     47 async function checkCacheStateForAllTabs(states) {
     48  for (let i = 0; i < tabs.length; i++) {
     49    const tab = tabs[i];
     50    await checkCacheEnabled(tab, states[i]);
     51  }
     52 }
     53 
     54 async function checkCacheEnabled(tabX, expected) {
     55  gBrowser.selectedTab = tabX.tab;
     56 
     57  await reloadTab(tabX);
     58 
     59  const oldGuid = await SpecialPowers.spawn(
     60    gBrowser.selectedBrowser,
     61    [],
     62    function () {
     63      const doc = content.document;
     64      const h1 = doc.querySelector("h1");
     65      return h1.textContent;
     66    }
     67  );
     68 
     69  await reloadTab(tabX);
     70 
     71  const guid = await SpecialPowers.spawn(
     72    gBrowser.selectedBrowser,
     73    [],
     74    function () {
     75      const doc = content.document;
     76      const h1 = doc.querySelector("h1");
     77      return h1.textContent;
     78    }
     79  );
     80 
     81  if (expected) {
     82    is(guid, oldGuid, tabX.title + " cache is enabled");
     83  } else {
     84    isnot(guid, oldGuid, tabX.title + " cache is not enabled");
     85  }
     86 }
     87 
     88 async function setDisableCacheCheckboxChecked(tabX, state) {
     89  gBrowser.selectedTab = tabX.tab;
     90 
     91  const panel = tabX.toolbox.getCurrentPanel();
     92  const cbx = panel.panelDoc.getElementById("devtools-disable-cache");
     93 
     94  if (cbx.checked !== state) {
     95    info("Setting disable cache checkbox to " + state + " for " + tabX.title);
     96    const onReconfigured = tabX.toolbox.once("new-configuration-applied");
     97    cbx.click();
     98 
     99    // We have to wait for the reconfigure request to be finished before reloading
    100    // the page.
    101    await onReconfigured;
    102  }
    103 }
    104 
    105 function reloadTab(tabX) {
    106  const browser = gBrowser.selectedBrowser;
    107 
    108  const reloadTabPromise = BrowserTestUtils.browserLoaded(browser).then(
    109    function () {
    110      info("Reloaded tab " + tabX.title);
    111    }
    112  );
    113 
    114  info("Reloading tab " + tabX.title);
    115  SpecialPowers.spawn(browser, [], () => {
    116    content.location.reload(false);
    117  });
    118 
    119  return reloadTabPromise;
    120 }
    121 
    122 async function destroyTab(tabX) {
    123  const toolbox = gDevTools.getToolboxForTab(tabX.tab);
    124 
    125  let onceDestroyed;
    126  if (toolbox) {
    127    onceDestroyed = gDevTools.once("toolbox-destroyed");
    128  }
    129 
    130  info("Removing tab " + tabX.title);
    131  gBrowser.removeTab(tabX.tab);
    132  info("Removed tab " + tabX.title);
    133 
    134  info("Waiting for toolbox-destroyed");
    135  await onceDestroyed;
    136 }
    137 
    138 async function finishUp() {
    139  for (const tab of tabs) {
    140    await destroyTab(tab);
    141  }
    142 
    143  tabs = null;
    144 }