tor-browser

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

browser_shim_disable_devtools.js (5151B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { require } = ChromeUtils.importESModule(
      7  "resource://devtools/shared/loader/Loader.sys.mjs"
      8 );
      9 
     10 const { gDevTools } = require("devtools/client/framework/devtools");
     11 
     12 async function simulateMenuOpen(menu) {
     13  return new Promise(resolve => {
     14    menu.addEventListener("popupshown", resolve, { once: true });
     15    menu.dispatchEvent(new MouseEvent("popupshowing"));
     16    menu.dispatchEvent(new MouseEvent("popupshown"));
     17  });
     18 }
     19 
     20 async function simulateMenuClosed(menu) {
     21  return new Promise(resolve => {
     22    menu.addEventListener("popuphidden", resolve, { once: true });
     23    menu.dispatchEvent(new MouseEvent("popuphiding"));
     24    menu.dispatchEvent(new MouseEvent("popuphidden"));
     25  });
     26 }
     27 
     28 /**
     29 * Test that the preference devtools.policy.disabled disables entry points for devtools.
     30 */
     31 add_task(async function () {
     32  info(
     33    "Disable DevTools entry points (does not apply to the already created window"
     34  );
     35  await new Promise(resolve => {
     36    const options = { set: [["devtools.policy.disabled", true]] };
     37    SpecialPowers.pushPrefEnv(options, resolve);
     38  });
     39 
     40  // In DEV_EDITION the browser starts with the developer-button in the toolbar. This
     41  // applies to all new windows and forces creating keyboard shortcuts. The preference
     42  // tested should not change without restart, but for the needs of the test, remove the
     43  // developer-button from the UI before opening a new window.
     44  if (AppConstants.MOZ_DEV_EDITION) {
     45    CustomizableUI.removeWidgetFromArea("developer-button");
     46  }
     47 
     48  info(
     49    "Open a new window, all window-specific hooks for DevTools will be disabled."
     50  );
     51  const win = OpenBrowserWindow({ private: false });
     52  await waitForDelayedStartupFinished(win);
     53 
     54  info(
     55    "Open a new tab on the new window to ensure the focus is on the new window"
     56  );
     57  const tab = BrowserTestUtils.addTab(
     58    win.gBrowser,
     59    "data:text/html;charset=utf-8,<title>foo</title>"
     60  );
     61  await BrowserTestUtils.browserLoaded(win.gBrowser.getBrowserForTab(tab));
     62 
     63  info(
     64    "Synthesize a DevTools shortcut, the toolbox should not open on this new window."
     65  );
     66  synthesizeToggleToolboxKey(win);
     67 
     68  // There is no event to wait for here as this shortcut should have no effect.
     69  /* eslint-disable mozilla/no-arbitrary-setTimeout */
     70  await new Promise(r => setTimeout(r, 1000));
     71 
     72  is(gDevTools._toolboxesPerCommands.size, 0, "No toolbox has been opened");
     73 
     74  info("Open the context menu for the content page.");
     75  const contextMenu = win.document.getElementById("contentAreaContextMenu");
     76  const popupShownPromise = BrowserTestUtils.waitForEvent(
     77    contextMenu,
     78    "popupshown"
     79  );
     80  EventUtils.synthesizeMouseAtCenter(
     81    win.document.documentElement,
     82    { type: "contextmenu", button: 2 },
     83    win
     84  );
     85  await popupShownPromise;
     86 
     87  const inspectElementItem = contextMenu.querySelector(`#context-inspect`);
     88  ok(
     89    inspectElementItem.hidden,
     90    "The inspect element item is hidden in the context menu"
     91  );
     92 
     93  info("Close the context menu");
     94  const onContextMenuHidden = BrowserTestUtils.waitForEvent(
     95    contextMenu,
     96    "popuphidden"
     97  );
     98  contextMenu.hidePopup();
     99  await onContextMenuHidden;
    100 
    101  info("Open the menubar Tools menu");
    102  const toolsMenuPopup = win.document.getElementById("menu_ToolsPopup");
    103  const browserToolsMenu = win.document.getElementById("browserToolsMenu");
    104  ok(
    105    !browserToolsMenu.hidden,
    106    "The Browser Tools item of the tools menu is visible"
    107  );
    108 
    109  await simulateMenuOpen(toolsMenuPopup);
    110  const subMenu = win.document.getElementById("menuWebDeveloperPopup");
    111 
    112  info("Open the Browser Tools sub-menu");
    113  await simulateMenuOpen(subMenu);
    114 
    115  const visibleMenuItems = Array.from(
    116    subMenu.querySelectorAll("menuitem")
    117  ).filter(item => !item.hidden);
    118 
    119  const { menuitems } = require("devtools/client/menus");
    120  for (const devtoolsItem of menuitems) {
    121    ok(
    122      !visibleMenuItems.some(item => item.id === devtoolsItem.id),
    123      "DevTools menu item is not visible in the Browser Tools menu"
    124    );
    125  }
    126 
    127  info("Close out the menu popups");
    128  await simulateMenuClosed(subMenu);
    129  await simulateMenuClosed(toolsMenuPopup);
    130 
    131  win.gBrowser.removeTab(tab);
    132 
    133  info("Close the test window");
    134  const winClosed = BrowserTestUtils.windowClosed(win);
    135  win.BrowserCommands.tryToCloseWindow();
    136  await winClosed;
    137 });
    138 
    139 function waitForDelayedStartupFinished(win) {
    140  return new Promise(resolve => {
    141    Services.obs.addObserver(function observer(subject) {
    142      if (win == subject) {
    143        Services.obs.removeObserver(
    144          observer,
    145          "browser-delayed-startup-finished"
    146        );
    147        resolve();
    148      }
    149    }, "browser-delayed-startup-finished");
    150  });
    151 }
    152 
    153 /**
    154 * Helper to call the toggle devtools shortcut.
    155 *
    156 * @param {Window} win
    157 */
    158 function synthesizeToggleToolboxKey(win) {
    159  info("Trigger the toogle toolbox shortcut");
    160  if (Services.appinfo.OS == "Darwin") {
    161    EventUtils.synthesizeKey("i", { accelKey: true, altKey: true }, win);
    162  } else {
    163    EventUtils.synthesizeKey("i", { accelKey: true, shiftKey: true }, win);
    164  }
    165 }