browser/devtools/debugger/test/browser_dbg_breakpoints-editor.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/debugger/test/browser_dbg_breakpoints-editor.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,302 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/**
     1.8 + * Bug 723069: Test the debugger breakpoint API and connection to the
     1.9 + * source editor.
    1.10 + */
    1.11 +
    1.12 +const TAB_URL = EXAMPLE_URL + "doc_script-switching-01.html";
    1.13 +
    1.14 +function test() {
    1.15 +  let gTab, gDebuggee, gPanel, gDebugger;
    1.16 +  let gEditor, gSources, gBreakpoints, gBreakpointsAdded, gBreakpointsRemoving;
    1.17 +
    1.18 +  initDebugger(TAB_URL).then(([aTab, aDebuggee, aPanel]) => {
    1.19 +    gTab = aTab;
    1.20 +    gDebuggee = aDebuggee;
    1.21 +    gPanel = aPanel;
    1.22 +    gDebugger = gPanel.panelWin;
    1.23 +    gEditor = gDebugger.DebuggerView.editor;
    1.24 +    gSources = gDebugger.DebuggerView.Sources;
    1.25 +    gBreakpoints = gDebugger.DebuggerController.Breakpoints;
    1.26 +    gBreakpointsAdded = gBreakpoints._added;
    1.27 +    gBreakpointsRemoving = gBreakpoints._removing;
    1.28 +
    1.29 +    waitForSourceAndCaretAndScopes(gPanel, "-02.js", 1).then(performTest);
    1.30 +    gDebuggee.firstCall();
    1.31 +  });
    1.32 +
    1.33 +  function performTest() {
    1.34 +    is(gDebugger.gThreadClient.state, "paused",
    1.35 +      "Should only be getting stack frames while paused.");
    1.36 +    is(gSources.itemCount, 2,
    1.37 +      "Found the expected number of sources.");
    1.38 +    is(gEditor.getText().indexOf("debugger"), 172,
    1.39 +      "The correct source was loaded initially.");
    1.40 +    is(gSources.selectedValue, gSources.values[1],
    1.41 +      "The correct source is selected.");
    1.42 +
    1.43 +    is(gBreakpointsAdded.size, 0,
    1.44 +      "No breakpoints currently added.");
    1.45 +    is(gBreakpointsRemoving.size, 0,
    1.46 +      "No breakpoints currently being removed.");
    1.47 +    is(gEditor.getBreakpoints().length, 0,
    1.48 +      "No breakpoints currently shown in the editor.");
    1.49 +
    1.50 +    ok(!gBreakpoints._getAdded({ url: "foo", line: 3 }),
    1.51 +      "_getAdded('foo', 3) returns falsey.");
    1.52 +    ok(!gBreakpoints._getRemoving({ url: "bar", line: 3 }),
    1.53 +      "_getRemoving('bar', 3) returns falsey.");
    1.54 +
    1.55 +    is(gSources.values[1], gSources.selectedValue,
    1.56 +      "The second source should be currently selected.");
    1.57 +
    1.58 +    info("Add the first breakpoint.");
    1.59 +    let location = { url: gSources.selectedValue, line: 6 };
    1.60 +    gEditor.once("breakpointAdded", onEditorBreakpointAddFirst);
    1.61 +    gPanel.addBreakpoint(location).then(onBreakpointAddFirst);
    1.62 +  }
    1.63 +
    1.64 +  let breakpointsAdded = 0;
    1.65 +  let breakpointsRemoved = 0;
    1.66 +  let editorBreakpointChanges = 0;
    1.67 +
    1.68 +  function onEditorBreakpointAddFirst(aEvent, aLine) {
    1.69 +    editorBreakpointChanges++;
    1.70 +
    1.71 +    ok(aEvent,
    1.72 +      "breakpoint1 added to the editor.");
    1.73 +    is(aLine, 5,
    1.74 +      "Editor breakpoint line is correct.");
    1.75 +
    1.76 +    is(gEditor.getBreakpoints().length, 1,
    1.77 +      "editor.getBreakpoints().length is correct.");
    1.78 +  }
    1.79 +
    1.80 +  function onBreakpointAddFirst(aBreakpointClient) {
    1.81 +    breakpointsAdded++;
    1.82 +
    1.83 +    ok(aBreakpointClient,
    1.84 +      "breakpoint1 added, client received.");
    1.85 +    is(aBreakpointClient.location.url, gSources.selectedValue,
    1.86 +      "breakpoint1 client url is correct.");
    1.87 +    is(aBreakpointClient.location.line, 6,
    1.88 +      "breakpoint1 client line is correct.");
    1.89 +
    1.90 +    ok(gBreakpoints._getAdded(aBreakpointClient.location),
    1.91 +      "breakpoint1 client found in the list of added breakpoints.");
    1.92 +    ok(!gBreakpoints._getRemoving(aBreakpointClient.location),
    1.93 +      "breakpoint1 client found in the list of removing breakpoints.");
    1.94 +
    1.95 +    is(gBreakpointsAdded.size, 1,
    1.96 +      "The list of added breakpoints holds only one breakpoint.");
    1.97 +    is(gBreakpointsRemoving.size, 0,
    1.98 +      "The list of removing breakpoints holds no breakpoint.");
    1.99 +
   1.100 +    gBreakpoints._getAdded(aBreakpointClient.location).then(aClient => {
   1.101 +      is(aClient, aBreakpointClient,
   1.102 +        "_getAdded() returns the correct breakpoint.");
   1.103 +    });
   1.104 +
   1.105 +    is(gSources.values[1], gSources.selectedValue,
   1.106 +      "The second source should be currently selected.");
   1.107 +
   1.108 +    info("Remove the first breakpoint.");
   1.109 +    gEditor.once("breakpointRemoved", onEditorBreakpointRemoveFirst);
   1.110 +    gPanel.removeBreakpoint(aBreakpointClient.location).then(onBreakpointRemoveFirst);
   1.111 +  }
   1.112 +
   1.113 +  function onEditorBreakpointRemoveFirst(aEvent, aLine) {
   1.114 +    editorBreakpointChanges++;
   1.115 +
   1.116 +    ok(aEvent,
   1.117 +      "breakpoint1 removed from the editor.");
   1.118 +    is(aLine, 5,
   1.119 +      "Editor breakpoint line is correct.");
   1.120 +
   1.121 +    is(gEditor.getBreakpoints().length, 0,
   1.122 +      "editor.getBreakpoints().length is correct.");
   1.123 +  }
   1.124 +
   1.125 +  function onBreakpointRemoveFirst(aLocation) {
   1.126 +    breakpointsRemoved++;
   1.127 +
   1.128 +    ok(aLocation,
   1.129 +      "breakpoint1 removed");
   1.130 +    is(aLocation.url, gSources.selectedValue,
   1.131 +      "breakpoint1 removal url is correct.");
   1.132 +    is(aLocation.line, 6,
   1.133 +      "breakpoint1 removal line is correct.");
   1.134 +
   1.135 +    testBreakpointAddBackground();
   1.136 +  }
   1.137 +
   1.138 +  function testBreakpointAddBackground() {
   1.139 +    is(gBreakpointsAdded.size, 0,
   1.140 +      "No breakpoints currently added.");
   1.141 +    is(gBreakpointsRemoving.size, 0,
   1.142 +      "No breakpoints currently being removed.");
   1.143 +    is(gEditor.getBreakpoints().length, 0,
   1.144 +      "No breakpoints currently shown in the editor.");
   1.145 +
   1.146 +    ok(!gBreakpoints._getAdded({ url: gSources.selectedValue, line: 6 }),
   1.147 +      "_getAdded('gSources.selectedValue', 6) returns falsey.");
   1.148 +    ok(!gBreakpoints._getRemoving({ url: gSources.selectedValue, line: 6 }),
   1.149 +      "_getRemoving('gSources.selectedValue', 6) returns falsey.");
   1.150 +
   1.151 +    is(gSources.values[1], gSources.selectedValue,
   1.152 +      "The second source should be currently selected.");
   1.153 +
   1.154 +    info("Add a breakpoint to the first source, which is not selected.");
   1.155 +    let location = { url: gSources.values[0], line: 5 };
   1.156 +    let options = { noEditorUpdate: true };
   1.157 +    gEditor.on("breakpointAdded", onEditorBreakpointAddBackgroundTrap);
   1.158 +    gPanel.addBreakpoint(location, options).then(onBreakpointAddBackground);
   1.159 +  }
   1.160 +
   1.161 +  function onEditorBreakpointAddBackgroundTrap() {
   1.162 +    // Trap listener: no breakpoint must be added to the editor when a
   1.163 +    // breakpoint is added to a source that is not currently selected.
   1.164 +    editorBreakpointChanges++;
   1.165 +    ok(false, "breakpoint2 must not be added to the editor.");
   1.166 +  }
   1.167 +
   1.168 +  function onBreakpointAddBackground(aBreakpointClient, aResponseError) {
   1.169 +    breakpointsAdded++;
   1.170 +
   1.171 +    ok(aBreakpointClient,
   1.172 +      "breakpoint2 added, client received");
   1.173 +    is(aBreakpointClient.location.url, gSources.values[0],
   1.174 +      "breakpoint2 client url is correct.");
   1.175 +    is(aBreakpointClient.location.line, 5,
   1.176 +      "breakpoint2 client line is correct.");
   1.177 +
   1.178 +    ok(gBreakpoints._getAdded(aBreakpointClient.location),
   1.179 +      "breakpoint2 client found in the list of added breakpoints.");
   1.180 +    ok(!gBreakpoints._getRemoving(aBreakpointClient.location),
   1.181 +      "breakpoint2 client found in the list of removing breakpoints.");
   1.182 +
   1.183 +    is(gBreakpointsAdded.size, 1,
   1.184 +      "The list of added breakpoints holds only one breakpoint.");
   1.185 +    is(gBreakpointsRemoving.size, 0,
   1.186 +      "The list of removing breakpoints holds no breakpoint.");
   1.187 +
   1.188 +    gBreakpoints._getAdded(aBreakpointClient.location).then(aClient => {
   1.189 +      is(aClient, aBreakpointClient,
   1.190 +        "_getAdded() returns the correct breakpoint.");
   1.191 +    });
   1.192 +
   1.193 +    is(gSources.values[1], gSources.selectedValue,
   1.194 +      "The second source should be currently selected.");
   1.195 +
   1.196 +    // Remove the trap listener.
   1.197 +    gEditor.off("breakpointAdded", onEditorBreakpointAddBackgroundTrap);
   1.198 +
   1.199 +    info("Switch to the first source, which is not yet selected");
   1.200 +    gEditor.once("breakpointAdded", onEditorBreakpointAddSwitch);
   1.201 +    gEditor.once("change", onEditorTextChanged);
   1.202 +    gSources.selectedIndex = 0;
   1.203 +  }
   1.204 +
   1.205 +  function onEditorBreakpointAddSwitch(aEvent, aLine) {
   1.206 +    editorBreakpointChanges++;
   1.207 +
   1.208 +    ok(aEvent,
   1.209 +      "breakpoint2 added to the editor.");
   1.210 +    is(aLine, 4,
   1.211 +      "Editor breakpoint line is correct.");
   1.212 +
   1.213 +    is(gEditor.getBreakpoints().length, 1,
   1.214 +      "editor.getBreakpoints().length is correct");
   1.215 +  }
   1.216 +
   1.217 +  function onEditorTextChanged() {
   1.218 +    // Wait for the actual text to be shown.
   1.219 +    if (gEditor.getText() == gDebugger.L10N.getStr("loadingText"))
   1.220 +      return void gEditor.once("change", onEditorTextChanged);
   1.221 +
   1.222 +    is(gEditor.getText().indexOf("debugger"), -1,
   1.223 +      "The second source is no longer displayed.");
   1.224 +    is(gEditor.getText().indexOf("firstCall"), 118,
   1.225 +      "The first source is displayed.");
   1.226 +
   1.227 +    is(gSources.values[0], gSources.selectedValue,
   1.228 +      "The first source should be currently selected.");
   1.229 +
   1.230 +    let window = gEditor.container.contentWindow;
   1.231 +    executeSoon(() => window.mozRequestAnimationFrame(onReadyForClick));
   1.232 +  }
   1.233 +
   1.234 +  function onReadyForClick() {
   1.235 +    info("Remove the second breakpoint using the mouse.");
   1.236 +    gEditor.once("breakpointRemoved", onEditorBreakpointRemoveSecond);
   1.237 +
   1.238 +    let iframe = gEditor.container;
   1.239 +    let testWin = iframe.ownerDocument.defaultView;
   1.240 +
   1.241 +    // Flush the layout for the iframe.
   1.242 +    info("rect " + iframe.contentDocument.documentElement.getBoundingClientRect());
   1.243 +
   1.244 +    let utils = testWin
   1.245 +      .QueryInterface(Ci.nsIInterfaceRequestor)
   1.246 +      .getInterface(Ci.nsIDOMWindowUtils);
   1.247 +
   1.248 +    let coords = gEditor.getCoordsFromPosition({ line: 4, ch: 0 });
   1.249 +    let rect = iframe.getBoundingClientRect();
   1.250 +    let left = rect.left + 10;
   1.251 +    let top = rect.top + coords.top + 4;
   1.252 +    utils.sendMouseEventToWindow("mousedown", left, top, 0, 1, 0, false, 0, 0);
   1.253 +    utils.sendMouseEventToWindow("mouseup", left, top, 0, 1, 0, false, 0, 0);
   1.254 +  }
   1.255 +
   1.256 +  function onEditorBreakpointRemoveSecond(aEvent, aLine) {
   1.257 +    editorBreakpointChanges++;
   1.258 +
   1.259 +    ok(aEvent,
   1.260 +      "breakpoint2 removed from the editor.");
   1.261 +    is(aLine, 4,
   1.262 +      "Editor breakpoint line is correct.");
   1.263 +
   1.264 +    is(gEditor.getBreakpoints().length, 0,
   1.265 +      "editor.getBreakpoints().length is correct.");
   1.266 +
   1.267 +    waitForDebuggerEvents(gPanel, gDebugger.EVENTS.AFTER_FRAMES_CLEARED).then(() => {
   1.268 +      finalCheck();
   1.269 +      closeDebuggerAndFinish(gPanel);
   1.270 +    });
   1.271 +
   1.272 +    gDebugger.gThreadClient.resume();
   1.273 +  }
   1.274 +
   1.275 +  function finalCheck() {
   1.276 +    is(gBreakpointsAdded.size, 0,
   1.277 +      "No breakpoints currently added.");
   1.278 +    is(gBreakpointsRemoving.size, 0,
   1.279 +      "No breakpoints currently being removed.");
   1.280 +    is(gEditor.getBreakpoints().length, 0,
   1.281 +      "No breakpoints currently shown in the editor.");
   1.282 +
   1.283 +    ok(!gBreakpoints._getAdded({ url: gSources.values[0], line: 5 }),
   1.284 +      "_getAdded('gSources.values[0]', 5) returns falsey.");
   1.285 +    ok(!gBreakpoints._getRemoving({ url: gSources.values[0], line: 5 }),
   1.286 +      "_getRemoving('gSources.values[0]', 5) returns falsey.");
   1.287 +
   1.288 +    ok(!gBreakpoints._getAdded({ url: gSources.values[1], line: 6 }),
   1.289 +      "_getAdded('gSources.values[1]', 6) returns falsey.");
   1.290 +    ok(!gBreakpoints._getRemoving({ url: gSources.values[1], line: 6 }),
   1.291 +      "_getRemoving('gSources.values[1]', 6) returns falsey.");
   1.292 +
   1.293 +    ok(!gBreakpoints._getAdded({ url: "foo", line: 3 }),
   1.294 +      "_getAdded('foo', 3) returns falsey.");
   1.295 +    ok(!gBreakpoints._getRemoving({ url: "bar", line: 3 }),
   1.296 +      "_getRemoving('bar', 3) returns falsey.");
   1.297 +
   1.298 +    is(breakpointsAdded, 2,
   1.299 +      "Correct number of breakpoints have been added.");
   1.300 +    is(breakpointsRemoved, 1,
   1.301 +      "Correct number of breakpoints have been removed.");
   1.302 +    is(editorBreakpointChanges, 4,
   1.303 +      "Correct number of editor breakpoint changes.");
   1.304 +  }
   1.305 +}

mercurial