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(e); | 10 reject_func(e); |
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', | |
23 'mojo/public/js/bindings', | 22 'mojo/public/js/bindings', |
24 'device/generic_sensor/public/interfaces/sensor_provider.mojom', | 23 'device/generic_sensor/public/interfaces/sensor_provider.mojom', |
25 'device/generic_sensor/public/interfaces/sensor.mojom', | 24 'device/generic_sensor/public/interfaces/sensor.mojom', |
26 ], (core, bindings, sensor_provider, sensor) => { | 25 ], (bindings, sensor_provider, sensor) => { |
27 | 26 |
28 // Helper function that returns resolved promise with result. | 27 // Helper function that returns resolved promise with result. |
29 function sensorResponse(success) { | 28 function sensorResponse(success) { |
30 return Promise.resolve({success}); | 29 return Promise.resolve({success}); |
31 } | 30 } |
32 | 31 |
33 // Class that mocks Sensor interface defined in sensor.mojom | 32 // Class that mocks Sensor interface defined in sensor.mojom |
34 class MockSensor { | 33 class MockSensor { |
35 constructor(sensorRequest, handle, offset, size, reportingMode) { | 34 constructor(sensorRequest, handle, offset, size, reportingMode) { |
36 this.client_ = null; | 35 this.client_ = null; |
37 this.expects_modified_reading_ = false; | 36 this.expects_modified_reading_ = false; |
38 this.start_should_fail_ = false; | 37 this.start_should_fail_ = false; |
39 this.reporting_mode_ = reportingMode; | 38 this.reporting_mode_ = reportingMode; |
40 this.sensor_reading_timer_id_ = null; | 39 this.sensor_reading_timer_id_ = null; |
41 this.update_reading_function_ = null; | 40 this.update_reading_function_ = null; |
42 this.reading_updates_count_ = 0; | 41 this.reading_updates_count_ = 0; |
43 this.suspend_called_ = null; | 42 this.suspend_called_ = null; |
44 this.resume_called_ = null; | 43 this.resume_called_ = null; |
45 this.add_configuration_called_ = null; | 44 this.add_configuration_called_ = null; |
46 this.remove_configuration_called_ = null; | 45 this.remove_configuration_called_ = null; |
47 this.active_sensor_configurations_ = []; | 46 this.active_sensor_configurations_ = []; |
48 let rv = core.mapBuffer(handle, offset, size, | 47 let rv = handle.mapBuffer(offset, size); |
49 core.MAP_BUFFER_FLAG_NONE); | 48 assert_equals(rv.result, Mojo.RESULT_OK, "Failed to map shared buffer"); |
50 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); | |
51 this.buffer_array_ = rv.buffer; | 49 this.buffer_array_ = rv.buffer; |
52 this.buffer_ = new Float64Array(this.buffer_array_); | 50 this.buffer_ = new Float64Array(this.buffer_array_); |
53 this.resetBuffer(); | 51 this.resetBuffer(); |
54 this.binding_ = new bindings.Binding(sensor.Sensor, this, | 52 this.binding_ = new bindings.Binding(sensor.Sensor, this, |
55 sensorRequest); | 53 sensorRequest); |
56 this.binding_.setConnectionErrorHandler(() => { | 54 this.binding_.setConnectionErrorHandler(() => { |
57 this.reset(); | 55 this.reset(); |
58 }); | 56 }); |
59 } | 57 } |
60 | 58 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 this.expects_modified_reading_ = false; | 130 this.expects_modified_reading_ = false; |
133 this.reading_updates_count_ = 0; | 131 this.reading_updates_count_ = 0; |
134 this.start_should_fail_ = false; | 132 this.start_should_fail_ = false; |
135 this.update_reading_function_ = null; | 133 this.update_reading_function_ = null; |
136 this.active_sensor_configurations_ = []; | 134 this.active_sensor_configurations_ = []; |
137 this.suspend_called_ = null; | 135 this.suspend_called_ = null; |
138 this.resume_called_ = null; | 136 this.resume_called_ = null; |
139 this.add_configuration_called_ = null; | 137 this.add_configuration_called_ = null; |
140 this.remove_configuration_called_ = null; | 138 this.remove_configuration_called_ = null; |
141 this.resetBuffer(); | 139 this.resetBuffer(); |
142 core.unmapBuffer(this.buffer_array_); | |
143 this.buffer_array_ = null; | 140 this.buffer_array_ = null; |
144 this.binding_.close(); | 141 this.binding_.close(); |
145 } | 142 } |
146 | 143 |
147 // Zeroes shared buffer. | 144 // Zeroes shared buffer. |
148 resetBuffer() { | 145 resetBuffer() { |
149 for (let i = 0; i < this.buffer_.length; ++i) { | 146 for (let i = 0; i < this.buffer_.length; ++i) { |
150 this.buffer_[i] = 0; | 147 this.buffer_[i] = 0; |
151 } | 148 } |
152 } | 149 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 } | 228 } |
232 | 229 |
233 // Class that mocks SensorProvider interface defined in | 230 // Class that mocks SensorProvider interface defined in |
234 // sensor_provider.mojom | 231 // sensor_provider.mojom |
235 class MockSensorProvider { | 232 class MockSensorProvider { |
236 constructor() { | 233 constructor() { |
237 this.reading_size_in_bytes_ = | 234 this.reading_size_in_bytes_ = |
238 sensor_provider.SensorInitParams.kReadBufferSizeForTests; | 235 sensor_provider.SensorInitParams.kReadBufferSizeForTests; |
239 this.shared_buffer_size_in_bytes_ = this.reading_size_in_bytes_ * | 236 this.shared_buffer_size_in_bytes_ = this.reading_size_in_bytes_ * |
240 sensor.SensorType.LAST; | 237 sensor.SensorType.LAST; |
241 let rv = | 238 let rv = Mojo.createSharedBuffer(this.shared_buffer_size_in_bytes_); |
242 core.createSharedBuffer( | 239 assert_equals(rv.result, Mojo.RESULT_OK, "Failed to create buffer"); |
243 this.shared_buffer_size_in_bytes_, | |
244 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); | |
245 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); | |
246 this.shared_buffer_handle_ = rv.handle; | 240 this.shared_buffer_handle_ = rv.handle; |
247 this.active_sensor_ = null; | 241 this.active_sensor_ = null; |
248 this.get_sensor_should_fail_ = false; | 242 this.get_sensor_should_fail_ = false; |
249 this.resolve_func_ = null; | 243 this.resolve_func_ = null; |
250 this.is_continuous_ = false; | 244 this.is_continuous_ = false; |
251 this.max_frequency_ = 60; | 245 this.max_frequency_ = 60; |
252 this.min_frequency_ = 1; | 246 this.min_frequency_ = 1; |
253 this.binding_ = new bindings.Binding(sensor_provider.SensorProvider, | 247 this.binding_ = new bindings.Binding(sensor_provider.SensorProvider, |
254 this); | 248 this); |
255 } | 249 } |
(...skipping 11 matching lines...) Expand all Loading... |
267 if (this.is_continuous_) { | 261 if (this.is_continuous_) { |
268 reporting_mode = sensor.ReportingMode.CONTINUOUS; | 262 reporting_mode = sensor.ReportingMode.CONTINUOUS; |
269 } | 263 } |
270 | 264 |
271 if (this.active_sensor_ == null) { | 265 if (this.active_sensor_ == null) { |
272 let mockSensor = new MockSensor(request, this.shared_buffer_handle_, | 266 let mockSensor = new MockSensor(request, this.shared_buffer_handle_, |
273 offset, this.reading_size_in_bytes_, reporting_mode); | 267 offset, this.reading_size_in_bytes_, reporting_mode); |
274 this.active_sensor_ = mockSensor; | 268 this.active_sensor_ = mockSensor; |
275 } | 269 } |
276 | 270 |
277 let rv = | 271 let rv = this.shared_buffer_handle_.duplicateBufferHandle(); |
278 core.duplicateBufferHandle( | 272 assert_equals(rv.result, Mojo.RESULT_OK); |
279 this.shared_buffer_handle_, | |
280 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); | |
281 | |
282 assert_equals(rv.result, core.RESULT_OK); | |
283 | 273 |
284 let default_config = {frequency: 5}; | 274 let default_config = {frequency: 5}; |
285 | 275 |
286 let init_params = | 276 let init_params = |
287 new sensor_provider.SensorInitParams( | 277 new sensor_provider.SensorInitParams( |
288 { memory: rv.handle, | 278 { memory: rv.handle, |
289 buffer_offset: offset, | 279 buffer_offset: offset, |
290 mode: reporting_mode, | 280 mode: reporting_mode, |
291 default_configuration: default_config, | 281 default_configuration: default_config, |
292 minimum_frequency: this.min_frequency_, | 282 minimum_frequency: this.min_frequency_, |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 }; | 373 }; |
384 | 374 |
385 let onFailure = error => { | 375 let onFailure = error => { |
386 sensor.mockSensorProvider.reset(); | 376 sensor.mockSensorProvider.reset(); |
387 return new Promise((resolve, reject) => { setTimeout(() => {reject(error);
}, 0); }); | 377 return new Promise((resolve, reject) => { setTimeout(() => {reject(error);
}, 0); }); |
388 }; | 378 }; |
389 | 379 |
390 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 380 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
391 }), name, properties); | 381 }), name, properties); |
392 } | 382 } |
OLD | NEW |