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

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

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

Powered by Google App Engine
This is Rietveld 408576698