Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: third_party/WebKit/LayoutTests/sensor/ambient-light-sensor.html

Issue 2481013004: [sensors] Reject test promise when callback throws an error (Closed)
Patch Set: Rebased to master Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <script src="../resources/testharness.js"></script> 2 <script src="../resources/testharness.js"></script>
3 <script src="../resources/testharnessreport.js"></script> 3 <script src="../resources/testharnessreport.js"></script>
4 <script src="../resources/mojo-helpers.js"></script> 4 <script src="../resources/mojo-helpers.js"></script>
5 <script src="resources/sensor-helpers.js"></script> 5 <script src="resources/sensor-helpers.js"></script>
6 <script> 6 <script>
7 7
8 'use strict'; 8 'use strict';
9 9
10 if (!window.testRunner) 10 if (!window.testRunner)
11 debug('This test cannot be run without the TestRunner'); 11 debug('This test cannot be run without the TestRunner');
12 12
13 const kDefaultReadingValue = 3.1415; 13 const kDefaultReadingValue = 3.1415;
14 14
15 function update_sensor_reading(buffer) { 15 function update_sensor_reading(buffer) {
16 buffer[1] = window.performance.now(); 16 buffer[1] = window.performance.now();
17 buffer[2] = kDefaultReadingValue; 17 buffer[2] = kDefaultReadingValue;
18 } 18 }
19 19
20 test(() => assert_throws( 20 test(() => assert_throws(
21 new RangeError(), 21 new RangeError(),
22 () => new AmbientLightSensor({frequency: -60})), 22 () => new AmbientLightSensor({frequency: -60})),
23 'Test that negative frequency causes exception from constructor.'); 23 'Test that negative frequency causes exception from constructor.');
24 24
25 sensor_test(sensor => { 25 sensor_test(sensor => {
26 sensor.mockSensorProvider.setGetSensorShouldFail(true); 26 sensor.mockSensorProvider.setGetSensorShouldFail(true);
27 let ambientLightSensor = new AmbientLightSensor(); 27 let ambientLightSensor = new AmbientLightSensor();
28 ambientLightSensor.start(); 28 ambientLightSensor.start();
29 return new Promise((resolve, reject) => { 29 return new Promise((resolve, reject) => {
30 ambientLightSensor.onstatechange = event => { 30 ambientLightSensor.onstatechange = () => {
31 if(ambientLightSensor.state == 'errored') { 31 if (ambientLightSensor.state == 'errored') {
32 resolve(); 32 resolve();
33 } 33 }
34 }; 34 };
35 }); 35 });
36 }, 'Test that sensor state changes to "errored" when sensor is not supported.'); 36 }, 'Test that sensor state changes to "errored" when sensor is not supported.');
37 37
38 sensor_test(sensor => { 38 sensor_test(sensor => {
39 sensor.mockSensorProvider.setGetSensorShouldFail(true); 39 sensor.mockSensorProvider.setGetSensorShouldFail(true);
40 let ambientLightSensor = new AmbientLightSensor(); 40 let ambientLightSensor = new AmbientLightSensor();
41 ambientLightSensor.start(); 41 ambientLightSensor.start();
42 return new Promise((resolve, reject) => { 42 return new Promise((resolve, reject) => {
43 ambientLightSensor.onerror = event => { 43 let wrapper = new CallbackWrapper(event => {
44 assert_equals(ambientLightSensor.state, 'errored'); 44 assert_equals(ambientLightSensor.state, 'errored');
45 console.log(event.error.message);
46 assert_equals(event.error.name, 'NotFoundError'); 45 assert_equals(event.error.name, 'NotFoundError');
47 ambientLightSensor.onerror = null; 46 ambientLightSensor.onerror = null;
48 resolve(); 47 resolve();
49 }; 48 }, reject);
50 49
50 ambientLightSensor.onerror = wrapper.callback;
51 }); 51 });
52 }, 'Test that "onerror" is send when sensor is not supported.'); 52 }, 'Test that "onerror" is send when sensor is not supported.');
53 53
54
55 sensor_test(sensor => { 54 sensor_test(sensor => {
56 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); 55 let ambientLightSensor = new AmbientLightSensor({frequency: 560});
57 ambientLightSensor.start(); 56 ambientLightSensor.start();
58 57
59 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 58 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
60 .then(mockSensor => { 59 .then(mockSensor => {
61 mockSensor.setStartShouldFail(true); 60 mockSensor.setStartShouldFail(true);
62 return mockSensor.addConfigurationCalled(); }) 61 return mockSensor.addConfigurationCalled(); })
63 .then(mockSensor => { 62 .then(mockSensor => {
64 return new Promise((resolve, reject) => { 63 return new Promise((resolve, reject) => {
65 ambientLightSensor.onerror = event => { 64 let wrapper = new CallbackWrapper(event => {
66 assert_equals(ambientLightSensor.state, 'errored'); 65 assert_equals(ambientLightSensor.state, 'errored');
67 assert_equals(event.error.name, 'OperationError'); 66 assert_equals(event.error.name, 'OperationError');
68 ambientLightSensor.onerror = null; 67 ambientLightSensor.onerror = null;
69 resolve(); 68 resolve();
70 }; 69 }, reject);
70
71 ambientLightSensor.onerror = wrapper.callback;
71 }); 72 });
72 }); 73 });
73 return testPromise; 74 return testPromise;
74 }, 'Test that "onerror" is send when start() call has failed.'); 75 }, 'Test that "onerror" is send when start() call has failed.');
75 76
76 sensor_test(sensor => { 77 sensor_test(sensor => {
77 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); 78 let ambientLightSensor = new AmbientLightSensor({frequency: 560});
78 ambientLightSensor.start(); 79 ambientLightSensor.start();
79 80
80 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 81 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
81 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) 82 .then(mockSensor => { return mockSensor.addConfigurationCalled(); })
82 .then(mockSensor => { 83 .then(mockSensor => {
83 return new Promise((resolve, reject) => { 84 return new Promise((resolve, reject) => {
84 ambientLightSensor.onstatechange = event => { 85 let wrapper = new CallbackWrapper(() => {
85 if (ambientLightSensor.state === 'idle') { 86 if (ambientLightSensor.state == 'idle') {
86 resolve(mockSensor); 87 resolve(mockSensor);
87 } 88 }
88 89
89 if (ambientLightSensor.state === 'active') { 90 if (ambientLightSensor.state == 'active') {
90 let configuration = mockSensor.active_sensor_configurations_[0]; 91 let configuration = mockSensor.active_sensor_configurations_[0];
91 assert_equals(configuration.frequency, 60); 92 assert_equals(configuration.frequency, 60);
92 ambientLightSensor.stop(); 93 ambientLightSensor.stop();
93 } 94 }
94 }; 95 }, reject);
96
97 ambientLightSensor.onstatechange = wrapper.callback;
98 ambientLightSensor.onerror = reject;
95 }); 99 });
96 }) 100 })
97 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 101 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
98 102
99 return testPromise; 103 return testPromise;
100 }, 'Test that frequency is capped to 60.0 Hz.'); 104 }, 'Test that frequency is capped to 60.0 Hz.');
101 105
102 sensor_test(sensor => { 106 sensor_test(sensor => {
103 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 107 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
104 ambientLightSensor.start(); 108 ambientLightSensor.start();
105 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 109 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
106 .then((mockSensor) => { 110 .then((mockSensor) => {
107 return new Promise((resolve, reject) => { 111 return new Promise((resolve, reject) => {
108 ambientLightSensor.onstatechange = event => { 112 ambientLightSensor.onstatechange = event => {
109 if (ambientLightSensor.state === 'idle') { 113 if (ambientLightSensor.state == 'idle') {
110 resolve(mockSensor); 114 resolve(mockSensor);
111 } 115 }
112 116
113 if (ambientLightSensor.state === 'active') { 117 if (ambientLightSensor.state == 'active') {
114 ambientLightSensor.stop(); 118 ambientLightSensor.stop();
115 } 119 }
116 }; 120 };
117 ambientLightSensor.onerror = reject; 121 ambientLightSensor.onerror = reject;
118 }); 122 });
119 }) 123 })
120 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 124 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
121 125
122 return testPromise; 126 return testPromise;
123 }, 'Test that sensor can be successfully created if sensor is supported.'); 127 }, 'Test that sensor can be successfully created if sensor is supported.');
124 128
125 sensor_test(sensor => { 129 sensor_test(sensor => {
126 let ambientLightSensor = new AmbientLightSensor(); 130 let ambientLightSensor = new AmbientLightSensor();
127 ambientLightSensor.start(); 131 ambientLightSensor.start();
128 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 132 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
129 .then((mockSensor) => { 133 .then((mockSensor) => {
130 return new Promise((resolve, reject) => { 134 return new Promise((resolve, reject) => {
131 ambientLightSensor.onstatechange = event => { 135 ambientLightSensor.onstatechange = event => {
132 if (ambientLightSensor.state === 'idle') { 136 if (ambientLightSensor.state == 'idle') {
133 resolve(mockSensor); 137 resolve(mockSensor);
134 } 138 }
135 139
136 if (ambientLightSensor.state === 'active') { 140 if (ambientLightSensor.state == 'active') {
137 ambientLightSensor.stop(); 141 ambientLightSensor.stop();
138 } 142 }
139 }; 143 };
140 144
141 ambientLightSensor.onerror = reject; 145 ambientLightSensor.onerror = reject;
142 }); 146 });
143 }) 147 })
144 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 148 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
145 149
146 return testPromise; 150 return testPromise;
147 }, 'Test that sensor can be constructed with default configuration.'); 151 }, 'Test that sensor can be constructed with default configuration.');
148 152
149 sensor_test(sensor => { 153 sensor_test(sensor => {
150 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 154 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
151 ambientLightSensor.start(); 155 ambientLightSensor.start();
152 156
153 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 157 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
154 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) 158 .then(mockSensor => { return mockSensor.addConfigurationCalled(); })
155 .then(mockSensor => { 159 .then(mockSensor => {
156 return new Promise((resolve, reject) => { 160 return new Promise((resolve, reject) => {
157 ambientLightSensor.onstatechange = event => { 161 ambientLightSensor.onstatechange = event => {
158 if (ambientLightSensor.state === 'idle') { 162 if (ambientLightSensor.state == 'idle') {
159 resolve(mockSensor); 163 resolve(mockSensor);
160 } 164 }
161 165
162 if (ambientLightSensor.state === 'active') { 166 if (ambientLightSensor.state == 'active') {
163 ambientLightSensor.stop(); 167 ambientLightSensor.stop();
164 } 168 }
165 }; 169 };
170
171 ambientLightSensor.onerror = reject;
166 }); 172 });
167 }) 173 })
168 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 174 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
169 175
170 return testPromise; 176 return testPromise;
171 }, 'Test that addConfiguration and removeConfiguration is called.'); 177 }, 'Test that addConfiguration and removeConfiguration is called.');
172 178
173 sensor_test(sensor => { 179 sensor_test(sensor => {
174 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 180 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
175 ambientLightSensor.start(); 181 ambientLightSensor.start();
176 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 182 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
177 .then(mockSensor => { 183 .then(mockSensor => {
178 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 184 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
179 }) 185 })
180 .then((mockSensor) => { 186 .then((mockSensor) => {
181 return new Promise((resolve, reject) => { 187 return new Promise((resolve, reject) => {
182 ambientLightSensor.onstatechange = event => { 188 let wrapper = new CallbackWrapper(() => {
183 if (ambientLightSensor.state === 'idle') { 189 assert_equals(ambientLightSensor.reading.illuminance,
190 kDefaultReadingValue);
191 ambientLightSensor.stop();
192 }, reject);
193
194 ambientLightSensor.onstatechange = () => {
195 if (ambientLightSensor.state == 'idle') {
184 resolve(mockSensor); 196 resolve(mockSensor);
185 } 197 }
186 }; 198 };
187 199
188 ambientLightSensor.onchange = () => { 200 ambientLightSensor.onchange = wrapper.callback;
189 assert_equals(ambientLightSensor.reading.illuminance, kDefaultReadin gValue);
190 ambientLightSensor.stop();
191 };
192
193 ambientLightSensor.onerror = reject; 201 ambientLightSensor.onerror = reject;
194 }); 202 });
195 }) 203 })
196 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 204 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
197 205
198 return testPromise; 206 return testPromise;
199 }, 'Test that onChange is called and sensor reading is valid.'); 207 }, 'Test that onChange is called and sensor reading is valid.');
200 208
201 sensor_test(sensor => { 209 sensor_test(sensor => {
202 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 210 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
203 ambientLightSensor.start(); 211 ambientLightSensor.start();
204 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 212 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
205 .then(mockSensor => { 213 .then(mockSensor => {
206 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 214 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
207 }) 215 })
208 .then((mockSensor) => { 216 .then((mockSensor) => {
209 return new Promise((resolve, reject) => { 217 return new Promise((resolve, reject) => {
210 ambientLightSensor.onstatechange = () => { 218 ambientLightSensor.onstatechange = new CallbackWrapper(() => {
211 if (ambientLightSensor.state === 'idle') { 219 if (ambientLightSensor.state == 'idle') {
212 assert_equals(ambientLightSensor.reading, null); 220 assert_equals(ambientLightSensor.reading, null);
213 resolve(mockSensor); 221 resolve(mockSensor);
214 } 222 }
215 } 223 }, reject).callback;
216 224
217 ambientLightSensor.onchange = () => { 225 ambientLightSensor.onchange = new CallbackWrapper(() => {
218 assert_equals(ambientLightSensor.reading.illuminance, kDefaultReadin gValue); 226 assert_equals(ambientLightSensor.reading.illuminance,
227 kDefaultReadingValue);
219 ambientLightSensor.stop(); 228 ambientLightSensor.stop();
220 } 229 }, reject).callback;
230
221 ambientLightSensor.onerror = reject; 231 ambientLightSensor.onerror = reject;
222 }); 232 });
223 }) 233 })
224 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 234 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
225 235
226 return testPromise; 236 return testPromise;
227 }, 'Test that sensor reading is not updated when sensor is stopped.'); 237 }, 'Test that sensor reading is not updated when sensor is stopped.');
228 238
229 sensor_test(sensor => { 239 sensor_test(sensor => {
230 let ambientLightSensor = new AmbientLightSensor(); 240 let ambientLightSensor = new AmbientLightSensor();
231 ambientLightSensor.start(); 241 ambientLightSensor.start();
232 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 242 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
233 .then(mockSensor => { 243 .then(mockSensor => {
234 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 244 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
235 }) 245 })
236 .then((mockSensor) => { 246 .then((mockSensor) => {
237 return new Promise((resolve, reject) => { 247 return new Promise((resolve, reject) => {
238 ambientLightSensor.onchange = () => { 248 ambientLightSensor.onchange = () => {
239 if (ambientLightSensor.reading.illuminance == kDefaultReadingValue) { 249 if (ambientLightSensor.reading.illuminance
250 == kDefaultReadingValue) {
240 resolve(mockSensor); 251 resolve(mockSensor);
241 } 252 }
242 } 253 }
243 ambientLightSensor.onerror = reject; 254 ambientLightSensor.onerror = reject;
244 }); 255 });
245 }) 256 })
246 .then((mockSensor) => { 257 .then((mockSensor) => {
247 testRunner.setPageVisibility("hidden"); 258 testRunner.setPageVisibility("hidden");
248 return mockSensor.suspendCalled(); 259 return mockSensor.suspendCalled();
249 }) 260 })
250 .then((mockSensor) => { 261 .then((mockSensor) => {
251 testRunner.setPageVisibility("visible"); 262 testRunner.setPageVisibility("visible");
252 return mockSensor.resumeCalled(); 263 return mockSensor.resumeCalled();
253 }) 264 })
254 .then((mockSensor) => { 265 .then((mockSensor) => {
255 return new Promise((resolve, reject) => { 266 return new Promise((resolve, reject) => {
256 ambientLightSensor.onstatechange = () => { 267 ambientLightSensor.onstatechange = () => {
257 if (ambientLightSensor.state === 'idle') { 268 if (ambientLightSensor.state == 'idle') {
258 resolve(mockSensor); 269 resolve(mockSensor);
259 } 270 }
260 } 271 }
261 ambientLightSensor.stop(); 272 ambientLightSensor.stop();
262 ambientLightSensor.onerror = reject; 273 ambientLightSensor.onerror = reject;
263 }); 274 });
264 }) 275 })
265 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 276 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
266 277
267 return testPromise; 278 return testPromise;
268 }, 'Test that sensor receives suspend / resume notifications when page' 279 }, 'Test that sensor receives suspend / resume notifications when page'
269 +' visibility changes.'); 280 +' visibility changes.');
270 281
271 282
272 sensor_test(sensor => { 283 sensor_test(sensor => {
273 let sensor1 = new AmbientLightSensor({frequency: 60}); 284 let sensor1 = new AmbientLightSensor({frequency: 60});
274 sensor1.start(); 285 sensor1.start();
275 286
276 let sensor2 = new AmbientLightSensor({frequency: 20}); 287 let sensor2 = new AmbientLightSensor({frequency: 20});
277 sensor2.start(); 288 sensor2.start();
278 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 289 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
279 .then(mockSensor => { 290 .then(mockSensor => {
280 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 291 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
281 }) 292 })
282 .then((mockSensor) => { 293 .then((mockSensor) => {
283 return new Promise((resolve, reject) => { 294 return new Promise((resolve, reject) => {
284 sensor1.onstatechange = event => { 295 sensor1.onstatechange = event => {
285 if (sensor1.state === 'idle') { 296 if (sensor1.state == 'idle') {
286 resolve(mockSensor); 297 resolve(mockSensor);
287 } 298 }
288 }; 299 };
289 300
290 sensor1.onchange = () => { 301 let wrapper = new CallbackWrapper(() => {
291 // Reading value is correct. 302 // Reading value is correct.
292 assert_equals(sensor1.reading.illuminance, kDefaultReadingValue); 303 assert_equals(sensor1.reading.illuminance, kDefaultReadingValue);
293 304
294 // Both sensors share the same reading instance. 305 // Both sensors share the same reading instance.
295 let reading = sensor1.reading; 306 let reading = sensor1.reading;
296 assert_equals(reading, sensor2.reading); 307 assert_equals(reading, sensor2.reading);
297 308
298 // After first sensor stops its reading is null, reading for second 309 // After first sensor stops its reading is null, reading for second
299 // sensor sensor remains. 310 // sensor sensor remains.
300 sensor1.stop(); 311 sensor1.stop();
301 assert_equals(sensor1.reading, null); 312 assert_equals(sensor1.reading, null);
302 assert_equals(sensor2.reading.illuminance, kDefaultReadingValue); 313 assert_equals(sensor2.reading.illuminance, kDefaultReadingValue);
303 314
304 sensor2.stop(); 315 sensor2.stop();
305 assert_equals(sensor2.reading, null); 316 assert_equals(sensor2.reading, null);
306 317
307 // Cached reading remains. 318 // Cached reading remains.
308 assert_equals(reading.illuminance, kDefaultReadingValue); 319 assert_equals(reading.illuminance, kDefaultReadingValue);
309 }; 320 }, reject);
310 321
322 sensor1.onchange = wrapper.callback;
311 sensor1.onerror = reject; 323 sensor1.onerror = reject;
312 sensor2.onerror = reject; 324 sensor2.onerror = reject;
313 }); 325 });
314 }) 326 })
315 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 327 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
316 328
317 return testPromise; 329 return testPromise;
318 }, 'Test that sensor reading is correct.'); 330 }, 'Test that sensor reading is correct.');
319 331
320 </script> 332 </script>
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/sensor/resources/sensor-helpers.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698