| OLD | NEW |
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 function sensor_mocks(mojo) { | 3 function sensor_mocks(mojo) { |
| 4 return define('Generic Sensor API mocks', [ | 4 return define('Generic Sensor API mocks', [ |
| 5 'mojo/public/js/core', | 5 'mojo/public/js/core', |
| 6 'mojo/public/js/bindings', | 6 'mojo/public/js/bindings', |
| 7 'mojo/public/js/connection', | 7 'mojo/public/js/connection', |
| 8 'device/generic_sensor/public/interfaces/sensor_provider.mojom', | 8 'device/generic_sensor/public/interfaces/sensor_provider.mojom', |
| 9 'device/generic_sensor/public/interfaces/sensor.mojom', | 9 'device/generic_sensor/public/interfaces/sensor.mojom', |
| 10 ], (core, bindings, connection, sensor_provider, sensor) => { | 10 ], (core, bindings, connection, sensor_provider, sensor) => { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 let rv = | 188 let rv = |
| 189 core.createSharedBuffer( | 189 core.createSharedBuffer( |
| 190 this.shared_buffer_size_in_bytes_, | 190 this.shared_buffer_size_in_bytes_, |
| 191 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); | 191 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); |
| 192 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); | 192 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); |
| 193 this.shared_buffer_handle_ = rv.handle; | 193 this.shared_buffer_handle_ = rv.handle; |
| 194 this.active_sensor_ = null; | 194 this.active_sensor_ = null; |
| 195 this.get_sensor_should_fail_ = false; | 195 this.get_sensor_should_fail_ = false; |
| 196 this.resolve_func_ = null; | 196 this.resolve_func_ = null; |
| 197 this.is_continuous_ = false; | 197 this.is_continuous_ = false; |
| 198 this.max_frequency_ = 60; |
| 198 } | 199 } |
| 199 | 200 |
| 200 // Returns initialized Sensor proxy to the client. | 201 // Returns initialized Sensor proxy to the client. |
| 201 getSensor(type, stub) { | 202 getSensor(type, stub) { |
| 202 if (this.get_sensor_should_fail_) { | 203 if (this.get_sensor_should_fail_) { |
| 203 return getSensorResponse(null, | 204 return getSensorResponse(null, |
| 204 connection.bindProxy(null, sensor.SensorClient)); | 205 connection.bindProxy(null, sensor.SensorClient)); |
| 205 } | 206 } |
| 206 | 207 |
| 207 let offset = | 208 let offset = |
| (...skipping 16 matching lines...) Expand all Loading... |
| 224 | 225 |
| 225 assert_equals(rv.result, core.RESULT_OK); | 226 assert_equals(rv.result, core.RESULT_OK); |
| 226 | 227 |
| 227 let default_config = {frequency: 5}; | 228 let default_config = {frequency: 5}; |
| 228 | 229 |
| 229 let init_params = | 230 let init_params = |
| 230 new sensor_provider.SensorInitParams( | 231 new sensor_provider.SensorInitParams( |
| 231 { memory: rv.handle, | 232 { memory: rv.handle, |
| 232 buffer_offset: offset, | 233 buffer_offset: offset, |
| 233 mode: reporting_mode, | 234 mode: reporting_mode, |
| 234 default_configuration: default_config }); | 235 default_configuration: default_config, |
| 236 maximum_frequency: this.max_frequency_}); |
| 235 | 237 |
| 236 if (this.resolve_func_ !== null) { | 238 if (this.resolve_func_ !== null) { |
| 237 this.resolve_func_(this.active_sensor_); | 239 this.resolve_func_(this.active_sensor_); |
| 238 } | 240 } |
| 239 | 241 |
| 240 var client_handle = connection.bindProxy(proxy => { | 242 var client_handle = connection.bindProxy(proxy => { |
| 241 this.active_sensor_.client_ = proxy; | 243 this.active_sensor_.client_ = proxy; |
| 242 }, sensor.SensorClient); | 244 }, sensor.SensorClient); |
| 243 | |
| 244 return getSensorResponse(init_params, client_handle); | 245 return getSensorResponse(init_params, client_handle); |
| 245 } | 246 } |
| 246 | 247 |
| 247 // Binds object to mojo message pipe | 248 // Binds object to mojo message pipe |
| 248 bindToPipe(pipe) { | 249 bindToPipe(pipe) { |
| 249 this.stub_ = connection.bindHandleToStub( | 250 this.stub_ = connection.bindHandleToStub( |
| 250 pipe, sensor_provider.SensorProvider); | 251 pipe, sensor_provider.SensorProvider); |
| 251 bindings.StubBindings(this.stub_).delegate = this; | 252 bindings.StubBindings(this.stub_).delegate = this; |
| 252 } | 253 } |
| 253 | 254 |
| 254 // Mock functions | 255 // Mock functions |
| 255 | 256 |
| 256 // Resets state of mock SensorProvider between test runs. | 257 // Resets state of mock SensorProvider between test runs. |
| 257 reset() { | 258 reset() { |
| 258 if (this.active_sensor_ != null) { | 259 if (this.active_sensor_ != null) { |
| 259 this.active_sensor_.reset(); | 260 this.active_sensor_.reset(); |
| 260 } | 261 } |
| 261 | 262 |
| 262 this.get_sensor_should_fail_ = false; | 263 this.get_sensor_should_fail_ = false; |
| 263 this.resolve_func_ = null; | 264 this.resolve_func_ = null; |
| 265 this.max_frequency_ = 60; |
| 264 } | 266 } |
| 265 | 267 |
| 266 // Sets flag that forces mock SensorProvider to fail when getSensor() is | 268 // Sets flag that forces mock SensorProvider to fail when getSensor() is |
| 267 // invoked. | 269 // invoked. |
| 268 setGetSensorShouldFail(should_fail) { | 270 setGetSensorShouldFail(should_fail) { |
| 269 this.get_sensor_should_fail_ = should_fail; | 271 this.get_sensor_should_fail_ = should_fail; |
| 270 } | 272 } |
| 271 | 273 |
| 272 // Returns mock sensor that was created in getSensor to the layout test. | 274 // Returns mock sensor that was created in getSensor to the layout test. |
| 273 getCreatedSensor() { | 275 getCreatedSensor() { |
| 274 if (this.active_sensor_ != null) { | 276 if (this.active_sensor_ != null) { |
| 275 return Promise.resolve(this.active_sensor_); | 277 return Promise.resolve(this.active_sensor_); |
| 276 } | 278 } |
| 277 | 279 |
| 278 return new Promise((resolve, reject) => { | 280 return new Promise((resolve, reject) => { |
| 279 this.resolve_func_ = resolve; | 281 this.resolve_func_ = resolve; |
| 280 }); | 282 }); |
| 281 } | 283 } |
| 282 | 284 |
| 283 // Forces sensor to use |reporting_mode| as an update mode. | 285 // Forces sensor to use |reporting_mode| as an update mode. |
| 284 setContinuousReportingMode(reporting_mode) { | 286 setContinuousReportingMode(reporting_mode) { |
| 285 this.is_continuous_ = reporting_mode; | 287 this.is_continuous_ = reporting_mode; |
| 286 } | 288 } |
| 289 |
| 290 // Sets the maximum frequency for a concrete sensor. |
| 291 setMaximumSupportedFrequency(frequency) { |
| 292 this.max_frequency_ = frequency; |
| 293 } |
| 287 } | 294 } |
| 288 | 295 |
| 289 let mockSensorProvider = new MockSensorProvider; | 296 let mockSensorProvider = new MockSensorProvider; |
| 290 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 297 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| 291 sensor_provider.SensorProvider.name, | 298 sensor_provider.SensorProvider.name, |
| 292 pipe => { | 299 pipe => { |
| 293 mockSensorProvider.bindToPipe(pipe); | 300 mockSensorProvider.bindToPipe(pipe); |
| 294 }); | 301 }); |
| 295 | 302 |
| 296 return Promise.resolve({ | 303 return Promise.resolve({ |
| 297 mockSensorProvider: mockSensorProvider, | 304 mockSensorProvider: mockSensorProvider, |
| 298 }); | 305 }); |
| 299 }); | 306 }); |
| 300 } | 307 } |
| 301 | 308 |
| 302 function sensor_test(func, name, properties) { | 309 function sensor_test(func, name, properties) { |
| 303 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { | 310 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { |
| 304 let result = Promise.resolve(func(sensor)); | 311 let result = Promise.resolve(func(sensor)); |
| 305 let cleanUp = () => { sensor.mockSensorProvider.reset(); }; | 312 let cleanUp = () => { sensor.mockSensorProvider.reset(); }; |
| 306 return result.then(cleanUp, cleanUp); | 313 return result.then(cleanUp, cleanUp); |
| 307 }), name, properties); | 314 }), name, properties); |
| 308 } | 315 } |
| OLD | NEW |