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

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

Issue 2481363003: [Sensors] Implement Sensor.onactivate (Closed)
Patch Set: Rebased 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/webexposed/global-interface-listing-expected.txt » ('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 = () => {
31 if (ambientLightSensor.state == 'errored') {
32 resolve();
33 }
34 };
35 });
36 }, 'Test that sensor state changes to "errored" when sensor is not supported.');
37
38 sensor_test(sensor => {
39 sensor.mockSensorProvider.setGetSensorShouldFail(true);
40 let ambientLightSensor = new AmbientLightSensor();
41 ambientLightSensor.start();
42 return new Promise((resolve, reject) => {
43 let wrapper = new CallbackWrapper(event => { 30 let wrapper = new CallbackWrapper(event => {
44 assert_equals(ambientLightSensor.state, 'errored'); 31 assert_equals(ambientLightSensor.state, 'errored');
45 assert_equals(event.error.name, 'NotFoundError'); 32 assert_equals(event.error.name, 'NotFoundError');
46 ambientLightSensor.onerror = null; 33 ambientLightSensor.onerror = null;
47 resolve(); 34 resolve();
48 }, reject); 35 }, reject);
49 36
50 ambientLightSensor.onerror = wrapper.callback; 37 ambientLightSensor.onerror = wrapper.callback;
51 }); 38 });
52 }, 'Test that "onerror" is send when sensor is not supported.'); 39 }, 'Test that "onerror" is send when sensor is not supported.');
(...skipping 23 matching lines...) Expand all
76 63
77 sensor_test(sensor => { 64 sensor_test(sensor => {
78 let ambientLightSensor = new AmbientLightSensor({frequency: 560}); 65 let ambientLightSensor = new AmbientLightSensor({frequency: 560});
79 ambientLightSensor.start(); 66 ambientLightSensor.start();
80 67
81 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 68 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
82 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) 69 .then(mockSensor => { return mockSensor.addConfigurationCalled(); })
83 .then(mockSensor => { 70 .then(mockSensor => {
84 return new Promise((resolve, reject) => { 71 return new Promise((resolve, reject) => {
85 let wrapper = new CallbackWrapper(() => { 72 let wrapper = new CallbackWrapper(() => {
86 if (ambientLightSensor.state == 'idle') { 73 let configuration = mockSensor.active_sensor_configurations_[0];
87 resolve(mockSensor); 74 assert_equals(configuration.frequency, 60);
88 } 75 ambientLightSensor.stop();
89 76 assert_equals(ambientLightSensor.state, 'idle');
90 if (ambientLightSensor.state == 'active') { 77 resolve(mockSensor);
91 let configuration = mockSensor.active_sensor_configurations_[0];
92 assert_equals(configuration.frequency, 60);
93 ambientLightSensor.stop();
94 }
95 }, reject); 78 }, reject);
96 79
97 ambientLightSensor.onstatechange = wrapper.callback; 80 ambientLightSensor.onactivate = wrapper.callback;
98 ambientLightSensor.onerror = reject; 81 ambientLightSensor.onerror = reject;
99 }); 82 });
100 }) 83 })
101 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 84 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
102 85
103 return testPromise; 86 return testPromise;
104 }, 'Test that frequency is capped to 60.0 Hz.'); 87 }, 'Test that frequency is capped to 60.0 Hz.');
105 88
106 sensor_test(sensor => { 89 sensor_test(sensor => {
107 let maxSupportedFrequency = 15; 90 let maxSupportedFrequency = 15;
108 sensor.mockSensorProvider.setMaximumSupportedFrequency(maxSupportedFrequency); 91 sensor.mockSensorProvider.setMaximumSupportedFrequency(maxSupportedFrequency);
109 let ambientLightSensor = new AmbientLightSensor({frequency: 50}); 92 let ambientLightSensor = new AmbientLightSensor({frequency: 50});
110 ambientLightSensor.start(); 93 ambientLightSensor.start();
111 94
112 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 95 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
113 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) 96 .then(mockSensor => { return mockSensor.addConfigurationCalled(); })
114 .then(mockSensor => { 97 .then(mockSensor => {
115 return new Promise((resolve, reject) => { 98 return new Promise((resolve, reject) => {
116 ambientLightSensor.onstatechange = event => { 99 let wrapper = new CallbackWrapper(() => {
117 if (ambientLightSensor.state === 'idle') { 100 let configuration = mockSensor.active_sensor_configurations_[0];
118 resolve(mockSensor); 101 assert_equals(configuration.frequency, maxSupportedFrequency);
119 } 102 ambientLightSensor.stop();
103 assert_equals(ambientLightSensor.state, 'idle');
104 resolve(mockSensor);
105 }, reject);
120 106
121 if (ambientLightSensor.state === 'active') { 107 ambientLightSensor.onactivate = wrapper.callback;
122 let configuration = mockSensor.active_sensor_configurations_[0]; 108 ambientLightSensor.onerror = reject;
123 assert_equals(configuration.frequency, maxSupportedFrequency);
124 ambientLightSensor.stop();
125 }
126 };
127 }); 109 });
128 }) 110 })
129 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 111 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
130 112
131 return testPromise; 113 return testPromise;
132 }, 'Test that frequency is capped to the maximum supported from frequency.'); 114 }, 'Test that frequency is capped to the maximum supported from frequency.');
133 115
134 sensor_test(sensor => { 116 sensor_test(sensor => {
135 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 117 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
136 ambientLightSensor.start(); 118 ambientLightSensor.start();
137 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 119 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
138 .then((mockSensor) => { 120 .then((mockSensor) => {
139 return new Promise((resolve, reject) => { 121 return new Promise((resolve, reject) => {
140 ambientLightSensor.onstatechange = event => { 122 let wrapper = new CallbackWrapper(() => {
141 if (ambientLightSensor.state == 'idle') { 123 assert_equals(ambientLightSensor.state, 'active');
142 resolve(mockSensor); 124 ambientLightSensor.stop();
143 } 125 assert_equals(ambientLightSensor.state, 'idle');
126 resolve(mockSensor);
127 }, reject);
144 128
145 if (ambientLightSensor.state == 'active') { 129 ambientLightSensor.onactivate = wrapper.callback;
146 ambientLightSensor.stop();
147 }
148 };
149 ambientLightSensor.onerror = reject; 130 ambientLightSensor.onerror = reject;
150 }); 131 });
151 }) 132 })
152 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 133 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
153 134
154 return testPromise; 135 return testPromise;
155 }, 'Test that sensor can be successfully created if sensor is supported.'); 136 }, 'Test that sensor can be successfully created if sensor is supported.');
156 137
157 sensor_test(sensor => { 138 sensor_test(sensor => {
158 let ambientLightSensor = new AmbientLightSensor(); 139 let ambientLightSensor = new AmbientLightSensor();
159 ambientLightSensor.start(); 140 ambientLightSensor.start();
160 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 141 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
161 .then((mockSensor) => { 142 .then((mockSensor) => {
162 return new Promise((resolve, reject) => { 143 return new Promise((resolve, reject) => {
163 ambientLightSensor.onstatechange = event => { 144 let wrapper = new CallbackWrapper(() => {
164 if (ambientLightSensor.state == 'idle') { 145 assert_equals(ambientLightSensor.state, 'active');
165 resolve(mockSensor); 146 ambientLightSensor.stop();
166 } 147 assert_equals(ambientLightSensor.state, 'idle');
148 resolve(mockSensor);
149 }, reject);
167 150
168 if (ambientLightSensor.state == 'active') { 151 ambientLightSensor.onactivate = wrapper.callback;
169 ambientLightSensor.stop();
170 }
171 };
172
173 ambientLightSensor.onerror = reject; 152 ambientLightSensor.onerror = reject;
174 }); 153 });
175 }) 154 })
176 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 155 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
177 156
178 return testPromise; 157 return testPromise;
179 }, 'Test that sensor can be constructed with default configuration.'); 158 }, 'Test that sensor can be constructed with default configuration.');
180 159
181 sensor_test(sensor => { 160 sensor_test(sensor => {
182 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 161 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
183 ambientLightSensor.start(); 162 ambientLightSensor.start();
184 163
185 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 164 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
186 .then(mockSensor => { return mockSensor.addConfigurationCalled(); }) 165 .then(mockSensor => { return mockSensor.addConfigurationCalled(); })
187 .then(mockSensor => { 166 .then(mockSensor => {
188 return new Promise((resolve, reject) => { 167 return new Promise((resolve, reject) => {
189 ambientLightSensor.onstatechange = event => { 168 let wrapper = new CallbackWrapper(() => {
190 if (ambientLightSensor.state == 'idle') { 169 assert_equals(ambientLightSensor.state, 'active');
191 resolve(mockSensor); 170 ambientLightSensor.stop();
192 } 171 assert_equals(ambientLightSensor.state, 'idle');
172 resolve(mockSensor);
173 }, reject);
193 174
194 if (ambientLightSensor.state == 'active') { 175 ambientLightSensor.onactivate = wrapper.callback;
195 ambientLightSensor.stop(); 176 ambientLightSensor.onerror = reject;
196 }
197 };
198
199 ambientLightSensor.onerror = reject;
200 }); 177 });
201 }) 178 })
202 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 179 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
203 180
204 return testPromise; 181 return testPromise;
205 }, 'Test that addConfiguration and removeConfiguration is called.'); 182 }, 'Test that addConfiguration and removeConfiguration is called.');
206 183
207 sensor_test(sensor => { 184 sensor_test(sensor => {
208 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 185 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
209 ambientLightSensor.start(); 186 ambientLightSensor.start();
210 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 187 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
211 .then(mockSensor => { 188 .then(mockSensor => {
212 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 189 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
213 }) 190 })
214 .then((mockSensor) => { 191 .then((mockSensor) => {
215 return new Promise((resolve, reject) => { 192 return new Promise((resolve, reject) => {
216 let wrapper = new CallbackWrapper(() => { 193 let wrapper = new CallbackWrapper(() => {
217 assert_equals(ambientLightSensor.reading.illuminance, 194 assert_equals(ambientLightSensor.reading.illuminance,
218 kDefaultReadingValue); 195 kDefaultReadingValue);
219 ambientLightSensor.stop(); 196 ambientLightSensor.stop();
197 assert_equals(ambientLightSensor.reading, null);
198 resolve(mockSensor);
220 }, reject); 199 }, reject);
221 200
222 ambientLightSensor.onstatechange = () => {
223 if (ambientLightSensor.state == 'idle') {
224 resolve(mockSensor);
225 }
226 };
227
228 ambientLightSensor.onchange = wrapper.callback; 201 ambientLightSensor.onchange = wrapper.callback;
229 ambientLightSensor.onerror = reject; 202 ambientLightSensor.onerror = reject;
230 }); 203 });
231 }) 204 })
232 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 205 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
233 206
234 return testPromise; 207 return testPromise;
235 }, 'Test that onChange is called and sensor reading is valid.'); 208 }, 'Test that onChange is called and sensor reading is valid.');
236 209
237 sensor_test(sensor => { 210 sensor_test(sensor => {
238 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
239 ambientLightSensor.start();
240 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
241 .then(mockSensor => {
242 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
243 })
244 .then((mockSensor) => {
245 return new Promise((resolve, reject) => {
246 ambientLightSensor.onstatechange = new CallbackWrapper(() => {
247 if (ambientLightSensor.state == 'idle') {
248 assert_equals(ambientLightSensor.reading, null);
249 resolve(mockSensor);
250 }
251 }, reject).callback;
252
253 ambientLightSensor.onchange = new CallbackWrapper(() => {
254 assert_equals(ambientLightSensor.reading.illuminance,
255 kDefaultReadingValue);
256 ambientLightSensor.stop();
257 }, reject).callback;
258
259 ambientLightSensor.onerror = reject;
260 });
261 })
262 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
263
264 return testPromise;
265 }, 'Test that sensor reading is not updated when sensor is stopped.');
266
267 sensor_test(sensor => {
268 let ambientLightSensor = new AmbientLightSensor(); 211 let ambientLightSensor = new AmbientLightSensor();
269 ambientLightSensor.start(); 212 ambientLightSensor.start();
270 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 213 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
271 .then(mockSensor => { 214 .then(mockSensor => {
272 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 215 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
273 }) 216 })
274 .then((mockSensor) => { 217 .then((mockSensor) => {
275 return new Promise((resolve, reject) => { 218 return new Promise((resolve, reject) => {
276 ambientLightSensor.onchange = () => { 219 ambientLightSensor.onchange = () => {
277 if (ambientLightSensor.reading.illuminance 220 if (ambientLightSensor.reading.illuminance
278 == kDefaultReadingValue) { 221 == kDefaultReadingValue) {
279 resolve(mockSensor); 222 resolve(mockSensor);
280 } 223 }
281 } 224 }
282 ambientLightSensor.onerror = reject; 225 ambientLightSensor.onerror = reject;
283 }); 226 });
284 }) 227 })
285 .then((mockSensor) => { 228 .then((mockSensor) => {
286 testRunner.setPageVisibility("hidden"); 229 testRunner.setPageVisibility("hidden");
287 return mockSensor.suspendCalled(); 230 return mockSensor.suspendCalled();
288 }) 231 })
289 .then((mockSensor) => { 232 .then((mockSensor) => {
290 testRunner.setPageVisibility("visible"); 233 testRunner.setPageVisibility("visible");
291 return mockSensor.resumeCalled(); 234 return mockSensor.resumeCalled();
292 }) 235 })
293 .then((mockSensor) => { 236 .then((mockSensor) => {
294 return new Promise((resolve, reject) => { 237 return new Promise((resolve, reject) => {
295 ambientLightSensor.onstatechange = () => {
296 if (ambientLightSensor.state == 'idle') {
297 resolve(mockSensor);
298 }
299 }
300 ambientLightSensor.stop(); 238 ambientLightSensor.stop();
239 resolve(mockSensor);
301 ambientLightSensor.onerror = reject; 240 ambientLightSensor.onerror = reject;
302 }); 241 });
303 }) 242 })
304 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 243 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
305 244
306 return testPromise; 245 return testPromise;
307 }, 'Test that sensor receives suspend / resume notifications when page' 246 }, 'Test that sensor receives suspend / resume notifications when page'
308 +' visibility changes.'); 247 +' visibility changes.');
309 248
310
311 sensor_test(sensor => { 249 sensor_test(sensor => {
312 let sensor1 = new AmbientLightSensor({frequency: 60}); 250 let sensor1 = new AmbientLightSensor({frequency: 60});
313 sensor1.start(); 251 sensor1.start();
314 252
315 let sensor2 = new AmbientLightSensor({frequency: 20}); 253 let sensor2 = new AmbientLightSensor({frequency: 20});
316 sensor2.start(); 254 sensor2.start();
317 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 255 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
318 .then(mockSensor => { 256 .then(mockSensor => {
319 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 257 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
320 }) 258 })
321 .then((mockSensor) => { 259 .then((mockSensor) => {
322 return new Promise((resolve, reject) => { 260 return new Promise((resolve, reject) => {
323 sensor1.onstatechange = event => {
324 if (sensor1.state == 'idle') {
325 resolve(mockSensor);
326 }
327 };
328
329 let wrapper = new CallbackWrapper(() => { 261 let wrapper = new CallbackWrapper(() => {
330 // Reading value is correct. 262 // Reading value is correct.
331 assert_equals(sensor1.reading.illuminance, kDefaultReadingValue); 263 assert_equals(sensor1.reading.illuminance, kDefaultReadingValue);
332 264
333 // Both sensors share the same reading instance. 265 // Both sensors share the same reading instance.
334 let reading = sensor1.reading; 266 let reading = sensor1.reading;
335 assert_equals(reading, sensor2.reading); 267 assert_equals(reading, sensor2.reading);
336 268
337 // After first sensor stops its reading is null, reading for second 269 // After first sensor stops its reading is null, reading for second
338 // sensor sensor remains. 270 // sensor sensor remains.
339 sensor1.stop(); 271 sensor1.stop();
340 assert_equals(sensor1.reading, null); 272 assert_equals(sensor1.reading, null);
341 assert_equals(sensor2.reading.illuminance, kDefaultReadingValue); 273 assert_equals(sensor2.reading.illuminance, kDefaultReadingValue);
342 274
343 sensor2.stop(); 275 sensor2.stop();
344 assert_equals(sensor2.reading, null); 276 assert_equals(sensor2.reading, null);
345 277
346 // Cached reading remains. 278 // Cached reading remains.
347 assert_equals(reading.illuminance, kDefaultReadingValue); 279 assert_equals(reading.illuminance, kDefaultReadingValue);
280 resolve(mockSensor);
348 }, reject); 281 }, reject);
349 282
350 sensor1.onchange = wrapper.callback; 283 sensor1.onchange = wrapper.callback;
351 sensor1.onerror = reject; 284 sensor1.onerror = reject;
352 sensor2.onerror = reject; 285 sensor2.onerror = reject;
353 }); 286 });
354 }) 287 })
355 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 288 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
356 289
357 return testPromise; 290 return testPromise;
358 }, 'Test that sensor reading is correct.'); 291 }, 'Test that sensor reading is correct.');
359 292
360 </script> 293 </script>
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/LayoutTests/webexposed/global-interface-listing-expected.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698