| 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(); |
| 11 } | 11 } |
| 12 } | 12 } |
| 13 } | 13 } |
| 14 | 14 |
| 15 get callback() { | 15 get callback() { |
| 16 return this.wrapper_func_; | 16 return this.wrapper_func_; |
| 17 } | 17 } |
| 18 } | 18 } |
| 19 | 19 |
| 20 function sensor_mocks(mojo) { | 20 function sensor_mocks(mojo) { |
| 21 return define('Generic Sensor API mocks', [ | 21 return define('Generic Sensor API mocks', [ |
| 22 'mojo/public/js/core', | 22 'mojo/public/js/core', |
| 23 'mojo/public/js/bindings', | 23 'mojo/public/js/bindings', |
| 24 'mojo/public/js/connection', |
| 24 'device/generic_sensor/public/interfaces/sensor_provider.mojom', | 25 'device/generic_sensor/public/interfaces/sensor_provider.mojom', |
| 25 'device/generic_sensor/public/interfaces/sensor.mojom', | 26 'device/generic_sensor/public/interfaces/sensor.mojom', |
| 26 ], (core, bindings, sensor_provider, sensor) => { | 27 ], (core, bindings, connection, sensor_provider, sensor) => { |
| 27 | 28 |
| 28 // Helper function that returns resolved promise with result. | 29 // Helper function that returns resolved promise with result. |
| 29 function sensorResponse(success) { | 30 function sensorResponse(success) { |
| 30 return Promise.resolve({success}); | 31 return Promise.resolve({success}); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // Class that mocks Sensor interface defined in sensor.mojom | 34 // Class that mocks Sensor interface defined in sensor.mojom |
| 34 class MockSensor { | 35 class MockSensor { |
| 35 constructor(sensorRequest, handle, offset, size, reportingMode) { | 36 constructor(stub, handle, offset, size, reportingMode) { |
| 36 this.client_ = null; | 37 this.client_ = null; |
| 38 this.stub_ = stub; |
| 37 this.expects_modified_reading_ = false; | 39 this.expects_modified_reading_ = false; |
| 38 this.start_should_fail_ = false; | 40 this.start_should_fail_ = false; |
| 39 this.reporting_mode_ = reportingMode; | 41 this.reporting_mode_ = reportingMode; |
| 40 this.sensor_reading_timer_id_ = null; | 42 this.sensor_reading_timer_id_ = null; |
| 41 this.update_reading_function_ = null; | 43 this.update_reading_function_ = null; |
| 42 this.suspend_called_ = null; | 44 this.suspend_called_ = null; |
| 43 this.resume_called_ = null; | 45 this.resume_called_ = null; |
| 44 this.add_configuration_called_ = null; | 46 this.add_configuration_called_ = null; |
| 45 this.remove_configuration_called_ = null; | 47 this.remove_configuration_called_ = null; |
| 46 this.active_sensor_configurations_ = []; | 48 this.active_sensor_configurations_ = []; |
| 47 let rv = core.mapBuffer(handle, offset, size, | 49 let rv = core.mapBuffer(handle, offset, size, |
| 48 core.MAP_BUFFER_FLAG_NONE); | 50 core.MAP_BUFFER_FLAG_NONE); |
| 49 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"); |
| 50 this.buffer_array_ = rv.buffer; | 52 this.buffer_array_ = rv.buffer; |
| 51 this.buffer_ = new Float64Array(this.buffer_array_); | 53 this.buffer_ = new Float64Array(this.buffer_array_); |
| 52 this.resetBuffer(); | 54 this.resetBuffer(); |
| 53 this.binding_ = new bindings.Binding(sensor.Sensor, this, | 55 bindings.StubBindings(this.stub_).delegate = this; |
| 54 sensorRequest); | 56 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { |
| 55 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 |
| 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) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 this.start_should_fail_ = false; | 130 this.start_should_fail_ = false; |
| 130 this.update_reading_function_ = null; | 131 this.update_reading_function_ = null; |
| 131 this.active_sensor_configurations_ = []; | 132 this.active_sensor_configurations_ = []; |
| 132 this.suspend_called_ = null; | 133 this.suspend_called_ = null; |
| 133 this.resume_called_ = null; | 134 this.resume_called_ = null; |
| 134 this.add_configuration_called_ = null; | 135 this.add_configuration_called_ = null; |
| 135 this.remove_configuration_called_ = null; | 136 this.remove_configuration_called_ = null; |
| 136 this.resetBuffer(); | 137 this.resetBuffer(); |
| 137 core.unmapBuffer(this.buffer_array_); | 138 core.unmapBuffer(this.buffer_array_); |
| 138 this.buffer_array_ = null; | 139 this.buffer_array_ = null; |
| 139 this.binding_.close(); | 140 bindings.StubBindings(this.stub_).close(); |
| 140 } | 141 } |
| 141 | 142 |
| 142 // Zeroes shared buffer. | 143 // Zeroes shared buffer. |
| 143 resetBuffer() { | 144 resetBuffer() { |
| 144 for (let i = 0; i < this.buffer_.length; ++i) { | 145 for (let i = 0; i < this.buffer_.length; ++i) { |
| 145 this.buffer_[i] = 0; | 146 this.buffer_[i] = 0; |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 // Sets callback that is used to deliver sensor reading updates. | 150 // Sets callback that is used to deliver sensor reading updates. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 core.createSharedBuffer( | 234 core.createSharedBuffer( |
| 234 this.shared_buffer_size_in_bytes_, | 235 this.shared_buffer_size_in_bytes_, |
| 235 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); | 236 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); |
| 236 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); | 237 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); |
| 237 this.shared_buffer_handle_ = rv.handle; | 238 this.shared_buffer_handle_ = rv.handle; |
| 238 this.active_sensor_ = null; | 239 this.active_sensor_ = null; |
| 239 this.get_sensor_should_fail_ = false; | 240 this.get_sensor_should_fail_ = false; |
| 240 this.resolve_func_ = null; | 241 this.resolve_func_ = null; |
| 241 this.is_continuous_ = false; | 242 this.is_continuous_ = false; |
| 242 this.max_frequency_ = 60; | 243 this.max_frequency_ = 60; |
| 243 this.binding_ = new bindings.Binding(sensor_provider.SensorProvider, | |
| 244 this); | |
| 245 } | 244 } |
| 246 | 245 |
| 247 // Returns initialized Sensor proxy to the client. | 246 // Returns initialized Sensor proxy to the client. |
| 248 getSensor(type, request) { | 247 getSensor(type, request) { |
| 249 if (this.get_sensor_should_fail_) { | 248 if (this.get_sensor_should_fail_) { |
| 250 var ignored = new sensor.SensorClientPtr(); | 249 var ignored = new sensor.SensorClientPtr(); |
| 251 return getSensorResponse(null, bindings.makeRequest(ignored)); | 250 return getSensorResponse(null, bindings.makeRequest(ignored)); |
| 252 } | 251 } |
| 253 | 252 |
| 254 let offset = | 253 let offset = |
| 255 (sensor.SensorType.LAST - type) * this.reading_size_in_bytes_; | 254 (sensor.SensorType.LAST - type) * this.reading_size_in_bytes_; |
| 256 let reporting_mode = sensor.ReportingMode.ON_CHANGE; | 255 let reporting_mode = sensor.ReportingMode.ON_CHANGE; |
| 257 if (this.is_continuous_) { | 256 if (this.is_continuous_) { |
| 258 reporting_mode = sensor.ReportingMode.CONTINUOUS; | 257 reporting_mode = sensor.ReportingMode.CONTINUOUS; |
| 259 } | 258 } |
| 260 | 259 |
| 261 if (this.active_sensor_ == null) { | 260 if (this.active_sensor_ == null) { |
| 262 let mockSensor = new MockSensor(request, this.shared_buffer_handle_, | 261 var stub = connection.bindHandleToStub(request.handle, sensor.Sensor); |
| 262 let mockSensor = new MockSensor(stub, this.shared_buffer_handle_, |
| 263 offset, this.reading_size_in_bytes_, reporting_mode); | 263 offset, this.reading_size_in_bytes_, reporting_mode); |
| 264 this.active_sensor_ = mockSensor; | 264 this.active_sensor_ = mockSensor; |
| 265 } | 265 } |
| 266 | 266 |
| 267 let rv = | 267 let rv = |
| 268 core.duplicateBufferHandle( | 268 core.duplicateBufferHandle( |
| 269 this.shared_buffer_handle_, | 269 this.shared_buffer_handle_, |
| 270 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); | 270 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); |
| 271 | 271 |
| 272 assert_equals(rv.result, core.RESULT_OK); | 272 assert_equals(rv.result, core.RESULT_OK); |
| 273 | 273 |
| 274 let default_config = {frequency: 5}; | 274 let default_config = {frequency: 5}; |
| 275 | 275 |
| 276 let init_params = | 276 let init_params = |
| 277 new sensor_provider.SensorInitParams( | 277 new sensor_provider.SensorInitParams( |
| 278 { memory: rv.handle, | 278 { memory: rv.handle, |
| 279 buffer_offset: offset, | 279 buffer_offset: offset, |
| 280 mode: reporting_mode, | 280 mode: reporting_mode, |
| 281 default_configuration: default_config, | 281 default_configuration: default_config, |
| 282 maximum_frequency: this.max_frequency_}); | 282 maximum_frequency: this.max_frequency_}); |
| 283 | 283 |
| 284 if (this.resolve_func_ !== null) { | 284 if (this.resolve_func_ !== null) { |
| 285 this.resolve_func_(this.active_sensor_); | 285 this.resolve_func_(this.active_sensor_); |
| 286 } | 286 } |
| 287 | 287 |
| 288 this.active_sensor_.client_ = new sensor.SensorClientPtr(); | 288 var client_request = new bindings.InterfaceRequest( |
| 289 return getSensorResponse( | 289 connection.bindProxy(proxy => { |
| 290 init_params, bindings.makeRequest(this.active_sensor_.client_)); | 290 this.active_sensor_.client_ = proxy; |
| 291 }, sensor.SensorClient)); |
| 292 return getSensorResponse(init_params, client_request); |
| 291 } | 293 } |
| 292 | 294 |
| 293 // Binds object to mojo message pipe | 295 // Binds object to mojo message pipe |
| 294 bindToPipe(pipe) { | 296 bindToPipe(pipe) { |
| 295 this.binding_.bind(pipe); | 297 this.stub_ = connection.bindHandleToStub( |
| 296 this.binding_.setConnectionErrorHandler(() => { | 298 pipe, sensor_provider.SensorProvider); |
| 299 bindings.StubBindings(this.stub_).delegate = this; |
| 300 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { |
| 297 this.reset(); | 301 this.reset(); |
| 298 }); | 302 }; |
| 299 } | 303 } |
| 300 | 304 |
| 301 // Mock functions | 305 // Mock functions |
| 302 | 306 |
| 303 // Resets state of mock SensorProvider between test runs. | 307 // Resets state of mock SensorProvider between test runs. |
| 304 reset() { | 308 reset() { |
| 305 if (this.active_sensor_ != null) { | 309 if (this.active_sensor_ != null) { |
| 306 this.active_sensor_.reset(); | 310 this.active_sensor_.reset(); |
| 307 this.active_sensor_ = null; | 311 this.active_sensor_ = null; |
| 308 } | 312 } |
| 309 | 313 |
| 310 this.get_sensor_should_fail_ = false; | 314 this.get_sensor_should_fail_ = false; |
| 311 this.resolve_func_ = null; | 315 this.resolve_func_ = null; |
| 312 this.max_frequency_ = 60; | 316 this.max_frequency_ = 60; |
| 313 this.is_continuous_ = false; | 317 this.is_continuous_ = false; |
| 314 this.binding_.close(); | 318 if (this.stub_) |
| 319 bindings.StubBindings(this.stub_).close(); |
| 315 } | 320 } |
| 316 | 321 |
| 317 // Sets flag that forces mock SensorProvider to fail when getSensor() is | 322 // Sets flag that forces mock SensorProvider to fail when getSensor() is |
| 318 // invoked. | 323 // invoked. |
| 319 setGetSensorShouldFail(should_fail) { | 324 setGetSensorShouldFail(should_fail) { |
| 320 this.get_sensor_should_fail_ = should_fail; | 325 this.get_sensor_should_fail_ = should_fail; |
| 321 } | 326 } |
| 322 | 327 |
| 323 // Returns mock sensor that was created in getSensor to the layout test. | 328 // Returns mock sensor that was created in getSensor to the layout test. |
| 324 getCreatedSensor() { | 329 getCreatedSensor() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 }; | 371 }; |
| 367 | 372 |
| 368 let onFailure = () => { | 373 let onFailure = () => { |
| 369 sensor.mockSensorProvider.reset(); | 374 sensor.mockSensorProvider.reset(); |
| 370 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 375 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
| 371 }; | 376 }; |
| 372 | 377 |
| 373 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 378 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
| 374 }), name, properties); | 379 }), name, properties); |
| 375 } | 380 } |
| OLD | NEW |