summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/xpcshell/test_watchpoint-02.js
blob: d0739c8a00004f0da4bf03bf8c1e26810cd16fe2 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint-disable no-shadow */

"use strict";

/*
Test that debugger advances instead of pausing twice on the
same line when encountering both a watchpoint and a breakpoint.
*/

add_task(
  threadFrontTest(async args => {
    await testBreakpointAndSetWatchpoint(args);
    await testBreakpointAndGetWatchpoint(args);
    await testLoops(args);
  })
);

// Test that we advance to the next line when a location
// has both a breakpoint and set watchpoint.
async function testBreakpointAndSetWatchpoint({
  commands,
  threadFront,
  debuggee,
}) {
  async function evaluateJS(input) {
    const { result } = await commands.scriptCommand.execute(input, {
      frameActor: packet.frame.actorID,
    });
    return result;
  }

  function evaluateTestCode(debuggee) {
    /* eslint-disable */
    Cu.evalInSandbox(
      `                                   // 1
      function stopMe(obj) {              // 2
        debugger;                         // 3
        obj.a = 2;                        // 4
        debugger;                         // 5
      }                                   //
      stopMe({a: 1})`,
      debuggee,
      "1.8",
      "test_watchpoint-02.js"
    );
    /* eslint-disable */
  }

  const packet = await executeOnNextTickAndWaitForPause(
    () => evaluateTestCode(debuggee),
    threadFront
  );

  info("Test that we pause on the debugger statement.");
  Assert.equal(packet.frame.where.line, 3);
  Assert.equal(packet.why.type, "debuggerStatement");

  info("Add set watchpoint.");
  const args = packet.frame.arguments;
  const obj = args[0];
  const objClient = threadFront.pauseGrip(obj);
  await objClient.addWatchpoint("a", "obj.a", "set");

  info("Add breakpoint.");
  const source = await getSourceById(threadFront, packet.frame.where.actor);

  const location = {
    sourceUrl: source.url,
    line: 4,
  };

  threadFront.setBreakpoint(location, {});

  info("Test that pause occurs on breakpoint.");
  const packet2 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet2.frame.where.line, 4);
  Assert.equal(packet2.why.type, "breakpoint");

  const packet3 = await resumeAndWaitForPause(threadFront);

  info("Test that we pause on the second debugger statement.");
  Assert.equal(packet3.frame.where.line, 5);
  Assert.equal(packet3.why.type, "debuggerStatement");

  info("Test that the value has updated.");
  const result = await evaluateJS("obj.a");
  Assert.equal(result, 2);

  info("Remove breakpoint and finish.");
  threadFront.removeBreakpoint(location, {});

  await resume(threadFront);
}

// Test that we advance to the next line when a location
// has both a breakpoint and get watchpoint.
async function testBreakpointAndGetWatchpoint({ threadFront, debuggee }) {
  function evaluateTestCode(debuggee) {
    /* eslint-disable */
    Cu.evalInSandbox(
      `                                   // 1
      function stopMe(obj) {              // 2
        debugger;                         // 3
        obj.a + 4;                        // 4
        debugger;                         // 5
      }                                   //
      stopMe({a: 1})`,
      debuggee,
      "1.8",
      "test_watchpoint-02.js"
    );
    /* eslint-disable */
  }

  const packet = await executeOnNextTickAndWaitForPause(
    () => evaluateTestCode(debuggee),
    threadFront
  );

  info("Test that we pause on the debugger statement.");
  Assert.equal(packet.frame.where.line, 3);

  info("Add get watchpoint.");
  const args = packet.frame.arguments;
  const obj = args[0];
  const objClient = threadFront.pauseGrip(obj);
  await objClient.addWatchpoint("a", "obj.a", "get");

  info("Add breakpoint.");
  const source = await getSourceById(threadFront, packet.frame.where.actor);

  const location = {
    sourceUrl: source.url,
    line: 4,
  };

  threadFront.setBreakpoint(location, {});

  info("Test that pause occurs on breakpoint.");
  const packet2 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet2.frame.where.line, 4);
  Assert.equal(packet2.why.type, "breakpoint");

  const packet3 = await resumeAndWaitForPause(threadFront);

  info("Test that we pause on the second debugger statement.");
  Assert.equal(packet3.frame.where.line, 5);
  Assert.equal(packet3.why.type, "debuggerStatement");

  info("Remove breakpoint and finish.");
  threadFront.removeBreakpoint(location, {});

  await resume(threadFront);
}

// Test that we can pause multiple times
// on the same line for a watchpoint.
async function testLoops({ commands, threadFront, debuggee }) {
  async function evaluateJS(input) {
    const { result } = await commands.scriptCommand.execute(input, {
      frameActor: packet.frame.actorID,
    });
    return result;
  }

  function evaluateTestCode(debuggee) {
    /* eslint-disable */
    Cu.evalInSandbox(
      `                                   // 1
      function stopMe(obj) {              // 2
        let i = 0;                        // 3
        debugger;                         // 4
        while (i++ < 2) {                 // 5
          obj.a = 2;                      // 6
        }                                 // 7
        debugger;                         // 8
      }                                   //
      stopMe({a: 1})`,
      debuggee,
      "1.8",
      "test_watchpoint-02.js"
    );
    /* eslint-disable */
  }

  const packet = await executeOnNextTickAndWaitForPause(
    () => evaluateTestCode(debuggee),
    threadFront
  );

  info("Test that we pause on the debugger statement.");
  Assert.equal(packet.frame.where.line, 4);
  Assert.equal(packet.why.type, "debuggerStatement");

  info("Add set watchpoint.");
  const args = packet.frame.arguments;
  const obj = args[0];
  const objClient = threadFront.pauseGrip(obj);
  await objClient.addWatchpoint("a", "obj.a", "set");

  info("Test that watchpoint triggers pause on set.");
  const packet2 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet2.frame.where.line, 6);
  Assert.equal(packet2.why.type, "setWatchpoint");
  let result = await evaluateJS("obj.a");
  Assert.equal(result, 1);

  info("Test that watchpoint triggers pause on set (2nd time).");
  const packet3 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet3.frame.where.line, 6);
  Assert.equal(packet3.why.type, "setWatchpoint");
  let result2 = await evaluateJS("obj.a");
  Assert.equal(result2, 2);

  info("Test that we pause on second debugger statement.");
  const packet4 = await resumeAndWaitForPause(threadFront);
  Assert.equal(packet4.frame.where.line, 8);
  Assert.equal(packet4.why.type, "debuggerStatement");

  await resume(threadFront);
}