tor-browser

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

browser_toolbox_disable_f12.js (3229B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function () {
      7  const tab = await addTab(
      8    "https://example.com/document-builder.sjs?html=test"
      9  );
     10 
     11  info("Enable F12 and check that devtools open");
     12  await pushPref("devtools.f12_enabled", true);
     13  await assertToolboxOpens(tab, { shouldOpen: true });
     14  await assertToolboxCloses(tab, { shouldClose: true });
     15 
     16  info("Disable F12 and check that devtools will not open");
     17  await pushPref("devtools.f12_enabled", false);
     18  await assertToolboxOpens(tab, { shouldOpen: false });
     19 
     20  info("Enable F12 again and open devtools");
     21  await pushPref("devtools.f12_enabled", true);
     22  await assertToolboxOpens(tab, { shouldOpen: true });
     23 
     24  info("Disable F12 and check F12 no longer closes devtools");
     25  await pushPref("devtools.f12_enabled", false);
     26  await assertToolboxCloses(tab, { shouldClose: false });
     27 
     28  info("Enable F12 and close devtools");
     29  await pushPref("devtools.f12_enabled", true);
     30  await assertToolboxCloses(tab, { shouldClose: true });
     31 
     32  info("Disable F12 and check other shortcuts still work");
     33  await pushPref("devtools.f12_enabled", false);
     34  const isMac = Services.appinfo.OS == "Darwin";
     35  const shortcut = {
     36    key: "i",
     37    options: { accelKey: true, altKey: isMac, shiftKey: !isMac },
     38  };
     39  await assertToolboxOpens(tab, { shouldOpen: true, shortcut });
     40  // Check F12 still doesn't close the toolbox
     41  await assertToolboxCloses(tab, { shouldClose: false });
     42  await assertToolboxCloses(tab, { shouldClose: true, shortcut });
     43 
     44  gBrowser.removeTab(tab);
     45 });
     46 
     47 const assertToolboxCloses = async function (tab, { shortcut, shouldClose }) {
     48  info(
     49    `Use ${
     50      shortcut ? "shortcut" : "F12"
     51    } to close the toolbox (close expected: ${shouldClose})`
     52  );
     53  const onToolboxDestroy = gDevTools.once("toolbox-destroyed");
     54 
     55  if (shortcut) {
     56    EventUtils.synthesizeKey(shortcut.key, shortcut.options);
     57  } else {
     58    EventUtils.synthesizeKey("VK_F12", {});
     59  }
     60 
     61  if (shouldClose) {
     62    await onToolboxDestroy;
     63  } else {
     64    const onTimeout = wait(1000).then(() => "TIMEOUT");
     65    const res = await Promise.race([onTimeout, onToolboxDestroy]);
     66    is(res, "TIMEOUT", "No toolbox-destroyed event received");
     67  }
     68  is(
     69    !gDevTools.getToolboxForTab(tab),
     70    shouldClose,
     71    `Toolbox was ${shouldClose ? "" : "not "}closed for the test tab`
     72  );
     73 };
     74 
     75 const assertToolboxOpens = async function (tab, { shortcut, shouldOpen }) {
     76  info(
     77    `Use ${
     78      shortcut ? "shortcut" : "F12"
     79    } to open the toolbox (open expected: ${shouldOpen})`
     80  );
     81  const onToolboxReady = gDevTools.once("toolbox-ready");
     82 
     83  if (shortcut) {
     84    EventUtils.synthesizeKey(shortcut.key, shortcut.options);
     85  } else {
     86    EventUtils.synthesizeKey("VK_F12", {});
     87  }
     88 
     89  if (shouldOpen) {
     90    await onToolboxReady;
     91    info(`Received toolbox-ready`);
     92  } else {
     93    const onTimeout = wait(1000).then(() => "TIMEOUT");
     94    const res = await Promise.race([onTimeout, onToolboxReady]);
     95    is(res, "TIMEOUT", "No toolbox-ready event received");
     96  }
     97  is(
     98    !!gDevTools.getToolboxForTab(tab),
     99    shouldOpen,
    100    `Toolbox was ${shouldOpen ? "" : "not "}opened for the test tab`
    101  );
    102 };