summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_location_logpoint_debugger_link.js
blob: d44246654fc3e5f72c0f55fcb06f0701084994b5 (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
183
184
185
186
187
188
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test clicking locations of logpoint logs and errors will open corresponding
// conditional panels in the debugger.

"use strict";

const TEST_URI =
  "https://example.com/browser/devtools/client/webconsole/" +
  "test/browser/test-location-debugger-link-logpoint.html";

add_task(async function () {
  // On e10s, the exception thrown in test-location-debugger-link-errors.js
  // is triggered in child process and is ignored by test harness
  if (!Services.appinfo.browserTabsRemoteAutostart) {
    expectUncaughtException();
  }

  // Eliminate interference from "saved" breakpoints
  // when running the test multiple times
  await clearDebuggerPreferences();
  const hud = await openNewTabAndConsole(TEST_URI);

  info("Open the Debugger panel");
  await openDebugger();

  const toolbox = hud.toolbox;
  const dbg = createDebuggerContext(toolbox);
  await selectSource(dbg, "test-location-debugger-link-logpoint-1.js");

  info("Add a logpoint with an invalid expression");
  await setLogPoint(dbg, 7, "undefinedVariable");

  info("Add a logpoint with a valid expression");
  await setLogPoint(dbg, 8, "`a is ${a}`");

  await assertEditorLogpoint(dbg, 7, { hasLog: true });
  await assertEditorLogpoint(dbg, 8, { hasLog: true });

  info("Close the file in the debugger");
  await closeTab(dbg, "test-location-debugger-link-logpoint-1.js");

  info("Selecting the console");
  await toolbox.selectTool("webconsole");

  info("Call the function");
  await invokeInTab("add");

  info("Wait for two messages");
  await waitFor(() => findAllMessages(hud).length === 2);

  await testOpenInDebugger(hud, {
    text: "undefinedVariable is not defined",
    typeSelector: ".logPointError",
    expectUrl: true,
    expectLine: false,
    expectColumn: false,
    logPointExpr: "undefinedVariable",
  });

  info("Selecting the console again");
  await toolbox.selectTool("webconsole");
  await testOpenInDebugger(hud, {
    text: "a is 1",
    typeSelector: ".logPoint",
    expectUrl: true,
    expectLine: false,
    expectColumn: false,
    logPointExpr: "`a is ${a}`",
  });

  // Test clicking location of a removed logpoint, or a newly added breakpoint
  // at an old logpoint's location will only highlight its line
  info("Remove the logpoints");
  const source = await findSource(
    dbg,
    "test-location-debugger-link-logpoint-1.js"
  );
  await removeBreakpoint(dbg, source.id, 7);
  await removeBreakpoint(dbg, source.id, 8);
  await addBreakpoint(dbg, "test-location-debugger-link-logpoint-1.js", 8);

  info("Selecting the console");
  await toolbox.selectTool("webconsole");
  await testOpenInDebugger(hud, {
    text: "undefinedVariable is not defined",
    typeSelector: ".logPointError",
    expectUrl: true,
    expectLine: true,
    expectColumn: true,
  });

  info("Selecting the console again");
  await toolbox.selectTool("webconsole");
  await testOpenInDebugger(hud, {
    text: "a is 1",
    typeSelector: ".logPoint",
    expectUrl: true,
    expectLine: true,
    expectColumn: true,
  });
});

// Test clicking locations of logpoints from different files
add_task(async function () {
  if (!Services.appinfo.browserTabsRemoteAutostart) {
    expectUncaughtException();
  }

  await clearDebuggerPreferences();
  const hud = await openNewTabAndConsole(TEST_URI);

  info("Open the Debugger panel");
  await openDebugger();

  const toolbox = hud.toolbox;
  const dbg = createDebuggerContext(toolbox);

  info("Add a logpoint to the first file");
  await selectSource(dbg, "test-location-debugger-link-logpoint-1.js");
  await setLogPoint(dbg, 8, "`a is ${a}`");

  info("Add a logpoint to the second file");
  await selectSource(dbg, "test-location-debugger-link-logpoint-2.js");
  await setLogPoint(dbg, 8, "`c is ${c}`");

  info("Selecting the console");
  await toolbox.selectTool("webconsole");

  info("Call the function from the first file");
  await invokeInTab("add");

  info("Wait for the first message");
  await waitFor(() => findAllMessages(hud).length === 1);
  await testOpenInDebugger(hud, {
    text: "a is 1",
    typeSelector: ".logPoint",
    expectUrl: true,
    expectLine: false,
    expectColumn: false,
    logPointExpr: "`a is ${a}`",
  });

  info("Selecting the console again");
  await toolbox.selectTool("webconsole");

  info("Call the function from the second file");
  await invokeInTab("subtract");

  info("Wait for the second message");
  await waitFor(() => findAllMessages(hud).length === 2);
  await testOpenInDebugger(hud, {
    text: "c is 1",
    typeSelector: ".logPoint",
    expectUrl: true,
    expectLine: false,
    expectColumn: false,
    logPointExpr: "`c is ${c}`",
  });
});

async function setLogPoint(dbg, index, expression) {
  rightClickElement(dbg, "gutter", index);
  await waitForContextMenu(dbg);
  selectContextMenuItem(
    dbg,
    `${selectors.addLogItem},${selectors.editLogItem}`
  );
  const onBreakpointSet = waitForDispatch(dbg.store, "SET_BREAKPOINT");
  await typeInPanel(dbg, expression);
  await onBreakpointSet;
}

function getLineEl(dbg, line) {
  const lines = dbg.win.document.querySelectorAll(".CodeMirror-code > div");
  return lines[line - 1];
}

function assertEditorLogpoint(dbg, line, { hasLog = false } = {}) {
  const hasLogClass = getLineEl(dbg, line).classList.contains("has-log");

  Assert.strictEqual(
    hasLogClass,
    hasLog,
    `Breakpoint log ${hasLog ? "exists" : "does not exist"} on line ${line}`
  );
}