Unreviewed, follow-up after r264210
[WebKit-https.git] / LayoutTests / inspector / console / command-line-api.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     // Each input is wrapped in the String constructor to force stringification.
9     let testInputs = [
10         {
11             title: "ArrayKeys",
12             input: "keys([3,4])",
13         },
14         {
15             title: "ArrayValues",
16             input: "values([3,4])",
17         },
18         {
19             title: "DollarZero",
20             input: "$0",
21             setup: selectElementForDollarZero,
22         },
23         {
24             title: "InvalidSelector",
25             input: "$('foo')",
26             setup: listenForConsoleWarning,
27         },
28         {
29             title: "IdSelector",
30             input: "$('#foo')",
31         },
32         {
33             title: "IdSelectorWithContext",
34             input: "$('#foo', document.body)",
35         },
36         {
37             title: "IdSelectorWithInvalidContext",
38             input: "$('#foo', 'non-node')",
39         },
40         {
41             title: "IdSelectorDynamicContext",
42             input: "$('#foo', $('#bar'))",
43         },
44         {
45             title: "ElementSelectorAll",
46             input: "$$('p')",
47         },
48         {
49             title: "ElementSelectorAllWithContext",
50             input: "$$('p', document.body)",
51         },
52     ];
53
54     let suite = InspectorTest.createAsyncSuite("Console.TestCommandLineAPI");
55
56     function addAPITestCase({title, input, setup}) {
57         suite.addTestCase({
58             name: `Evaluate${title}`,
59             setup,
60             test(resolve, reject) {
61                 InspectorTest.log(`Input: ${input}`);
62
63                 function callback(result, wasThrown) {
64                     if (result) {
65                         InspectorTest.log(`Output: ${result.description}`);
66                         resolve();
67                     } else
68                         reject();
69                 }
70
71                 WI.runtimeManager.evaluateInInspectedWindow(`String(${input})`, {objectGroup: "test", includeCommandLineAPI: true}, callback);
72             }
73         })
74     }
75
76     for (let input of testInputs)
77         addAPITestCase(input);
78
79     suite.runTestCasesAndFinish();
80
81     function listenForConsoleWarning(resolve, reject) {
82         // Detect console warning for $('foo').
83         WI.consoleManager.singleFireEventListener(WI.ConsoleManager.Event.MessageAdded, (event) => {
84             let message = event.data.message;
85             InspectorTest.assert(message instanceof WI.ConsoleMessage);
86             InspectorTest.log(`CONSOLE: ${message.messageText}`);
87         });
88
89         resolve();
90     }
91
92     function selectElementForDollarZero(resolve, reject) {
93         WI.domManager.requestDocument((documentNode) => {
94             if (!documentNode) {
95                 InspectorTest.log("FAIL: DOM node not found.");
96                 return reject();
97             }
98
99             // Select div#foo for $0.
100             documentNode.querySelector("#foo", (contentNodeId) => {
101                 if (!contentNodeId) {
102                     InspectorTest.log("FAIL: DOM node not found.");
103                     return reject();
104                 }
105
106                 DOMAgent.setInspectedNode(contentNodeId);
107                 resolve();
108             });
109         });
110     }
111 }
112 </script>
113 </head>
114 <body onload="runTest();">
115     <p id="foo">Tests that command line api works.</p>
116     <p id="bar"></p>
117 </body>
118 </html>