Web Inspector: adopt Object.awaitEvent in LayoutTests/inspector/model
[WebKit-https.git] / LayoutTests / inspector / model / scope-chain-node.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script src="resources/all-scopes.js"></script>
6 <script src="resources/function-name-scopes.js"></script>
7 <script src="resources/block-scopes.js"></script>
8 <script>
9 function test()
10 {
11     function firstLine(str) {
12         let newlineIndex = str.indexOf("\n");
13         if (newlineIndex !== -1)
14             return str.substr(0, newlineIndex);
15         return str;
16     }
17
18     function scopeTypeToString(type) {
19         for (let key in WebInspector.ScopeChainNode.Type) {
20             if (WebInspector.ScopeChainNode.Type[key] === type)
21                 return key;
22         }
23         return "Unexpected Scope Type";
24     }
25
26     function harvestScopeChain(scopeChain) {
27         let promises = [];
28         for (let scope of scopeChain) {
29             promises.push(new Promise((resolve, reject) => {
30                 if (scope.type === WebInspector.ScopeChainNode.Type.Global)
31                     resolve({scope, propertyDescriptors: []});
32                 else {
33                     scope.objects[0].getDisplayablePropertyDescriptors((propertyDescriptors) => {
34                         resolve({scope, propertyDescriptors});
35                     });
36                 }
37             }));
38         }
39         return Promise.all(promises);
40     }
41
42     function logScopeChain(scopeChain) {
43         return harvestScopeChain(scopeChain)
44         .then((results) => {
45             InspectorTest.log("SCOPE CHAIN:");
46             for (let {scope, propertyDescriptors} of results) {
47                 InspectorTest.log(`    ${scopeTypeToString(scope.type)}`);
48                 if (scope.type !== WebInspector.ScopeChainNode.Type.Global) {
49                     for (let descriptor of propertyDescriptors)
50                         InspectorTest.log(`      - ${descriptor.name}: ${firstLine(descriptor.value.description)}`);
51                 }
52             }
53             return results;
54         });
55     }
56
57
58     let suite = InspectorTest.createAsyncSuite("WebInspector.ScopeChainNode");
59
60     suite.addTestCase({
61         name: "WebInspector.ScopeChainNode.AllTypes",
62         description: "Tests for each of the different scope chain node types.",
63         test(resolve, reject) {
64             WebInspector.debuggerManager.awaitEvent(WebInspector.DebuggerManager.Event.CallFramesDidChange)
65             .then((event) => {
66                 let scopeChain = WebInspector.debuggerManager.activeCallFrame.scopeChain;
67                 InspectorTest.expectThat(scopeChain.length === 13, "ScopeChain should have 13 scopes.");
68                 return logScopeChain(scopeChain);
69             })
70             .then((result) => {
71                 let promise = WebInspector.debuggerManager.awaitEvent(WebInspector.DebuggerManager.Event.Resumed);
72                 WebInspector.debuggerManager.resume();
73                 return promise;
74             })
75             .then(resolve, reject);
76
77             InspectorTest.evaluateInPage("setTimeout(testAllScopes)");
78         }
79     });
80
81     suite.addTestCase({
82         name: "WebInspector.ScopeChainNode.FunctionNameInFunctionExpression",
83         description: "Tests that there should be a FunctionName scope inside a function expression.",
84         test(resolve, reject) {
85             WebInspector.debuggerManager.awaitEvent(WebInspector.DebuggerManager.Event.CallFramesDidChange)
86             .then((event) => {
87                 let scopeChain = WebInspector.debuggerManager.activeCallFrame.scopeChain;
88                 return logScopeChain(scopeChain);
89             })
90             .then((result) => {
91                 let promise = WebInspector.debuggerManager.awaitEvent(WebInspector.DebuggerManager.Event.Resumed);
92                 WebInspector.debuggerManager.resume();
93                 return promise;
94             })
95             .then(resolve, reject);
96
97             InspectorTest.evaluateInPage("setTimeout(testFunctionNameScope1)");
98         }
99     });
100
101     suite.addTestCase({
102         name: "WebInspector.ScopeChainNode.FunctionNameInClassMethod",
103         description: "Tests that there should be a FunctionName scope inside a class method.",
104         test(resolve, reject) {
105             WebInspector.debuggerManager.awaitEvent(WebInspector.DebuggerManager.Event.CallFramesDidChange)
106             .then((event) => {
107                 let scopeChain = WebInspector.debuggerManager.activeCallFrame.scopeChain;
108                 return logScopeChain(scopeChain);
109             })
110             .then((result) => {
111                 let promise = WebInspector.debuggerManager.awaitEvent(WebInspector.DebuggerManager.Event.Resumed);
112                 WebInspector.debuggerManager.resume();
113                 return promise;
114             })
115             .then(resolve, reject);
116
117             InspectorTest.evaluateInPage("setTimeout(testFunctionNameScope2)");
118         }
119     });
120
121     suite.addTestCase({
122         name: "WebInspector.ScopeChainNode.BlockScopes",
123         description: "Tests for a Block scope inside all the different types of blocks.",
124         test(resolve, reject) {
125             let pauseCount = 0;
126             let pauseEventsExpected = 19;
127             function callFramesDidChangeListener(event) {
128                 if (!WebInspector.debuggerManager.activeCallFrame)
129                     return;
130
131                 pauseCount++;
132                 let scopeChain = WebInspector.debuggerManager.activeCallFrame.scopeChain;
133
134                 // First, normal function scope.
135                 if (pauseCount === 1) {
136                     InspectorTest.expectThat(scopeChain[0].type === WebInspector.ScopeChainNode.Type.Closure, "Pause #1 - Top scope should be normal function Closure/Local scope.");
137                     WebInspector.debuggerManager.resume();
138                     return;
139                 }
140
141                 // Last, normal function scope.
142                 if (pauseCount === pauseEventsExpected) {
143                     InspectorTest.expectThat(scopeChain[0].type === WebInspector.ScopeChainNode.Type.Closure, `Pause #${pauseCount} - Top scope should be normal function Closure/Local scope.`);
144                     WebInspector.debuggerManager.removeEventListener(WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange, callFramesDidChangeListener);
145                     WebInspector.debuggerManager.resume();
146                     resolve();
147                     return;
148                 }
149
150                 // The first 15 cases the top scope should be a Block scope.
151                 if (pauseCount <= 15) {
152                     InspectorTest.expectThat(scopeChain[0].type === WebInspector.ScopeChainNode.Type.Block, `Pause #${pauseCount} - Top scope should be Block scope.`);
153                     WebInspector.debuggerManager.resume();
154                     return;
155                 }
156
157                 // Print out the full scope of the last few where there are nested or buried block scopes.
158                 InspectorTest.expectThat(scopeChain.some((scopeChain) => scopeChain.type === WebInspector.ScopeChainNode.Type.Block), `Pause #${pauseCount} - Contains a Block scope.`);
159
160                 logScopeChain(scopeChain)
161                 .then((result) => {
162                     WebInspector.debuggerManager.resume();
163                 });
164             }
165
166             WebInspector.debuggerManager.addEventListener(WebInspector.DebuggerManager.Event.ActiveCallFrameDidChange, callFramesDidChangeListener);
167             InspectorTest.evaluateInPage("setTimeout(testBlockScopes)");
168         }
169     });
170
171     suite.runTestCasesAndFinish();
172 }
173 </script>
174 </head>
175 <body onload="runTest()">
176 <p>Tests for the WebInspector.ScopeChainNode model object.</p>
177 </body>
178 </html>