OLD | NEW |
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 // Wraps callback and calls reject_func if callback throws an error. | 3 // Wraps callback and calls reject_func if callback throws an error. |
4 class CallbackWrapper { | 4 class CallbackWrapper { |
5 constructor(callback, reject_func) { | 5 constructor(callback, reject_func) { |
6 this.wrapper_func_ = (args) => { | 6 this.wrapper_func_ = (args) => { |
7 try { | 7 try { |
8 callback(args); | 8 callback(args); |
9 } catch(e) { | 9 } catch(e) { |
10 reject_func(e); | 10 reject_func(e); |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 // Helper function that returns resolved promise with result. | 28 // Helper function that returns resolved promise with result. |
29 function sensorResponse(success) { | 29 function sensorResponse(success) { |
30 return Promise.resolve({success}); | 30 return Promise.resolve({success}); |
31 } | 31 } |
32 | 32 |
33 // Class that mocks Sensor interface defined in sensor.mojom | 33 // Class that mocks Sensor interface defined in sensor.mojom |
34 class MockSensor { | 34 class MockSensor { |
35 constructor(sensorRequest, handle, offset, size, reportingMode) { | 35 constructor(sensorRequest, handle, offset, size, reportingMode) { |
36 this.client_ = null; | 36 this.client_ = null; |
37 this.expects_modified_reading_ = false; | |
38 this.start_should_fail_ = false; | 37 this.start_should_fail_ = false; |
39 this.reporting_mode_ = reportingMode; | 38 this.reporting_mode_ = reportingMode; |
40 this.sensor_reading_timer_id_ = null; | 39 this.sensor_reading_timer_id_ = null; |
41 this.update_reading_function_ = null; | 40 this.update_reading_function_ = null; |
42 this.reading_updates_count_ = 0; | 41 this.reading_updates_count_ = 0; |
43 this.suspend_called_ = null; | 42 this.suspend_called_ = null; |
44 this.resume_called_ = null; | 43 this.resume_called_ = null; |
45 this.add_configuration_called_ = null; | 44 this.add_configuration_called_ = null; |
46 this.remove_configuration_called_ = null; | 45 this.remove_configuration_called_ = null; |
47 this.active_sensor_configurations_ = []; | 46 this.active_sensor_configurations_ = []; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 this.resume_called_(this); | 121 this.resume_called_(this); |
123 } | 122 } |
124 } | 123 } |
125 | 124 |
126 // Mock functions | 125 // Mock functions |
127 | 126 |
128 // Resets mock Sensor state. | 127 // Resets mock Sensor state. |
129 reset() { | 128 reset() { |
130 this.stopReading(); | 129 this.stopReading(); |
131 | 130 |
132 this.expects_modified_reading_ = false; | |
133 this.reading_updates_count_ = 0; | 131 this.reading_updates_count_ = 0; |
134 this.start_should_fail_ = false; | 132 this.start_should_fail_ = false; |
135 this.update_reading_function_ = null; | 133 this.update_reading_function_ = null; |
136 this.active_sensor_configurations_ = []; | 134 this.active_sensor_configurations_ = []; |
137 this.suspend_called_ = null; | 135 this.suspend_called_ = null; |
138 this.resume_called_ = null; | 136 this.resume_called_ = null; |
139 this.add_configuration_called_ = null; | 137 this.add_configuration_called_ = null; |
140 this.remove_configuration_called_ = null; | 138 this.remove_configuration_called_ = null; |
141 this.resetBuffer(); | 139 this.resetBuffer(); |
142 core.unmapBuffer(this.buffer_array_); | 140 core.unmapBuffer(this.buffer_array_); |
(...skipping 12 matching lines...) Expand all Loading... |
155 setUpdateSensorReadingFunction(update_reading_function) { | 153 setUpdateSensorReadingFunction(update_reading_function) { |
156 this.update_reading_function_ = update_reading_function; | 154 this.update_reading_function_ = update_reading_function; |
157 return Promise.resolve(this); | 155 return Promise.resolve(this); |
158 } | 156 } |
159 | 157 |
160 // Sets flag that forces sensor to fail when addConfiguration is invoked. | 158 // Sets flag that forces sensor to fail when addConfiguration is invoked. |
161 setStartShouldFail(should_fail) { | 159 setStartShouldFail(should_fail) { |
162 this.start_should_fail_ = should_fail; | 160 this.start_should_fail_ = should_fail; |
163 } | 161 } |
164 | 162 |
165 // Sets flags that asks for a modified reading values at each iteration | |
166 // to initiate 'onchange' event broadcasting. | |
167 setExpectsModifiedReading(expects_modified_reading) { | |
168 this.expects_modified_reading_ = expects_modified_reading; | |
169 } | |
170 | |
171 // Returns resolved promise if suspend() was called, rejected otherwise. | 163 // Returns resolved promise if suspend() was called, rejected otherwise. |
172 suspendCalled() { | 164 suspendCalled() { |
173 return new Promise((resolve, reject) => { | 165 return new Promise((resolve, reject) => { |
174 this.suspend_called_ = resolve; | 166 this.suspend_called_ = resolve; |
175 }); | 167 }); |
176 } | 168 } |
177 | 169 |
178 // Returns resolved promise if resume() was called, rejected otherwise. | 170 // Returns resolved promise if resume() was called, rejected otherwise. |
179 resumeCalled() { | 171 resumeCalled() { |
180 return new Promise((resolve, reject) => { | 172 return new Promise((resolve, reject) => { |
(...skipping 16 matching lines...) Expand all Loading... |
197 } | 189 } |
198 | 190 |
199 startReading() { | 191 startReading() { |
200 if (this.update_reading_function_ != null) { | 192 if (this.update_reading_function_ != null) { |
201 this.stopReading(); | 193 this.stopReading(); |
202 let max_frequency_used = | 194 let max_frequency_used = |
203 this.active_sensor_configurations_[0].frequency; | 195 this.active_sensor_configurations_[0].frequency; |
204 let timeout = (1 / max_frequency_used) * 1000; | 196 let timeout = (1 / max_frequency_used) * 1000; |
205 this.sensor_reading_timer_id_ = window.setInterval(() => { | 197 this.sensor_reading_timer_id_ = window.setInterval(() => { |
206 if (this.update_reading_function_) { | 198 if (this.update_reading_function_) { |
207 this.update_reading_function_(this.buffer_, | 199 this.update_reading_function_(this.buffer_); |
208 this.expects_modified_reading_, | |
209 this.reading_updates_count_); | |
210 this.reading_updates_count_++; | 200 this.reading_updates_count_++; |
211 } | 201 } |
212 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | 202 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
213 this.client_.sensorReadingChanged(); | 203 this.client_.sensorReadingChanged(); |
214 } | 204 } |
215 }, timeout); | 205 }, timeout); |
216 } | 206 } |
217 } | 207 } |
218 | 208 |
219 stopReading() { | 209 stopReading() { |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 }; | 373 }; |
384 | 374 |
385 let onFailure = error => { | 375 let onFailure = error => { |
386 sensor.mockSensorProvider.reset(); | 376 sensor.mockSensorProvider.reset(); |
387 return new Promise((resolve, reject) => { setTimeout(() => {reject(error);
}, 0); }); | 377 return new Promise((resolve, reject) => { setTimeout(() => {reject(error);
}, 0); }); |
388 }; | 378 }; |
389 | 379 |
390 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 380 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
391 }), name, properties); | 381 }), name, properties); |
392 } | 382 } |
OLD | NEW |