| OLD | NEW |
| (Empty) |
| 1 'use strict'; | |
| 2 | |
| 3 function usbMocks(mojo) { | |
| 4 return define('USB Mocks', [ | |
| 5 'device/usb/public/interfaces/device_manager.mojom', | |
| 6 'device/usb/public/interfaces/device.mojom', | |
| 7 'device/usb/public/interfaces/permission_provider.mojom', | |
| 8 'content/public/renderer/service_provider', | |
| 9 ], (deviceManager, device, permissionProvider) => { | |
| 10 function assertDeviceInfoEquals(device, info) { | |
| 11 assert_equals(device.guid, info.guid); | |
| 12 assert_equals(device.usbVersionMajor, info.usb_version_major); | |
| 13 assert_equals(device.usbVersionMinor, info.usb_version_minor); | |
| 14 assert_equals(device.usbVersionSubminor, info.usb_version_subminor); | |
| 15 assert_equals(device.deviceClass, info.class_code); | |
| 16 assert_equals(device.deviceSubclass, info.subclass_code); | |
| 17 assert_equals(device.deviceProtocol, info.protocol_code); | |
| 18 assert_equals(device.vendorId, info.vendor_id); | |
| 19 assert_equals(device.productId, info.product_id); | |
| 20 assert_equals(device.deviceVersionMajor, info.device_version_major); | |
| 21 assert_equals(device.deviceVersionMinor, info.device_version_minor); | |
| 22 assert_equals(device.deviceVersionSubminor, | |
| 23 info.device_version_subminor); | |
| 24 assert_equals(device.manufacturerName, info.manufacturer_name); | |
| 25 assert_equals(device.productName, info.product_name); | |
| 26 assert_equals(device.serialNumber, info.serial_number); | |
| 27 assert_equals(device.configurations.length, | |
| 28 info.configurations.length); | |
| 29 for (var i = 0; i < device.configurations.length; ++i) { | |
| 30 assertConfigurationInfoEquals(device.configurations[i], | |
| 31 info.configurations[i]); | |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 function assertConfigurationInfoEquals(configuration, info) { | |
| 36 assert_equals(configuration.configurationValue, | |
| 37 info.configuration_value); | |
| 38 assert_equals(configuration.configurationName, | |
| 39 info.configuration_name); | |
| 40 assert_equals(configuration.interfaces.length, | |
| 41 info.interfaces.length); | |
| 42 for (var i = 0; i < configuration.interfaces.length; ++i) { | |
| 43 assertInterfaceInfoEquals(configuration.interfaces[i], | |
| 44 info.interfaces[i]); | |
| 45 } | |
| 46 }; | |
| 47 | |
| 48 function assertInterfaceInfoEquals(iface, info) { | |
| 49 assert_equals(iface.interfaceNumber, info.interface_number); | |
| 50 assert_equals(iface.alternates.length, info.alternates.length); | |
| 51 for (var i = 0; i < iface.alternates.length; ++i) { | |
| 52 assertAlternateInfoEquals(iface.alternates[i], info.alternates[i]); | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 function assertAlternateInfoEquals(alternate, info) { | |
| 57 assert_equals(alternate.alternateSetting, info.alternate_setting); | |
| 58 assert_equals(alternate.interfaceClass, info.class_code); | |
| 59 assert_equals(alternate.interfaceSubclass, info.subclass_code); | |
| 60 assert_equals(alternate.interfaceProtocol, info.protocol_code); | |
| 61 assert_equals(alternate.interfaceName, info.interface_name); | |
| 62 assert_equals(alternate.endpoints.length, info.endpoints.length); | |
| 63 for (var i = 0; i < alternate.endpoints.length; ++i) { | |
| 64 assertEndpointInfoEquals(alternate.endpoints[i], info.endpoints[i]); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 function assertEndpointInfoEquals(endpoint, info) { | |
| 69 var direction; | |
| 70 switch (info.direction) { | |
| 71 case device.TransferDirection.INBOUND: | |
| 72 direction = "in"; | |
| 73 break; | |
| 74 case device.TransferDirection.OUTBOUND: | |
| 75 direction = "out"; | |
| 76 break; | |
| 77 } | |
| 78 | |
| 79 var type; | |
| 80 switch (info.type) { | |
| 81 case device.EndpointType.BULK: | |
| 82 type = "bulk"; | |
| 83 break; | |
| 84 case device.EndpointType.INTERRUPT: | |
| 85 type = "interrupt"; | |
| 86 break; | |
| 87 case device.EndpointType.ISOCHRONOUS: | |
| 88 type = "isochronous"; | |
| 89 break; | |
| 90 } | |
| 91 | |
| 92 assert_equals(endpoint.endpointNumber, info.endpoint_number); | |
| 93 assert_equals(endpoint.direction, direction); | |
| 94 assert_equals(endpoint.type, type); | |
| 95 assert_equals(endpoint.packetSize, info.packet_size); | |
| 96 } | |
| 97 | |
| 98 class MockDevice extends device.Device.stubClass { | |
| 99 constructor(info, pipe) { | |
| 100 super(); | |
| 101 this.info_ = info; | |
| 102 this.pipe_ = pipe; | |
| 103 this.router_ = new mojo.router.Router(pipe); | |
| 104 this.router_.setIncomingReceiver(this); | |
| 105 this.opened_ = false; | |
| 106 this.currentConfiguration_ = undefined; | |
| 107 this.claimedInterfaces_ = new Map(); | |
| 108 } | |
| 109 | |
| 110 getDeviceInfo() { | |
| 111 return Promise.resolve({ info: this.info_ }); | |
| 112 } | |
| 113 | |
| 114 getConfiguration() { | |
| 115 if (this.currentConfiguration_ === undefined) { | |
| 116 return Promise.resolve({ value: 0 }); | |
| 117 } else { | |
| 118 return Promise.resolve({ | |
| 119 value: this.currentConfiguration_.configuration_value }); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 open() { | |
| 124 // TODO(reillyg): Check if the device is opened and return | |
| 125 // OpenDeviceError.ALREADY_OPEN. | |
| 126 this.opened_ = true; | |
| 127 return Promise.resolve({ error: device.OpenDeviceError.OK }); | |
| 128 } | |
| 129 | |
| 130 close() { | |
| 131 this.opened_ = false; | |
| 132 return Promise.resolve({}); | |
| 133 } | |
| 134 | |
| 135 setConfiguration(value) { | |
| 136 if (!this.opened_) | |
| 137 return Promise.resolve({ success: false }); | |
| 138 | |
| 139 let selected_configuration = this.info_.configurations.find( | |
| 140 configuration => configuration.configuration_value == value); | |
| 141 if (selected_configuration !== undefined) { | |
| 142 this.currentConfiguration_ = selected_configuration; | |
| 143 return Promise.resolve({ success: true }); | |
| 144 } else { | |
| 145 return Promise.resolve({ success: false }); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 claimInterface(interfaceNumber) { | |
| 150 if (!this.opened_) | |
| 151 return Promise.resolve({ success: false }); | |
| 152 | |
| 153 if (this.currentConfiguration_ === undefined) | |
| 154 return Promise.resolve({ success: false }); | |
| 155 | |
| 156 if (this.claimedInterfaces_.has(interfaceNumber)) | |
| 157 return Promise.resolve({ success: false }); | |
| 158 | |
| 159 if (this.currentConfiguration_.interfaces.some( | |
| 160 iface => iface.interface_number == interfaceNumber)) { | |
| 161 this.claimedInterfaces_.set(interfaceNumber, 0); | |
| 162 return Promise.resolve({ success: true }); | |
| 163 } else { | |
| 164 return Promise.resolve({ success: false }); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 releaseInterface(interfaceNumber) { | |
| 169 if (!this.opened_) | |
| 170 return Promise.resolve({ success: false }); | |
| 171 | |
| 172 if (this.currentConfiguration_ === undefined) | |
| 173 return Promise.resolve({ success: false }); | |
| 174 | |
| 175 if (this.claimedInterfaces_.has(interfaceNumber)) { | |
| 176 this.claimedInterfaces_.delete(interfaceNumber); | |
| 177 return Promise.resolve({ success: true }); | |
| 178 } else { | |
| 179 return Promise.resolve({ success: false }); | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 setInterfaceAlternateSetting(interfaceNumber, alternateSetting) { | |
| 184 if (!this.opened_ || this.currentConfiguration_ === undefined) | |
| 185 return Promise.resolve({ success: false }); | |
| 186 | |
| 187 if (!this.claimedInterfaces_.has(interfaceNumber)) | |
| 188 return Promise.resolve({ success: false }); | |
| 189 | |
| 190 let iface = this.currentConfiguration_.interfaces.find( | |
| 191 iface => iface.interface_number == interfaceNumber); | |
| 192 if (iface === undefined) | |
| 193 return Promise.resolve({ success: false }); | |
| 194 | |
| 195 if (iface.alternates.some( | |
| 196 x => x.alternate_setting == alternateSetting)) { | |
| 197 this.claimedInterfaces_.set(interfaceNumber, alternateSetting); | |
| 198 return Promise.resolve({ success: true }); | |
| 199 } else { | |
| 200 return Promise.resolve({ success: false }); | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 reset() { throw 'Not implemented!'; } | |
| 205 clearHalt(endpoint) { throw 'Not implemented!'; } | |
| 206 | |
| 207 controlTransferIn(params, length, timeout) { | |
| 208 return Promise.resolve({ | |
| 209 status: device.TransferStatus.OK, | |
| 210 data: [length >> 8, length & 0xff, params.request, params.value >> 8, | |
| 211 params.value & 0xff, params.index >> 8, params.index & 0xff] | |
| 212 }); | |
| 213 } | |
| 214 | |
| 215 controlTransferOut(params, data, timeout) { | |
| 216 throw 'Not implemented!'; | |
| 217 } | |
| 218 genericTransferIn(endpointNumber, length, timeout) { | |
| 219 throw 'Not implemented!'; | |
| 220 } | |
| 221 genericTransferOut(endpointNumber, data, timeout) { | |
| 222 throw 'Not implemented!'; | |
| 223 } | |
| 224 isochronousTransferIn(endpointNumber, numPackets, packetLength, | |
| 225 timeout) { | |
| 226 throw 'Not implemented!'; | |
| 227 } | |
| 228 isochronousTransferOut(endpointNumber, packets, timeout) { | |
| 229 throw 'Not implemented!'; | |
| 230 } | |
| 231 }; | |
| 232 | |
| 233 class MockDeviceManager extends deviceManager.DeviceManager.stubClass { | |
| 234 constructor() { | |
| 235 super(); | |
| 236 this.router_ = null; | |
| 237 this.mockDevices_ = new Map(); | |
| 238 this.addedDevices_ = []; | |
| 239 this.removedDevices_ = []; | |
| 240 this.deviceChangePromiseResolvers_ = []; | |
| 241 } | |
| 242 | |
| 243 reset() { | |
| 244 this.mockDevices_.forEach(device => { | |
| 245 for (var handle of device.handles) | |
| 246 mojo.core.close(handle.pipe_); | |
| 247 this.removedDevices_.push(device.info); | |
| 248 }); | |
| 249 this.mockDevices_.clear(); | |
| 250 this.maybeResolveDeviceChangePromise(); | |
| 251 } | |
| 252 | |
| 253 addMockDevice(info) { | |
| 254 let device = { | |
| 255 info: info, | |
| 256 handles: [] | |
| 257 }; | |
| 258 this.mockDevices_.set(info.guid, device); | |
| 259 this.addedDevices_.push(info); | |
| 260 this.maybeResolveDeviceChangePromise(); | |
| 261 } | |
| 262 | |
| 263 removeMockDevice(info) { | |
| 264 let device = this.mockDevices_.get(info.guid); | |
| 265 for (var handle of device.handles) | |
| 266 mojo.core.close(handle.pipe_); | |
| 267 this.mockDevices_.delete(info.guid); | |
| 268 this.removedDevices_.push(info); | |
| 269 this.maybeResolveDeviceChangePromise(); | |
| 270 } | |
| 271 | |
| 272 bindToPipe(pipe) { | |
| 273 assert_equals(this.router_, null); | |
| 274 this.router_ = new mojo.router.Router(pipe); | |
| 275 this.router_.setIncomingReceiver(this); | |
| 276 } | |
| 277 | |
| 278 getDevices(options) { | |
| 279 let devices = []; | |
| 280 this.mockDevices_.forEach(device => { | |
| 281 devices.push(device.info); | |
| 282 }); | |
| 283 return Promise.resolve({ results: devices }); | |
| 284 } | |
| 285 | |
| 286 getDeviceChanges() { | |
| 287 let promise = new Promise((resolve, reject) => { | |
| 288 this.deviceChangePromiseResolvers_.push(resolve); | |
| 289 }); | |
| 290 this.maybeResolveDeviceChangePromise(); | |
| 291 return promise; | |
| 292 } | |
| 293 | |
| 294 maybeResolveDeviceChangePromise() { | |
| 295 if (this.addedDevices_.length == 0 && | |
| 296 this.removedDevices_.length == 0) { | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 let resolve = this.deviceChangePromiseResolvers_.shift(); | |
| 301 if (resolve === undefined) | |
| 302 return; | |
| 303 | |
| 304 resolve({ | |
| 305 changes: { | |
| 306 devices_added: this.addedDevices_, | |
| 307 devices_removed: this.removedDevices_ | |
| 308 } | |
| 309 }); | |
| 310 this.addedDevices_ = []; | |
| 311 this.removedDevices_ = []; | |
| 312 } | |
| 313 | |
| 314 getDevice(guid, pipe) { | |
| 315 let device = this.mockDevices_.get(guid); | |
| 316 if (device !== undefined) { | |
| 317 var mock = new MockDevice(device.info, pipe); | |
| 318 device.handles.push(mock); | |
| 319 } | |
| 320 } | |
| 321 } | |
| 322 | |
| 323 let mockDeviceManager = new MockDeviceManager; | |
| 324 mojo.serviceRegistry.addServiceOverrideForTesting( | |
| 325 deviceManager.DeviceManager.name, | |
| 326 pipe => { | |
| 327 mockDeviceManager.bindToPipe(pipe); | |
| 328 }); | |
| 329 | |
| 330 mojo.serviceRegistry.addServiceOverrideForTesting( | |
| 331 permissionProvider.PermissionProvider.name, | |
| 332 pipe => { | |
| 333 console.log('Connected to PermissionProvider!'); | |
| 334 }); | |
| 335 | |
| 336 return fakeUsbDevices().then(fakeDevices => Promise.resolve({ | |
| 337 DeviceManager: deviceManager.DeviceManager, | |
| 338 Device: device.Device, | |
| 339 PermissionProvider: permissionProvider.PermissionProvider, | |
| 340 mockDeviceManager: mockDeviceManager, | |
| 341 fakeDevices: fakeDevices, | |
| 342 assertDeviceInfoEquals: assertDeviceInfoEquals, | |
| 343 assertConfigurationInfoEquals: assertConfigurationInfoEquals, | |
| 344 })); | |
| 345 }); | |
| 346 } | |
| 347 | |
| 348 function usb_test(func, name, properties) { | |
| 349 mojo_test(mojo => usbMocks(mojo).then(usb => { | |
| 350 let result = Promise.resolve(func(usb)); | |
| 351 let cleanUp = () => usb.mockDeviceManager.reset(); | |
| 352 return result.then(cleanUp, cleanUp); | |
| 353 }), name, properties); | |
| 354 } | |
| OLD | NEW |