Chromium Code Reviews| 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 21 matching lines...) Expand all Loading... | |
| 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; | 37 this.expects_modified_reading_ = false; |
| 38 this.start_should_fail_ = false; | 38 this.start_should_fail_ = false; |
| 39 this.reporting_mode_ = reportingMode; | 39 this.reporting_mode_ = reportingMode; |
| 40 this.sensor_reading_timer_id_ = null; | 40 this.sensor_reading_timer_id_ = null; |
| 41 this.update_reading_function_ = null; | 41 this.update_reading_function_ = null; |
| 42 this.reading_updates_count_ = 0; | |
| 42 this.suspend_called_ = null; | 43 this.suspend_called_ = null; |
| 43 this.resume_called_ = null; | 44 this.resume_called_ = null; |
| 44 this.add_configuration_called_ = null; | 45 this.add_configuration_called_ = null; |
| 45 this.remove_configuration_called_ = null; | 46 this.remove_configuration_called_ = null; |
| 46 this.active_sensor_configurations_ = []; | 47 this.active_sensor_configurations_ = []; |
| 47 let rv = core.mapBuffer(handle, offset, size, | 48 let rv = core.mapBuffer(handle, offset, size, |
| 48 core.MAP_BUFFER_FLAG_NONE); | 49 core.MAP_BUFFER_FLAG_NONE); |
| 49 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); | 50 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); |
| 50 this.buffer_array_ = rv.buffer; | 51 this.buffer_array_ = rv.buffer; |
| 51 this.buffer_ = new Float64Array(this.buffer_array_); | 52 this.buffer_ = new Float64Array(this.buffer_array_); |
| 52 this.resetBuffer(); | 53 this.resetBuffer(); |
| 53 this.binding_ = new bindings.Binding(sensor.Sensor, this, | 54 this.binding_ = new bindings.Binding(sensor.Sensor, this, |
| 54 sensorRequest); | 55 sensorRequest); |
| 55 this.binding_.setConnectionErrorHandler(() => { | 56 this.binding_.setConnectionErrorHandler(() => { |
| 56 this.reset(); | 57 this.reset(); |
| 57 }); | 58 }); |
| 58 } | 59 } |
| 59 | 60 |
| 60 // Returns default configuration. | 61 // Returns default configuration. |
| 61 getDefaultConfiguration() { | 62 getDefaultConfiguration() { |
| 62 return Promise.resolve({frequency: 5}); | 63 return Promise.resolve({frequency: 5}); |
| 63 } | 64 } |
| 64 | 65 |
| 66 reading_updates_count() { | |
| 67 return this.reading_updates_count_; | |
| 68 } | |
| 65 // Adds configuration for the sensor and starts reporting fake data | 69 // Adds configuration for the sensor and starts reporting fake data |
| 66 // through update_reading_function_ callback. | 70 // through update_reading_function_ callback. |
| 67 addConfiguration(configuration) { | 71 addConfiguration(configuration) { |
| 68 assert_not_equals(configuration, null, "Invalid sensor configuration."); | 72 assert_not_equals(configuration, null, "Invalid sensor configuration."); |
| 69 | 73 |
| 70 this.active_sensor_configurations_.push(configuration); | 74 this.active_sensor_configurations_.push(configuration); |
| 71 // Sort using descending order. | 75 // Sort using descending order. |
| 72 this.active_sensor_configurations_.sort( | 76 this.active_sensor_configurations_.sort( |
| 73 (first, second) => { return second.frequency - first.frequency }); | 77 (first, second) => { return second.frequency - first.frequency }); |
| 74 | 78 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 return new Promise((resolve, reject) => { | 193 return new Promise((resolve, reject) => { |
| 190 this.remove_configuration_called_ = resolve; | 194 this.remove_configuration_called_ = resolve; |
| 191 }); | 195 }); |
| 192 } | 196 } |
| 193 | 197 |
| 194 startReading() { | 198 startReading() { |
| 195 if (this.update_reading_function_ != null) { | 199 if (this.update_reading_function_ != null) { |
| 196 let max_frequency_used = | 200 let max_frequency_used = |
| 197 this.active_sensor_configurations_[0].frequency; | 201 this.active_sensor_configurations_[0].frequency; |
| 198 let timeout = (1 / max_frequency_used) * 1000; | 202 let timeout = (1 / max_frequency_used) * 1000; |
| 199 this.sensor_reading_timer_id_ = window.setInterval(() => { | 203 this.sensor_reading_timer_id_ = window.setInterval(() => { |
|
shalamov
2016/12/21 10:41:03
Should we clear previous interval if addConfigurat
Mikhail
2016/12/21 13:06:28
Right, thanks for the catch!
| |
| 200 if (this.update_reading_function_) | 204 if (this.update_reading_function_) { |
| 201 this.update_reading_function_(this.buffer_, | 205 this.update_reading_function_(this.buffer_, |
| 202 this.expects_modified_reading_); | 206 this.expects_modified_reading_, |
| 207 this.reading_updates_count_); | |
| 208 this.reading_updates_count_++; | |
| 209 } | |
| 203 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | 210 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
| 204 this.client_.sensorReadingChanged(); | 211 this.client_.sensorReadingChanged(); |
| 205 } | 212 } |
| 206 }, timeout); | 213 }, timeout); |
| 207 } | 214 } |
| 208 } | 215 } |
| 209 | 216 |
| 210 stopReading() { | 217 stopReading() { |
| 211 if (this.sensor_reading_timer_id_ != null) { | 218 if (this.sensor_reading_timer_id_ != null) { |
| 212 window.clearInterval(this.sensor_reading_timer_id_); | 219 window.clearInterval(this.sensor_reading_timer_id_); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 }; | 373 }; |
| 367 | 374 |
| 368 let onFailure = () => { | 375 let onFailure = () => { |
| 369 sensor.mockSensorProvider.reset(); | 376 sensor.mockSensorProvider.reset(); |
| 370 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 377 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
| 371 }; | 378 }; |
| 372 | 379 |
| 373 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 380 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
| 374 }), name, properties); | 381 }), name, properties); |
| 375 } | 382 } |
| OLD | NEW |