summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_context_menu_object_in_sidebar.js
blob: dbd053944eda35f7f5caac85fb4d1f0b2f19838b (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that the "Open in sidebar" context menu entry is active for
// the correct objects and opens the sidebar when clicked.

"use strict";

const TEST_URI =
  "data:text/html;charset=utf8,<!DOCTYPE html>" +
  `<script>
    console.log({a:1}, 100, {b:1}, 'foo', false, null, undefined);

    var error = new Error("oh my");
    error.customProperty = {code: 500, message: "Internal Server Error"};
    error.name = "CustomServerError";
    console.info(error);
  </script>`;

add_task(async function () {
  // Should be removed when sidebar work is complete
  await pushPref("devtools.webconsole.sidebarToggle", true);

  const hud = await openNewTabAndConsole(TEST_URI);

  const message = await waitFor(() =>
    findConsoleAPIMessage(hud, "Object { a: 1 }")
  );
  const [objectA, objectB] = message.querySelectorAll(
    ".object-inspector .objectBox-object"
  );
  const number = findMessagePartByType(hud, {
    text: "100",
    typeSelector: ".console-api",
    partSelector: ".objectBox",
  });
  const string = findMessagePartByType(hud, {
    text: "foo",
    typeSelector: ".console-api",
    partSelector: ".objectBox",
  });
  const bool = findMessagePartByType(hud, {
    text: "false",
    typeSelector: ".console-api",
    partSelector: ".objectBox",
  });
  const nullMessage = findMessagePartByType(hud, {
    text: "null",
    typeSelector: ".console-api",
    partSelector: ".objectBox",
  });
  const undefinedMsg = findMessagePartByType(hud, {
    text: "undefined",
    typeSelector: ".console-api",
    partSelector: ".objectBox",
  });

  info("Showing sidebar for {a:1}");
  await showSidebarWithContextMenu(hud, objectA, true);

  let sidebarContents = hud.ui.document.querySelector(".sidebar-contents");
  let objectInspector = sidebarContents.querySelector(".object-inspector");
  let oiNodes = objectInspector.querySelectorAll(".node");
  if (oiNodes.length === 1) {
    // If this is the case, we wait for the properties to be fetched and displayed.
    await waitForNodeMutation(objectInspector, {
      childList: true,
    });
  }

  let sidebarText =
    hud.ui.document.querySelector(".sidebar-contents").textContent;
  ok(sidebarText.includes("a: 1"), "Sidebar is shown for {a:1}");

  info("Showing sidebar for {a:1} again");
  await showSidebarWithContextMenu(hud, objectA, false);
  ok(
    hud.ui.document.querySelector(".sidebar"),
    "Sidebar is still shown after clicking on same object"
  );
  is(
    hud.ui.document.querySelector(".sidebar-contents").textContent,
    sidebarText,
    "Sidebar is not updated after clicking on same object"
  );

  info("Showing sidebar for {b:1}");
  await showSidebarWithContextMenu(hud, objectB, false);

  sidebarContents = hud.ui.document.querySelector(".sidebar-contents");
  objectInspector = sidebarContents.querySelector(".object-inspector");
  oiNodes = objectInspector.querySelectorAll(".node");
  if (oiNodes.length === 1) {
    // If this is the case, we wait for the properties to be fetched and displayed.
    await waitForNodeMutation(objectInspector, {
      childList: true,
    });
  }

  isnot(
    hud.ui.document.querySelector(".sidebar-contents").textContent,
    sidebarText,
    "Sidebar is updated for {b:1}"
  );
  sidebarText = hud.ui.document.querySelector(".sidebar-contents").textContent;

  ok(sidebarText.includes("b: 1"), "Sidebar contents shown for {b:1}");

  info("Showing sidebar for Error object");
  const errorMsg = findConsoleAPIMessage(hud, "CustomServerError:");
  await showSidebarWithContextMenu(hud, errorMsg, false);

  sidebarContents = hud.ui.document.querySelector(".sidebar-contents");
  objectInspector = sidebarContents.querySelector(".object-inspector");
  oiNodes = objectInspector.querySelectorAll(".node");
  if (oiNodes.length === 1) {
    // If this is the case, we wait for the properties to be fetched and displayed.
    await waitForNodeMutation(objectInspector, {
      childList: true,
    });
  }
  sidebarText = hud.ui.document.querySelector(".sidebar-contents").textContent;
  is(
    oiNodes[0].textContent,
    "CustomServerError: oh my",
    "First node has expected content"
  );
  ok(
    sidebarText.includes(`customProperty:`),
    "Sidebar contents shown for the error object"
  );

  info("Checking context menu entry is disabled for number");
  const numberContextMenuEnabled = await isContextMenuEntryEnabled(hud, number);
  ok(!numberContextMenuEnabled, "Context menu entry is disabled for number");

  info("Checking context menu entry is disabled for string");
  const stringContextMenuEnabled = await isContextMenuEntryEnabled(hud, string);
  ok(!stringContextMenuEnabled, "Context menu entry is disabled for string");

  info("Checking context menu entry is disabled for bool");
  const boolContextMenuEnabled = await isContextMenuEntryEnabled(hud, bool);
  ok(!boolContextMenuEnabled, "Context menu entry is disabled for bool");

  info("Checking context menu entry is disabled for null message");
  const nullContextMenuEnabled = await isContextMenuEntryEnabled(
    hud,
    nullMessage
  );
  ok(!nullContextMenuEnabled, "Context menu entry is disabled for nullMessage");

  info("Checking context menu entry is disabled for undefined message");
  const undefinedContextMenuEnabled = await isContextMenuEntryEnabled(
    hud,
    undefinedMsg
  );
  ok(
    !undefinedContextMenuEnabled,
    "Context menu entry is disabled for undefinedMsg"
  );
});

async function showSidebarWithContextMenu(hud, node, expectMutation) {
  const appNode = hud.ui.document.querySelector(".webconsole-app");
  const onSidebarShown = waitForNodeMutation(appNode, { childList: true });

  const contextMenu = await openContextMenu(hud, node);
  const openInSidebar = contextMenu.querySelector("#console-menu-open-sidebar");
  openInSidebar.click();
  if (expectMutation) {
    await onSidebarShown;
  }
  await hideContextMenu(hud);
}

async function isContextMenuEntryEnabled(hud, node) {
  const contextMenu = await openContextMenu(hud, node);
  const openInSidebar = contextMenu.querySelector("#console-menu-open-sidebar");
  const enabled = !openInSidebar.attributes.disabled;
  await hideContextMenu(hud);
  return enabled;
}