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

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: 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 => { 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(event => {
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) => {
188 let wrapper = new CallbackWrapper(event => {
189 assert_equals(event.reading.illuminance, kDefaultReadingValue);
190 ambientLightSensor.stop();
191 }, reject);
192
182 ambientLightSensor.onstatechange = event => { 193 ambientLightSensor.onstatechange = event => {
183 if (ambientLightSensor.state === 'idle') { 194 if (ambientLightSensor.state == 'idle') {
184 resolve(mockSensor); 195 resolve(mockSensor);
185 } 196 }
186 }; 197 };
187 198
188 ambientLightSensor.onchange = e => { 199 ambientLightSensor.onchange = wrapper.callback();
189 assert_equals(e.reading.illuminance, kDefaultReadingValue);
190 ambientLightSensor.stop();
191 };
192
193 ambientLightSensor.onerror = reject; 200 ambientLightSensor.onerror = reject;
194 }); 201 });
195 }) 202 })
196 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 203 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
197 204
198 return testPromise; 205 return testPromise;
199 }, 'Test that onChange is called and sensor reading is valid.'); 206 }, 'Test that onChange is called and sensor reading is valid.');
200 207
201 sensor_test(sensor => { 208 sensor_test(sensor => {
202 let ambientLightSensor = new AmbientLightSensor({frequency: 60}); 209 let ambientLightSensor = new AmbientLightSensor({frequency: 60});
203 ambientLightSensor.start(); 210 ambientLightSensor.start();
204 let testPromise = sensor.mockSensorProvider.getCreatedSensor() 211 let testPromise = sensor.mockSensorProvider.getCreatedSensor()
205 .then(mockSensor => { 212 .then(mockSensor => {
206 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading); 213 return mockSensor.setUpdateSensorReadingFunction(update_sensor_reading);
207 }) 214 })
208 .then((mockSensor) => { 215 .then((mockSensor) => {
209 return new Promise((resolve, reject) => { 216 return new Promise((resolve, reject) => {
210 ambientLightSensor.onstatechange = () => { 217 ambientLightSensor.onstatechange = new CallbackWrapper(() => {
211 if (ambientLightSensor.state === 'idle') { 218 if (ambientLightSensor.state == 'idle') {
212 assert_equals(ambientLightSensor.reading, null); 219 assert_equals(ambientLightSensor.reading, null);
213 resolve(mockSensor); 220 resolve(mockSensor);
214 } 221 }
215 } 222 }, reject).callback();
216 223
217 ambientLightSensor.onchange = e => { 224 ambientLightSensor.onchange = new CallbackWrapper(event => {
218 assert_equals(e.reading.illuminance, kDefaultReadingValue); 225 assert_equals(event.reading.illuminance, kDefaultReadingValue);
219 ambientLightSensor.stop(); 226 ambientLightSensor.stop();
220 } 227 }, reject).callback();
228
221 ambientLightSensor.onerror = reject; 229 ambientLightSensor.onerror = reject;
222 }); 230 });
223 }) 231 })
224 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 232 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
225 233
226 return testPromise; 234 return testPromise;
227 }, 'Test that sensor reading is not updated when sensor is stopped.'); 235 }, 'Test that sensor reading is not updated when sensor is stopped.');
228 236
229 sensor_test(sensor => { 237 sensor_test(sensor => {
230 let ambientLightSensor = new AmbientLightSensor(); 238 let ambientLightSensor = new AmbientLightSensor();
(...skipping 16 matching lines...) Expand all
247 testRunner.setPageVisibility("hidden"); 255 testRunner.setPageVisibility("hidden");
248 return mockSensor.suspendCalled(); 256 return mockSensor.suspendCalled();
249 }) 257 })
250 .then((mockSensor) => { 258 .then((mockSensor) => {
251 testRunner.setPageVisibility("visible"); 259 testRunner.setPageVisibility("visible");
252 return mockSensor.resumeCalled(); 260 return mockSensor.resumeCalled();
253 }) 261 })
254 .then((mockSensor) => { 262 .then((mockSensor) => {
255 return new Promise((resolve, reject) => { 263 return new Promise((resolve, reject) => {
256 ambientLightSensor.onstatechange = () => { 264 ambientLightSensor.onstatechange = () => {
257 if (ambientLightSensor.state === 'idle') { 265 if (ambientLightSensor.state == 'idle') {
258 resolve(mockSensor); 266 resolve(mockSensor);
259 } 267 }
260 } 268 }
261 ambientLightSensor.stop(); 269 ambientLightSensor.stop();
262 ambientLightSensor.onerror = reject; 270 ambientLightSensor.onerror = reject;
263 }); 271 });
264 }) 272 })
265 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); }); 273 .then(mockSensor => { return mockSensor.removeConfigurationCalled(); });
266 274
267 return testPromise; 275 return testPromise;
268 }, 'Test that sensor receives suspend / resume notifications when page' 276 }, 'Test that sensor receives suspend / resume notifications when page'
269 +' visibility changes.'); 277 +' visibility changes.');
270 278
271 </script> 279 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698