tor-browser

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

browser_console_context_menu_entries.js (5659B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Check that we display the expected context menu entries.
      7 
      8 const TEST_URI =
      9  "http://example.com/browser/devtools/client/webconsole/" +
     10  "test/browser/test-console.html";
     11 
     12 add_task(async function () {
     13  await pushPref("devtools.browsertoolbox.scope", "everything");
     14  // Enable net messages in the console for this test.
     15  await pushPref("devtools.browserconsole.filter.net", true);
     16  await pushPref("devtools.browserconsole.filter.netxhr", true);
     17  // This is required for testing the text input in the browser console:
     18  await pushPref("devtools.chrome.enabled", true);
     19 
     20  await addTab(TEST_URI);
     21  const hud = await BrowserConsoleManager.toggleBrowserConsole();
     22 
     23  // Network monitoring is turned off by default in the browser console
     24  info("Turn on network monitoring");
     25  await toggleNetworkMonitoringConsoleSetting(hud, true);
     26 
     27  info("Reload the content window to produce a network log");
     28  const onNetworkMessage = waitForMessageByType(
     29    hud,
     30    "test-console.html",
     31    ".network"
     32  );
     33  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
     34    await content.wrappedJSObject.fetch("./test-console.html");
     35  });
     36  const networkMessage = await onNetworkMessage;
     37 
     38  info("Open and check the context menu for the network message");
     39  let menuPopup = await openContextMenu(hud, networkMessage.node);
     40  ok(menuPopup, "The context menu is displayed on a network message");
     41 
     42  let expectedContextMenu = addPrefBasedEntries([
     43    "#console-menu-copy-url (a)",
     44    "#console-menu-open-url (T)",
     45    "#console-menu-store (S) [disabled]",
     46    "#console-menu-copy (C)",
     47    "#console-menu-copy-object (o) [disabled]",
     48    "#console-menu-export-clipboard (M)",
     49    "#console-menu-export-file (F)",
     50  ]);
     51  is(
     52    getSimplifiedContextMenu(menuPopup).join("\n"),
     53    expectedContextMenu.join("\n"),
     54    "The context menu has the expected entries for a network message"
     55  );
     56  await hideContextMenu(hud);
     57 
     58  info("Logging a text message in the content window");
     59  const onLogMessage = waitForMessageByType(
     60    hud,
     61    "simple text message",
     62    ".console-api"
     63  );
     64  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
     65    content.wrappedJSObject.console.log("simple text message");
     66  });
     67 
     68  const logMessage = await onLogMessage;
     69  menuPopup = await openContextMenu(hud, logMessage.node);
     70  ok(menuPopup, "The context menu is displayed on a log message");
     71 
     72  expectedContextMenu = addPrefBasedEntries([
     73    "#console-menu-store (S) [disabled]",
     74    "#console-menu-copy (C)",
     75    "#console-menu-copy-object (o) [disabled]",
     76    "#console-menu-export-clipboard (M)",
     77    "#console-menu-export-file (F)",
     78  ]);
     79  is(
     80    getSimplifiedContextMenu(menuPopup).join("\n"),
     81    expectedContextMenu.join("\n"),
     82    "The context menu has the expected entries for a simple log message"
     83  );
     84  await hideContextMenu(hud);
     85 
     86  menuPopup = await openContextMenu(hud, hud.jsterm.node);
     87 
     88  let actualEntries = getL10NContextMenu(menuPopup);
     89  is(
     90    actualEntries.length,
     91    6,
     92    "The context menu has the right number of entries."
     93  );
     94  is(actualEntries[0], "#editmenu-undo (text-action-undo) [disabled]");
     95  is(actualEntries[1], "#editmenu-cut (text-action-cut) [disabled]");
     96  is(actualEntries[2], "#editmenu-copy (text-action-copy) [disabled]");
     97  // Paste may or may not be enabled depending on what ran before this.
     98  // If emptyClipboard is fixed (666254) we could assert if it's enabled/disabled.
     99  ok(actualEntries[3].startsWith("#editmenu-paste (text-action-paste)"));
    100  is(actualEntries[4], "#editmenu-delete (text-action-delete) [disabled]");
    101  is(
    102    actualEntries[5],
    103    "#editmenu-selectAll (text-action-select-all) [disabled]"
    104  );
    105  await hideContextMenu(hud);
    106 
    107  const node = hud.jsterm.node;
    108  const inputContainer = node.closest(".jsterm-input-container");
    109  await openContextMenu(hud, inputContainer);
    110 
    111  actualEntries = getL10NContextMenu(menuPopup);
    112  is(
    113    actualEntries.length,
    114    6,
    115    "The context menu has the right number of entries."
    116  );
    117  is(actualEntries[0], "#editmenu-undo (text-action-undo) [disabled]");
    118  is(actualEntries[1], "#editmenu-cut (text-action-cut) [disabled]");
    119  is(actualEntries[2], "#editmenu-copy (text-action-copy) [disabled]");
    120  // Paste may or may not be enabled depending on what ran before this.
    121  // If emptyClipboard is fixed (666254) we could assert if it's enabled/disabled.
    122  ok(actualEntries[3].startsWith("#editmenu-paste (text-action-paste)"));
    123  is(actualEntries[4], "#editmenu-delete (text-action-delete) [disabled]");
    124  is(
    125    actualEntries[5],
    126    "#editmenu-selectAll (text-action-select-all) [disabled]"
    127  );
    128 
    129  await hideContextMenu(hud);
    130  await toggleNetworkMonitoringConsoleSetting(hud, false);
    131 });
    132 
    133 function addPrefBasedEntries(expectedEntries) {
    134  if (Services.prefs.getBoolPref("devtools.webconsole.sidebarToggle", false)) {
    135    expectedEntries.push("#console-menu-open-sidebar (V) [disabled]");
    136  }
    137 
    138  return expectedEntries;
    139 }
    140 
    141 function getL10NContextMenu(popupElement) {
    142  return [...popupElement.querySelectorAll("menuitem")].map(entry => {
    143    const l10nID = entry.getAttribute("data-l10n-id");
    144    const disabled = entry.hasAttribute("disabled");
    145    return `#${entry.id} (${l10nID})${disabled ? " [disabled]" : ""}`;
    146  });
    147 }
    148 
    149 function getSimplifiedContextMenu(popupElement) {
    150  return [...popupElement.querySelectorAll("menuitem")].map(entry => {
    151    const key = entry.getAttribute("accesskey");
    152    const disabled = entry.hasAttribute("disabled");
    153    return `#${entry.id} (${key})${disabled ? " [disabled]" : ""}`;
    154  });
    155 }