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