| 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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 let rv = | 210 let rv = |
| 211 core.createSharedBuffer( | 211 core.createSharedBuffer( |
| 212 this.shared_buffer_size_in_bytes_, | 212 this.shared_buffer_size_in_bytes_, |
| 213 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); | 213 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); |
| 214 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); | 214 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); |
| 215 this.shared_buffer_handle_ = rv.handle; | 215 this.shared_buffer_handle_ = rv.handle; |
| 216 this.active_sensor_ = null; | 216 this.active_sensor_ = null; |
| 217 this.get_sensor_should_fail_ = false; | 217 this.get_sensor_should_fail_ = false; |
| 218 this.resolve_func_ = null; | 218 this.resolve_func_ = null; |
| 219 this.is_continuous_ = false; | 219 this.is_continuous_ = false; |
| 220 this.max_frequency_ = 60; |
| 220 } | 221 } |
| 221 | 222 |
| 222 // Returns initialized Sensor proxy to the client. | 223 // Returns initialized Sensor proxy to the client. |
| 223 getSensor(type, stub) { | 224 getSensor(type, stub) { |
| 224 if (this.get_sensor_should_fail_) { | 225 if (this.get_sensor_should_fail_) { |
| 225 return getSensorResponse(null, | 226 return getSensorResponse(null, |
| 226 connection.bindProxy(null, sensor.SensorClient)); | 227 connection.bindProxy(null, sensor.SensorClient)); |
| 227 } | 228 } |
| 228 | 229 |
| 229 let offset = | 230 let offset = |
| (...skipping 16 matching lines...) Expand all Loading... |
| 246 | 247 |
| 247 assert_equals(rv.result, core.RESULT_OK); | 248 assert_equals(rv.result, core.RESULT_OK); |
| 248 | 249 |
| 249 let default_config = {frequency: 5}; | 250 let default_config = {frequency: 5}; |
| 250 | 251 |
| 251 let init_params = | 252 let init_params = |
| 252 new sensor_provider.SensorInitParams( | 253 new sensor_provider.SensorInitParams( |
| 253 { memory: rv.handle, | 254 { memory: rv.handle, |
| 254 buffer_offset: offset, | 255 buffer_offset: offset, |
| 255 mode: reporting_mode, | 256 mode: reporting_mode, |
| 256 default_configuration: default_config }); | 257 default_configuration: default_config, |
| 258 maximum_frequency: this.max_frequency_}); |
| 257 | 259 |
| 258 if (this.resolve_func_ !== null) { | 260 if (this.resolve_func_ !== null) { |
| 259 this.resolve_func_(this.active_sensor_); | 261 this.resolve_func_(this.active_sensor_); |
| 260 } | 262 } |
| 261 | 263 |
| 262 var client_handle = connection.bindProxy(proxy => { | 264 var client_handle = connection.bindProxy(proxy => { |
| 263 this.active_sensor_.client_ = proxy; | 265 this.active_sensor_.client_ = proxy; |
| 264 }, sensor.SensorClient); | 266 }, sensor.SensorClient); |
| 265 | |
| 266 return getSensorResponse(init_params, client_handle); | 267 return getSensorResponse(init_params, client_handle); |
| 267 } | 268 } |
| 268 | 269 |
| 269 // Binds object to mojo message pipe | 270 // Binds object to mojo message pipe |
| 270 bindToPipe(pipe) { | 271 bindToPipe(pipe) { |
| 271 this.stub_ = connection.bindHandleToStub( | 272 this.stub_ = connection.bindHandleToStub( |
| 272 pipe, sensor_provider.SensorProvider); | 273 pipe, sensor_provider.SensorProvider); |
| 273 bindings.StubBindings(this.stub_).delegate = this; | 274 bindings.StubBindings(this.stub_).delegate = this; |
| 274 } | 275 } |
| 275 | 276 |
| 276 // Mock functions | 277 // Mock functions |
| 277 | 278 |
| 278 // Resets state of mock SensorProvider between test runs. | 279 // Resets state of mock SensorProvider between test runs. |
| 279 reset() { | 280 reset() { |
| 280 if (this.active_sensor_ != null) { | 281 if (this.active_sensor_ != null) { |
| 281 this.active_sensor_.reset(); | 282 this.active_sensor_.reset(); |
| 282 this.active_sensor_ = null; | 283 this.active_sensor_ = null; |
| 283 } | 284 } |
| 284 | 285 |
| 285 this.get_sensor_should_fail_ = false; | 286 this.get_sensor_should_fail_ = false; |
| 286 this.resolve_func_ = null; | 287 this.resolve_func_ = null; |
| 288 this.max_frequency_ = 60; |
| 287 } | 289 } |
| 288 | 290 |
| 289 // Sets flag that forces mock SensorProvider to fail when getSensor() is | 291 // Sets flag that forces mock SensorProvider to fail when getSensor() is |
| 290 // invoked. | 292 // invoked. |
| 291 setGetSensorShouldFail(should_fail) { | 293 setGetSensorShouldFail(should_fail) { |
| 292 this.get_sensor_should_fail_ = should_fail; | 294 this.get_sensor_should_fail_ = should_fail; |
| 293 } | 295 } |
| 294 | 296 |
| 295 // Returns mock sensor that was created in getSensor to the layout test. | 297 // Returns mock sensor that was created in getSensor to the layout test. |
| 296 getCreatedSensor() { | 298 getCreatedSensor() { |
| 297 if (this.active_sensor_ != null) { | 299 if (this.active_sensor_ != null) { |
| 298 return Promise.resolve(this.active_sensor_); | 300 return Promise.resolve(this.active_sensor_); |
| 299 } | 301 } |
| 300 | 302 |
| 301 return new Promise((resolve, reject) => { | 303 return new Promise((resolve, reject) => { |
| 302 this.resolve_func_ = resolve; | 304 this.resolve_func_ = resolve; |
| 303 }); | 305 }); |
| 304 } | 306 } |
| 305 | 307 |
| 306 // Forces sensor to use |reporting_mode| as an update mode. | 308 // Forces sensor to use |reporting_mode| as an update mode. |
| 307 setContinuousReportingMode(reporting_mode) { | 309 setContinuousReportingMode(reporting_mode) { |
| 308 this.is_continuous_ = reporting_mode; | 310 this.is_continuous_ = reporting_mode; |
| 309 } | 311 } |
| 312 |
| 313 // Sets the maximum frequency for a concrete sensor. |
| 314 setMaximumSupportedFrequency(frequency) { |
| 315 this.max_frequency_ = frequency; |
| 316 } |
| 310 } | 317 } |
| 311 | 318 |
| 312 let mockSensorProvider = new MockSensorProvider; | 319 let mockSensorProvider = new MockSensorProvider; |
| 313 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 320 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| 314 sensor_provider.SensorProvider.name, | 321 sensor_provider.SensorProvider.name, |
| 315 pipe => { | 322 pipe => { |
| 316 mockSensorProvider.bindToPipe(pipe); | 323 mockSensorProvider.bindToPipe(pipe); |
| 317 }); | 324 }); |
| 318 | 325 |
| 319 return Promise.resolve({ | 326 return Promise.resolve({ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 333 }; | 340 }; |
| 334 | 341 |
| 335 let onFailure = () => { | 342 let onFailure = () => { |
| 336 sensor.mockSensorProvider.reset(); | 343 sensor.mockSensorProvider.reset(); |
| 337 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 344 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
| 338 }; | 345 }; |
| 339 | 346 |
| 340 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 347 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
| 341 }), name, properties); | 348 }), name, properties); |
| 342 } | 349 } |
| OLD | NEW |