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 15 matching lines...) Expand all Loading... |
26 this.suspend_called_ = null; | 26 this.suspend_called_ = null; |
27 this.resume_called_ = null; | 27 this.resume_called_ = null; |
28 this.add_configuration_called_ = null; | 28 this.add_configuration_called_ = null; |
29 this.remove_configuration_called_ = null; | 29 this.remove_configuration_called_ = null; |
30 this.active_sensor_configurations_ = []; | 30 this.active_sensor_configurations_ = []; |
31 let rv = core.mapBuffer(handle, offset, size, | 31 let rv = core.mapBuffer(handle, offset, size, |
32 core.MAP_BUFFER_FLAG_NONE); | 32 core.MAP_BUFFER_FLAG_NONE); |
33 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); | 33 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); |
34 this.buffer_array_ = rv.buffer; | 34 this.buffer_array_ = rv.buffer; |
35 this.buffer_ = new Float64Array(this.buffer_array_); | 35 this.buffer_ = new Float64Array(this.buffer_array_); |
| 36 this.resetBuffer(); |
36 bindings.StubBindings(this.stub_).delegate = this; | 37 bindings.StubBindings(this.stub_).delegate = this; |
37 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { | 38 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { |
38 reset(); | 39 reset(); |
39 }; | 40 }; |
40 } | 41 } |
41 | 42 |
42 // Returns default configuration. | 43 // Returns default configuration. |
43 getDefaultConfiguration() { | 44 getDefaultConfiguration() { |
44 return Promise.resolve({frequency: 5}); | 45 return Promise.resolve({frequency: 5}); |
45 } | 46 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 window.clearTimeout(this.sensor_reading_timer_id_); | 114 window.clearTimeout(this.sensor_reading_timer_id_); |
114 } | 115 } |
115 | 116 |
116 this.start_should_fail_ = false; | 117 this.start_should_fail_ = false; |
117 this.sensor_reading_timer_id_ = null; | 118 this.sensor_reading_timer_id_ = null; |
118 this.active_sensor_configurations_ = []; | 119 this.active_sensor_configurations_ = []; |
119 this.suspend_called_ = null; | 120 this.suspend_called_ = null; |
120 this.resume_called_ = null; | 121 this.resume_called_ = null; |
121 this.add_configuration_called_ = null; | 122 this.add_configuration_called_ = null; |
122 this.remove_configuration_called_ = null; | 123 this.remove_configuration_called_ = null; |
| 124 this.resetBuffer(); |
| 125 } |
| 126 |
| 127 // Zeroes shared buffer. |
| 128 resetBuffer() { |
123 for (let i = 0; i < this.buffer_.length; ++i) { | 129 for (let i = 0; i < this.buffer_.length; ++i) { |
124 this.buffer_[i] = 0; | 130 this.buffer_[i] = 0; |
125 } | 131 } |
126 } | 132 } |
127 | 133 |
128 // Sets callback that is used to deliver sensor reading updates. | 134 // Sets callback that is used to deliver sensor reading updates. |
129 setUpdateSensorReadingFunction(update_reading_function) { | 135 setUpdateSensorReadingFunction(update_reading_function) { |
130 this.update_reading_function_ = update_reading_function; | 136 this.update_reading_function_ = update_reading_function; |
131 return Promise.resolve(this); | 137 return Promise.resolve(this); |
132 } | 138 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 // Helper function that returns resolved promise for getSensor() function. | 175 // Helper function that returns resolved promise for getSensor() function. |
170 function getSensorResponse(init_params, client_request) { | 176 function getSensorResponse(init_params, client_request) { |
171 return Promise.resolve({init_params, client_request}); | 177 return Promise.resolve({init_params, client_request}); |
172 } | 178 } |
173 | 179 |
174 // Class that mocks SensorProvider interface defined in | 180 // Class that mocks SensorProvider interface defined in |
175 // sensor_provider.mojom | 181 // sensor_provider.mojom |
176 class MockSensorProvider { | 182 class MockSensorProvider { |
177 constructor() { | 183 constructor() { |
178 this.reading_size_in_bytes_ = | 184 this.reading_size_in_bytes_ = |
179 sensor_provider.SensorInitParams.kReadBufferSize; | 185 sensor_provider.SensorInitParams.kReadBufferSizeForTests; |
180 this.shared_buffer_size_in_bytes_ = this.reading_size_in_bytes_ * | 186 this.shared_buffer_size_in_bytes_ = this.reading_size_in_bytes_ * |
181 sensor.SensorType.LAST; | 187 sensor.SensorType.LAST; |
182 let rv = | 188 let rv = |
183 core.createSharedBuffer( | 189 core.createSharedBuffer( |
184 this.shared_buffer_size_in_bytes_, | 190 this.shared_buffer_size_in_bytes_, |
185 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); | 191 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); |
186 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); | 192 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); |
187 this.shared_buffer_handle_ = rv.handle; | 193 this.shared_buffer_handle_ = rv.handle; |
188 this.active_sensor_ = null; | 194 this.active_sensor_ = null; |
189 this.get_sensor_should_fail_ = false; | 195 this.get_sensor_should_fail_ = false; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 }); | 299 }); |
294 } | 300 } |
295 | 301 |
296 function sensor_test(func, name, properties) { | 302 function sensor_test(func, name, properties) { |
297 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { | 303 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { |
298 let result = Promise.resolve(func(sensor)); | 304 let result = Promise.resolve(func(sensor)); |
299 let cleanUp = () => { sensor.mockSensorProvider.reset(); }; | 305 let cleanUp = () => { sensor.mockSensorProvider.reset(); }; |
300 return result.then(cleanUp, cleanUp); | 306 return result.then(cleanUp, cleanUp); |
301 }), name, properties); | 307 }), name, properties); |
302 } | 308 } |
OLD | NEW |