OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 function sensor_mocks(mojo) { |
| 4 return define('Generic Sensor API mocks', [ |
| 5 'mojo/public/js/core', |
| 6 'mojo/public/js/bindings', |
| 7 'mojo/public/js/connection', |
| 8 'device/generic_sensor/public/interfaces/sensor_provider.mojom', |
| 9 'device/generic_sensor/public/interfaces/sensor.mojom', |
| 10 ], (core, bindings, connection, sensor_provider, sensor) => { |
| 11 |
| 12 // Helper function that returns resolved promise with result. |
| 13 function sensorResponse(success) { |
| 14 return Promise.resolve({success}); |
| 15 } |
| 16 |
| 17 // Class that mocks Sensor interface defined in sensor.mojom |
| 18 class MockSensor { |
| 19 constructor(type, stub, handle, offset, size, reportingMode) { |
| 20 this.type_ = type; |
| 21 this.stub_ = stub; |
| 22 this.client_ = null; |
| 23 this.start_should_fail_ = false; |
| 24 this.reporting_mode_ = reportingMode; |
| 25 this.sensor_reading_timer_id_ = null; |
| 26 this.update_reading_function_ = null; |
| 27 this.suspend_called_ = false; |
| 28 this.resume_called_ = false; |
| 29 this.active_sensor_configurations_ = []; |
| 30 let rv = core.mapBuffer(handle, offset, size, |
| 31 core.MAP_BUFFER_FLAG_NONE); |
| 32 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); |
| 33 this.buffer_array_ = rv.buffer; |
| 34 this.buffer_ = new Float64Array(this.buffer_array_); |
| 35 bindings.StubBindings(this.stub_).delegate = this; |
| 36 bindings.StubBindings(this.stub_).connectionErrorHandler = () => { |
| 37 reset(); |
| 38 }; |
| 39 } |
| 40 |
| 41 // Returns default configuration. |
| 42 getDefaultConfiguration() { |
| 43 return Promise.resolve({frequency: 5}); |
| 44 } |
| 45 |
| 46 // Adds configuration for the sensor and starts reporting fake data |
| 47 // through update_reading_function_ callback. |
| 48 addConfiguration(configuration) { |
| 49 assert_not_equals(configuration, null, "Invalid sensor configuration."); |
| 50 |
| 51 if (!this.start_should_fail_ && this.update_reading_function_ != null) { |
| 52 let timeout = (1 / configuration.frequency) * 1000; |
| 53 this.sensor_reading_timer_id_ = window.setTimeout(() => { |
| 54 this.update_reading_function_(this.buffer_); |
| 55 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
| 56 this.client_.sensorReadingChanged(); |
| 57 } |
| 58 }, timeout); |
| 59 |
| 60 this.active_sensor_configurations_.push(configuration); |
| 61 } |
| 62 |
| 63 return sensorResponse(!this.start_should_fail_); |
| 64 } |
| 65 |
| 66 // Removes sensor configuration from the list of active configurations and |
| 67 // stops notification about sensor reading changes if |
| 68 // active_sensor_configurations_ is empty. |
| 69 removeConfiguration(configuration) { |
| 70 let index = this.active_sensor_configurations_.findIndex(configuration); |
| 71 if (index !== -1) { |
| 72 this.active_sensor_configurations_.splice(index, 1); |
| 73 } else { |
| 74 return sensorResponse(false); |
| 75 } |
| 76 |
| 77 if (this.sensor_reading_timer_id_ != null |
| 78 && this.active_sensor_configurations_.length === 0) { |
| 79 window.clearTimeout(this.sensor_reading_timer_id_); |
| 80 this.sensor_reading_timer_id_ = null; |
| 81 } |
| 82 |
| 83 return sensorResponse(true); |
| 84 } |
| 85 |
| 86 suspend() { |
| 87 this.suspend_called_ = true; |
| 88 } |
| 89 |
| 90 resume() { |
| 91 this.resume_called_ = true; |
| 92 } |
| 93 |
| 94 // Mock functions |
| 95 |
| 96 // Resets mock Sensor state. |
| 97 reset() { |
| 98 if (this.sensor_reading_timer_id_) { |
| 99 window.clearTimeout(this.sensor_reading_timer_id_); |
| 100 } |
| 101 |
| 102 bindings.StubBindings(this.stub_).close(); |
| 103 core.unmapBuffer(this.buffer_array_); |
| 104 this.type_ = null; |
| 105 this.stub_ = null; |
| 106 this.client_ = null; |
| 107 this.start_should_fail_ = false; |
| 108 this.sensor_reading_timer_id_ = null; |
| 109 this.active_sensor_configurations_ = []; |
| 110 this.suspend_called_ = false; |
| 111 this.resume_called_ = false; |
| 112 } |
| 113 |
| 114 // Sets callback that is used to deliver sensor reading updates. |
| 115 setUpdateSensorReadingFunction(update_reading_function) { |
| 116 this.update_reading_function_ = update_reading_function; |
| 117 return Promise.resolve(); |
| 118 } |
| 119 |
| 120 // Sets flag that forces sensor to fail when addConfiguration is invoked. |
| 121 setStartShouldFail(should_fail) { |
| 122 this.start_should_fail_ = should_fail; |
| 123 } |
| 124 |
| 125 // Returns resolved promise if suspend() was called, rejected otherwise. |
| 126 isSuspendCalled() { |
| 127 if (this.suspend_called_) |
| 128 return Promise.resolve(); |
| 129 return Promise.reject(); |
| 130 } |
| 131 |
| 132 // Returns resolved promise if resume() was called, rejected otherwise. |
| 133 isResumeCalled() { |
| 134 if (this.resume_called_) |
| 135 return Promise.resolve(); |
| 136 return Promise.reject(); |
| 137 } |
| 138 } |
| 139 |
| 140 // Helper function that returns resolved promise for getSensor() function. |
| 141 function getSensorResponse(init_params, client_request) { |
| 142 return Promise.resolve({init_params, client_request}); |
| 143 } |
| 144 |
| 145 // Class that mocks SensorProvider interface defined in |
| 146 // sensor_provider.mojom |
| 147 class MockSensorProvider { |
| 148 constructor() { |
| 149 this.reading_size_in_bytes_ = |
| 150 sensor_provider.SensorInitParams.kReadBufferSize; |
| 151 this.shared_buffer_size_in_bytes_ = this.reading_size_in_bytes_ * |
| 152 sensor.SensorType.LAST; |
| 153 let rv = |
| 154 core.createSharedBuffer( |
| 155 this.shared_buffer_size_in_bytes_, |
| 156 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); |
| 157 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); |
| 158 this.shared_buffer_handle_ = rv.handle; |
| 159 this.active_sensors_ = {}; |
| 160 this.get_sensor_should_fail_ = false; |
| 161 this.resolve_func_ = null; |
| 162 this.is_continuous_ = false; |
| 163 } |
| 164 |
| 165 // Returns initialized Sensor proxy to the client. |
| 166 getSensor(type, stub) { |
| 167 if (this.get_sensor_should_fail_) { |
| 168 return getSensorResponse(null, null); |
| 169 } |
| 170 |
| 171 let offset = |
| 172 (sensor.SensorType.LAST - type) * this.reading_size_in_bytes_; |
| 173 let reporting_mode = sensor.ReportingMode.ON_CHANGE; |
| 174 if (this.is_continuous_) { |
| 175 reporting_mode = sensor.ReportingMode.CONTINUOUS; |
| 176 } |
| 177 |
| 178 if (!(type in this.active_sensors_)) { |
| 179 let mockSensor = new MockSensor(type, stub, |
| 180 this.shared_buffer_handle_, offset, this.reading_size_in_bytes_, |
| 181 reporting_mode); |
| 182 this.active_sensors_[type] = mockSensor; |
| 183 } |
| 184 |
| 185 let rv = |
| 186 core.duplicateBufferHandle( |
| 187 this.shared_buffer_handle_, |
| 188 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); |
| 189 |
| 190 assert_equals(rv.result, core.RESULT_OK); |
| 191 |
| 192 let default_config = {frequency: 5}; |
| 193 |
| 194 let init_params = |
| 195 new sensor_provider.SensorInitParams( |
| 196 { memory: rv.handle, |
| 197 buffer_offset: offset, |
| 198 mode: reporting_mode, |
| 199 default_configuration: default_config }); |
| 200 |
| 201 if (this.resolve_func_ !== null) |
| 202 this.resolve_func_(this.active_sensors_[type]); |
| 203 |
| 204 var client_handle = connection.bindProxy(proxy => { |
| 205 this.active_sensors_[type].client_ = proxy; |
| 206 }, sensor.SensorClient); |
| 207 |
| 208 return getSensorResponse(init_params, client_handle); |
| 209 } |
| 210 |
| 211 // Binds object to mojo message pipe |
| 212 bindToPipe(pipe) { |
| 213 this.stub_ = connection.bindHandleToStub( |
| 214 pipe, sensor_provider.SensorProvider); |
| 215 bindings.StubBindings(this.stub_).delegate = this; |
| 216 } |
| 217 |
| 218 // Mock functions |
| 219 |
| 220 // Resets state of mock SensorProvider between test runs. |
| 221 reset() { |
| 222 for (let id in this.active_sensors_) { |
| 223 this.active_sensors_[id].reset(); |
| 224 } |
| 225 |
| 226 this.active_sensors_ = {}; |
| 227 this.get_sensor_should_fail_ = false; |
| 228 this.resolve_func_ = null; |
| 229 } |
| 230 |
| 231 // Sets flag that forces mock SensorProvider to fail when getSensor() is |
| 232 // invoked. |
| 233 setGetSensorShouldFail(should_fail) { |
| 234 this.get_sensor_should_fail_ = should_fail; |
| 235 } |
| 236 |
| 237 // Returns mock sensor that was created in getSensor to the layout test. |
| 238 getCreatedSensor() { |
| 239 return new Promise((resolve, reject) => { |
| 240 this.resolve_func_ = resolve; |
| 241 }); |
| 242 } |
| 243 |
| 244 // Forces sensor to use |reporting_mode| as an update mode. |
| 245 setContinuousReportingMode(reporting_mode) { |
| 246 this.is_continuous_ = reporting_mode; |
| 247 } |
| 248 } |
| 249 |
| 250 let mockSensorProvider = new MockSensorProvider; |
| 251 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| 252 sensor_provider.SensorProvider.name, |
| 253 pipe => { |
| 254 mockSensorProvider.bindToPipe(pipe); |
| 255 }); |
| 256 |
| 257 return Promise.resolve({ |
| 258 mockSensorProvider: mockSensorProvider, |
| 259 }); |
| 260 }); |
| 261 } |
| 262 |
| 263 function sensor_test(func, name, properties) { |
| 264 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { |
| 265 let result = Promise.resolve(func(sensor)); |
| 266 let cleanupPromise = new Promise((resolve, reject) => { |
| 267 let cleanup = |
| 268 func => { |
| 269 setTimeout(() => { |
| 270 sensor.mockSensorProvider.reset(); |
| 271 func(); |
| 272 }, 0); |
| 273 } |
| 274 result.then(cleanup(resolve), cleanup(reject)); |
| 275 }); |
| 276 return cleanupPromise; |
| 277 }), name, properties); |
| 278 } |
OLD | NEW |