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