tor-browser

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

browser_toolbox_options_disable_js.js (3941B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests that disabling JavaScript for a tab works as it should.
      5 
      6 const TEST_URI = URL_ROOT_SSL + "browser_toolbox_options_disable_js.html";
      7 
      8 add_task(async function () {
      9  const tab = await addTab(TEST_URI);
     10 
     11  // Start on the options panel from where we will toggle the disabling javascript
     12  // option.
     13  const toolbox = await gDevTools.showToolboxForTab(tab, { toolId: "options" });
     14 
     15  await testJSEnabled();
     16  await testJSEnabledIframe();
     17 
     18  // Disable JS.
     19  await toggleJS(toolbox);
     20 
     21  await testJSDisabled();
     22  await testJSDisabledIframe();
     23 
     24  // Navigate and check JS is still disabled
     25  for (let i = 0; i < 10; i++) {
     26    await navigateTo(`${TEST_URI}?nocache=${i}`);
     27    await testJSDisabled();
     28    await testJSDisabledIframe();
     29  }
     30 
     31  // Re-enable JS.
     32  await toggleJS(toolbox);
     33 
     34  await testJSEnabled();
     35  await testJSEnabledIframe();
     36 
     37  await toolbox.destroy();
     38  gBrowser.removeCurrentTab();
     39 });
     40 
     41 async function testJSEnabled() {
     42  info("Testing that JS is enabled");
     43 
     44  // We use waitForTick here because switching browsingContext.allowJavascript
     45  // to true takes a while to become live.
     46  await waitForTick();
     47 
     48  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     49    const doc = content.document;
     50    const output = doc.getElementById("output");
     51    doc.querySelector("#logJSEnabled").click();
     52    is(
     53      output.textContent,
     54      "JavaScript Enabled",
     55      'Output is "JavaScript Enabled"'
     56    );
     57  });
     58 }
     59 
     60 async function testJSEnabledIframe() {
     61  info("Testing that JS is enabled in the iframe");
     62 
     63  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
     64    const doc = content.document;
     65    const iframe = doc.querySelector("iframe");
     66    const iframeDoc = iframe.contentDocument;
     67    const output = iframeDoc.getElementById("output");
     68    iframeDoc.querySelector("#logJSEnabled").click();
     69    is(
     70      output.textContent,
     71      "JavaScript Enabled",
     72      'Output is "JavaScript Enabled" in iframe'
     73    );
     74  });
     75 }
     76 
     77 async function toggleJS(toolbox) {
     78  const panel = toolbox.getCurrentPanel();
     79  const cbx = panel.panelDoc.getElementById("devtools-disable-javascript");
     80 
     81  if (cbx.checked) {
     82    info("Clearing checkbox to re-enable JS");
     83  } else {
     84    info("Checking checkbox to disable JS");
     85  }
     86 
     87  let javascriptEnabled =
     88    await toolbox.commands.targetConfigurationCommand.isJavascriptEnabled();
     89  is(
     90    javascriptEnabled,
     91    !cbx.checked,
     92    "targetConfigurationCommand.isJavascriptEnabled is correct before the toggle"
     93  );
     94 
     95  const browserLoaded = BrowserTestUtils.browserLoaded(
     96    gBrowser.selectedBrowser
     97  );
     98  cbx.click();
     99  await browserLoaded;
    100 
    101  javascriptEnabled =
    102    await toolbox.commands.targetConfigurationCommand.isJavascriptEnabled();
    103  is(
    104    javascriptEnabled,
    105    !cbx.checked,
    106    "targetConfigurationCommand.isJavascriptEnabled is correctly updated"
    107  );
    108 }
    109 
    110 async function testJSDisabled() {
    111  info("Testing that JS is disabled");
    112 
    113  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    114    const doc = content.document;
    115    const output = doc.getElementById("output");
    116    doc.querySelector("#logJSDisabled").click();
    117 
    118    Assert.notStrictEqual(
    119      output.textContent,
    120      "JavaScript Disabled",
    121      'output is not "JavaScript Disabled"'
    122    );
    123  });
    124 }
    125 
    126 async function testJSDisabledIframe() {
    127  info("Testing that JS is disabled in the iframe");
    128 
    129  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    130    const doc = content.document;
    131    const iframe = doc.querySelector("iframe");
    132    const iframeDoc = iframe.contentDocument;
    133    const output = iframeDoc.getElementById("output");
    134    iframeDoc.querySelector("#logJSDisabled").click();
    135    Assert.notStrictEqual(
    136      output.textContent,
    137      "JavaScript Disabled",
    138      'output is not "JavaScript Disabled" in iframe'
    139    );
    140  });
    141 }