tor-browser

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

browser_console_content_object_in_sidebar.js (5393B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that the "Open in sidebar" context menu entry is active for
      5 // the content objects and opens the sidebar when clicked.
      6 
      7 "use strict";
      8 
      9 const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><script>
     10  console.log(
     11    {a:1},
     12    100,
     13    {b:1},
     14    'foo',
     15    false,
     16    null,
     17    undefined
     18  );
     19 </script>`;
     20 
     21 add_task(async function () {
     22  // Enable sidebar
     23  await pushPref("devtools.webconsole.sidebarToggle", true);
     24  // Show the content messages
     25  await pushPref("devtools.browsertoolbox.scope", "everything");
     26 
     27  await addTab(TEST_URI);
     28 
     29  info("Open the Browser Console");
     30  const hud = await BrowserConsoleManager.toggleBrowserConsole();
     31 
     32  const message = await waitFor(() => findConsoleAPIMessage(hud, "foo"));
     33  const [objectA, objectB] = message.querySelectorAll(
     34    ".object-inspector .objectBox-object"
     35  );
     36  const number = findMessagePartByType(hud, {
     37    text: "100",
     38    typeSelector: ".console-api",
     39    partSelector: ".objectBox",
     40  });
     41  const string = findMessagePartByType(hud, {
     42    text: "foo",
     43    typeSelector: ".console-api",
     44    partSelector: ".objectBox",
     45  });
     46  const bool = findMessagePartByType(hud, {
     47    text: "false",
     48    typeSelector: ".console-api",
     49    partSelector: ".objectBox",
     50  });
     51  const nullMessage = findMessagePartByType(hud, {
     52    text: "null",
     53    typeSelector: ".console-api",
     54    partSelector: ".objectBox",
     55  });
     56  const undefinedMsg = findMessagePartByType(hud, {
     57    text: "undefined",
     58    typeSelector: ".console-api",
     59    partSelector: ".objectBox",
     60  });
     61 
     62  info("Showing sidebar for {a:1}");
     63  await showSidebarWithContextMenu(hud, objectA, true);
     64 
     65  let sidebarContents = hud.ui.document.querySelector(".sidebar-contents");
     66  let objectInspector = sidebarContents.querySelector(".object-inspector");
     67  let oiNodes = objectInspector.querySelectorAll(".node");
     68  if (oiNodes.length === 1) {
     69    // If this is the case, we wait for the properties to be fetched and displayed.
     70    await waitForNodeMutation(objectInspector, {
     71      childList: true,
     72    });
     73  }
     74 
     75  let sidebarText =
     76    hud.ui.document.querySelector(".sidebar-contents").textContent;
     77  ok(sidebarText.includes("a: 1"), "Sidebar is shown for {a:1}");
     78 
     79  info("Showing sidebar for {a:1} again");
     80  await showSidebarWithContextMenu(hud, objectA, false);
     81  ok(
     82    hud.ui.document.querySelector(".sidebar"),
     83    "Sidebar is still shown after clicking on same object"
     84  );
     85  is(
     86    hud.ui.document.querySelector(".sidebar-contents").textContent,
     87    sidebarText,
     88    "Sidebar is not updated after clicking on same object"
     89  );
     90 
     91  info("Showing sidebar for {b:1}");
     92  await showSidebarWithContextMenu(hud, objectB, false);
     93 
     94  sidebarContents = hud.ui.document.querySelector(".sidebar-contents");
     95  objectInspector = sidebarContents.querySelector(".object-inspector");
     96  oiNodes = objectInspector.querySelectorAll(".node");
     97  if (oiNodes.length === 1) {
     98    // If this is the case, we wait for the properties to be fetched and displayed.
     99    await waitForNodeMutation(objectInspector, {
    100      childList: true,
    101    });
    102  }
    103 
    104  isnot(
    105    hud.ui.document.querySelector(".sidebar-contents").textContent,
    106    sidebarText,
    107    "Sidebar is updated for {b:1}"
    108  );
    109  sidebarText = hud.ui.document.querySelector(".sidebar-contents").textContent;
    110 
    111  ok(sidebarText.includes("b: 1"), "Sidebar contents shown for {b:1}");
    112 
    113  info("Checking context menu entry is disabled for number");
    114  const numberContextMenuEnabled = await isContextMenuEntryEnabled(hud, number);
    115  ok(!numberContextMenuEnabled, "Context menu entry is disabled for number");
    116 
    117  info("Checking context menu entry is disabled for string");
    118  const stringContextMenuEnabled = await isContextMenuEntryEnabled(hud, string);
    119  ok(!stringContextMenuEnabled, "Context menu entry is disabled for string");
    120 
    121  info("Checking context menu entry is disabled for bool");
    122  const boolContextMenuEnabled = await isContextMenuEntryEnabled(hud, bool);
    123  ok(!boolContextMenuEnabled, "Context menu entry is disabled for bool");
    124 
    125  info("Checking context menu entry is disabled for null message");
    126  const nullContextMenuEnabled = await isContextMenuEntryEnabled(
    127    hud,
    128    nullMessage
    129  );
    130  ok(!nullContextMenuEnabled, "Context menu entry is disabled for nullMessage");
    131 
    132  info("Checking context menu entry is disabled for undefined message");
    133  const undefinedContextMenuEnabled = await isContextMenuEntryEnabled(
    134    hud,
    135    undefinedMsg
    136  );
    137  ok(
    138    !undefinedContextMenuEnabled,
    139    "Context menu entry is disabled for undefinedMsg"
    140  );
    141 });
    142 
    143 async function showSidebarWithContextMenu(hud, node, expectMutation) {
    144  const appNode = hud.ui.document.querySelector(".webconsole-app");
    145  const onSidebarShown = waitForNodeMutation(appNode, { childList: true });
    146 
    147  const contextMenu = await openContextMenu(hud, node);
    148  const openInSidebar = contextMenu.querySelector("#console-menu-open-sidebar");
    149  openInSidebar.click();
    150  if (expectMutation) {
    151    await onSidebarShown;
    152  }
    153  await hideContextMenu(hud);
    154 }
    155 
    156 async function isContextMenuEntryEnabled(hud, node) {
    157  const contextMenu = await openContextMenu(hud, node);
    158  const openInSidebar = contextMenu.querySelector("#console-menu-open-sidebar");
    159  const enabled = !openInSidebar.attributes.disabled;
    160  await hideContextMenu(hud);
    161  return enabled;
    162 }