0f2d55a42464c530600fe2d0de18117784a1f9fc
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / inspector / debugger-test.js
1 var initialize_DebuggerTest = function() {
2
3 InspectorTest.startDebuggerTest = function(callback, quiet)
4 {
5     console.assert(WebInspector.debuggerModel.debuggerEnabled(), "Debugger has to be enabled");
6     if (quiet !== undefined)
7         InspectorTest._quiet = quiet;
8     WebInspector.inspectorView.showPanel("sources");
9
10     InspectorTest.addSniffer(WebInspector.debuggerModel, "_pausedScript", InspectorTest._pausedScript, true);
11     InspectorTest.addSniffer(WebInspector.debuggerModel, "_resumedScript", InspectorTest._resumedScript, true);
12     InspectorTest.safeWrap(callback)();
13 };
14
15 InspectorTest.completeDebuggerTest = function()
16 {
17     WebInspector.breakpointManager.setBreakpointsActive(true);
18     InspectorTest.resumeExecution(InspectorTest.completeTest.bind(InspectorTest));
19 };
20
21 InspectorTest.runDebuggerTestSuite = function(testSuite)
22 {
23     var testSuiteTests = testSuite.slice();
24
25     function runner()
26     {
27         if (!testSuiteTests.length) {
28             InspectorTest.completeDebuggerTest();
29             return;
30         }
31
32         var nextTest = testSuiteTests.shift();
33         InspectorTest.addResult("");
34         InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
35         InspectorTest.safeWrap(nextTest)(runner, runner);
36     }
37
38     InspectorTest.startDebuggerTest(runner);
39 };
40
41 InspectorTest.runTestFunction = function()
42 {
43     InspectorTest.evaluateInConsole("setTimeout(testFunction, 0)");
44     InspectorTest.addResult("Set timer for test function.");
45 };
46
47 InspectorTest.runTestFunctionAndWaitUntilPaused = function(callback)
48 {
49     InspectorTest.runTestFunction();
50     InspectorTest.waitUntilPaused(callback);
51 };
52
53 InspectorTest.runAsyncCallStacksTest = function(totalDebuggerStatements, maxAsyncCallStackDepth)
54 {
55     InspectorTest.setQuiet(true);
56     InspectorTest.startDebuggerTest(step1);
57
58     function step1()
59     {
60         DebuggerAgent.setAsyncCallStackDepth(maxAsyncCallStackDepth, step2);
61     }
62
63     function step2()
64     {
65         InspectorTest.runTestFunctionAndWaitUntilPaused(didPause);
66     }
67
68     var step = 0;
69     var callStacksOutput = [];
70     function didPause(callFrames, reason, breakpointIds, asyncStackTrace)
71     {
72         ++step;
73         callStacksOutput.push(InspectorTest.captureStackTraceIntoString(callFrames, asyncStackTrace) + "\n");
74         if (step < totalDebuggerStatements) {
75             InspectorTest.resumeExecution(InspectorTest.waitUntilPaused.bind(InspectorTest, didPause));
76         } else {
77             InspectorTest.addResult("Captured call stacks in no particular order:");
78             callStacksOutput.sort();
79             InspectorTest.addResults(callStacksOutput);
80             InspectorTest.completeDebuggerTest();
81         }
82     }
83 };
84
85 InspectorTest.waitUntilPausedNextTime = function(callback)
86 {
87     InspectorTest._waitUntilPausedCallback = InspectorTest.safeWrap(callback);
88 };
89
90 InspectorTest.waitUntilPaused = function(callback)
91 {
92     callback = InspectorTest.safeWrap(callback);
93
94     if (InspectorTest._pausedScriptArguments)
95         callback.apply(callback, InspectorTest._pausedScriptArguments);
96     else
97         InspectorTest._waitUntilPausedCallback = callback;
98 };
99
100 InspectorTest.waitUntilResumedNextTime = function(callback)
101 {
102     InspectorTest._waitUntilResumedCallback = InspectorTest.safeWrap(callback);
103 };
104
105 InspectorTest.waitUntilResumed = function(callback)
106 {
107     callback = InspectorTest.safeWrap(callback);
108
109     if (!InspectorTest._pausedScriptArguments)
110         callback();
111     else
112         InspectorTest._waitUntilResumedCallback = callback;
113 };
114
115 InspectorTest.resumeExecution = function(callback)
116 {
117     if (WebInspector.panels.sources.paused())
118         WebInspector.panels.sources.togglePause();
119     InspectorTest.waitUntilResumed(callback);
120 };
121
122 InspectorTest.waitUntilPausedAndDumpStackAndResume = function(callback, options)
123 {
124     InspectorTest.waitUntilPaused(paused);
125     InspectorTest.addSniffer(WebInspector.CallStackSidebarPane.prototype, "setStatus", setStatus);
126
127     var caption;
128     var callFrames;
129     var asyncStackTrace;
130
131     function setStatus(status)
132     {
133         if (typeof status === "string")
134             caption = status;
135         else
136             caption = status.textContent;
137         if (callFrames)
138             step1();
139     }
140
141     function paused(frames, reason, breakpointIds, async)
142     {
143         callFrames = frames;
144         asyncStackTrace = async;
145         if (typeof caption === "string")
146             step1();
147     }
148
149     function step1()
150     {
151         InspectorTest.captureStackTrace(callFrames, asyncStackTrace, options);
152         InspectorTest.addResult(caption);
153         InspectorTest.runAfterPendingDispatches(step2);
154     }
155
156     function step2()
157     {
158         InspectorTest.resumeExecution(InspectorTest.safeWrap(callback));
159     }
160 };
161
162 InspectorTest.waitUntilPausedAndPerformSteppingActions = function(actions, callback)
163 {
164     callback = InspectorTest.safeWrap(callback);
165     InspectorTest.waitUntilPaused(didPause);
166
167     function didPause(callFrames, reason, breakpointIds, asyncStackTrace)
168     {
169         var action = actions.shift();
170         if (action === "Print") {
171             InspectorTest.captureStackTrace(callFrames, asyncStackTrace);
172             InspectorTest.addResult("");
173             while (action === "Print")
174                 action = actions.shift();
175         }
176
177         if (!action) {
178             callback()
179             return;
180         }
181
182         InspectorTest.addResult("Executing " + action + "...");
183
184         switch (action) {
185         case "StepInto":
186             WebInspector.panels.sources._stepIntoButton.element.click();
187             break;
188         case "StepOver":
189             WebInspector.panels.sources._stepOverButton.element.click();
190             break;
191         case "StepOut":
192             WebInspector.panels.sources._stepOutButton.element.click();
193             break;
194         case "Resume":
195             WebInspector.panels.sources.togglePause();
196             break;
197         default:
198             InspectorTest.addResult("FAIL: Unknown action: " + action);
199             callback()
200             return;
201         }
202
203         InspectorTest.waitUntilResumed(InspectorTest.waitUntilPaused.bind(InspectorTest, didPause));
204     }
205 };
206
207 InspectorTest.captureStackTrace = function(callFrames, asyncStackTrace, options)
208 {
209     InspectorTest.addResult(InspectorTest.captureStackTraceIntoString(callFrames, asyncStackTrace, options));
210 };
211
212 InspectorTest.captureStackTraceIntoString = function(callFrames, asyncStackTrace, options)
213 {
214     var results = [];
215     options = options || {};
216
217     function printCallFrames(callFrames)
218     {
219         var printed = 0;
220         for (var i = 0; i < callFrames.length; i++) {
221             var frame = callFrames[i];
222             var script = WebInspector.debuggerModel.scriptForId(frame.location().scriptId);
223             var isFramework = WebInspector.BlackboxSupport.isBlackboxedURL(script.sourceURL);
224             if (options.dropFrameworkCallFrames && isFramework)
225                 continue;
226             var url;
227             var lineNumber;
228             if (script) {
229                 url = WebInspector.displayNameForURL(script.sourceURL);
230                 lineNumber = frame.location().lineNumber + 1;
231             } else {
232                 url = "(internal script)";
233                 lineNumber = "(line number)";
234             }
235             var s = (isFramework ? "  * " : "    ") + (printed++) + ") " + frame.functionName + " (" + url + (options.dropLineNumbers ? "" : ":" + lineNumber) + ")";
236             results.push(s);
237             if (options.printReturnValue && frame.returnValue())
238                 results.push("       <return>: " + frame.returnValue().description);
239         }
240         return printed;
241     }
242
243     results.push("Call stack:");
244     printCallFrames(callFrames);
245
246     while (asyncStackTrace) {
247         results.push("    [" + (asyncStackTrace.description || "Async Call") + "]");
248         var printed = printCallFrames(WebInspector.DebuggerModel.CallFrame.fromPayloadArray(WebInspector.targetManager.mainTarget(), asyncStackTrace.callFrames));
249         if (!printed)
250             results.pop();
251         if (asyncStackTrace.callFrames.peekLast().functionName === "testFunction")
252             break;
253         asyncStackTrace = asyncStackTrace.asyncStackTrace;
254     }
255     return results.join("\n");
256 };
257
258 InspectorTest.dumpSourceFrameContents = function(sourceFrame)
259 {
260     InspectorTest.addResult("==Source frame contents start==");
261     var textEditor = sourceFrame._textEditor;
262     for (var i = 0; i < textEditor.linesCount; ++i)
263         InspectorTest.addResult(textEditor.line(i));
264     InspectorTest.addResult("==Source frame contents end==");
265 };
266
267 InspectorTest._pausedScript = function(callFrames, reason, auxData, breakpointIds, asyncStackTrace)
268 {
269     if (!InspectorTest._quiet)
270         InspectorTest.addResult("Script execution paused.");
271     InspectorTest._pausedScriptArguments = [WebInspector.DebuggerModel.CallFrame.fromPayloadArray(WebInspector.targetManager.mainTarget(), callFrames), reason, breakpointIds, asyncStackTrace, auxData];
272     if (InspectorTest._waitUntilPausedCallback) {
273         var callback = InspectorTest._waitUntilPausedCallback;
274         delete InspectorTest._waitUntilPausedCallback;
275         callback.apply(callback, InspectorTest._pausedScriptArguments);
276     }
277 };
278
279 InspectorTest._resumedScript = function()
280 {
281     if (!InspectorTest._quiet)
282         InspectorTest.addResult("Script execution resumed.");
283     delete InspectorTest._pausedScriptArguments;
284     if (InspectorTest._waitUntilResumedCallback) {
285         var callback = InspectorTest._waitUntilResumedCallback;
286         delete InspectorTest._waitUntilResumedCallback;
287         callback();
288     }
289 };
290
291 InspectorTest.showUISourceCode = function(uiSourceCode, callback)
292 {
293     var panel = WebInspector.inspectorView.showPanel("sources");
294     panel.showUISourceCode(uiSourceCode);
295     var sourceFrame = panel.visibleView;
296     if (sourceFrame.loaded)
297         callback(sourceFrame);
298     else
299         InspectorTest.addSniffer(sourceFrame, "onTextEditorContentLoaded", callback && callback.bind(null, sourceFrame));
300 };
301
302 InspectorTest.showScriptSource = function(scriptName, callback)
303 {
304     InspectorTest.waitForScriptSource(scriptName, function(uiSourceCode) { InspectorTest.showUISourceCode(uiSourceCode, callback); });
305 };
306
307 InspectorTest.waitForScriptSource = function(scriptName, callback)
308 {
309     var panel = WebInspector.inspectorView.showPanel("sources");
310     var uiSourceCodes = panel._workspace.uiSourceCodes();
311     for (var i = 0; i < uiSourceCodes.length; ++i) {
312         if (uiSourceCodes[i].name() === scriptName) {
313             callback(uiSourceCodes[i]);
314             return;
315         }
316     }
317
318     InspectorTest.addSniffer(WebInspector.SourcesView.prototype, "_addUISourceCode", InspectorTest.waitForScriptSource.bind(InspectorTest, scriptName, callback));
319 };
320
321 InspectorTest.dumpNavigatorView = function(navigatorView, id, prefix)
322 {
323     InspectorTest.addResult(prefix + "Dumping ScriptsNavigator " + id + " tab:");
324     dumpNavigatorTreeOutline(prefix, navigatorView._scriptsTree);
325
326     function dumpNavigatorTreeElement(prefix, treeElement)
327     {
328         InspectorTest.addResult(prefix + treeElement.titleText);
329         for (var i = 0; i < treeElement.children.length; ++i)
330             dumpNavigatorTreeElement(prefix + "  ", treeElement.children[i]);
331     }
332
333     function dumpNavigatorTreeOutline(prefix, treeOutline)
334     {
335         for (var i = 0; i < treeOutline.children.length; ++i)
336             dumpNavigatorTreeElement(prefix + "  ", treeOutline.children[i]);
337     }
338 };
339
340 InspectorTest.setBreakpoint = function(sourceFrame, lineNumber, condition, enabled)
341 {
342     if (!sourceFrame._muted)
343         sourceFrame._setBreakpoint(lineNumber, 0, condition, enabled);
344 };
345
346 InspectorTest.removeBreakpoint = function(sourceFrame, lineNumber)
347 {
348     sourceFrame._breakpointManager.findBreakpointOnLine(sourceFrame._uiSourceCode, lineNumber).remove();
349 };
350
351 InspectorTest.dumpBreakpointSidebarPane = function(title)
352 {
353     var paneElement = WebInspector.panels.sources.sidebarPanes.jsBreakpoints.listElement;
354     InspectorTest.addResult("Breakpoint sidebar pane " + (title || ""));
355     InspectorTest.addResult(InspectorTest.textContentWithLineBreaks(paneElement));
356 };
357
358 InspectorTest.expandProperties = function(properties, callback)
359 {
360     var index = 0;
361     function expandNextPath()
362     {
363         if (index === properties.length) {
364             InspectorTest.safeWrap(callback)();
365             return;
366         }
367         var parentTreeElement = properties[index++];
368         var path = properties[index++];
369         InspectorTest._expandProperty(parentTreeElement, path, 0, expandNextPath);
370     }
371     InspectorTest.runAfterPendingDispatches(expandNextPath);
372 };
373
374 InspectorTest._expandProperty = function(parentTreeElement, path, pathIndex, callback)
375 {
376     if (pathIndex === path.length) {
377         InspectorTest.addResult("Expanded property: " + path.join("."));
378         callback();
379         return;
380     }
381     var name = path[pathIndex++];
382     var propertyTreeElement = InspectorTest._findChildPropertyTreeElement(parentTreeElement, name);
383     if (!propertyTreeElement) {
384        InspectorTest.addResult("Failed to expand property: " + path.slice(0, pathIndex).join("."));
385        InspectorTest.completeDebuggerTest();
386        return;
387     }
388     propertyTreeElement.expand();
389     InspectorTest.runAfterPendingDispatches(InspectorTest._expandProperty.bind(InspectorTest, propertyTreeElement, path, pathIndex, callback));
390 };
391
392 InspectorTest._findChildPropertyTreeElement = function(parent, childName)
393 {
394     var children = parent.children;
395     for (var i = 0; i < children.length; i++) {
396         var treeElement = children[i];
397         var property = treeElement.property;
398         if (property.name === childName)
399             return treeElement;
400     }
401 };
402
403 InspectorTest.setQuiet = function(quiet)
404 {
405     InspectorTest._quiet = quiet;
406 };
407
408 InspectorTest.queryScripts = function(filter)
409 {
410     var scripts = [];
411     for (var scriptId in WebInspector.debuggerModel._scripts) {
412         var script = WebInspector.debuggerModel._scripts[scriptId];
413         if (!filter || filter(script))
414             scripts.push(script);
415     }
416     return scripts;
417 };
418
419 InspectorTest.createScriptMock = function(url, startLine, startColumn, isContentScript, source, target, preRegisterCallback)
420 {
421     target = target || WebInspector.targetManager.mainTarget();
422     var scriptId = ++InspectorTest._lastScriptId;
423     var lineCount = source.lineEndings().length;
424     var endLine = startLine + lineCount - 1;
425     var endColumn = lineCount === 1 ? startColumn + source.length : source.length - source.lineEndings()[lineCount - 2];
426     var hasSourceURL = !!source.match(/\/\/#\ssourceURL=\s*(\S*?)\s*$/m) || !!source.match(/\/\/@\ssourceURL=\s*(\S*?)\s*$/m);
427     var script = new WebInspector.Script(target, scriptId, url, startLine, startColumn, endLine, endColumn, isContentScript, null, hasSourceURL);
428     script.requestContent = function(callback)
429     {
430         var trimmedSource = WebInspector.Script._trimSourceURLComment(source);
431         callback(trimmedSource, false, "text/javascript");
432     };
433     if (preRegisterCallback)
434         preRegisterCallback(script);
435     target.debuggerModel._registerScript(script);
436     return script;
437 };
438
439 InspectorTest._lastScriptId = 0;
440
441 InspectorTest.checkRawLocation = function(script, lineNumber, columnNumber, location)
442 {
443     InspectorTest.assertEquals(script.scriptId, location.scriptId, "Incorrect scriptId");
444     InspectorTest.assertEquals(lineNumber, location.lineNumber, "Incorrect lineNumber");
445     InspectorTest.assertEquals(columnNumber, location.columnNumber, "Incorrect columnNumber");
446 };
447
448 InspectorTest.checkUILocation = function(uiSourceCode, lineNumber, columnNumber, location)
449 {
450     InspectorTest.assertEquals(uiSourceCode, location.uiSourceCode, "Incorrect uiSourceCode, expected '" + (uiSourceCode ? uiSourceCode.originURL() : null) + "'," +
451                                                                     " but got '" + (location.uiSourceCode ? location.uiSourceCode.originURL() : null) + "'");
452     InspectorTest.assertEquals(lineNumber, location.lineNumber, "Incorrect lineNumber, expected '" + lineNumber + "', but got '" + location.lineNumber + "'");
453     InspectorTest.assertEquals(columnNumber, location.columnNumber, "Incorrect columnNumber, expected '" + columnNumber + "', but got '" + location.columnNumber + "'");
454 };
455
456 InspectorTest.scriptFormatter = function()
457 {
458     var editorActions = self.runtime.instances(WebInspector.SourcesView.EditorAction);
459     for (var i = 0; i < editorActions.length; ++i) {
460         if (editorActions[i] instanceof WebInspector.ScriptFormatterEditorAction)
461             return editorActions[i];
462     }
463 };
464
465 };