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