summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_breakpoints-editor.js
blob: d5232a50dd6c4163a488dc99588db7be4d4fec1f (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Bug 723069: Test the debugger breakpoint API and connection to the
 * source editor.
 */

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

function test() {
  let options = {
    source: EXAMPLE_URL + "code_script-switching-01.js",
    line: 1
  };
  initDebugger(TAB_URL, options).then(([aTab,, aPanel]) => {
    const gTab = aTab;
    const gPanel = aPanel;
    const gDebugger = gPanel.panelWin;
    const gEditor = gDebugger.DebuggerView.editor;
    const gSources = gDebugger.DebuggerView.Sources;
    const queries = gDebugger.require("./content/queries");
    const constants = gDebugger.require("./content/constants");
    const actions = bindActionCreators(gPanel);
    const getState = gDebugger.DebuggerController.getState;

    Task.spawn(function* () {
      yield waitForSourceAndCaretAndScopes(gPanel, "-02.js", 1);

      is(gDebugger.gThreadClient.state, "paused",
         "Should only be getting stack frames while paused.");
      is(queries.getSourceCount(getState()), 2,
         "Found the expected number of sources.");
      is(gEditor.getText().indexOf("debugger"), 166,
         "The correct source was loaded initially.");
      is(queries.getSelectedSource(getState()).actor, gSources.values[1],
         "The correct source is selected.");

      is(queries.getBreakpoints(getState()).length, 0,
         "No breakpoints currently added.");

      info("Add the first breakpoint.");
      gEditor.once("breakpointAdded", onEditorBreakpointAddFirst);
      let location = { actor: gSources.selectedValue, line: 6 };
      yield actions.addBreakpoint(location);
      checkFirstBreakpoint(location);

      info("Remove the first breakpoint.");
      gEditor.once("breakpointRemoved", onEditorBreakpointRemoveFirst);
      yield actions.removeBreakpoint(location);
      checkFirstBreakpointRemoved(location);
      checkBackgroundBreakpoint(yield testBreakpointAddBackground());

      info("Switch to the first source, which is not yet selected");
      gEditor.once("breakpointAdded", onEditorBreakpointAddSwitch);
      gEditor.once("change", onEditorTextChanged);
      actions.selectSource(gSources.items[0].attachment.source);
      yield waitForDebuggerEvents(gPanel, gDebugger.EVENTS.SOURCE_SHOWN);
      onReadyForClick();
    });

    callInTab(gTab, "firstCall");

    let breakpointsAdded = 0;
    let breakpointsRemoved = 0;
    let editorBreakpointChanges = 0;

    function onEditorBreakpointAddFirst(aEvent, aLine) {
      editorBreakpointChanges++;

      ok(aEvent,
         "breakpoint1 added to the editor.");
      is(aLine, 5,
         "Editor breakpoint line is correct.");

      is(gEditor.getBreakpoints().length, 1,
         "editor.getBreakpoints().length is correct.");
    }

    function onEditorBreakpointRemoveFirst(aEvent, aLine) {
      editorBreakpointChanges++;

      ok(aEvent,
         "breakpoint1 removed from the editor.");
      is(aLine, 5,
         "Editor breakpoint line is correct.");

      is(gEditor.getBreakpoints().length, 0,
         "editor.getBreakpoints().length is correct.");
    }

    function checkFirstBreakpoint(location) {
      breakpointsAdded++;
      const bp = queries.getBreakpoint(getState(), location);

      ok(bp,
         "breakpoint1 exists");
      is(bp.location.actor, queries.getSelectedSource(getState()).actor,
         "breakpoint1 actor is correct.");
      is(bp.location.line, 6,
         "breakpoint1 line is correct.");

      is(queries.getBreakpoints(getState()).length, 1,
         "The list of added breakpoints holds only one breakpoint.");

      is(queries.getSelectedSource(getState()).actor, gSources.values[1],
         "The second source should be currently selected.");
    }

    function checkFirstBreakpointRemoved(location) {
      breakpointsRemoved++;
      const bp = queries.getBreakpoint(getState(), location);
      ok(!bp, "breakpoint1 removed");
    }

    function testBreakpointAddBackground() {
      is(queries.getBreakpoints(getState()).length, 0,
         "No breakpoints currently added.");

      is(gSources.values[1], gSources.selectedValue,
         "The second source should be currently selected.");

      info("Add a breakpoint to the first source, which is not selected.");
      let location = { actor: gSources.values[0], line: 5 };
      gEditor.on("breakpointAdded", onEditorBreakpointAddBackgroundTrap);
      return actions.addBreakpoint(location).then(() => location);
    }

    function onEditorBreakpointAddBackgroundTrap() {
      // Trap listener: no breakpoint must be added to the editor when a
      // breakpoint is added to a source that is not currently selected.
      editorBreakpointChanges++;
      ok(false, "breakpoint2 must not be added to the editor.");
    }

    function checkBackgroundBreakpoint(location) {
      breakpointsAdded++;
      const bp = queries.getBreakpoint(getState(), location);

      ok(bp,
        "breakpoint2 added, client received");
      is(bp.location.actor, gSources.values[0],
        "breakpoint2 client url is correct.");
      is(bp.location.line, 5,
        "breakpoint2 client line is correct.");

      ok(queries.getBreakpoint(getState(), bp.location),
         "breakpoint2 found in the list of added breakpoints.");

      is(queries.getBreakpoints(getState()).length, 1,
         "The list of added breakpoints holds only one breakpoint.");

      is(queries.getSelectedSource(getState()).actor, gSources.values[1],
         "The second source should be currently selected.");

      // Remove the trap listener.
      gEditor.off("breakpointAdded", onEditorBreakpointAddBackgroundTrap);
    }

    function onEditorBreakpointAddSwitch(aEvent, aLine) {
      editorBreakpointChanges++;

      ok(aEvent,
        "breakpoint2 added to the editor.");
      is(aLine, 4,
        "Editor breakpoint line is correct.");

      is(gEditor.getBreakpoints().length, 1,
        "editor.getBreakpoints().length is correct");
    }

    function onEditorTextChanged() {
      // Wait for the actual text to be shown.
      if (gEditor.getText() == gDebugger.L10N.getStr("loadingText"))
        return void gEditor.once("change", onEditorTextChanged);

      is(gEditor.getText().indexOf("debugger"), -1,
        "The second source is no longer displayed.");
      is(gEditor.getText().indexOf("firstCall"), 118,
        "The first source is displayed.");

      is(gSources.values[0], gSources.selectedValue,
        "The first source should be currently selected.");
    }

    function onReadyForClick() {
      info("Remove the second breakpoint using the mouse.");
      gEditor.once("breakpointRemoved", onEditorBreakpointRemoveSecond);

      let iframe = gEditor.container;
      let testWin = iframe.ownerDocument.defaultView;

      // Flush the layout for the iframe.
      info("rect " + iframe.contentDocument.documentElement.getBoundingClientRect());

      let utils = testWin
        .QueryInterface(Ci.nsIInterfaceRequestor)
        .getInterface(Ci.nsIDOMWindowUtils);

      let coords = gEditor.getCoordsFromPosition({ line: 4, ch: 0 });
      let rect = iframe.getBoundingClientRect();
      let left = rect.left + 10;
      let top = rect.top + coords.top + 4;
      utils.sendMouseEventToWindow("mousedown", left, top, 0, 1, 0, false, 0, 0);
      utils.sendMouseEventToWindow("mouseup", left, top, 0, 1, 0, false, 0, 0);
    }

    function onEditorBreakpointRemoveSecond(aEvent, aLine) {
      editorBreakpointChanges++;

      ok(aEvent,
        "breakpoint2 removed from the editor.");
      is(aLine, 4,
        "Editor breakpoint line is correct.");

      is(gEditor.getBreakpoints().length, 0,
        "editor.getBreakpoints().length is correct.");

      waitForDebuggerEvents(gPanel, gDebugger.EVENTS.AFTER_FRAMES_CLEARED).then(() => {
        finalCheck();
        closeDebuggerAndFinish(gPanel);
      });

      gDebugger.gThreadClient.resume();
    }

    function finalCheck() {
      is(queries.getBreakpoints(getState()).length, 0,
         "No breakpoints currently added.");

      is(breakpointsAdded, 2,
         "Correct number of breakpoints have been added.");
      is(breakpointsRemoved, 1,
         "Correct number of breakpoints have been removed.");
      is(editorBreakpointChanges, 4,
         "Correct number of editor breakpoint changes.");
    }
  });
}