4ca23f3254a8eb2746b840144ddd301817165d05
[WebKit-https.git] / LayoutTests / inspector / controller / runtime-controller.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 let resultNumber = 100;
7 function asyncOperation() {
8     return new Promise((resolve, reject) => {
9         setTimeout(() => {
10             resolve(resultNumber++);
11         }, 50);
12     });
13 }
14
15 function rejectedEventually() {
16     return new Promise((resolve, reject) => {
17         setTimeout(() => {
18             reject("Rejection");
19         }, 50);
20     });
21 }
22
23 function causeExceptionImmediately() {
24     throw "Thrown exception";
25 }
26
27 function test()
28 {
29     let suite = InspectorTest.createAsyncSuite("RuntimeManager");
30
31     suite.addTestCase({
32         name: "RuntimeManager.prototype.evaluateInInspectedWindow.ObjectLiteralConvenience",
33         description: "Test evaluating an object literal string conveniently converts wraps it in parenthesis to avoid misinterpretation as a program with a block and labeled statement.",
34         test(resolve, reject) {
35             function testSource(expression, callback) {
36                 WebInspector.runtimeManager.evaluateInInspectedWindow(expression, {objectGroup: "test"}, (result, wasThrown) => {
37                     InspectorTest.log("Source: " + expression);
38                     callback(result, wasThrown);
39                 });
40             }
41
42             function expectObject(result, wasThrown) {
43                 InspectorTest.expectThat(result.type === "object", "Evaluation should produce an object.");
44             }
45
46             function expectException(result, wasThrown) {
47                 InspectorTest.expectThat(wasThrown, "Evaluation should produce an exception.");
48             }
49
50             function expectValue(value) {
51                 return function(result, wasThrown) {
52                     InspectorTest.expectThat(result.value === value, "Evaluation should produce the labeled statement's value.");
53                 }
54             }
55
56             // The convenience will detect these and make them objects.
57             testSource("{a:1}", expectObject);
58             testSource("{a:1, b:2}", expectObject);
59             testSource("   {a:1, b:2}  ", expectObject);
60
61             // The convenience will incorrectly apply to these that would otherwise have been valid programs.
62             testSource("{ let a = 1; a += 1; a }", expectException);
63
64             // Test we can run non-convenience cases by not starting / ending with characters other than curly braces.
65             testSource(";{a:1}", expectValue(1));
66             testSource("{a:1};", expectValue(1));
67             testSource(";{a:1, b:2}", expectException);
68             testSource(";{ let a = 1; a += 1; a }", expectValue(2));
69
70             InspectorBackend.runAfterPendingDispatches(resolve);
71         }
72     });
73
74     suite.addTestCase({
75         name: "RuntimeManager.prototype.evaluateInInspectedWindow.AwaitConvenience",
76         description: "Test evaluating a simple await expression wraps it into an async function and eventually resolves the value.",
77         test(resolve, reject) {
78             function testSource(expression, callback) {
79                 WebInspector.runtimeManager.evaluateInInspectedWindow(expression, {objectGroup: "test"}, (result, wasThrown) => {
80                     InspectorTest.log("Source: " + expression);
81                     callback(result, wasThrown);
82                 });
83             }
84
85             function expectUndefined(result, wasThrown) {
86                 InspectorTest.expectThat(result.isUndefined(), "Transformed. Should log the value or an exception.");
87             }
88
89             function expectException(result, wasThrown) {
90                 InspectorTest.expectThat(wasThrown, "Exception. Should not get transformed and produce a SyntaxError.");
91             }
92
93             // The convenience will detect these and make an async task.
94             const expected = 6;
95             testSource("await 1", expectUndefined);
96             testSource("   await 2   ", expectUndefined);
97             testSource("var x = await 3", expectUndefined);
98             testSource("await causeExceptionImmediately()", expectUndefined);
99             testSource("await Promise.resolve(4)", expectUndefined);
100             testSource("await Promise.reject('Promise.reject')", expectUndefined);
101             testSource("await rejectedEventually()", expectUndefined);
102             testSource("await asyncOperation()", expectUndefined);
103             testSource("x = await asyncOperation()", expectUndefined);
104
105             InspectorTest.log("");
106
107             // The convenience will not apply to these expressions.
108             testSource("return 10", expectException);
109             testSource("await 10; 1", expectException);
110             testSource("1; await 10;", expectException);
111             testSource("x = y = await 10", expectException);
112
113             // We can resolve after receiving the expected number of console.info messages.
114             let seen = 0;
115             let listener = WebInspector.logManager.addEventListener(WebInspector.LogManager.Event.MessageAdded, (event) => {
116                 let consoleMessage = event.data.message;
117                 if (consoleMessage.level !== WebInspector.ConsoleMessage.MessageLevel.Info)
118                     return;
119                 if (++seen !== expected)
120                     return;
121                 WebInspector.logManager.removeEventListener(WebInspector.LogManager.Event.MessageAdded, listener);
122                 resolve();
123             });
124         }
125     });
126
127     suite.runTestCasesAndFinish();
128 }
129 </script>
130 </head>
131 <body onload="runTest()">
132 <p>Tests for RuntimeManager operations.</p>
133 </body>
134 </html>