Web Inspector: add RemoteObject.fetchProperties and some basic tests for RemoteObject API
[WebKit-https.git] / LayoutTests / inspector / model / remote-object-api.html
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 window.testObject = {
7     1: true,
8     "name": "Favorites",
9     "size": 456,
10     "data": {x: 42, y: 50},
11 };
12
13 function test()
14 {
15     let object;
16     let suite = InspectorTest.createAsyncSuite("RemoteObject.API");
17
18     suite.addTestCase({
19         name: "RemoteObject.getProperty.SuccessWithCallback.String",
20         async test() {
21             let finished = new WI.WrappedPromise;
22             object.getProperty("name", (error, result, wasThrown) => {
23                 InspectorTest.assert(!error, "Should not have a protocol error.");
24
25                 InspectorTest.expectFalse(wasThrown, "Should not have a thrown exception.");
26                 InspectorTest.expectThat(result.hasValue(), "Fetched property should have a primitive value.");
27                 InspectorTest.expectEqual(result.value, "Favorites", "Fetched property value should be as expected.");
28                 error ? finished.reject(error) : finished.resolve();
29             });
30
31             await finished.promise;
32         }
33     });
34
35     suite.addTestCase({
36         name: "RemoteObject.getProperty.SuccessWithCallback.Number",
37         async test() {
38             let finished = new WI.WrappedPromise;
39             object.getProperty(1, (error, result, wasThrown) => {
40                 InspectorTest.assert(!error, "Should not have a protocol error.");
41
42                 InspectorTest.expectFalse(wasThrown, "Should not have a thrown exception.");
43                 InspectorTest.expectThat(result.hasValue(), "Fetched property should have a primitive value.");
44                 InspectorTest.expectEqual(result.value, true, "Fetched property value should be as expected.");
45                 error ? finished.reject(error) : finished.resolve();
46             });
47
48             await finished.promise;
49         }
50     });
51
52     suite.addTestCase({
53         name: "RemoteObject.getProperty.NotFoundWithCallback",
54         async test() {
55             let finished = new WI.WrappedPromise;
56             object.getProperty("doesNotExist", (error, result, wasThrown) => {
57                 InspectorTest.assert(!error, "Should not have a protocol error.");
58
59                 InspectorTest.expectFalse(wasThrown, "Should not have a thrown exception.");
60                 InspectorTest.expectThat(result.hasValue(), "Fetched property should have a primitive value.");
61                 InspectorTest.expectEqual(result.value, undefined, "Fetched property value should be as expected.");
62                 error ? finished.reject(error) : finished.resolve();
63             });
64
65             await finished.promise;
66         }
67     });
68
69     suite.addTestCase({
70         name: "RemoteObject.getProperty.FailureWithCallback",
71         async test() {
72             let finished = new WI.WrappedPromise;
73             object.getProperty({}, (error, result, wasThrown) => {
74                 InspectorTest.assert(!error, "Should not have a protocol error.");
75
76                 InspectorTest.expectThat(wasThrown, "Should have a thrown exception.");
77                 InspectorTest.expectFalse(result.hasValue(), "Fetched property should not have a primitive value.");
78                 InspectorTest.expectEqual(result.type, "object", "Fetched property value should have type `object`.");
79                 InspectorTest.expectEqual(result.subtype, "error", "Fetched property value should have subtype `error`.");
80
81                 error ? finished.reject(error) : finished.resolve();
82             });
83
84             await finished.promise;
85         }
86     });
87
88     suite.addTestCase({
89         name: "RemoteObject.getProperty.SuccessWithPromise.String",
90         async test() {
91             let result = await object.getProperty("name");
92             InspectorTest.expectThat(result.hasValue(), "Fetched property should have a primitive value.");
93             InspectorTest.expectEqual(result.value, "Favorites", "Fetched property value should be as expected.");
94         }
95     });
96
97     suite.addTestCase({
98         name: "RemoteObject.getProperty.SuccessWithPromise.Number",
99         async test() {
100             let result = await object.getProperty(1);
101             InspectorTest.expectThat(result.hasValue(), "Fetched property should have a primitive value.");
102             InspectorTest.expectEqual(result.value, true, "Fetched property value should be as expected.");
103         }
104     });
105
106     suite.addTestCase({
107         name: "RemoteObject.getProperty.FailureWithPromise",
108         async test() {
109             await InspectorTest.expectException(async () => {
110                 await object.getProperty({});
111             });
112         }
113     });
114
115     suite.addTestCase({
116         name: "RemoteObject.fetchProperties.Success",
117         async test() {
118             let keys = ["name", "size", "data"];
119             let result = await object.fetchProperties(keys);
120
121             InspectorTest.expectEqual(Object.keys(result).length, 3, "Result object should contain three keys.");
122             for (let key of keys)
123                 InspectorTest.expectThat(key in result, `Result object should contain fetched property '${key}'.`);
124         }
125     });
126
127     suite.addTestCase({
128         name: "RemoteObject.fetchProperties.SuccessWithDuplicateKey",
129         async test() {
130             let keys = ["name", "size", "data", "data"];
131             let result = await object.fetchProperties(keys);
132
133             InspectorTest.expectEqual(Object.keys(result).length, 3, "Result object should contain three keys.");
134             for (let key of new Set(keys))
135                 InspectorTest.expectThat(key in result, `Result object should contain fetched property '${key}'.`);
136         }
137     });
138
139     suite.addTestCase({
140         name: "RemoteObject.fetchProperties.SuccessWithOutputObject",
141         async test() {
142             let resultObject = {"existingKey": true};
143             let keys = ["name", "size", "data"];
144             let returnValue = await object.fetchProperties(keys, resultObject);
145             InspectorTest.expectEqual(returnValue, resultObject, "Resolved value should be the passed-in result object.");
146             for (let key of keys)
147                 InspectorTest.expectThat(key in resultObject, `Result object should contain fetched property '${key}'.`);
148
149             let {name, size, data} = returnValue;
150             InspectorTest.expectEqual(name, "Favorites", `Fetched property 'name' should equal 'Favorites'.`);
151             InspectorTest.expectEqual(size, 456, `Fetched property 'size' should equal '456'.`);
152             InspectorTest.expectThat(data instanceof WI.RemoteObject, `Fetched property 'data' should be a WI.RemoteObject.`);
153         }
154     });
155
156     suite.addTestCase({
157         name: "RemoteObject.fetchProperties.FailureWithInvalidKey",
158         async test() {
159             let keys = ["name", "size", {}];
160             await InspectorTest.expectException(async () => {
161                 await object.fetchProperties(keys);
162             });
163         }
164     });
165
166     InspectorTest.evaluateInPage(`window.testObject`).then((result) => {
167         object = result;
168         InspectorTest.assert(object instanceof WI.RemoteObject, "Should get a remote object.");
169
170         suite.runTestCasesAndFinish();
171     }).catch(reportUnhandledRejection);
172 }
173 </script>
174 </head>
175 <body onload="runTest()">
176     <p>Testing basic API and convenience methods of RemoteObject.</p>
177 </body>
178 </html>