Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / http / tests / inspector / debugger-test.js
blob38d074e029a163cccebaf97bdfd23aca35163bb4
1 var initialize_DebuggerTest = function() {
3 InspectorTest.preloadPanel("sources");
5 InspectorTest.startDebuggerTest = function(callback, quiet)
7 console.assert(InspectorTest.debuggerModel.debuggerEnabled(), "Debugger has to be enabled");
8 if (quiet !== undefined)
9 InspectorTest._quiet = quiet;
10 WebInspector.SourcesPanel.show();
12 InspectorTest.addSniffer(WebInspector.DebuggerModel.prototype, "_pausedScript", InspectorTest._pausedScript, true);
13 InspectorTest.addSniffer(WebInspector.DebuggerModel.prototype, "_resumedScript", InspectorTest._resumedScript, true);
14 InspectorTest.safeWrap(callback)();
17 InspectorTest.completeDebuggerTest = function()
19 WebInspector.breakpointManager.setBreakpointsActive(true);
20 InspectorTest.resumeExecution(InspectorTest.completeTest.bind(InspectorTest));
23 (function() {
24 // FIXME: Until there is no window.onerror() for uncaught exceptions in promises
25 // we use this hack to print the exceptions instead of just timing out.
27 var origThen = Promise.prototype.then;
28 var origCatch = Promise.prototype.catch;
30 Promise.prototype.then = function()
32 var result = origThen.apply(this, arguments);
33 origThen.call(result, undefined, onUncaughtPromiseReject);
34 return result;
37 Promise.prototype.catch = function()
39 var result = origCatch.apply(this, arguments);
40 origThen.call(result, undefined, onUncaughtPromiseReject);
41 return result;
44 function onUncaughtPromiseReject(e)
46 var message = (typeof e === "object" && e.stack) || e;
47 InspectorTest.addResult("FAIL: Uncaught exception in promise: " + message);
48 InspectorTest.completeDebuggerTest();
50 })();
52 InspectorTest.runDebuggerTestSuite = function(testSuite)
54 var testSuiteTests = testSuite.slice();
56 function runner()
58 if (!testSuiteTests.length) {
59 InspectorTest.completeDebuggerTest();
60 return;
63 var nextTest = testSuiteTests.shift();
64 InspectorTest.addResult("");
65 InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
66 InspectorTest.safeWrap(nextTest)(runner, runner);
69 InspectorTest.startDebuggerTest(runner);
72 InspectorTest.runTestFunction = function()
74 InspectorTest.evaluateInPage("setTimeout(testFunction, 0)");
75 InspectorTest.addResult("Set timer for test function.");
78 InspectorTest.runTestFunctionAndWaitUntilPaused = function(callback)
80 InspectorTest.runTestFunction();
81 InspectorTest.waitUntilPaused(callback);
84 InspectorTest.runAsyncCallStacksTest = function(totalDebuggerStatements, maxAsyncCallStackDepth)
86 InspectorTest.setQuiet(true);
87 InspectorTest.startDebuggerTest(step1);
89 function step1()
91 InspectorTest.DebuggerAgent.setAsyncCallStackDepth(maxAsyncCallStackDepth, step2);
94 function step2()
96 InspectorTest.runTestFunctionAndWaitUntilPaused(didPause);
99 var step = 0;
100 var callStacksOutput = [];
101 function didPause(callFrames, reason, breakpointIds, asyncStackTrace)
103 ++step;
104 callStacksOutput.push(InspectorTest.captureStackTraceIntoString(callFrames, asyncStackTrace) + "\n");
105 if (step < totalDebuggerStatements) {
106 InspectorTest.resumeExecution(InspectorTest.waitUntilPaused.bind(InspectorTest, didPause));
107 } else {
108 InspectorTest.addResult("Captured call stacks in no particular order:");
109 callStacksOutput.sort();
110 InspectorTest.addResults(callStacksOutput);
111 InspectorTest.completeDebuggerTest();
116 InspectorTest.waitUntilPausedNextTime = function(callback)
118 InspectorTest._waitUntilPausedCallback = InspectorTest.safeWrap(callback);
121 InspectorTest.waitUntilPaused = function(callback)
123 callback = InspectorTest.safeWrap(callback);
125 if (InspectorTest._pausedScriptArguments)
126 callback.apply(callback, InspectorTest._pausedScriptArguments);
127 else
128 InspectorTest._waitUntilPausedCallback = callback;
131 InspectorTest.waitUntilResumedNextTime = function(callback)
133 InspectorTest._waitUntilResumedCallback = InspectorTest.safeWrap(callback);
136 InspectorTest.waitUntilResumed = function(callback)
138 callback = InspectorTest.safeWrap(callback);
140 if (!InspectorTest._pausedScriptArguments)
141 callback();
142 else
143 InspectorTest._waitUntilResumedCallback = callback;
146 InspectorTest.resumeExecution = function(callback)
148 if (WebInspector.panels.sources.paused())
149 WebInspector.panels.sources.togglePause();
150 InspectorTest.waitUntilResumed(callback);
153 InspectorTest.waitUntilPausedAndDumpStackAndResume = function(callback, options)
155 InspectorTest.waitUntilPaused(paused);
156 InspectorTest.addSniffer(WebInspector.CallStackSidebarPane.prototype, "setStatus", setStatus);
158 var caption;
159 var callFrames;
160 var asyncStackTrace;
162 function setStatus(status)
164 if (typeof status === "string")
165 caption = status;
166 else
167 caption = status.textContent;
168 if (callFrames)
169 step1();
172 function paused(frames, reason, breakpointIds, async)
174 callFrames = frames;
175 asyncStackTrace = async;
176 if (typeof caption === "string")
177 step1();
180 function step1()
182 InspectorTest.captureStackTrace(callFrames, asyncStackTrace, options);
183 InspectorTest.addResult(InspectorTest.clearSpecificInfoFromStackFrames(caption));
184 InspectorTest.runAfterPendingDispatches(step2);
187 function step2()
189 InspectorTest.resumeExecution(InspectorTest.safeWrap(callback));
193 InspectorTest.waitUntilPausedAndPerformSteppingActions = function(actions, callback)
195 callback = InspectorTest.safeWrap(callback);
196 InspectorTest.waitUntilPaused(didPause);
198 function didPause(callFrames, reason, breakpointIds, asyncStackTrace)
200 var action = actions.shift();
201 if (action === "Print") {
202 InspectorTest.captureStackTrace(callFrames, asyncStackTrace);
203 InspectorTest.addResult("");
204 while (action === "Print")
205 action = actions.shift();
208 if (!action) {
209 callback()
210 return;
213 InspectorTest.addResult("Executing " + action + "...");
215 switch (action) {
216 case "StepInto":
217 WebInspector.panels.sources._stepIntoButton.element.click();
218 break;
219 case "StepOver":
220 WebInspector.panels.sources._stepOverButton.element.click();
221 break;
222 case "StepOut":
223 WebInspector.panels.sources._stepOutButton.element.click();
224 break;
225 case "Resume":
226 WebInspector.panels.sources.togglePause();
227 break;
228 case "StepIntoAsync":
229 InspectorTest.DebuggerAgent.stepIntoAsync();
230 break;
231 default:
232 InspectorTest.addResult("FAIL: Unknown action: " + action);
233 callback()
234 return;
237 InspectorTest.waitUntilResumed(actions.length ? InspectorTest.waitUntilPaused.bind(InspectorTest, didPause) : callback);
241 InspectorTest.captureStackTrace = function(callFrames, asyncStackTrace, options)
243 InspectorTest.addResult(InspectorTest.captureStackTraceIntoString(callFrames, asyncStackTrace, options));
246 InspectorTest.captureStackTraceIntoString = function(callFrames, asyncStackTrace, options)
248 var results = [];
249 options = options || {};
251 function printCallFrames(callFrames)
253 var printed = 0;
254 for (var i = 0; i < callFrames.length; i++) {
255 var frame = callFrames[i];
256 var script = frame.location().script();
257 var uiLocation = WebInspector.debuggerWorkspaceBinding.rawLocationToUILocation(frame.location());
258 var isFramework = WebInspector.BlackboxSupport.isBlackboxedURL(script.sourceURL);
259 if (options.dropFrameworkCallFrames && isFramework)
260 continue;
261 var url;
262 var lineNumber;
263 if (uiLocation && uiLocation.uiSourceCode.project().type() !== WebInspector.projectTypes.Debugger) {
264 url = uiLocation.uiSourceCode.name();
265 lineNumber = uiLocation.lineNumber + 1;
266 } else {
267 url = WebInspector.displayNameForURL(script.sourceURL);
268 lineNumber = frame.location().lineNumber + 1;
270 var s = (isFramework ? " * " : " ") + (printed++) + ") " + frame.functionName + " (" + url + (options.dropLineNumbers ? "" : ":" + lineNumber) + ")";
271 results.push(s);
272 if (options.printReturnValue && frame.returnValue())
273 results.push(" <return>: " + frame.returnValue().description);
275 return printed;
278 results.push("Call stack:");
279 printCallFrames(callFrames);
281 while (asyncStackTrace) {
282 results.push(" [" + (asyncStackTrace.description || "Async Call") + "]");
283 var debuggerModel = WebInspector.DebuggerModel.fromTarget(WebInspector.targetManager.mainTarget());
284 var printed = printCallFrames(WebInspector.DebuggerModel.CallFrame.fromPayloadArray(debuggerModel, asyncStackTrace.callFrames));
285 if (!printed)
286 results.pop();
287 if (asyncStackTrace.callFrames.peekLast().functionName === "testFunction")
288 break;
289 asyncStackTrace = asyncStackTrace.asyncStackTrace;
291 return results.join("\n");
294 InspectorTest.dumpSourceFrameContents = function(sourceFrame)
296 InspectorTest.addResult("==Source frame contents start==");
297 var textEditor = sourceFrame._textEditor;
298 for (var i = 0; i < textEditor.linesCount; ++i)
299 InspectorTest.addResult(textEditor.line(i));
300 InspectorTest.addResult("==Source frame contents end==");
303 InspectorTest._pausedScript = function(callFrames, reason, auxData, breakpointIds, asyncStackTrace)
305 if (!InspectorTest._quiet)
306 InspectorTest.addResult("Script execution paused.");
307 var debuggerModel = WebInspector.DebuggerModel.fromTarget(this.target());
308 InspectorTest._pausedScriptArguments = [WebInspector.DebuggerModel.CallFrame.fromPayloadArray(debuggerModel, callFrames), reason, breakpointIds, asyncStackTrace, auxData];
309 if (InspectorTest._waitUntilPausedCallback) {
310 var callback = InspectorTest._waitUntilPausedCallback;
311 delete InspectorTest._waitUntilPausedCallback;
312 callback.apply(callback, InspectorTest._pausedScriptArguments);
316 InspectorTest._resumedScript = function()
318 if (!InspectorTest._quiet)
319 InspectorTest.addResult("Script execution resumed.");
320 delete InspectorTest._pausedScriptArguments;
321 if (InspectorTest._waitUntilResumedCallback) {
322 var callback = InspectorTest._waitUntilResumedCallback;
323 delete InspectorTest._waitUntilResumedCallback;
324 callback();
328 InspectorTest.showUISourceCode = function(uiSourceCode, callback)
330 var panel = WebInspector.panels.sources;
331 panel.showUISourceCode(uiSourceCode);
332 var sourceFrame = panel.visibleView;
333 if (sourceFrame.loaded)
334 callback(sourceFrame);
335 else
336 InspectorTest.addSniffer(sourceFrame, "onTextEditorContentLoaded", callback && callback.bind(null, sourceFrame));
339 InspectorTest.showScriptSource = function(scriptName, callback)
341 InspectorTest.waitForScriptSource(scriptName, function(uiSourceCode) { InspectorTest.showUISourceCode(uiSourceCode, callback); });
344 InspectorTest.waitForScriptSource = function(scriptName, callback)
346 var panel = WebInspector.panels.sources;
347 var uiSourceCodes = panel._workspace.uiSourceCodes();
348 for (var i = 0; i < uiSourceCodes.length; ++i) {
349 if (uiSourceCodes[i].project().type() === WebInspector.projectTypes.Service)
350 continue;
351 if (uiSourceCodes[i].name() === scriptName) {
352 callback(uiSourceCodes[i]);
353 return;
357 InspectorTest.addSniffer(WebInspector.SourcesView.prototype, "_addUISourceCode", InspectorTest.waitForScriptSource.bind(InspectorTest, scriptName, callback));
360 InspectorTest.dumpNavigatorView = function(navigatorView, id, prefix)
362 InspectorTest.addResult(prefix + "Dumping ScriptsNavigator " + id + " tab:");
363 dumpNavigatorTreeOutline(prefix, navigatorView._scriptsTree);
365 function dumpNavigatorTreeElement(prefix, treeElement)
367 InspectorTest.addResult(prefix + treeElement.titleText);
368 var children = treeElement.children();
369 for (var i = 0; i < children.length; ++i)
370 dumpNavigatorTreeElement(prefix + " ", children[i]);
373 function dumpNavigatorTreeOutline(prefix, treeOutline)
375 var children = treeOutline.rootElement().children();
376 for (var i = 0; i < children.length; ++i)
377 dumpNavigatorTreeElement(prefix + " ", children[i]);
381 InspectorTest.setBreakpoint = function(sourceFrame, lineNumber, condition, enabled)
383 if (!sourceFrame._muted)
384 sourceFrame._setBreakpoint(lineNumber, 0, condition, enabled);
387 InspectorTest.removeBreakpoint = function(sourceFrame, lineNumber)
389 sourceFrame._breakpointManager.findBreakpointOnLine(sourceFrame._uiSourceCode, lineNumber).remove();
392 InspectorTest.dumpBreakpointSidebarPane = function(title)
394 var paneElement = WebInspector.panels.sources.sidebarPanes.jsBreakpoints.listElement;
395 InspectorTest.addResult("Breakpoint sidebar pane " + (title || ""));
396 InspectorTest.addResult(InspectorTest.textContentWithLineBreaks(paneElement));
399 InspectorTest.dumpScopeVariablesSidebarPane = function()
401 InspectorTest.addResult("Scope variables sidebar pane:");
402 var sections = WebInspector.panels.sources.sidebarPanes.scopechain._sections;
403 for (var i = 0; i < sections.length; ++i) {
404 var textContent = InspectorTest.textContentWithLineBreaks(sections[i].element);
405 var text = InspectorTest.clearSpecificInfoFromStackFrames(textContent);
406 if (text.length > 0)
407 InspectorTest.addResult(text);
408 if (!sections[i].objectTreeElement().expanded)
409 InspectorTest.addResult(" <section collapsed>");
413 InspectorTest.expandScopeVariablesSidebarPane = function(callback)
415 // Expand all but the global scope. Expanding global scope takes for too long so we keep it collapsed.
416 var sections = WebInspector.panels.sources.sidebarPanes.scopechain._sections;
417 for (var i = 0; i < sections.length - 1; ++i)
418 sections[i].expand();
419 InspectorTest.runAfterPendingDispatches(callback);
422 InspectorTest.expandProperties = function(properties, callback)
424 var index = 0;
425 function expandNextPath()
427 if (index === properties.length) {
428 InspectorTest.safeWrap(callback)();
429 return;
431 var parentTreeElement = properties[index++];
432 var path = properties[index++];
433 InspectorTest._expandProperty(parentTreeElement, path, 0, expandNextPath);
435 InspectorTest.runAfterPendingDispatches(expandNextPath);
438 InspectorTest._expandProperty = function(parentTreeElement, path, pathIndex, callback)
440 if (pathIndex === path.length) {
441 InspectorTest.addResult("Expanded property: " + path.join("."));
442 callback();
443 return;
445 var name = path[pathIndex++];
446 var propertyTreeElement = InspectorTest._findChildPropertyTreeElement(parentTreeElement, name);
447 if (!propertyTreeElement) {
448 InspectorTest.addResult("Failed to expand property: " + path.slice(0, pathIndex).join("."));
449 InspectorTest.completeDebuggerTest();
450 return;
452 propertyTreeElement.expand();
453 InspectorTest.runAfterPendingDispatches(InspectorTest._expandProperty.bind(InspectorTest, propertyTreeElement, path, pathIndex, callback));
456 InspectorTest._findChildPropertyTreeElement = function(parent, childName)
458 var children = parent.children();
459 for (var i = 0; i < children.length; i++) {
460 var treeElement = children[i];
461 var property = treeElement.property;
462 if (property.name === childName)
463 return treeElement;
467 InspectorTest.setQuiet = function(quiet)
469 InspectorTest._quiet = quiet;
472 InspectorTest.queryScripts = function(filter)
474 var scripts = [];
475 for (var scriptId in InspectorTest.debuggerModel._scripts) {
476 var script = InspectorTest.debuggerModel._scripts[scriptId];
477 if (!filter || filter(script))
478 scripts.push(script);
480 return scripts;
483 InspectorTest.createScriptMock = function(url, startLine, startColumn, isContentScript, source, target, preRegisterCallback)
485 target = target || WebInspector.targetManager.mainTarget();
486 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target);
487 var scriptId = ++InspectorTest._lastScriptId + "";
488 var lineCount = source.lineEndings().length;
489 var endLine = startLine + lineCount - 1;
490 var endColumn = lineCount === 1 ? startColumn + source.length : source.length - source.lineEndings()[lineCount - 2];
491 var hasSourceURL = !!source.match(/\/\/#\ssourceURL=\s*(\S*?)\s*$/m) || !!source.match(/\/\/@\ssourceURL=\s*(\S*?)\s*$/m);
492 var script = new WebInspector.Script(debuggerModel, scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, null, hasSourceURL);
493 script.requestContent = function(callback)
495 var trimmedSource = WebInspector.Script._trimSourceURLComment(source);
496 callback(trimmedSource, false, "text/javascript");
498 if (preRegisterCallback)
499 preRegisterCallback(script);
500 debuggerModel._registerScript(script);
501 return script;
504 InspectorTest._lastScriptId = 0;
506 InspectorTest.checkRawLocation = function(script, lineNumber, columnNumber, location)
508 InspectorTest.assertEquals(script.scriptId, location.scriptId, "Incorrect scriptId");
509 InspectorTest.assertEquals(lineNumber, location.lineNumber, "Incorrect lineNumber");
510 InspectorTest.assertEquals(columnNumber, location.columnNumber, "Incorrect columnNumber");
513 InspectorTest.checkUILocation = function(uiSourceCode, lineNumber, columnNumber, location)
515 InspectorTest.assertEquals(uiSourceCode, location.uiSourceCode, "Incorrect uiSourceCode, expected '" + (uiSourceCode ? uiSourceCode.uri() : null) + "'," +
516 " but got '" + (location.uiSourceCode ? location.uiSourceCode.uri() : null) + "'");
517 InspectorTest.assertEquals(lineNumber, location.lineNumber, "Incorrect lineNumber, expected '" + lineNumber + "', but got '" + location.lineNumber + "'");
518 InspectorTest.assertEquals(columnNumber, location.columnNumber, "Incorrect columnNumber, expected '" + columnNumber + "', but got '" + location.columnNumber + "'");
521 InspectorTest.scriptFormatter = function()
523 return self.runtime.instancesPromise(WebInspector.SourcesView.EditorAction).then(function(editorActions) {
524 for (var i = 0; i < editorActions.length; ++i) {
525 if (editorActions[i] instanceof WebInspector.ScriptFormatterEditorAction)
526 return editorActions[i];
528 return null;