| 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( |
| 22 'mojo/public/js/core', | 22 'Generic Sensor API mocks', |
| 23 'mojo/public/js/bindings', | 23 [ |
| 24 'device/generic_sensor/public/interfaces/sensor_provider.mojom', | 24 'mojo/public/js/core', |
| 25 'device/generic_sensor/public/interfaces/sensor.mojom', | 25 'mojo/public/js/bindings', |
| 26 ], (core, bindings, sensor_provider, sensor) => { | 26 'device/generic_sensor/public/interfaces/sensor_provider.mojom', |
| 27 | 27 'device/generic_sensor/public/interfaces/sensor.mojom', |
| 28 // Helper function that returns resolved promise with result. | 28 'third_party/WebKit/public/platform/modules/permissions/permission.mojom
', |
| 29 function sensorResponse(success) { | 29 'third_party/WebKit/public/platform/modules/permissions/permission_statu
s.mojom', |
| 30 return Promise.resolve({success}); | 30 ], |
| 31 } | 31 (core, bindings, sensor_provider, sensor, permission, |
| 32 | 32 permissionStatus) => { |
| 33 // Class that mocks Sensor interface defined in sensor.mojom | 33 |
| 34 class MockSensor { | 34 // Helper function that returns resolved promise with result. |
| 35 constructor(sensorRequest, handle, offset, size, reportingMode) { | 35 function sensorResponse(success) { return Promise.resolve({success}); } |
| 36 this.client_ = null; | 36 |
| 37 this.start_should_fail_ = false; | 37 class MockPermissionService { |
| 38 this.reporting_mode_ = reportingMode; | 38 constructor() { |
| 39 this.sensor_reading_timer_id_ = null; | 39 this.permissionStatus_ = permissionStatus.PermissionStatus.ASK; |
| 40 this.update_reading_function_ = null; | 40 this.pendingPermissionRequest_ = null; |
| 41 this.reading_updates_count_ = 0; | 41 this.rejectPermissionConnections_ = false; |
| 42 this.suspend_called_ = null; | 42 this.binding_ = |
| 43 this.resume_called_ = null; | 43 new bindings.Binding(permission.PermissionService, this); |
| 44 this.add_configuration_called_ = null; | 44 } |
| 45 this.remove_configuration_called_ = null; | 45 |
| 46 this.active_sensor_configurations_ = []; | 46 // Binds object to mojo message pipe |
| 47 let rv = core.mapBuffer(handle, offset, size, | 47 bindToPipe(pipe) { |
| 48 core.MAP_BUFFER_FLAG_NONE); | 48 this.binding_.bind(pipe); |
| 49 assert_equals(rv.result, core.RESULT_OK, "Failed to map shared buffer"); | 49 this.binding_.setConnectionErrorHandler(() => { this.reset(); }); |
| 50 this.buffer_array_ = rv.buffer; | 50 } |
| 51 this.buffer_ = new Float64Array(this.buffer_array_); | 51 |
| 52 this.resetBuffer(); | 52 // Resets state of MockPermissionService between test runs. |
| 53 this.binding_ = new bindings.Binding(sensor.Sensor, this, | 53 reset() { |
| 54 sensorRequest); | 54 this.permissionStatus_ = permissionStatus.PermissionStatus.ASK; |
| 55 this.binding_.setConnectionErrorHandler(() => { | 55 this.binding_.close(); |
| 56 this.reset(); | 56 } |
| 57 }); | 57 |
| 58 } | 58 requestPermission(permissionDescriptor) { |
| 59 | 59 if (permissionDescriptor.name != permission.PermissionName.SENSORS) |
| 60 // Returns default configuration. | 60 return Promise.resolve(permissionStatus.PermissionStatus.DENIED); |
| 61 getDefaultConfiguration() { | 61 |
| 62 return Promise.resolve({frequency: 5}); | 62 return new Promise(resolve => { |
| 63 } | 63 if (this.pendingPermissionRequest_) |
| 64 | 64 this.pendingPermissionRequest_( |
| 65 reading_updates_count() { | 65 permissionStatus.PermissionStatus.ASK); |
| 66 return this.reading_updates_count_; | 66 this.pendingPermissionRequest_ = resolve; |
| 67 } | 67 this.runPermissionCallback_(); |
| 68 // Adds configuration for the sensor and starts reporting fake data | 68 }); |
| 69 // through update_reading_function_ callback. | 69 } |
| 70 addConfiguration(configuration) { | 70 |
| 71 assert_not_equals(configuration, null, "Invalid sensor configuration."); | 71 runPermissionCallback_() { |
| 72 | 72 if (this.permissionStatus_ == |
| 73 this.active_sensor_configurations_.push(configuration); | 73 permissionStatus.PermissionStatus.ASK || |
| 74 // Sort using descending order. | 74 !this.pendingPermissionRequest_) |
| 75 this.active_sensor_configurations_.sort( | 75 return; |
| 76 (first, second) => { return second.frequency - first.frequency }); | 76 |
| 77 | 77 this.pendingPermissionRequest_({status: this.permissionStatus_}); |
| 78 if (!this.start_should_fail_ ) | 78 this.permissionStatus_ = permissionStatus.PermissionStatus.ASK; |
| 79 this.startReading(); | 79 this.pendingPermissionRequest_ = null; |
| 80 | 80 } |
| 81 if (this.add_configuration_called_ != null) | 81 |
| 82 this.add_configuration_called_(this); | 82 setSensorPermission(allowed) { |
| 83 | 83 this.permissionStatus_ = allowed ? |
| 84 return sensorResponse(!this.start_should_fail_); | 84 permissionStatus.PermissionStatus.GRANTED : |
| 85 } | 85 permissionStatus.PermissionStatus.DENIED; |
| 86 | 86 this.runPermissionCallback_(); |
| 87 // Removes sensor configuration from the list of active configurations and | 87 } |
| 88 // stops notification about sensor reading changes if | 88 |
| 89 // active_sensor_configurations_ is empty. | 89 rejectPermissionConnections() { |
| 90 removeConfiguration(configuration) { | 90 this.rejectPermissionConnections_ = true; |
| 91 if (this.remove_configuration_called_ != null) { | 91 } |
| 92 this.remove_configuration_called_(this); | 92 |
| 93 } // End of MockPermissionService |
| 94 |
| 95 // Class that mocks Sensor interface defined in sensor.mojom |
| 96 class MockSensor { |
| 97 constructor(sensorRequest, handle, offset, size, reportingMode) { |
| 98 this.client_ = null; |
| 99 this.expects_modified_reading_ = false; |
| 100 this.start_should_fail_ = false; |
| 101 this.reporting_mode_ = reportingMode; |
| 102 this.sensor_reading_timer_id_ = null; |
| 103 this.update_reading_function_ = null; |
| 104 this.reading_updates_count_ = 0; |
| 105 this.suspend_called_ = null; |
| 106 this.resume_called_ = null; |
| 107 this.add_configuration_called_ = null; |
| 108 this.remove_configuration_called_ = null; |
| 109 this.active_sensor_configurations_ = []; |
| 110 let rv = |
| 111 core.mapBuffer(handle, offset, size, core.MAP_BUFFER_FLAG_NONE); |
| 112 assert_equals( |
| 113 rv.result, core.RESULT_OK, 'Failed to map shared buffer'); |
| 114 this.buffer_array_ = rv.buffer; |
| 115 this.buffer_ = new Float64Array(this.buffer_array_); |
| 116 this.resetBuffer(); |
| 117 this.binding_ = |
| 118 new bindings.Binding(sensor.Sensor, this, sensorRequest); |
| 119 this.binding_.setConnectionErrorHandler(() => { this.reset(); }); |
| 120 } |
| 121 |
| 122 // Returns default configuration. |
| 123 getDefaultConfiguration() { return Promise.resolve({frequency: 5}); } |
| 124 |
| 125 reading_updates_count() { return this.reading_updates_count_; } |
| 126 // Adds configuration for the sensor and starts reporting fake data |
| 127 // through update_reading_function_ callback. |
| 128 addConfiguration(configuration) { |
| 129 assert_not_equals( |
| 130 configuration, null, 'Invalid sensor configuration.'); |
| 131 |
| 132 this.active_sensor_configurations_.push(configuration); |
| 133 // Sort using descending order. |
| 134 this.active_sensor_configurations_.sort( |
| 135 (first, second) => {return second.frequency - first.frequency}); |
| 136 |
| 137 if (!this.start_should_fail_) |
| 138 this.startReading(); |
| 139 |
| 140 if (this.add_configuration_called_ != null) |
| 141 this.add_configuration_called_(this); |
| 142 |
| 143 return sensorResponse(!this.start_should_fail_); |
| 144 } |
| 145 |
| 146 // Removes sensor configuration from the list of active configurations |
| 147 // and |
| 148 // stops notification about sensor reading changes if |
| 149 // active_sensor_configurations_ is empty. |
| 150 removeConfiguration(configuration) { |
| 151 if (this.remove_configuration_called_ != null) { |
| 152 this.remove_configuration_called_(this); |
| 153 } |
| 154 |
| 155 let index = |
| 156 this.active_sensor_configurations_.indexOf(configuration); |
| 157 if (index !== -1) { |
| 158 this.active_sensor_configurations_.splice(index, 1); |
| 159 } else { |
| 160 return sensorResponse(false); |
| 161 } |
| 162 |
| 163 if (this.active_sensor_configurations_.length === 0) |
| 164 this.stopReading(); |
| 165 |
| 166 return sensorResponse(true); |
| 167 } |
| 168 |
| 169 // Suspends sensor. |
| 170 suspend() { |
| 171 this.stopReading(); |
| 172 if (this.suspend_called_ != null) { |
| 173 this.suspend_called_(this); |
| 174 } |
| 175 } |
| 176 |
| 177 // Resumes sensor. |
| 178 resume() { |
| 179 assert_equals(this.sensor_reading_timer_id_, null); |
| 180 this.startReading(); |
| 181 if (this.resume_called_ != null) { |
| 182 this.resume_called_(this); |
| 183 } |
| 184 } |
| 185 |
| 186 // Mock functions |
| 187 |
| 188 // Resets mock Sensor state. |
| 189 reset() { |
| 190 this.stopReading(); |
| 191 |
| 192 this.expects_modified_reading_ = false; |
| 193 this.reading_updates_count_ = 0; |
| 194 this.start_should_fail_ = false; |
| 195 this.update_reading_function_ = null; |
| 196 this.active_sensor_configurations_ = []; |
| 197 this.suspend_called_ = null; |
| 198 this.resume_called_ = null; |
| 199 this.add_configuration_called_ = null; |
| 200 this.remove_configuration_called_ = null; |
| 201 this.resetBuffer(); |
| 202 core.unmapBuffer(this.buffer_array_); |
| 203 this.buffer_array_ = null; |
| 204 this.binding_.close(); |
| 205 } |
| 206 |
| 207 // Zeroes shared buffer. |
| 208 resetBuffer() { |
| 209 for (let i = 0; i < this.buffer_.length; ++i) { |
| 210 this.buffer_[i] = 0; |
| 211 } |
| 212 } |
| 213 |
| 214 // Sets callback that is used to deliver sensor reading updates. |
| 215 setUpdateSensorReadingFunction(update_reading_function) { |
| 216 this.update_reading_function_ = update_reading_function; |
| 217 return Promise.resolve(this); |
| 218 } |
| 219 |
| 220 // Sets flag that forces sensor to fail when addConfiguration is |
| 221 // invoked. |
| 222 setStartShouldFail(should_fail) { |
| 223 this.start_should_fail_ = should_fail; |
| 224 } |
| 225 |
| 226 // Sets flags that asks for a modified reading values at each |
| 227 // iteration |
| 228 // to initiate 'onchange' event broadcasting. |
| 229 setExpectsModifiedReading(expects_modified_reading) { |
| 230 this.expects_modified_reading_ = expects_modified_reading; |
| 231 } |
| 232 |
| 233 // Returns resolved promise if suspend() was called, rejected |
| 234 // otherwise. |
| 235 suspendCalled() { |
| 236 return new Promise( |
| 237 (resolve, reject) => { this.suspend_called_ = resolve; }); |
| 238 } |
| 239 |
| 240 // Returns resolved promise if resume() was called, rejected |
| 241 // otherwise. |
| 242 resumeCalled() { |
| 243 return new Promise( |
| 244 (resolve, reject) => { this.resume_called_ = resolve; }); |
| 245 } |
| 246 |
| 247 // Resolves promise when addConfiguration() is called. |
| 248 addConfigurationCalled() { |
| 249 return new Promise((resolve, reject) => { |
| 250 this.add_configuration_called_ = resolve; |
| 251 }); |
| 252 } |
| 253 |
| 254 // Resolves promise when removeConfiguration() is called. |
| 255 removeConfigurationCalled() { |
| 256 return new Promise((resolve, reject) => { |
| 257 this.remove_configuration_called_ = resolve; |
| 258 }); |
| 259 } |
| 260 |
| 261 startReading() { |
| 262 if (this.update_reading_function_ != null) { |
| 263 this.stopReading(); |
| 264 let max_frequency_used = |
| 265 this.active_sensor_configurations_[0].frequency; |
| 266 let timeout = (1 / max_frequency_used) * 1000; |
| 267 this.sensor_reading_timer_id_ = window.setInterval(() => { |
| 268 if (this.update_reading_function_) { |
| 269 this.update_reading_function_( |
| 270 this.buffer_, this.expects_modified_reading_, |
| 271 this.reading_updates_count_); |
| 272 this.reading_updates_count_++; |
| 273 } |
| 274 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
| 275 this.client_.sensorReadingChanged(); |
| 276 } |
| 277 }, timeout); |
| 278 } |
| 279 } |
| 280 |
| 281 stopReading() { |
| 282 if (this.sensor_reading_timer_id_ != null) { |
| 283 window.clearInterval(this.sensor_reading_timer_id_); |
| 284 this.sensor_reading_timer_id_ = null; |
| 285 } |
| 286 } |
| 93 } | 287 } |
| 94 | 288 |
| 95 let index = this.active_sensor_configurations_.indexOf(configuration); | 289 // Helper function that returns resolved promise for getSensor() |
| 96 if (index !== -1) { | 290 // function. |
| 97 this.active_sensor_configurations_.splice(index, 1); | 291 function getSensorResponse(init_params, client_request) { |
| 98 } else { | 292 return Promise.resolve({init_params, client_request}); |
| 99 return sensorResponse(false); | |
| 100 } | 293 } |
| 101 | 294 |
| 102 if (this.active_sensor_configurations_.length === 0) | 295 // Class that mocks SensorProvider interface defined in |
| 103 this.stopReading(); | 296 // sensor_provider.mojom |
| 104 | 297 class MockSensorProvider { |
| 105 return sensorResponse(true); | 298 constructor() { |
| 106 } | 299 this.reading_size_in_bytes_ = |
| 107 | 300 sensor_provider.SensorInitParams.kReadBufferSizeForTests; |
| 108 // Suspends sensor. | 301 this.shared_buffer_size_in_bytes_ = |
| 109 suspend() { | 302 this.reading_size_in_bytes_ * sensor.SensorType.LAST; |
| 110 this.stopReading(); | 303 let rv = core.createSharedBuffer( |
| 111 if (this.suspend_called_ != null) { | 304 this.shared_buffer_size_in_bytes_, |
| 112 this.suspend_called_(this); | 305 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); |
| 113 } | 306 assert_equals(rv.result, core.RESULT_OK, 'Failed to create buffer'); |
| 114 } | 307 this.shared_buffer_handle_ = rv.handle; |
| 115 | 308 this.active_sensor_ = null; |
| 116 // Resumes sensor. | 309 this.get_sensor_should_fail_ = false; |
| 117 resume() { | 310 this.resolve_func_ = null; |
| 118 assert_equals(this.sensor_reading_timer_id_, null); | 311 this.is_continuous_ = false; |
| 119 this.startReading(); | 312 this.max_frequency_ = 60; |
| 120 if (this.resume_called_ != null) { | 313 this.binding_ = |
| 121 this.resume_called_(this); | 314 new bindings.Binding(sensor_provider.SensorProvider, this); |
| 122 } | 315 } |
| 123 } | 316 |
| 124 | 317 // Returns initialized Sensor proxy to the client. |
| 125 // Mock functions | 318 getSensor(type, request) { |
| 126 | 319 if (this.get_sensor_should_fail_) { |
| 127 // Resets mock Sensor state. | 320 var ignored = new sensor.SensorClientPtr(); |
| 128 reset() { | 321 return getSensorResponse(null, bindings.makeRequest(ignored)); |
| 129 this.stopReading(); | 322 } |
| 130 | 323 |
| 131 this.reading_updates_count_ = 0; | 324 let offset = |
| 132 this.start_should_fail_ = false; | |
| 133 this.update_reading_function_ = null; | |
| 134 this.active_sensor_configurations_ = []; | |
| 135 this.suspend_called_ = null; | |
| 136 this.resume_called_ = null; | |
| 137 this.add_configuration_called_ = null; | |
| 138 this.remove_configuration_called_ = null; | |
| 139 this.resetBuffer(); | |
| 140 core.unmapBuffer(this.buffer_array_); | |
| 141 this.buffer_array_ = null; | |
| 142 this.binding_.close(); | |
| 143 } | |
| 144 | |
| 145 // Zeroes shared buffer. | |
| 146 resetBuffer() { | |
| 147 for (let i = 0; i < this.buffer_.length; ++i) { | |
| 148 this.buffer_[i] = 0; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 // Sets callback that is used to deliver sensor reading updates. | |
| 153 setUpdateSensorReadingFunction(update_reading_function) { | |
| 154 this.update_reading_function_ = update_reading_function; | |
| 155 return Promise.resolve(this); | |
| 156 } | |
| 157 | |
| 158 // Sets flag that forces sensor to fail when addConfiguration is invoked. | |
| 159 setStartShouldFail(should_fail) { | |
| 160 this.start_should_fail_ = should_fail; | |
| 161 } | |
| 162 | |
| 163 // Returns resolved promise if suspend() was called, rejected otherwise. | |
| 164 suspendCalled() { | |
| 165 return new Promise((resolve, reject) => { | |
| 166 this.suspend_called_ = resolve; | |
| 167 }); | |
| 168 } | |
| 169 | |
| 170 // Returns resolved promise if resume() was called, rejected otherwise. | |
| 171 resumeCalled() { | |
| 172 return new Promise((resolve, reject) => { | |
| 173 this.resume_called_ = resolve; | |
| 174 }); | |
| 175 } | |
| 176 | |
| 177 // Resolves promise when addConfiguration() is called. | |
| 178 addConfigurationCalled() { | |
| 179 return new Promise((resolve, reject) => { | |
| 180 this.add_configuration_called_ = resolve; | |
| 181 }); | |
| 182 } | |
| 183 | |
| 184 // Resolves promise when removeConfiguration() is called. | |
| 185 removeConfigurationCalled() { | |
| 186 return new Promise((resolve, reject) => { | |
| 187 this.remove_configuration_called_ = resolve; | |
| 188 }); | |
| 189 } | |
| 190 | |
| 191 startReading() { | |
| 192 if (this.update_reading_function_ != null) { | |
| 193 this.stopReading(); | |
| 194 let max_frequency_used = | |
| 195 this.active_sensor_configurations_[0].frequency; | |
| 196 let timeout = (1 / max_frequency_used) * 1000; | |
| 197 this.sensor_reading_timer_id_ = window.setInterval(() => { | |
| 198 if (this.update_reading_function_) { | |
| 199 this.update_reading_function_(this.buffer_); | |
| 200 this.reading_updates_count_++; | |
| 201 } | |
| 202 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | |
| 203 this.client_.sensorReadingChanged(); | |
| 204 } | |
| 205 }, timeout); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 stopReading() { | |
| 210 if (this.sensor_reading_timer_id_ != null) { | |
| 211 window.clearInterval(this.sensor_reading_timer_id_); | |
| 212 this.sensor_reading_timer_id_ = null; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 } | |
| 217 | |
| 218 // Helper function that returns resolved promise for getSensor() function. | |
| 219 function getSensorResponse(init_params, client_request) { | |
| 220 return Promise.resolve({init_params, client_request}); | |
| 221 } | |
| 222 | |
| 223 // Class that mocks SensorProvider interface defined in | |
| 224 // sensor_provider.mojom | |
| 225 class MockSensorProvider { | |
| 226 constructor() { | |
| 227 this.reading_size_in_bytes_ = | |
| 228 sensor_provider.SensorInitParams.kReadBufferSizeForTests; | |
| 229 this.shared_buffer_size_in_bytes_ = this.reading_size_in_bytes_ * | |
| 230 sensor.SensorType.LAST; | |
| 231 let rv = | |
| 232 core.createSharedBuffer( | |
| 233 this.shared_buffer_size_in_bytes_, | |
| 234 core.CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE); | |
| 235 assert_equals(rv.result, core.RESULT_OK, "Failed to create buffer"); | |
| 236 this.shared_buffer_handle_ = rv.handle; | |
| 237 this.active_sensor_ = null; | |
| 238 this.get_sensor_should_fail_ = false; | |
| 239 this.resolve_func_ = null; | |
| 240 this.is_continuous_ = false; | |
| 241 this.max_frequency_ = 60; | |
| 242 this.min_frequency_ = 1; | |
| 243 this.binding_ = new bindings.Binding(sensor_provider.SensorProvider, | |
| 244 this); | |
| 245 } | |
| 246 | |
| 247 // Returns initialized Sensor proxy to the client. | |
| 248 getSensor(type, request) { | |
| 249 if (this.get_sensor_should_fail_) { | |
| 250 var ignored = new sensor.SensorClientPtr(); | |
| 251 return getSensorResponse(null, bindings.makeRequest(ignored)); | |
| 252 } | |
| 253 | |
| 254 let offset = | |
| 255 (sensor.SensorType.LAST - type) * this.reading_size_in_bytes_; | 325 (sensor.SensorType.LAST - type) * this.reading_size_in_bytes_; |
| 256 let reporting_mode = sensor.ReportingMode.ON_CHANGE; | 326 let reporting_mode = sensor.ReportingMode.ON_CHANGE; |
| 257 if (this.is_continuous_) { | 327 if (this.is_continuous_) { |
| 258 reporting_mode = sensor.ReportingMode.CONTINUOUS; | 328 reporting_mode = sensor.ReportingMode.CONTINUOUS; |
| 259 } | 329 } |
| 260 | 330 |
| 261 if (this.active_sensor_ == null) { | 331 if (this.active_sensor_ == null) { |
| 262 let mockSensor = new MockSensor(request, this.shared_buffer_handle_, | 332 let mockSensor = new MockSensor( |
| 263 offset, this.reading_size_in_bytes_, reporting_mode); | 333 request, this.shared_buffer_handle_, offset, |
| 264 this.active_sensor_ = mockSensor; | 334 this.reading_size_in_bytes_, reporting_mode); |
| 265 } | 335 this.active_sensor_ = mockSensor; |
| 266 | 336 } |
| 267 let rv = | 337 |
| 268 core.duplicateBufferHandle( | 338 let rv = core.duplicateBufferHandle( |
| 269 this.shared_buffer_handle_, | 339 this.shared_buffer_handle_, |
| 270 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); | 340 core.DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE); |
| 271 | 341 |
| 272 assert_equals(rv.result, core.RESULT_OK); | 342 assert_equals(rv.result, core.RESULT_OK); |
| 273 | 343 |
| 274 let default_config = {frequency: 5}; | 344 let default_config = {frequency: 5}; |
| 275 | 345 |
| 276 let init_params = | 346 let init_params = |
| 277 new sensor_provider.SensorInitParams( | 347 new sensor_provider.SensorInitParams( |
| 278 { memory: rv.handle, | 348 { memory: rv.handle, |
| 279 buffer_offset: offset, | 349 buffer_offset: offset, |
| 280 mode: reporting_mode, | 350 mode: reporting_mode, |
| 281 default_configuration: default_config, | 351 default_configuration: default_config, |
| 282 minimum_frequency: this.min_frequency_, | 352 minimum_frequency: this.min_frequency_, |
| 283 maximum_frequency: this.max_frequency_}); | 353 maximum_frequency: this.max_frequency_}); |
| 284 | 354 |
| 285 if (this.resolve_func_ !== null) { | 355 if (this.resolve_func_ !== null) { |
| 286 this.resolve_func_(this.active_sensor_); | 356 this.resolve_func_(this.active_sensor_); |
| 287 } | 357 } |
| 288 | 358 |
| 289 this.active_sensor_.client_ = new sensor.SensorClientPtr(); | 359 this.active_sensor_.client_ = new sensor.SensorClientPtr(); |
| 290 return getSensorResponse( | 360 return getSensorResponse( |
| 291 init_params, bindings.makeRequest(this.active_sensor_.client_)); | 361 init_params, bindings.makeRequest(this.active_sensor_.client_)); |
| 292 } | 362 } |
| 293 | 363 |
| 294 // Binds object to mojo message pipe | 364 // Binds object to mojo message pipe |
| 295 bindToPipe(pipe) { | 365 bindToPipe(pipe) { |
| 296 this.binding_.bind(pipe); | 366 this.binding_.bind(pipe); |
| 297 this.binding_.setConnectionErrorHandler(() => { | 367 this.binding_.setConnectionErrorHandler(() => { this.reset(); }); |
| 298 this.reset(); | 368 } |
| 299 }); | |
| 300 } | |
| 301 | 369 |
| 302 // Mock functions | 370 // Mock functions |
| 303 | 371 |
| 304 // Resets state of mock SensorProvider between test runs. | 372 // Resets state of mock SensorProvider between test runs. |
| 305 reset() { | 373 reset() { |
| 306 if (this.active_sensor_ != null) { | 374 if (this.active_sensor_ != null) { |
| 307 this.active_sensor_.reset(); | 375 this.active_sensor_.reset(); |
| 308 this.active_sensor_ = null; | 376 this.active_sensor_ = null; |
| 309 } | 377 } |
| 310 | 378 |
| 311 this.get_sensor_should_fail_ = false; | 379 this.get_sensor_should_fail_ = false; |
| 312 this.resolve_func_ = null; | 380 this.resolve_func_ = null; |
| 313 this.max_frequency_ = 60; | 381 this.max_frequency_ = 60; |
| 314 this.min_frequency_ = 1; | 382 this.is_continuous_ = false; |
| 315 this.is_continuous_ = false; | 383 this.min_frequency_ = 1; |
| 316 this.binding_.close(); | 384 this.binding_.close(); |
| 317 } | 385 } |
| 318 | 386 |
| 319 // Sets flag that forces mock SensorProvider to fail when getSensor() is | 387 // Sets flag that forces mock SensorProvider to fail when getSensor() |
| 320 // invoked. | 388 // is |
| 321 setGetSensorShouldFail(should_fail) { | 389 // invoked. |
| 322 this.get_sensor_should_fail_ = should_fail; | 390 setGetSensorShouldFail(should_fail) { |
| 323 } | 391 this.get_sensor_should_fail_ = should_fail; |
| 392 } |
| 324 | 393 |
| 325 // Returns mock sensor that was created in getSensor to the layout test. | 394 // Returns mock sensor that was created in getSensor to the layout |
| 326 getCreatedSensor() { | 395 // test. |
| 327 if (this.active_sensor_ != null) { | 396 getCreatedSensor() { |
| 328 return Promise.resolve(this.active_sensor_); | 397 if (this.active_sensor_ != null) { |
| 329 } | 398 return Promise.resolve(this.active_sensor_); |
| 399 } |
| 330 | 400 |
| 331 return new Promise((resolve, reject) => { | 401 return new Promise( |
| 332 this.resolve_func_ = resolve; | 402 (resolve, reject) => { this.resolve_func_ = resolve; }); |
| 333 }); | 403 } |
| 334 } | |
| 335 | 404 |
| 336 // Forces sensor to use |reporting_mode| as an update mode. | 405 // Forces sensor to use |reporting_mode| as an update mode. |
| 337 setContinuousReportingMode() { | 406 setContinuousReportingMode() { this.is_continuous_ = true; } |
| 338 this.is_continuous_ = true; | |
| 339 } | |
| 340 | 407 |
| 341 // Sets the maximum frequency for a concrete sensor. | 408 // Sets the maximum frequency for a concrete sensor. |
| 342 setMaximumSupportedFrequency(frequency) { | 409 setMaximumSupportedFrequency(frequency) { |
| 343 this.max_frequency_ = frequency; | 410 this.max_frequency_ = frequency; |
| 344 } | 411 } |
| 345 | 412 |
| 346 // Sets the minimum frequency for a concrete sensor. | 413 // Sets the minimum frequency for a concrete sensor. |
| 347 setMinimumSupportedFrequency(frequency) { | 414 setMinimumSupportedFrequency(frequency) { |
| 348 this.min_frequency_ = frequency; | 415 this.min_frequency_ = frequency; |
| 349 } | 416 } |
| 350 } | 417 } |
| 418 let mockSensorProvider = new MockSensorProvider; |
| 419 let mockPermissionService = new MockPermissionService; |
| 420 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| 421 sensor_provider.SensorProvider.name, |
| 422 pipe => { mockSensorProvider.bindToPipe(pipe); }); |
| 351 | 423 |
| 352 let mockSensorProvider = new MockSensorProvider; | 424 mojo.frameInterfaces.addInterfaceOverrideForTesting( |
| 353 mojo.frameInterfaces.addInterfaceOverrideForTesting( | 425 permission.PermissionService.name, |
| 354 sensor_provider.SensorProvider.name, | 426 pipe => { mockPermissionService.bindToPipe(pipe); }); |
| 355 pipe => { | 427 |
| 356 mockSensorProvider.bindToPipe(pipe); | 428 return Promise.resolve({ |
| 429 mockSensorProvider: mockSensorProvider, |
| 430 mockPermissionService: mockPermissionService, |
| 357 }); | 431 }); |
| 358 | 432 |
| 359 return Promise.resolve({ | 433 }); |
| 360 mockSensorProvider: mockSensorProvider, | |
| 361 }); | |
| 362 }); | |
| 363 } | 434 } |
| 364 | 435 |
| 365 function sensor_test(func, name, properties) { | 436 function sensor_test(func, name, properties) { |
| 366 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { | 437 mojo_test(mojo => sensor_mocks(mojo).then(sensor => { |
| 367 // Clean up and reset mock sensor stubs asynchronously, so that the blink | 438 // Clean up and reset mock sensor stubs asynchronously, so that the blink |
| 368 // side closes its proxies and notifies JS sensor objects before new test is | 439 // side closes its proxies and notifies JS sensor objects before new test is |
| 369 // started. | 440 // started. |
| 370 let onSuccess = () => { | 441 let onSuccess = () => { |
| 371 sensor.mockSensorProvider.reset(); | 442 sensor.mockSensorProvider.reset(); |
| 372 return new Promise((resolve, reject) => { setTimeout(resolve, 0); }); | 443 return new Promise((resolve, reject) => { setTimeout(resolve, 0); }); |
| 373 }; | 444 }; |
| 374 | 445 |
| 375 let onFailure = error => { | 446 let onFailure = error => { |
| 376 sensor.mockSensorProvider.reset(); | 447 sensor.mockSensorProvider.reset(); |
| 377 return new Promise((resolve, reject) => { setTimeout(() => {reject(error);
}, 0); }); | 448 return new Promise((resolve, reject) => { setTimeout(() => {reject(error);
}, 0); }); |
| 378 }; | 449 }; |
| 379 | 450 |
| 380 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 451 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
| 381 }), name, properties); | 452 }), name, properties); |
| 382 } | 453 } |
| OLD | NEW |