summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_console_context_menu_entries.js
blob: 1f1c011997ff14ba3ba700a0e2d5b5aea282237d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Check that we display the expected context menu entries.

const TEST_URI =
  "http://example.com/browser/devtools/client/webconsole/" +
  "test/browser/test-console.html";

add_task(async function () {
  await pushPref("devtools.browsertoolbox.scope", "everything");
  // Enable net messages in the console for this test.
  await pushPref("devtools.browserconsole.filter.net", true);
  // This is required for testing the text input in the browser console:
  await pushPref("devtools.chrome.enabled", true);

  await addTab(TEST_URI);
  const hud = await BrowserConsoleManager.toggleBrowserConsole();

  // Network monitoring is turned off by default in the browser console
  info("Turn on network monitoring");
  await toggleNetworkMonitoringConsoleSetting(hud, true);

  info("Reload the content window to produce a network log");
  const onNetworkMessage = waitForMessageByType(
    hud,
    "test-console.html",
    ".network"
  );
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.wrappedJSObject.location.reload();
  });
  const networkMessage = await onNetworkMessage;

  info("Open and check the context menu for the network message");
  let menuPopup = await openContextMenu(hud, networkMessage.node);
  ok(menuPopup, "The context menu is displayed on a network message");

  let expectedContextMenu = addPrefBasedEntries([
    "#console-menu-copy-url (a)",
    "#console-menu-open-url (T)",
    "#console-menu-store (S) [disabled]",
    "#console-menu-copy (C)",
    "#console-menu-copy-object (o) [disabled]",
    "#console-menu-export-clipboard (M)",
    "#console-menu-export-file (F)",
  ]);
  is(
    getSimplifiedContextMenu(menuPopup).join("\n"),
    expectedContextMenu.join("\n"),
    "The context menu has the expected entries for a network message"
  );

  info("Logging a text message in the content window");
  const onLogMessage = waitForMessageByType(
    hud,
    "simple text message",
    ".console-api"
  );
  SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    content.wrappedJSObject.console.log("simple text message");
  });

  const logMessage = await onLogMessage;
  menuPopup = await openContextMenu(hud, logMessage.node);
  ok(menuPopup, "The context menu is displayed on a log message");

  expectedContextMenu = addPrefBasedEntries([
    "#console-menu-store (S) [disabled]",
    "#console-menu-copy (C)",
    "#console-menu-copy-object (o) [disabled]",
    "#console-menu-export-clipboard (M)",
    "#console-menu-export-file (F)",
  ]);
  is(
    getSimplifiedContextMenu(menuPopup).join("\n"),
    expectedContextMenu.join("\n"),
    "The context menu has the expected entries for a simple log message"
  );

  menuPopup = await openContextMenu(hud, hud.jsterm.node);

  let actualEntries = getL10NContextMenu(menuPopup);
  is(
    actualEntries.length,
    6,
    "The context menu has the right number of entries."
  );
  is(actualEntries[0], "#editmenu-undo (text-action-undo) [disabled]");
  is(actualEntries[1], "#editmenu-cut (text-action-cut) [disabled]");
  is(actualEntries[2], "#editmenu-copy (text-action-copy) [disabled]");
  // Paste may or may not be enabled depending on what ran before this.
  // If emptyClipboard is fixed (666254) we could assert if it's enabled/disabled.
  ok(actualEntries[3].startsWith("#editmenu-paste (text-action-paste)"));
  is(actualEntries[4], "#editmenu-delete (text-action-delete) [disabled]");
  is(
    actualEntries[5],
    "#editmenu-selectAll (text-action-select-all) [disabled]"
  );

  const node = hud.jsterm.node;
  const inputContainer = node.closest(".jsterm-input-container");
  await openContextMenu(hud, inputContainer);

  actualEntries = getL10NContextMenu(menuPopup);
  is(
    actualEntries.length,
    6,
    "The context menu has the right number of entries."
  );
  is(actualEntries[0], "#editmenu-undo (text-action-undo) [disabled]");
  is(actualEntries[1], "#editmenu-cut (text-action-cut) [disabled]");
  is(actualEntries[2], "#editmenu-copy (text-action-copy) [disabled]");
  // Paste may or may not be enabled depending on what ran before this.
  // If emptyClipboard is fixed (666254) we could assert if it's enabled/disabled.
  ok(actualEntries[3].startsWith("#editmenu-paste (text-action-paste)"));
  is(actualEntries[4], "#editmenu-delete (text-action-delete) [disabled]");
  is(
    actualEntries[5],
    "#editmenu-selectAll (text-action-select-all) [disabled]"
  );

  await hideContextMenu(hud);
  await toggleNetworkMonitoringConsoleSetting(hud, false);
});

function addPrefBasedEntries(expectedEntries) {
  if (Services.prefs.getBoolPref("devtools.webconsole.sidebarToggle", false)) {
    expectedEntries.push("#console-menu-open-sidebar (V) [disabled]");
  }

  return expectedEntries;
}

function getL10NContextMenu(popupElement) {
  return [...popupElement.querySelectorAll("menuitem")].map(entry => {
    const l10nID = entry.getAttribute("data-l10n-id");
    const disabled = entry.hasAttribute("disabled");
    return `#${entry.id} (${l10nID})${disabled ? " [disabled]" : ""}`;
  });
}

function getSimplifiedContextMenu(popupElement) {
  return [...popupElement.querySelectorAll("menuitem")].map(entry => {
    const key = entry.getAttribute("accesskey");
    const disabled = entry.hasAttribute("disabled");
    return `#${entry.id} (${key})${disabled ? " [disabled]" : ""}`;
  });
}