| 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 18 matching lines...) Expand all Loading... |
| 29 // Helper function that returns resolved promise with result. | 29 // Helper function that returns resolved promise with result. |
| 30 function sensorResponse(success) { | 30 function sensorResponse(success) { |
| 31 return Promise.resolve({success}); | 31 return Promise.resolve({success}); |
| 32 } | 32 } |
| 33 | 33 |
| 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.start_should_fail_ = false; | 40 this.start_should_fail_ = false; |
| 40 this.reporting_mode_ = reportingMode; | 41 this.reporting_mode_ = reportingMode; |
| 41 this.sensor_reading_timer_id_ = null; | 42 this.sensor_reading_timer_id_ = null; |
| 42 this.update_reading_function_ = null; | 43 this.update_reading_function_ = null; |
| 43 this.suspend_called_ = null; | 44 this.suspend_called_ = null; |
| 44 this.resume_called_ = null; | 45 this.resume_called_ = null; |
| 45 this.add_configuration_called_ = null; | 46 this.add_configuration_called_ = null; |
| 46 this.remove_configuration_called_ = null; | 47 this.remove_configuration_called_ = null; |
| 47 this.active_sensor_configurations_ = []; | 48 this.active_sensor_configurations_ = []; |
| 48 let rv = core.mapBuffer(handle, offset, size, | 49 let rv = core.mapBuffer(handle, offset, size, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 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 |
| 65 // Adds configuration for the sensor and starts reporting fake data | 66 // Adds configuration for the sensor and starts reporting fake data |
| 66 // through update_reading_function_ callback. | 67 // through update_reading_function_ callback. |
| 67 addConfiguration(configuration) { | 68 addConfiguration(configuration) { |
| 68 assert_not_equals(configuration, null, "Invalid sensor configuration."); | 69 assert_not_equals(configuration, null, "Invalid sensor configuration."); |
| 69 | 70 |
| 70 if (!this.start_should_fail_ && this.update_reading_function_ != null) { | 71 this.active_sensor_configurations_.push(configuration); |
| 71 let timeout = (1 / configuration.frequency) * 1000; | 72 // Sort using descending order. |
| 72 this.sensor_reading_timer_id_ = window.setTimeout(() => { | 73 this.active_sensor_configurations_.sort( |
| 73 if (this.update_reading_function_) | 74 (first, second) => { return second.frequency - first.frequency }); |
| 74 this.update_reading_function_(this.buffer_); | |
| 75 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | |
| 76 this.client_.sensorReadingChanged(); | |
| 77 } | |
| 78 }, timeout); | |
| 79 } | |
| 80 | 75 |
| 81 this.active_sensor_configurations_.push(configuration); | 76 if (!this.start_should_fail_ ) |
| 77 this.startReading(); |
| 82 | 78 |
| 83 if (this.add_configuration_called_ != null) | 79 if (this.add_configuration_called_ != null) |
| 84 this.add_configuration_called_(this); | 80 this.add_configuration_called_(this); |
| 85 | 81 |
| 86 return sensorResponse(!this.start_should_fail_); | 82 return sensorResponse(!this.start_should_fail_); |
| 87 } | 83 } |
| 88 | 84 |
| 89 // Removes sensor configuration from the list of active configurations and | 85 // Removes sensor configuration from the list of active configurations and |
| 90 // stops notification about sensor reading changes if | 86 // stops notification about sensor reading changes if |
| 91 // active_sensor_configurations_ is empty. | 87 // active_sensor_configurations_ is empty. |
| 92 removeConfiguration(configuration) { | 88 removeConfiguration(configuration) { |
| 93 if (this.remove_configuration_called_ != null) { | 89 if (this.remove_configuration_called_ != null) { |
| 94 this.remove_configuration_called_(this); | 90 this.remove_configuration_called_(this); |
| 95 } | 91 } |
| 96 | 92 |
| 97 let index = this.active_sensor_configurations_.indexOf(configuration); | 93 let index = this.active_sensor_configurations_.indexOf(configuration); |
| 98 if (index !== -1) { | 94 if (index !== -1) { |
| 99 this.active_sensor_configurations_.splice(index, 1); | 95 this.active_sensor_configurations_.splice(index, 1); |
| 100 } else { | 96 } else { |
| 101 return sensorResponse(false); | 97 return sensorResponse(false); |
| 102 } | 98 } |
| 103 | 99 |
| 104 if (this.sensor_reading_timer_id_ != null | 100 if (this.active_sensor_configurations_.length === 0) |
| 105 && this.active_sensor_configurations_.length === 0) { | 101 this.stopReading(); |
| 106 window.clearTimeout(this.sensor_reading_timer_id_); | |
| 107 this.sensor_reading_timer_id_ = null; | |
| 108 } | |
| 109 | 102 |
| 110 return sensorResponse(true); | 103 return sensorResponse(true); |
| 111 } | 104 } |
| 112 | 105 |
| 113 // Suspends sensor. | 106 // Suspends sensor. |
| 114 suspend() { | 107 suspend() { |
| 108 this.stopReading(); |
| 115 if (this.suspend_called_ != null) { | 109 if (this.suspend_called_ != null) { |
| 116 this.suspend_called_(this); | 110 this.suspend_called_(this); |
| 117 } | 111 } |
| 118 } | 112 } |
| 119 | 113 |
| 120 // Resumes sensor. | 114 // Resumes sensor. |
| 121 resume() { | 115 resume() { |
| 116 assert_equals(this.sensor_reading_timer_id_, null); |
| 117 this.startReading(); |
| 122 if (this.resume_called_ != null) { | 118 if (this.resume_called_ != null) { |
| 123 this.resume_called_(this); | 119 this.resume_called_(this); |
| 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 if (this.sensor_reading_timer_id_) { | 127 this.stopReading(); |
| 132 window.clearTimeout(this.sensor_reading_timer_id_); | |
| 133 this.sensor_reading_timer_id_ = null; | |
| 134 } | |
| 135 | 128 |
| 129 this.expects_modified_reading_ = false; |
| 136 this.start_should_fail_ = false; | 130 this.start_should_fail_ = false; |
| 137 this.update_reading_function_ = null; | 131 this.update_reading_function_ = null; |
| 138 this.active_sensor_configurations_ = []; | 132 this.active_sensor_configurations_ = []; |
| 139 this.suspend_called_ = null; | 133 this.suspend_called_ = null; |
| 140 this.resume_called_ = null; | 134 this.resume_called_ = null; |
| 141 this.add_configuration_called_ = null; | 135 this.add_configuration_called_ = null; |
| 142 this.remove_configuration_called_ = null; | 136 this.remove_configuration_called_ = null; |
| 143 this.resetBuffer(); | 137 this.resetBuffer(); |
| 144 core.unmapBuffer(this.buffer_array_); | 138 core.unmapBuffer(this.buffer_array_); |
| 145 this.buffer_array_ = null; | 139 this.buffer_array_ = null; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 157 setUpdateSensorReadingFunction(update_reading_function) { | 151 setUpdateSensorReadingFunction(update_reading_function) { |
| 158 this.update_reading_function_ = update_reading_function; | 152 this.update_reading_function_ = update_reading_function; |
| 159 return Promise.resolve(this); | 153 return Promise.resolve(this); |
| 160 } | 154 } |
| 161 | 155 |
| 162 // Sets flag that forces sensor to fail when addConfiguration is invoked. | 156 // Sets flag that forces sensor to fail when addConfiguration is invoked. |
| 163 setStartShouldFail(should_fail) { | 157 setStartShouldFail(should_fail) { |
| 164 this.start_should_fail_ = should_fail; | 158 this.start_should_fail_ = should_fail; |
| 165 } | 159 } |
| 166 | 160 |
| 161 // Sets flags that asks for a modified reading values at each iteration |
| 162 // to initiate 'onchange' event broadcasting. |
| 163 setExpectsModifiedReading(expects_modified_reading) { |
| 164 this.expects_modified_reading_ = expects_modified_reading; |
| 165 } |
| 166 |
| 167 // Returns resolved promise if suspend() was called, rejected otherwise. | 167 // Returns resolved promise if suspend() was called, rejected otherwise. |
| 168 suspendCalled() { | 168 suspendCalled() { |
| 169 return new Promise((resolve, reject) => { | 169 return new Promise((resolve, reject) => { |
| 170 this.suspend_called_ = resolve; | 170 this.suspend_called_ = resolve; |
| 171 }); | 171 }); |
| 172 } | 172 } |
| 173 | 173 |
| 174 // Returns resolved promise if resume() was called, rejected otherwise. | 174 // Returns resolved promise if resume() was called, rejected otherwise. |
| 175 resumeCalled() { | 175 resumeCalled() { |
| 176 return new Promise((resolve, reject) => { | 176 return new Promise((resolve, reject) => { |
| 177 this.resume_called_ = resolve; | 177 this.resume_called_ = resolve; |
| 178 }); | 178 }); |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Resolves promise when addConfiguration() is called. | 181 // Resolves promise when addConfiguration() is called. |
| 182 addConfigurationCalled() { | 182 addConfigurationCalled() { |
| 183 return new Promise((resolve, reject) => { | 183 return new Promise((resolve, reject) => { |
| 184 this.add_configuration_called_ = resolve; | 184 this.add_configuration_called_ = resolve; |
| 185 }); | 185 }); |
| 186 } | 186 } |
| 187 | 187 |
| 188 // Resolves promise when removeConfiguration() is called. | 188 // Resolves promise when removeConfiguration() is called. |
| 189 removeConfigurationCalled() { | 189 removeConfigurationCalled() { |
| 190 return new Promise((resolve, reject) => { | 190 return new Promise((resolve, reject) => { |
| 191 this.remove_configuration_called_ = resolve; | 191 this.remove_configuration_called_ = resolve; |
| 192 }); | 192 }); |
| 193 } | 193 } |
| 194 | 194 |
| 195 startReading() { |
| 196 if (this.update_reading_function_ != null) { |
| 197 let max_frequency_used = |
| 198 this.active_sensor_configurations_[0].frequency; |
| 199 let timeout = (1 / max_frequency_used) * 1000; |
| 200 this.sensor_reading_timer_id_ = window.setInterval(() => { |
| 201 if (this.update_reading_function_) |
| 202 this.update_reading_function_(this.buffer_, |
| 203 this.expects_modified_reading_); |
| 204 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
| 205 this.client_.sensorReadingChanged(); |
| 206 } |
| 207 }, timeout); |
| 208 } |
| 209 } |
| 210 |
| 211 stopReading() { |
| 212 if (this.sensor_reading_timer_id_ != null) { |
| 213 window.clearInterval(this.sensor_reading_timer_id_); |
| 214 this.sensor_reading_timer_id_ = null; |
| 215 } |
| 216 } |
| 217 |
| 195 } | 218 } |
| 196 | 219 |
| 197 // Helper function that returns resolved promise for getSensor() function. | 220 // Helper function that returns resolved promise for getSensor() function. |
| 198 function getSensorResponse(init_params, client_request) { | 221 function getSensorResponse(init_params, client_request) { |
| 199 return Promise.resolve({init_params, client_request}); | 222 return Promise.resolve({init_params, client_request}); |
| 200 } | 223 } |
| 201 | 224 |
| 202 // Class that mocks SensorProvider interface defined in | 225 // Class that mocks SensorProvider interface defined in |
| 203 // sensor_provider.mojom | 226 // sensor_provider.mojom |
| 204 class MockSensorProvider { | 227 class MockSensorProvider { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 // Resets state of mock SensorProvider between test runs. | 305 // Resets state of mock SensorProvider between test runs. |
| 283 reset() { | 306 reset() { |
| 284 if (this.active_sensor_ != null) { | 307 if (this.active_sensor_ != null) { |
| 285 this.active_sensor_.reset(); | 308 this.active_sensor_.reset(); |
| 286 this.active_sensor_ = null; | 309 this.active_sensor_ = null; |
| 287 } | 310 } |
| 288 | 311 |
| 289 this.get_sensor_should_fail_ = false; | 312 this.get_sensor_should_fail_ = false; |
| 290 this.resolve_func_ = null; | 313 this.resolve_func_ = null; |
| 291 this.max_frequency_ = 60; | 314 this.max_frequency_ = 60; |
| 315 this.is_continuous_ = false; |
| 292 if (this.stub_) | 316 if (this.stub_) |
| 293 bindings.StubBindings(this.stub_).close(); | 317 bindings.StubBindings(this.stub_).close(); |
| 294 } | 318 } |
| 295 | 319 |
| 296 // Sets flag that forces mock SensorProvider to fail when getSensor() is | 320 // Sets flag that forces mock SensorProvider to fail when getSensor() is |
| 297 // invoked. | 321 // invoked. |
| 298 setGetSensorShouldFail(should_fail) { | 322 setGetSensorShouldFail(should_fail) { |
| 299 this.get_sensor_should_fail_ = should_fail; | 323 this.get_sensor_should_fail_ = should_fail; |
| 300 } | 324 } |
| 301 | 325 |
| 302 // Returns mock sensor that was created in getSensor to the layout test. | 326 // Returns mock sensor that was created in getSensor to the layout test. |
| 303 getCreatedSensor() { | 327 getCreatedSensor() { |
| 304 if (this.active_sensor_ != null) { | 328 if (this.active_sensor_ != null) { |
| 305 return Promise.resolve(this.active_sensor_); | 329 return Promise.resolve(this.active_sensor_); |
| 306 } | 330 } |
| 307 | 331 |
| 308 return new Promise((resolve, reject) => { | 332 return new Promise((resolve, reject) => { |
| 309 this.resolve_func_ = resolve; | 333 this.resolve_func_ = resolve; |
| 310 }); | 334 }); |
| 311 } | 335 } |
| 312 | 336 |
| 313 // Forces sensor to use |reporting_mode| as an update mode. | 337 // Forces sensor to use |reporting_mode| as an update mode. |
| 314 setContinuousReportingMode(reporting_mode) { | 338 setContinuousReportingMode() { |
| 315 this.is_continuous_ = reporting_mode; | 339 this.is_continuous_ = true; |
| 316 } | 340 } |
| 317 | 341 |
| 318 // Sets the maximum frequency for a concrete sensor. | 342 // Sets the maximum frequency for a concrete sensor. |
| 319 setMaximumSupportedFrequency(frequency) { | 343 setMaximumSupportedFrequency(frequency) { |
| 320 this.max_frequency_ = frequency; | 344 this.max_frequency_ = frequency; |
| 321 } | 345 } |
| 322 } | 346 } |
| 323 | 347 |
| 324 let mockSensorProvider = new MockSensorProvider; | 348 let mockSensorProvider = new MockSensorProvider; |
| 325 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 349 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 345 }; | 369 }; |
| 346 | 370 |
| 347 let onFailure = () => { | 371 let onFailure = () => { |
| 348 sensor.mockSensorProvider.reset(); | 372 sensor.mockSensorProvider.reset(); |
| 349 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 373 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
| 350 }; | 374 }; |
| 351 | 375 |
| 352 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 376 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
| 353 }), name, properties); | 377 }), name, properties); |
| 354 } | 378 } |
| OLD | NEW |