summaryrefslogtreecommitdiff
path: root/toolkit/devtools/debugger/test/browser_dbg_breakpoints-contextmenu.js
blob: 2b8139dd35b33b85464d3afe8ab3ddbbffbe8d00 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test if the context menu associated with each breakpoint does what it should.
 */

const TAB_URL = EXAMPLE_URL + "doc_script-switching-01.html";

function test() {
  // Debug test slaves are a bit slow at this test.
  requestLongerTimeout(2);

  let gTab, gPanel, gDebugger;
  let gSources, gBreakpoints;

  initDebugger(TAB_URL).then(([aTab,, aPanel]) => {
    gTab = aTab;
    gPanel = aPanel;
    gDebugger = gPanel.panelWin;
    gSources = gDebugger.DebuggerView.Sources;
    gBreakpoints = gDebugger.DebuggerController.Breakpoints;

    waitForSourceShown(gPanel, "-01.js")
      .then(performTestWhileNotPaused)
      .then(performTestWhilePaused)
      .then(() => resumeDebuggerThenCloseAndFinish(gPanel))
      .then(null, aError => {
        ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
      });
  });

  function addBreakpoints() {
    return promise.resolve(null)
      .then(() => gPanel.addBreakpoint({ actor: gSources.values[0], line: 5 }))
      .then(() => gPanel.addBreakpoint({ actor: gSources.values[1], line: 6 }))
      .then(() => gPanel.addBreakpoint({ actor: gSources.values[1], line: 7 }))
      .then(() => gPanel.addBreakpoint({ actor: gSources.values[1], line: 8 }))
      .then(() => gPanel.addBreakpoint({ actor: gSources.values[1], line: 9 }))
      .then(() => ensureThreadClientState(gPanel, "resumed"));
  }

  function performTestWhileNotPaused() {
    info("Performing test while not paused...");

    return addBreakpoints()
      .then(initialChecks)
      .then(() => checkBreakpointToggleSelf(0))
      .then(() => checkBreakpointToggleOthers(0))
      .then(() => checkBreakpointToggleSelf(1))
      .then(() => checkBreakpointToggleOthers(1))
      .then(() => checkBreakpointToggleSelf(2))
      .then(() => checkBreakpointToggleOthers(2))
      .then(() => checkBreakpointToggleSelf(3))
      .then(() => checkBreakpointToggleOthers(3))
      .then(() => checkBreakpointToggleSelf(4))
      .then(() => checkBreakpointToggleOthers(4))
      .then(testDeleteAll);
  }

  function performTestWhilePaused() {
    info("Performing test while paused...");

    return addBreakpoints()
      .then(initialChecks)
      .then(pauseAndCheck)
      .then(() => checkBreakpointToggleSelf(0))
      .then(() => checkBreakpointToggleOthers(0))
      .then(() => checkBreakpointToggleSelf(1))
      .then(() => checkBreakpointToggleOthers(1))
      .then(() => checkBreakpointToggleSelf(2))
      .then(() => checkBreakpointToggleOthers(2))
      .then(() => checkBreakpointToggleSelf(3))
      .then(() => checkBreakpointToggleOthers(3))
      .then(() => checkBreakpointToggleSelf(4))
      .then(() => checkBreakpointToggleOthers(4))
      .then(testDeleteAll);
  }

  function pauseAndCheck() {
    let finished = waitForSourceAndCaretAndScopes(gPanel, "-01.js", 5).then(() => {
      let source = gSources.selectedItem.attachment.source;
      is(source.url, EXAMPLE_URL + "code_script-switching-01.js",
        "The currently selected source is incorrect (3).");
      is(gSources.selectedIndex, 0,
        "The currently selected source is incorrect (4).");
      ok(isCaretPos(gPanel, 5),
        "The editor location is correct after pausing.");
    });

    let source = gSources.selectedItem.attachment.source;
    is(source.url, EXAMPLE_URL + "code_script-switching-02.js",
      "The currently selected source is incorrect (1).");
    is(gSources.selectedIndex, 1,
      "The currently selected source is incorrect (2).");
    ok(isCaretPos(gPanel, 9),
      "The editor location is correct before pausing.");

    sendMouseClickToTab(gTab, content.document.querySelector("button"));

    return finished;
  }

  function initialChecks() {
    for (let source of gSources) {
      for (let breakpoint of source) {
        ok(gBreakpoints._getAdded(breakpoint.attachment),
          "All breakpoint items should have corresponding promises (1).");
        ok(!gBreakpoints._getRemoving(breakpoint.attachment),
          "All breakpoint items should have corresponding promises (2).");
        ok(breakpoint.attachment.actor,
          "All breakpoint items should have corresponding promises (3).");
        is(!!breakpoint.attachment.disabled, false,
          "All breakpoints should initially be enabled.");

        let prefix = "bp-cMenu-"; // "breakpoints context menu"
        let identifier = gBreakpoints.getIdentifier(breakpoint.attachment);
        let enableSelfId = prefix + "enableSelf-" + identifier + "-menuitem";
        let disableSelfId = prefix + "disableSelf-" + identifier + "-menuitem";

        is(gDebugger.document.getElementById(enableSelfId).getAttribute("hidden"), "true",
          "The 'Enable breakpoint' context menu item should initially be hidden'.");
        ok(!gDebugger.document.getElementById(disableSelfId).hasAttribute("hidden"),
          "The 'Disable breakpoint' context menu item should initially not be hidden'.");
        is(breakpoint.attachment.view.checkbox.getAttribute("checked"), "true",
          "All breakpoints should initially have a checked checkbox.");
      }
    }
  }

  function checkBreakpointToggleSelf(aIndex) {
    let deferred = promise.defer();

    EventUtils.sendMouseEvent({ type: "click" },
      gDebugger.document.querySelectorAll(".dbg-breakpoint")[aIndex],
      gDebugger);

    let selectedBreakpoint = gSources._selectedBreakpointItem;

    ok(gBreakpoints._getAdded(selectedBreakpoint.attachment),
      "There should be a breakpoint client available (1).");
    ok(!gBreakpoints._getRemoving(selectedBreakpoint.attachment),
      "There should be a breakpoint client available (2).");
    ok(selectedBreakpoint.attachment.actor,
      "There should be a breakpoint client available (3).");
    is(!!selectedBreakpoint.attachment.disabled, false,
      "The breakpoint should not be disabled yet (" + aIndex + ").");

    gBreakpoints._getAdded(selectedBreakpoint.attachment).then(aBreakpointClient => {
      ok(aBreakpointClient,
        "There should be a breakpoint client available as a promise.");
    });

    let prefix = "bp-cMenu-"; // "breakpoints context menu"
    let identifier = gBreakpoints.getIdentifier(selectedBreakpoint.attachment);
    let enableSelfId = prefix + "enableSelf-" + identifier + "-menuitem";
    let disableSelfId = prefix + "disableSelf-" + identifier + "-menuitem";

    is(gDebugger.document.getElementById(enableSelfId).getAttribute("hidden"), "true",
      "The 'Enable breakpoint' context menu item should be hidden'.");
    ok(!gDebugger.document.getElementById(disableSelfId).hasAttribute("hidden"),
      "The 'Disable breakpoint' context menu item should not be hidden'.");

    ok(isCaretPos(gPanel, selectedBreakpoint.attachment.line),
      "The source editor caret position was incorrect (" + aIndex + ").");

    waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_REMOVED).then(() => {
      ok(!gBreakpoints._getAdded(selectedBreakpoint.attachment),
        "There should be no breakpoint client available (4).");

      waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_ADDED).then(() => {
        ok(gBreakpoints._getAdded(selectedBreakpoint.attachment),
          "There should be a breakpoint client available (5).");

        deferred.resolve();
      });

      // Test re-disabling this breakpoint.
      gSources._onEnableSelf(selectedBreakpoint.attachment);
      is(selectedBreakpoint.attachment.disabled, false,
        "The current breakpoint should now be enabled.")

      is(gDebugger.document.getElementById(enableSelfId).getAttribute("hidden"), "true",
        "The 'Enable breakpoint' context menu item should be hidden'.");
      ok(!gDebugger.document.getElementById(disableSelfId).hasAttribute("hidden"),
        "The 'Disable breakpoint' context menu item should not be hidden'.");
      ok(selectedBreakpoint.attachment.view.checkbox.hasAttribute("checked"),
        "The breakpoint should now be checked.");
    });

    // Test disabling this breakpoint.
    gSources._onDisableSelf(selectedBreakpoint.attachment);
    is(selectedBreakpoint.attachment.disabled, true,
      "The current breakpoint should now be disabled.")

    ok(!gDebugger.document.getElementById(enableSelfId).hasAttribute("hidden"),
      "The 'Enable breakpoint' context menu item should not be hidden'.");
    is(gDebugger.document.getElementById(disableSelfId).getAttribute("hidden"), "true",
      "The 'Disable breakpoint' context menu item should be hidden'.");
    ok(!selectedBreakpoint.attachment.view.checkbox.hasAttribute("checked"),
      "The breakpoint should now be unchecked.");

    return deferred.promise;
  }

  function checkBreakpointToggleOthers(aIndex) {
    let deferred = promise.defer();

    waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_REMOVED, 4).then(() => {
      let selectedBreakpoint = gSources._selectedBreakpointItem;

      ok(gBreakpoints._getAdded(selectedBreakpoint.attachment),
        "There should be a breakpoint client available (6).");
      ok(!gBreakpoints._getRemoving(selectedBreakpoint.attachment),
        "There should be a breakpoint client available (7).");
      ok(selectedBreakpoint.attachment.actor,
        "There should be a breakpoint client available (8).");
      is(!!selectedBreakpoint.attachment.disabled, false,
        "The targetted breakpoint should not have been disabled (" + aIndex + ").");

      for (let source of gSources) {
        for (let otherBreakpoint of source) {
          if (otherBreakpoint != selectedBreakpoint) {
            ok(!gBreakpoints._getAdded(otherBreakpoint.attachment),
              "There should be no breakpoint client for a disabled breakpoint (9).");
            is(otherBreakpoint.attachment.disabled, true,
              "Non-targetted breakpoints should have been disabled (10).");
          }
        }
      }

      waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_ADDED, 4).then(() => {
        for (let source of gSources) {
          for (let someBreakpoint of source) {
            ok(gBreakpoints._getAdded(someBreakpoint.attachment),
              "There should be a breakpoint client for all enabled breakpoints (11).");
            is(someBreakpoint.attachment.disabled, false,
              "All breakpoints should now have been enabled (12).");
          }
        }

        waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_REMOVED, 5).then(() => {
          for (let source of gSources) {
            for (let someBreakpoint of source) {
              ok(!gBreakpoints._getAdded(someBreakpoint.attachment),
                "There should be no breakpoint client for a disabled breakpoint (13).");
              is(someBreakpoint.attachment.disabled, true,
                "All breakpoints should now have been disabled (14).");
            }
          }

          waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_ADDED, 5).then(() => {
            for (let source of gSources) {
              for (let someBreakpoint of source) {
                ok(gBreakpoints._getAdded(someBreakpoint.attachment),
                  "There should be a breakpoint client for all enabled breakpoints (15).");
                is(someBreakpoint.attachment.disabled, false,
                  "All breakpoints should now have been enabled (16).");
              }
            }

            // Done.
            deferred.resolve();
          });

          // Test re-enabling all breakpoints.
          enableAll();
        });

        // Test disabling all breakpoints.
        disableAll();
      });

      // Test re-enabling other breakpoints.
      enableOthers();
    });

    // Test disabling other breakpoints.
    disableOthers();

    return deferred.promise;
  }

  function testDeleteAll() {
    let deferred = promise.defer();

    waitForDebuggerEvents(gPanel, gDebugger.EVENTS.BREAKPOINT_REMOVED, 5).then(() => {
      ok(!gSources._selectedBreakpointItem,
        "There should be no breakpoint available after removing all breakpoints.");

      for (let source of gSources) {
        for (let otherBreakpoint of source) {
          ok(false, "It's a trap!");
        }
      }

      // Done.
      deferred.resolve()
    });

    // Test deleting all breakpoints.
    deleteAll();

    return deferred.promise;
  }

  function disableOthers() {
    gSources._onDisableOthers(gSources._selectedBreakpointItem.attachment);
  }
  function enableOthers() {
    gSources._onEnableOthers(gSources._selectedBreakpointItem.attachment);
  }
  function disableAll() {
    gSources._onDisableAll();
  }
  function enableAll() {
    gSources._onEnableAll();
  }
  function deleteAll() {
    gSources._onDeleteAll();
  }
}