6c19ff93448315a25196a727ceb50cdd6afc4484
[WebKit-https.git] / LayoutTests / http / tests / inspector / debugger-test2.js
1 var initialize_DebuggerTest = function() {
2
3 InspectorTest.startDebuggerTest = function(callback)
4 {
5     WebInspector.showPanel("scripts");
6
7     if (WebInspector.panels.scripts._debuggerEnabled)
8         startTest();
9     else {
10         InspectorTest._addSniffer(WebInspector, "debuggerWasEnabled", startTest);
11         WebInspector.panels.scripts._toggleDebugging(false);
12     }
13
14     function startTest()
15     {
16         InspectorTest.addResult("Debugger was enabled.");
17         InspectorTest._addSniffer(WebInspector, "pausedScript", InspectorTest._pausedScript, true);
18         InspectorTest._addSniffer(WebInspector, "resumedScript", InspectorTest._resumedScript, true);
19         callback();
20     }
21 };
22
23 InspectorTest.completeDebuggerTest = function()
24 {
25     var scriptsPanel = WebInspector.panels.scripts;
26
27     if (!scriptsPanel.breakpointsActivated)
28         scriptsPanel.toggleBreakpointsButton.element.click();
29
30     InspectorTest.resumeExecution(disableDebugger);
31
32     function disableDebugger()
33     {
34         if (!scriptsPanel._debuggerEnabled)
35             completeTest();
36         else {
37             InspectorTest._addSniffer(WebInspector, "debuggerWasDisabled", completeTest);
38             scriptsPanel._toggleDebugging(false);
39         }
40     }
41
42     function completeTest()
43     {
44         InspectorTest.addResult("Debugger was disabled.");
45         InspectorTest.completeTest();
46     }
47 };
48
49 InspectorTest.runTestFunctionAndWaitUntilPaused = function(callback)
50 {
51     InspectorTest.evaluateInConsole("setTimeout(testFunction, 0)");
52     InspectorTest.addResult("Set timer for test function.");
53     InspectorTest.waitUntilPaused(callback);
54 };
55
56 InspectorTest.waitUntilPaused = function(callback)
57 {
58     if (InspectorTest._callFrames)
59         callback(InspectorTest._callFrames);
60     else
61         InspectorTest._waitUntilPausedCallback = callback;
62 };
63
64 InspectorTest.waitUntilResumed = function(callback)
65 {
66     if (!InspectorTest._callFrames)
67         callback();
68     else
69         InspectorTest._waitUntilResumedCallback = callback;
70 };
71
72 InspectorTest.resumeExecution = function(callback)
73 {
74     if (WebInspector.panels.scripts.paused)
75         WebInspector.panels.scripts._togglePause();
76     if (callback)
77         InspectorTest.waitUntilResumed(callback);
78 };
79
80 InspectorTest.captureStackTrace = function(callFrames)
81 {
82     InspectorTest.addResult("Call stack:");
83     for (var i = 0; i < callFrames.length; i++) {
84         var frame = callFrames[i];
85         var scriptOrResource = WebInspector.panels.scripts._sourceIDMap[frame.sourceID];
86         var url = scriptOrResource && WebInspector.displayNameForURL(scriptOrResource.sourceURL || scriptOrResource.url);
87         var s = "    " + i + ") " + frame.functionName + " (" + url + ":" + frame.line + ")";
88         InspectorTest.addResult(s);
89     }
90 };
91
92 InspectorTest._pausedScript = function(details)
93 {
94     InspectorTest.addResult("Script execution paused.");
95     InspectorTest._callFrames = details.callFrames;
96     if (InspectorTest._waitUntilPausedCallback) {
97         var callback = InspectorTest._waitUntilPausedCallback;
98         delete InspectorTest._waitUntilPausedCallback;
99         callback(InspectorTest._callFrames);
100     }
101 }
102
103 InspectorTest._resumedScript = function()
104 {
105     InspectorTest.addResult("Script execution resumed.");
106     delete InspectorTest._callFrames;
107     if (InspectorTest._waitUntilResumedCallback) {
108         var callback = InspectorTest._waitUntilResumedCallback;
109         delete InspectorTest._waitUntilResumedCallback;
110         callback();
111     }
112 };
113
114 InspectorTest.showScriptSource = function(scriptName, callback)
115 {
116     if (InspectorTest._scriptsAreParsed([scriptName]))
117         InspectorTest._showScriptSource(scriptName, callback);
118     else
119         InspectorTest._addSniffer(WebInspector, "parsedScriptSource", InspectorTest.showScriptSource.bind(InspectorTest, scriptName, callback));
120 };
121
122 InspectorTest.waitUntilCurrentSourceFrameIsLoaded = function(callback)
123 {
124     var sourceFrame = WebInspector.currentPanel.visibleView.sourceFrame;
125     if (sourceFrame._loaded)
126         callback(sourceFrame);
127     else
128         InspectorTest._addSniffer(sourceFrame, "setContent", callback.bind(null, sourceFrame));
129 };
130
131 InspectorTest._scriptsAreParsed = function(scripts)
132 {
133     var scriptSelect = document.getElementById("scripts-files");
134     var options = scriptSelect.options;
135
136     // Check that all the expected scripts are present.
137     for (var i = 0; i < scripts.length; i++) {
138         var found = false;
139         for (var j = 0; j < options.length; j++) {
140             if (options[j].text === scripts[i]) {
141                 found = true;
142                 break;
143             }
144         }
145         if (!found)
146             return false;
147     }
148     return true;
149 };
150
151 InspectorTest._showScriptSource = function(scriptName, callback)
152 {
153     var scriptSelect = document.getElementById("scripts-files");
154     var options = scriptSelect.options;
155     var scriptsPanel = WebInspector.panels.scripts;
156
157     // Select page's script if it's not current option.
158     var scriptResource;
159     if (options[scriptSelect.selectedIndex].text === scriptName)
160         scriptResource = options[scriptSelect.selectedIndex].representedObject;
161     else {
162         var pageScriptIndex = -1;
163         for (var i = 0; i < options.length; i++) {
164             if (options[i].text === scriptName) {
165                 pageScriptIndex = i;
166                 break;
167             }
168         }
169         scriptResource = options[pageScriptIndex].representedObject;
170         scriptsPanel._showScriptOrResource(scriptResource);
171     }
172
173     InspectorTest.waitUntilCurrentSourceFrameIsLoaded(callback);
174 };
175
176 InspectorTest.expandProperties = function(properties, callback)
177 {
178     var index = 0;
179     function expandNextPath()
180     {
181         if (index === properties.length) {
182             callback();
183             return;
184         }
185         var parentTreeElement = properties[index++];
186         var path = properties[index++];
187         InspectorTest._expandProperty(parentTreeElement, path, 0, expandNextPath);
188     }
189     InspectorTest.runAfterPendingDispatches(expandNextPath);
190 };
191
192 InspectorTest._expandProperty = function(parentTreeElement, path, pathIndex, callback)
193 {
194     if (pathIndex === path.length) {
195         InspectorTest.addResult("Expanded property: " + path.join("."));
196         callback();
197         return;
198     }
199     var name = path[pathIndex++];
200     var propertyTreeElement = InspectorTest._findChildPropertyTreeElement(parentTreeElement, name);
201     if (!propertyTreeElement) {
202        InspectorTest.addResult("Failed to expand property: " + path.slice(0, pathIndex).join("."));
203        InspectorTest.completeDebuggerTest();
204        return;
205     }
206     propertyTreeElement.expand();
207     InspectorTest.runAfterPendingDispatches(InspectorTest._expandProperty.bind(InspectorTest, propertyTreeElement, path, pathIndex, callback));
208 };
209
210 InspectorTest._findChildPropertyTreeElement = function(parent, childName)
211 {
212     var children = parent.children;
213     for (var i = 0; i < children.length; i++) {
214         var treeElement = children[i];
215         var property = treeElement.property;
216         if (property.name === childName)
217             return treeElement;
218     }
219 };
220
221 };