tor-browser

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

browser_keybindings_01.js (4183B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 requestLongerTimeout(3);
      7 
      8 // Tests that the keybindings for opening and closing the inspector work as expected
      9 // Can probably make this a shared test that tests all of the tools global keybindings
     10 const TEST_URL =
     11  "data:text/html,<html><head><title>Test for the " +
     12  "highlighter keybindings</title></head><body>" +
     13  "<h1>Keybindings!</h1></body></html>";
     14 
     15 const {
     16  gDevToolsBrowser,
     17 } = require("resource://devtools/client/framework/devtools-browser.js");
     18 
     19 const isMac = AppConstants.platform == "macosx";
     20 
     21 const allKeys = [];
     22 function buildDevtoolsKeysetMap(keyset) {
     23  // Fetches all the keyboard shortcuts which were defined by lazyGetter 'KeyShortcuts' in
     24  // devtools-startup.js and added to the DOM by 'hookKeyShortcuts'
     25  [...keyset.querySelectorAll("key")].forEach(key => {
     26    if (!key.getAttribute("key")) {
     27      return;
     28    }
     29 
     30    const modifiers = key.getAttribute("modifiers");
     31    allKeys.push({
     32      toolId: key.id.split("_")[1],
     33      key: key.getAttribute("key"),
     34      modifiers,
     35      modifierOpt: {
     36        shiftKey: modifiers.match("shift"),
     37        ctrlKey: modifiers.match("ctrl"),
     38        altKey: modifiers.match("alt"),
     39        metaKey: modifiers.match("meta"),
     40        accelKey: modifiers.match("accel"),
     41      },
     42      synthesizeKey() {
     43        EventUtils.synthesizeKey(this.key, this.modifierOpt);
     44      },
     45    });
     46  });
     47 }
     48 
     49 function setupKeyBindingsTest() {
     50  for (const win of gDevToolsBrowser._trackedBrowserWindows) {
     51    buildDevtoolsKeysetMap(win.document.getElementById("devtoolsKeyset"));
     52  }
     53 }
     54 
     55 add_task(async function () {
     56  await addTab(TEST_URL);
     57  await new Promise(done => waitForFocus(done));
     58 
     59  setupKeyBindingsTest();
     60 
     61  const tests = [
     62    { id: "inspector", toolId: "inspector" },
     63    { id: "webconsole", toolId: "webconsole" },
     64    { id: "netmonitor", toolId: "netmonitor" },
     65    { id: "jsdebugger", toolId: "jsdebugger" },
     66  ];
     67 
     68  // There are two possible keyboard shortcuts to open the inspector on macOS
     69  if (isMac) {
     70    tests.push({ id: "inspectorMac", toolId: "inspector" });
     71  }
     72 
     73  // Toolbox reference will be set by first tool to open.
     74  let toolbox;
     75 
     76  for (const test of tests) {
     77    const onToolboxReady = gDevTools.once("toolbox-ready");
     78    const onSelectTool = gDevTools.once("select-tool-command");
     79 
     80    info(`Run the keyboard shortcut for ${test.id}`);
     81    const key = allKeys.filter(({ toolId }) => toolId === test.id)[0];
     82    key.synthesizeKey();
     83 
     84    if (!toolbox) {
     85      toolbox = await onToolboxReady;
     86    }
     87 
     88    if (test.toolId === "inspector") {
     89      const onPickerStart = toolbox.nodePicker.once("picker-started");
     90      await onPickerStart;
     91      ok(true, "picker-started event received, highlighter started");
     92 
     93      info(
     94        `Run the keyboard shortcut for ${test.id} again to stop the node picker`
     95      );
     96      const onPickerStop = toolbox.nodePicker.once("picker-stopped");
     97      key.synthesizeKey();
     98      await onPickerStop;
     99      ok(true, "picker-stopped event received, highlighter stopped");
    100 
    101      info(
    102        `Run the keyboard shortcut for ${test.id} with picker shortcut disabled`
    103      );
    104      await pushPref("devtools.command-button-pick.enabled", false);
    105 
    106      // Switch to another panel to assure the hotkey still opens inspector
    107      await toolbox.selectTool("webconsole");
    108      await waitUntil(() => toolbox.currentToolId === "webconsole");
    109 
    110      // Check if picker event times out
    111      const onHasPickerStarted = Promise.race([
    112        toolbox.nodePicker.once("picker-started").then(() => true),
    113        wait(100).then(() => false),
    114      ]);
    115 
    116      key.synthesizeKey();
    117      const hasPickerStarted = await onHasPickerStarted;
    118 
    119      ok(!hasPickerStarted, "picker was not started on shortcut");
    120      is(
    121        toolbox.currentToolId,
    122        "inspector",
    123        "shortcut still switches tab to inspector"
    124      );
    125 
    126      await pushPref("devtools.command-button-pick.enabled", true);
    127    }
    128 
    129    await onSelectTool;
    130    is(toolbox.currentToolId, test.toolId, `${test.toolId} should be selected`);
    131  }
    132 
    133  gBrowser.removeCurrentTab();
    134 });