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(); | 10 reject_func(); |
(...skipping 23 matching lines...) Expand all Loading... |
34 // Class that mocks Sensor interface defined in sensor.mojom | 34 // Class that mocks Sensor interface defined in sensor.mojom |
35 class MockSensor { | 35 class MockSensor { |
36 constructor(stub, handle, offset, size, reportingMode) { | 36 constructor(stub, handle, offset, size, reportingMode) { |
37 this.client_ = null; | 37 this.client_ = null; |
38 this.stub_ = stub; | 38 this.stub_ = stub; |
39 this.expects_modified_reading_ = false; | 39 this.expects_modified_reading_ = false; |
40 this.start_should_fail_ = false; | 40 this.start_should_fail_ = false; |
41 this.reporting_mode_ = reportingMode; | 41 this.reporting_mode_ = reportingMode; |
42 this.sensor_reading_timer_id_ = null; | 42 this.sensor_reading_timer_id_ = null; |
43 this.update_reading_function_ = null; | 43 this.update_reading_function_ = null; |
44 this.reading_updates_count_ = 0; | |
45 this.suspend_called_ = null; | 44 this.suspend_called_ = null; |
46 this.resume_called_ = null; | 45 this.resume_called_ = null; |
47 this.add_configuration_called_ = null; | 46 this.add_configuration_called_ = null; |
48 this.remove_configuration_called_ = null; | 47 this.remove_configuration_called_ = null; |
49 this.active_sensor_configurations_ = []; | 48 this.active_sensor_configurations_ = []; |
50 let rv = core.mapBuffer(handle, offset, size, | 49 let rv = core.mapBuffer(handle, offset, size, |
51 core.MAP_BUFFER_FLAG_NONE); | 50 core.MAP_BUFFER_FLAG_NONE); |
52 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); | 51 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); |
53 this.buffer_array_ = rv.buffer; | 52 this.buffer_array_ = rv.buffer; |
54 this.buffer_ = new Float64Array(this.buffer_array_); | 53 this.buffer_ = new Float64Array(this.buffer_array_); |
55 this.resetBuffer(); | 54 this.resetBuffer(); |
56 bindings.StubBindings(this.stub_).delegate = this; | 55 bindings.StubBindings(this.stub_).delegate = this; |
57 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { | 56 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { |
58 this.reset(); | 57 this.reset(); |
59 }; | 58 }; |
60 } | 59 } |
61 | 60 |
62 // Returns default configuration. | 61 // Returns default configuration. |
63 getDefaultConfiguration() { | 62 getDefaultConfiguration() { |
64 return Promise.resolve({frequency: 5}); | 63 return Promise.resolve({frequency: 5}); |
65 } | 64 } |
66 | 65 |
67 reading_updates_count() { | |
68 return this.reading_updates_count_; | |
69 } | |
70 // Adds configuration for the sensor and starts reporting fake data | 66 // Adds configuration for the sensor and starts reporting fake data |
71 // through update_reading_function_ callback. | 67 // through update_reading_function_ callback. |
72 addConfiguration(configuration) { | 68 addConfiguration(configuration) { |
73 assert_not_equals(configuration, null, "Invalid sensor configuration."); | 69 assert_not_equals(configuration, null, "Invalid sensor configuration."); |
74 | 70 |
75 this.active_sensor_configurations_.push(configuration); | 71 this.active_sensor_configurations_.push(configuration); |
76 // Sort using descending order. | 72 // Sort using descending order. |
77 this.active_sensor_configurations_.sort( | 73 this.active_sensor_configurations_.sort( |
78 (first, second) => { return second.frequency - first.frequency }); | 74 (first, second) => { return second.frequency - first.frequency }); |
79 | 75 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 120 } |
125 } | 121 } |
126 | 122 |
127 // Mock functions | 123 // Mock functions |
128 | 124 |
129 // Resets mock Sensor state. | 125 // Resets mock Sensor state. |
130 reset() { | 126 reset() { |
131 this.stopReading(); | 127 this.stopReading(); |
132 | 128 |
133 this.expects_modified_reading_ = false; | 129 this.expects_modified_reading_ = false; |
134 this.reading_updates_count_ = 0; | |
135 this.start_should_fail_ = false; | 130 this.start_should_fail_ = false; |
136 this.update_reading_function_ = null; | 131 this.update_reading_function_ = null; |
137 this.active_sensor_configurations_ = []; | 132 this.active_sensor_configurations_ = []; |
138 this.suspend_called_ = null; | 133 this.suspend_called_ = null; |
139 this.resume_called_ = null; | 134 this.resume_called_ = null; |
140 this.add_configuration_called_ = null; | 135 this.add_configuration_called_ = null; |
141 this.remove_configuration_called_ = null; | 136 this.remove_configuration_called_ = null; |
142 this.resetBuffer(); | 137 this.resetBuffer(); |
143 core.unmapBuffer(this.buffer_array_); | 138 core.unmapBuffer(this.buffer_array_); |
144 this.buffer_array_ = null; | 139 this.buffer_array_ = null; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 187 |
193 // Resolves promise when removeConfiguration() is called. | 188 // Resolves promise when removeConfiguration() is called. |
194 removeConfigurationCalled() { | 189 removeConfigurationCalled() { |
195 return new Promise((resolve, reject) => { | 190 return new Promise((resolve, reject) => { |
196 this.remove_configuration_called_ = resolve; | 191 this.remove_configuration_called_ = resolve; |
197 }); | 192 }); |
198 } | 193 } |
199 | 194 |
200 startReading() { | 195 startReading() { |
201 if (this.update_reading_function_ != null) { | 196 if (this.update_reading_function_ != null) { |
202 this.stopReading(); | |
203 let max_frequency_used = | 197 let max_frequency_used = |
204 this.active_sensor_configurations_[0].frequency; | 198 this.active_sensor_configurations_[0].frequency; |
205 let timeout = (1 / max_frequency_used) * 1000; | 199 let timeout = (1 / max_frequency_used) * 1000; |
206 this.sensor_reading_timer_id_ = window.setInterval(() => { | 200 this.sensor_reading_timer_id_ = window.setInterval(() => { |
207 if (this.update_reading_function_) { | 201 if (this.update_reading_function_) |
208 this.update_reading_function_(this.buffer_, | 202 this.update_reading_function_(this.buffer_, |
209 this.expects_modified_reading_, | 203 this.expects_modified_reading_); |
210 this.reading_updates_count_); | |
211 this.reading_updates_count_++; | |
212 } | |
213 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | 204 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
214 this.client_.sensorReadingChanged(); | 205 this.client_.sensorReadingChanged(); |
215 } | 206 } |
216 }, timeout); | 207 }, timeout); |
217 } | 208 } |
218 } | 209 } |
219 | 210 |
220 stopReading() { | 211 stopReading() { |
221 if (this.sensor_reading_timer_id_ != null) { | 212 if (this.sensor_reading_timer_id_ != null) { |
222 window.clearInterval(this.sensor_reading_timer_id_); | 213 window.clearInterval(this.sensor_reading_timer_id_); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 }; | 371 }; |
381 | 372 |
382 let onFailure = () => { | 373 let onFailure = () => { |
383 sensor.mockSensorProvider.reset(); | 374 sensor.mockSensorProvider.reset(); |
384 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 375 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
385 }; | 376 }; |
386 | 377 |
387 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 378 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
388 }), name, properties); | 379 }), name, properties); |
389 } | 380 } |
OLD | NEW |