Unreviewed, rolling out r215476.
[WebKit-https.git] / LayoutTests / inspector / runtime / evaluate-CommandLineAPI.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 var varGlobalVariable = 1;
7 let letGlobalVariable = 2;
8 const constGlobalVariable = 3;
9
10 function test()
11 {
12     const objectGroup = "test";
13     const includeCommandLineAPI = true;
14     const returnByValue = true;
15
16     function testEvaluate(expression, callback) {
17         RuntimeAgent.evaluate.invoke({expression, objectGroup, includeCommandLineAPI, returnByValue}, (error, resultValue, wasThrown) => {
18             InspectorTest.assert(!error, "Should not be a protocol error.");
19             InspectorTest.assert(!wasThrown, "Should not be a runtime error.");
20             if (callback)
21                 callback(resultValue.value);
22         });
23     }
24
25     function testEvaluateThrow(expression, callback) {
26         RuntimeAgent.evaluate.invoke({expression, objectGroup, includeCommandLineAPI, returnByValue}, (error, resultValue, wasThrown) => {
27             InspectorTest.assert(!error, "Should not be a protocol error.");
28             InspectorTest.assert(wasThrown, "Should be a runtime error.");
29             if (callback)
30                 callback(wasThrown);
31         });
32     }
33
34     let suite = InspectorTest.createAsyncSuite("Runtime.evaluate.CommandLineAPI");
35
36     suite.addTestCase({
37         name: "AccessCommandLineAPI",
38         description: "Test evaluate can access CommandLineAPI methods.",
39         test(resolve, reject) {
40             testEvaluate("keys.toString()", (resultValue) => {
41                 InspectorTest.expectThat(resultValue.includes("[Command Line API]"), "CommandLineAPI `keys` can be accessed.");
42             });
43             testEvaluate("keys({a:1, b:2})", (resultValue) => {
44                 InspectorTest.expectThat(resultValue.length === 2 && resultValue[0] === "a" && resultValue[1] === "b", "CommandLineAPI `keys` should work with a simple object.");
45                 resolve();
46             });
47         }
48     });
49
50     suite.addTestCase({
51         name: "AccessGlobalVariable",
52         description: "Test evaluate can access global variables.",
53         test(resolve, reject) {
54             testEvaluate("varGlobalVariable", (resultValue) => {
55                 InspectorTest.expectThat(resultValue === 1, "Should be able to access var in global scope.");
56             });
57             testEvaluate("letGlobalVariable", (resultValue) => {
58                 InspectorTest.expectThat(resultValue === 2, "Should be able to access let in global scope.");
59             });
60             testEvaluate("constGlobalVariable", (resultValue) => {
61                 InspectorTest.expectThat(resultValue === 3, "Should be able to access const in global scope.");
62                 resolve();
63             });
64         }
65     });
66
67     suite.addTestCase({
68         name: "CreateGlobalVariable",
69         description: "Test evaluate can create global variables.",
70         test(resolve, reject) {
71             testEvaluate(`
72                 var createdVarGlobalVariable = 1;
73                 let createdLetGlobalVariable = 2;
74                 const createdConstGlobalVariable = 3;
75             `);
76             testEvaluate("createdVarGlobalVariable", (resultValue) => {
77                 InspectorTest.expectThat(resultValue === 1, "Should be able to access created var in global scope.");
78             });
79             testEvaluate("createdLetGlobalVariable", (resultValue) => {
80                 InspectorTest.expectThat(resultValue === 2, "Should be able to access created let in global scope.");
81             });
82             testEvaluate("createdConstGlobalVariable", (resultValue) => {
83                 InspectorTest.expectThat(resultValue === 3, "Should be able to access created const in global scope.");
84                 resolve();
85             });
86         }
87     });
88
89     suite.addTestCase({
90         name: "CreateGlobalClass",
91         description: "Test evaluate can create a class.",
92         test(resolve, reject) {
93             testEvaluate(`
94                 class Foo {
95                     static staticMethod() {
96                         return 42;
97                     }
98                 }
99             `);
100             testEvaluate("Foo.staticMethod()", (resultValue) => {
101                 InspectorTest.expectThat(resultValue === 42, "Should be able to access class created in earlier evaluation.");
102                 resolve();
103             });
104         }
105     });
106
107     suite.addTestCase({
108         name: "ExpectedExceptionCreatingDuplicateLexicalGlobalVariables",
109         description: "Test evaluate produces expected exception creating duplicate lexical global variables across evaluations.",
110         test(resolve, reject) {
111             testEvaluate(`let duplicateGlobalVariable = 1`);
112             testEvaluateThrow(`let duplicateGlobalVariable = 1`, (wasThrown) => {
113                 InspectorTest.expectThat(wasThrown, "Should be an exception defining a lexical global multiple times.");
114                 resolve();
115             });
116         }
117     });
118
119     suite.addTestCase({
120         name: "NonStrictAndStrictEvaluations",
121         description: "Test evaluate can run strict and non-strict programs.",
122         test(resolve, reject) {
123             testEvaluate(`
124                 // Not strict, this is okay.
125                 (function f() {
126                     return arguments.callee.name;
127                 })();
128             `, (resultValue) => {
129                 InspectorTest.expectThat(resultValue === "f", "Non-strict evaluation. Should be able to access arguments.callee.");
130             });
131             testEvaluateThrow(`"use strict";
132                 // Strict, this throw an exception.
133                 (function() {
134                     return arguments.callee;
135                 })();`
136             , (wasThrown) => {
137                 InspectorTest.expectThat(wasThrown, "Script evaluation. Should not be able to access arguments.callee.");
138                 resolve();
139             });
140         }
141     });
142
143     suite.addTestCase({
144         name: "CommandLineAPIDoesNotShadowVariables",
145         description: "Test CommandLineAPI does not shadow global variables.",
146         test(resolve, reject) {
147             testEvaluate("keys.toString()", (resultValue) => {
148                 InspectorTest.expectThat(resultValue.includes("[Command Line API]"), "`keys` is currently CommandLineAPI function.");
149             });
150             testEvaluate("var keys = 123; keys", (resultValue) => {
151                 InspectorTest.expectThat(resultValue === 123, "Creating global `keys` variable should be okay.");
152             });
153             testEvaluate("keys", (resultValue) => {
154                 InspectorTest.expectThat(resultValue === 123, "Global `keys` variable should not be shadowed by CommandLineAPI function.");
155                 resolve();
156             });
157         }
158     });
159
160     suite.addTestCase({
161         name: "CommandLineAPIDoesNotShadowGlobalObjectProperties",
162         description: "Test CommandLineAPI does not shadow global object properties.",
163         test(resolve, reject) {
164             testEvaluate("values.toString()", (resultValue) => {
165                 InspectorTest.expectThat(resultValue === "[object HTMLDivElement]", "`values` should be `window.values` and not shadowed by CommandLineAPI `values` function.");
166                 resolve();
167             });
168         }
169     });
170
171     suite.runTestCasesAndFinish();
172 }
173 </script>
174 </head>
175 <body onload="runTest()">
176 <div id="values"></div> <!-- This introduces the named property `window.values` on the window global object. -->
177 <p>Tests for the Runtime.evaluate with the Command Line API.</p>
178 </body>
179 </html>