summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_breakpoint-24.js
blob: a240a237f04fa5098d86809120c8ec9837be1572 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* eslint-disable max-nested-callbacks */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Bug 1441183 - Verify that the debugger advances to a new location
 * when encountering debugger statements and brakpoints
 *
 * Bug 1613165 - Verify that debugger statement is not disabled by
 *  adding/removing a breakpoint
 */
add_task(
  threadFrontTest(async props => {
    await testDebuggerStatements(props);
    await testBreakpoints(props);
    await testBreakpointsAndDebuggerStatements(props);
    await testLoops(props);
    await testRemovingBreakpoint(props);
    await testAddingBreakpoint(props);
  })
);

// Ensure that we advance to the next line when we
// step to a debugger statement and resume.
async function testDebuggerStatements({ commands, threadFront }) {
  commands.scriptCommand.execute(`function foo(stop) {
      debugger;
      debugger;
      debugger;
    }
    foo();
    //# sourceURL=http://example.com/code.js`);

  await performActions(threadFront, [
    [
      "paused at first debugger statement",
      { line: 2, type: "debuggerStatement" },
      "stepOver",
    ],
    [
      "paused at the second debugger statement",
      { line: 3, type: "resumeLimit" },
      "resume",
    ],
    [
      "paused at the third debugger statement",
      { line: 4, type: "debuggerStatement" },
      "resume",
    ],
  ]);
}

// Ensure that we advance to the next line when we hit a breakpoint
// on a line with a debugger statement and resume.
async function testBreakpointsAndDebuggerStatements({ commands, threadFront }) {
  commands.scriptCommand.execute(`function foo(stop) {
      debugger;
      debugger;
      debugger;
    }
    foo();
    //# sourceURL=http://example.com/testBreakpointsAndDebuggerStatements.js`);

  threadFront.setBreakpoint(
    {
      sourceUrl: "http://example.com/testBreakpointsAndDebuggerStatements.js",
      line: 3,
    },
    {}
  );

  await performActions(threadFront, [
    [
      "paused at first debugger statement",
      { line: 2, type: "debuggerStatement" },
      "resume",
    ],
    [
      "paused at the breakpoint at the second debugger statement",
      { line: 3, type: "breakpoint" },
      "resume",
    ],
    [
      "pause at the third debugger statement",
      { line: 4, type: "debuggerStatement" },
      "resume",
    ],
  ]);
}

// Ensure that we advance to the next line when we step to
// a line with a breakpoint and resume.
async function testBreakpoints({ commands, threadFront }) {
  commands.scriptCommand.execute(`function foo(stop) {
      debugger;
      a();
      debugger;
    }
    function a() {}
    foo();
    //# sourceURL=http://example.com/testBreakpoints.js`);

  threadFront.setBreakpoint(
    { sourceUrl: "http://example.com/testBreakpoints.js", line: 3, column: 6 },
    {}
  );

  await performActions(threadFront, [
    [
      "paused at first debugger statement",
      { line: 2, type: "debuggerStatement" },
      "stepOver",
    ],
    ["paused at a()", { line: 3, type: "resumeLimit" }, "resume"],
    [
      "pause at the second debugger satement",
      { line: 4, type: "debuggerStatement" },
      "resume",
    ],
  ]);
}

// Ensure that we advance to the next line when we step to
// a line with a breakpoint and resume.
async function testLoops({ commands, threadFront }) {
  commands.scriptCommand.execute(`function foo(stop) {
      let i = 0;
      debugger;
      while (i++ < 2) {
        debugger;
      }
      debugger;
    }
    foo();
    //# sourceURL=http://example.com/testLoops.js`);

  await performActions(threadFront, [
    [
      "paused at first debugger statement",
      { line: 3, type: "debuggerStatement" },
      "resume",
    ],
    [
      "pause at the second debugger satement",
      { line: 5, type: "debuggerStatement" },
      "resume",
    ],
    [
      "pause at the second debugger satement (2nd time)",
      { line: 5, type: "debuggerStatement" },
      "resume",
    ],
    [
      "pause at the third debugger satement",
      { line: 7, type: "debuggerStatement" },
      "resume",
    ],
  ]);
}

// Bug 1613165 - ensure that if you pause on a breakpoint on a line with
// debugger statement, remove the breakpoint, and try to pause on the
// debugger statement before pausing anywhere else, debugger pauses instead of
// skipping debugger statement.
async function testRemovingBreakpoint({ commands, threadFront }) {
  commands.scriptCommand.execute(`function foo(stop) {
      debugger;
    }
    foo();
    foo();
    //# sourceURL=http://example.com/testRemovingBreakpoint.js`);

  const location = {
    sourceUrl: "http://example.com/testRemovingBreakpoint.js",
    line: 2,
    column: 6,
  };

  threadFront.setBreakpoint(location, {});

  info("paused at the breakpoint at the first debugger statement");
  const packet = await waitForEvent(threadFront, "paused");
  Assert.equal(packet.frame.where.line, 2);
  Assert.equal(packet.why.type, "breakpoint");
  threadFront.removeBreakpoint(location);

  info("paused at the first debugger statement");
  const packet2 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet2.frame.where.line, 2);
  Assert.equal(packet2.why.type, "debuggerStatement");
  await threadFront.resume();
}

// Bug 1613165 - ensure if you pause on a debugger statement, add a
// breakpoint on the same line, and try to pause on the breakpoint
// before pausing anywhere else, debugger pauses on that line instead of
// skipping breakpoint.
async function testAddingBreakpoint({ commands, threadFront }) {
  commands.scriptCommand.execute(`function foo(stop) {
      debugger;
    }
    foo();
    foo();
    //# sourceURL=http://example.com/testAddingBreakpoint.js`);

  const location = {
    sourceUrl: "http://example.com/testAddingBreakpoint.js",
    line: 2,
    column: 6,
  };

  info("paused at the first debugger statement");
  const packet = await waitForEvent(threadFront, "paused");
  Assert.equal(packet.frame.where.line, 2);
  Assert.equal(packet.why.type, "debuggerStatement");
  threadFront.setBreakpoint(location, {});

  info("paused at the breakpoint at the first debugger statement");
  const packet2 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet2.frame.where.line, 2);
  Assert.equal(packet2.why.type, "breakpoint");
  await threadFront.resume();
}

async function performActions(threadFront, actions) {
  for (const action of actions) {
    await performAction(threadFront, action);
  }
}

async function performAction(threadFront, [description, result, action]) {
  info(description);
  const packet = await waitForEvent(threadFront, "paused");
  Assert.equal(packet.frame.where.line, result.line);
  Assert.equal(packet.why.type, result.type);
  await threadFront[action]();
}