Web Inspector: debugger-activation-crash2.html are flaky.
[WebKit-https.git] / 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.showPanel("scripts");
8
9     if (WebInspector.panels.scripts._debuggerEnabled)
10         startTest();
11     else {
12         InspectorTest.addSniffer(WebInspector.debuggerModel, "_debuggerWasEnabled", startTest);
13         WebInspector.panels.scripts._toggleDebugging(false);
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.completeDebuggerTest = function()
26 {
27     var scriptsPanel = WebInspector.panels.scripts;
28
29     if (!scriptsPanel.breakpointsActivated)
30         scriptsPanel.toggleBreakpointsButton.element.click();
31
32     InspectorTest.resumeExecution(disableDebugger);
33
34     function disableDebugger()
35     {
36         if (!scriptsPanel._debuggerEnabled)
37             completeTest();
38         else {
39             InspectorTest.addSniffer(WebInspector.debuggerModel, "_debuggerWasDisabled", completeTest);
40             scriptsPanel._toggleDebugging(false);
41         }
42     }
43
44     function completeTest()
45     {
46         InspectorTest.addResult("Debugger was disabled.");
47         InspectorTest.completeTest();
48     }
49 };
50
51 InspectorTest.runDebuggerTestSuite = function(testSuite)
52 {
53     var testSuiteTests = testSuite.slice();
54
55     function runner()
56     {
57         if (!testSuiteTests.length) {
58             InspectorTest.completeDebuggerTest();
59             return;
60         }
61
62         var nextTest = testSuiteTests.shift();
63         InspectorTest.addResult("");
64         InspectorTest.addResult("Running: " + /function\s([^(]*)/.exec(nextTest)[1]);
65         InspectorTest.safeWrap(nextTest)(runner, runner);
66     }
67
68     InspectorTest.startDebuggerTest(runner);
69 }
70
71 InspectorTest.runTestFunctionAndWaitUntilPaused = function(callback)
72 {
73     InspectorTest.evaluateInConsole("setTimeout(testFunction, 0)");
74     InspectorTest.addResult("Set timer for test function.");
75     InspectorTest.waitUntilPaused(callback);
76 };
77
78 InspectorTest.waitUntilPaused = function(callback)
79 {
80     callback = InspectorTest.safeWrap(callback);
81
82     if (InspectorTest._callFrames)
83         callback(InspectorTest._callFrames);
84     else
85         InspectorTest._waitUntilPausedCallback = callback;
86 };
87
88 InspectorTest.waitUntilResumed = function(callback)
89 {
90     callback = InspectorTest.safeWrap(callback);
91
92     if (!InspectorTest._callFrames)
93         callback();
94     else
95         InspectorTest._waitUntilResumedCallback = callback;
96 };
97
98 InspectorTest.resumeExecution = function(callback)
99 {
100     if (WebInspector.panels.scripts.paused)
101         WebInspector.panels.scripts._togglePause();
102     InspectorTest.waitUntilResumed(callback);
103 };
104
105 InspectorTest.captureStackTrace = function(callFrames, dropLineNumbers)
106 {
107     InspectorTest.addResult("Call stack:");
108     for (var i = 0; i < callFrames.length; i++) {
109         var frame = callFrames[i];
110         var script = WebInspector.debuggerModel.scriptForSourceID(frame.location.scriptId);
111         var url;
112         var lineNumber;
113         if (script) {
114             url = WebInspector.displayNameForURL(script.sourceURL);
115             lineNumber = frame.location.lineNumber + 1;
116         } else {
117             url = "(internal script)";
118             lineNumber = "(line number)";
119         }
120         var s = "    " + i + ") " + frame.functionName + " (" + url + (dropLineNumbers ? "" : ":" + lineNumber) + ")";
121         InspectorTest.addResult(s);
122     }
123 };
124
125 InspectorTest.dumpSourceFrameContents = function(sourceFrame)
126 {
127     InspectorTest.addResult("==Source frame contents start==");
128     var textModel = sourceFrame._textModel;
129     for (var i = 0; i < textModel.linesCount; ++i)
130         InspectorTest.addResult(textModel.line(i));
131     InspectorTest.addResult("==Source frame contents end==");
132 };
133
134 InspectorTest._pausedScript = function(callFrames, reason, auxData)
135 {
136     if (!InspectorTest._quiet)
137         InspectorTest.addResult("Script execution paused.");
138     InspectorTest._callFrames = callFrames;
139     if (InspectorTest._waitUntilPausedCallback) {
140         var callback = InspectorTest._waitUntilPausedCallback;
141         delete InspectorTest._waitUntilPausedCallback;
142         callback(InspectorTest._callFrames);
143     }
144 }
145
146 InspectorTest._resumedScript = function()
147 {
148     if (!InspectorTest._quiet)
149         InspectorTest.addResult("Script execution resumed.");
150     delete InspectorTest._callFrames;
151     if (InspectorTest._waitUntilResumedCallback) {
152         var callback = InspectorTest._waitUntilResumedCallback;
153         delete InspectorTest._waitUntilResumedCallback;
154         callback();
155     }
156 };
157
158 InspectorTest.showScriptSource = function(scriptName, callback)
159 {
160     var filesSelect = document.getElementById("scripts-files");
161     for (var i = 0; i < filesSelect.length; ++i) {
162         if (filesSelect[i].displayName === scriptName) {
163             filesSelect.selectedIndex = i;
164             WebInspector.panels.scripts._filesSelectChanged();
165             var sourceFrame = WebInspector.panels.scripts.visibleView;
166             if (sourceFrame.loaded)
167                 callback(sourceFrame);
168             else
169                 sourceFrame.addEventListener(WebInspector.SourceFrame.Events.Loaded, callback.bind(null, sourceFrame));
170             return;
171         }
172     }
173     InspectorTest.addSniffer(WebInspector.panels.scripts, "_addOptionToFilesSelect", InspectorTest.showScriptSource.bind(InspectorTest, scriptName, callback));
174 };
175
176 InspectorTest.setBreakpoint = function(sourceFrame, lineNumber, condition, enabled)
177 {
178     sourceFrame._delegate.setBreakpoint(lineNumber, condition, enabled);
179 };
180
181 InspectorTest.removeBreakpoint = function(sourceFrame, lineNumber)
182 {
183     sourceFrame._delegate.removeBreakpoint(lineNumber);
184 };
185
186
187 InspectorTest.expandProperties = function(properties, callback)
188 {
189     var index = 0;
190     function expandNextPath()
191     {
192         if (index === properties.length) {
193             InspectorTest.safeWrap(callback)();
194             return;
195         }
196         var parentTreeElement = properties[index++];
197         var path = properties[index++];
198         InspectorTest._expandProperty(parentTreeElement, path, 0, expandNextPath);
199     }
200     InspectorTest.runAfterPendingDispatches(expandNextPath);
201 };
202
203 InspectorTest._expandProperty = function(parentTreeElement, path, pathIndex, callback)
204 {
205     if (pathIndex === path.length) {
206         InspectorTest.addResult("Expanded property: " + path.join("."));
207         callback();
208         return;
209     }
210     var name = path[pathIndex++];
211     var propertyTreeElement = InspectorTest._findChildPropertyTreeElement(parentTreeElement, name);
212     if (!propertyTreeElement) {
213        InspectorTest.addResult("Failed to expand property: " + path.slice(0, pathIndex).join("."));
214        InspectorTest.completeDebuggerTest();
215        return;
216     }
217     propertyTreeElement.expand();
218     InspectorTest.runAfterPendingDispatches(InspectorTest._expandProperty.bind(InspectorTest, propertyTreeElement, path, pathIndex, callback));
219 };
220
221 InspectorTest._findChildPropertyTreeElement = function(parent, childName)
222 {
223     var children = parent.children;
224     for (var i = 0; i < children.length; i++) {
225         var treeElement = children[i];
226         var property = treeElement.property;
227         if (property.name === childName)
228             return treeElement;
229     }
230 };
231
232 InspectorTest.setQuiet = function(quiet)
233 {
234     InspectorTest._quiet = quiet;
235 };
236
237 };