Chromium Code Reviews| Index: third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| diff --git a/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js b/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d76790e9a309edcf6e417ee1c49925116719ff12 |
| --- /dev/null |
| +++ b/third_party/WebKit/LayoutTests/usb/resources/usb-helpers.js |
| @@ -0,0 +1,369 @@ |
| +'use strict'; |
| + |
| +const usbMocks = new Promise(resolve => { |
|
Ken Rockot(use gerrit already)
2016/02/23 23:12:49
How about making usbMocks a function which takes a
Reilly Grant (use Gerrit)
2016/02/24 20:18:47
Done.
|
| + define('USB Mocks', [ |
| + 'device/usb/public/interfaces/device_manager.mojom', |
| + 'device/usb/public/interfaces/device.mojom', |
| + 'device/usb/public/interfaces/permission_provider.mojom', |
| + 'content/public/renderer/service_provider', |
|
Ken Rockot(use gerrit already)
2016/02/23 23:12:48
nit: I suppose we should keep these sorted
Reilly Grant (use Gerrit)
2016/02/24 20:18:47
No longer an issue now that it takes mojo as a par
|
| + 'mojo/public/js/core', |
| + 'mojo/public/js/router', |
| + ], (deviceManager, device, permissionProvider, serviceRegistry, core, |
| + router) => { |
| + function assertDeviceInfoEquals(device, info) { |
| + assert_equals(device.guid, info.guid); |
| + assert_equals(device.usbVersionMajor, info.usb_version_major); |
| + assert_equals(device.usbVersionMinor, info.usb_version_minor); |
| + assert_equals(device.usbVersionSubminor, info.usb_version_subminor); |
| + assert_equals(device.deviceClass, info.class_code); |
| + assert_equals(device.deviceSubclass, info.subclass_code); |
| + assert_equals(device.deviceProtocol, info.protocol_code); |
| + assert_equals(device.vendorId, info.vendor_id); |
| + assert_equals(device.productId, info.product_id); |
| + assert_equals(device.deviceVersionMajor, info.device_version_major); |
| + assert_equals(device.deviceVersionMinor, info.device_version_minor); |
| + assert_equals(device.deviceVersionSubminor, |
| + info.device_version_subminor); |
| + assert_equals(device.manufacturerName, info.manufacturer_name); |
| + assert_equals(device.productName, info.product_name); |
| + assert_equals(device.serialNumber, info.serial_number); |
| + assert_equals(device.configurations.length, |
| + info.configurations.length); |
| + for (var i = 0; i < device.configurations.length; ++i) { |
| + assertConfigurationInfoEquals(device.configurations[i], |
| + info.configurations[i]); |
| + } |
| + }; |
| + |
| + function assertConfigurationInfoEquals(configuration, info) { |
| + assert_equals(configuration.configurationValue, |
| + info.configuration_value); |
| + assert_equals(configuration.configurationName, |
| + info.configuration_name); |
| + assert_equals(configuration.interfaces.length, |
| + info.interfaces.length); |
| + for (var i = 0; i < configuration.interfaces.length; ++i) { |
| + assertInterfaceInfoEquals(configuration.interfaces[i], |
| + info.interfaces[i]); |
| + } |
| + }; |
| + |
| + function assertInterfaceInfoEquals(iface, info) { |
| + assert_equals(iface.interfaceNumber, info.interface_number); |
| + assert_equals(iface.alternates.length, info.alternates.length); |
| + for (var i = 0; i < iface.alternates.length; ++i) { |
| + assertAlternateInfoEquals(iface.alternates[i], info.alternates[i]); |
| + } |
| + }; |
| + |
| + function assertAlternateInfoEquals(alternate, info) { |
| + assert_equals(alternate.alternateSetting, info.alternate_setting); |
| + assert_equals(alternate.interfaceClass, info.class_code); |
| + assert_equals(alternate.interfaceSubclass, info.subclass_code); |
| + assert_equals(alternate.interfaceProtocol, info.protocol_code); |
| + assert_equals(alternate.interfaceName, info.interface_name); |
| + assert_equals(alternate.endpoints.length, info.endpoints.length); |
| + for (var i = 0; i < alternate.endpoints.length; ++i) { |
| + assertEndpointInfoEquals(alternate.endpoints[i], info.endpoints[i]); |
| + } |
| + } |
| + |
| + function assertEndpointInfoEquals(endpoint, info) { |
| + var direction; |
| + switch (info.direction) { |
| + case device.TransferDirection.INBOUND: |
| + direction = "in"; |
| + break; |
| + case device.TransferDirection.OUTBOUND: |
| + direction = "out"; |
| + break; |
| + } |
| + |
| + var type; |
| + switch (info.type) { |
| + case device.EndpointType.BULK: |
| + type = "bulk"; |
| + break; |
| + case device.EndpointType.INTERRUPT: |
| + type = "interrupt"; |
| + break; |
| + case device.EndpointType.ISOCHRONOUS: |
| + type = "isochronous"; |
| + break; |
| + } |
| + |
| + assert_equals(endpoint.endpointNumber, info.endpoint_number); |
| + assert_equals(endpoint.direction, direction); |
| + assert_equals(endpoint.type, type); |
| + assert_equals(endpoint.packetSize, info.packet_size); |
| + } |
| + |
| + class MockDevice extends device.Device.stubClass { |
| + constructor(info, pipe) { |
| + super(); |
| + this.info_ = info; |
| + this.pipe_ = pipe; |
| + this.router_ = new router.Router(pipe); |
| + this.router_.setIncomingReceiver(this); |
| + this.opened_ = false; |
| + this.currentConfiguration_ = undefined; |
| + this.claimedInterfaces_ = new Map(); |
| + } |
| + |
| + getDeviceInfo() { |
| + return Promise.resolve({ info: this.info_ }); |
| + } |
| + |
| + getConfiguration() { |
| + if (this.currentConfiguration_ === undefined) { |
| + return Promise.resolve({ value: 0 }); |
| + } else { |
| + return Promise.resolve({ |
| + value: this.currentConfiguration_.configuration_value }); |
| + } |
| + } |
| + |
| + open() { |
| + // TODO(reillyg): Check if the device is opened and return |
| + // OpenDeviceError.ALREADY_OPEN. |
| + this.opened_ = true; |
| + return Promise.resolve({ error: device.OpenDeviceError.OK }); |
| + } |
| + |
| + close() { |
| + this.opened_ = false; |
| + return Promise.resolve({}); |
| + } |
| + |
| + setConfiguration(value) { |
| + if (!this.opened_) |
| + return Promise.resolve({ success: false }); |
| + |
| + let selected_configuration = this.info_.configurations.find( |
| + configuration => configuration.configuration_value == value); |
| + if (selected_configuration !== undefined) { |
| + this.currentConfiguration_ = selected_configuration; |
| + return Promise.resolve({ success: true }); |
| + } else { |
| + return Promise.resolve({ success: false }); |
| + } |
| + } |
| + |
| + claimInterface(interfaceNumber) { |
| + if (!this.opened_) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (this.currentConfiguration_ === undefined) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (this.claimedInterfaces_.has(interfaceNumber)) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (this.currentConfiguration_.interfaces.some( |
| + iface => iface.interface_number == interfaceNumber)) { |
| + this.claimedInterfaces_.set(interfaceNumber, 0); |
| + return Promise.resolve({ success: true }); |
| + } else { |
| + return Promise.resolve({ success: false }); |
| + } |
| + } |
| + |
| + releaseInterface(interfaceNumber) { |
| + if (!this.opened_) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (this.currentConfiguration_ === undefined) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (this.claimedInterfaces_.has(interfaceNumber)) { |
| + this.claimedInterfaces_.delete(interfaceNumber); |
| + return Promise.resolve({ success: true }); |
| + } else { |
| + return Promise.resolve({ success: false }); |
| + } |
| + } |
| + |
| + setInterfaceAlternateSetting(interfaceNumber, alternateSetting) { |
| + if (!this.opened_ || this.currentConfiguration_ === undefined) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (!this.claimedInterfaces_.has(interfaceNumber)) |
| + return Promise.resolve({ success: false }); |
| + |
| + let iface = this.currentConfiguration_.interfaces.find( |
| + iface => iface.interface_number == interfaceNumber); |
| + if (iface === undefined) |
| + return Promise.resolve({ success: false }); |
| + |
| + if (iface.alternates.some( |
| + x => x.alternate_setting == alternateSetting)) { |
| + this.claimedInterfaces_.set(interfaceNumber, alternateSetting); |
| + return Promise.resolve({ success: true }); |
| + } else { |
| + return Promise.resolve({ success: false }); |
| + } |
| + } |
| + |
| + reset() { throw 'Not implemented!'; } |
| + clearHalt(endpoint) { throw 'Not implemented!'; } |
| + |
| + controlTransferIn(params, length, timeout) { |
| + return Promise.resolve({ |
| + status: device.TransferStatus.OK, |
| + data: [length >> 8, length & 0xff, params.request, params.value >> 8, |
| + params.value & 0xff, params.index >> 8, params.index & 0xff] |
| + }); |
| + } |
| + |
| + controlTransferOut(params, data, timeout) { |
| + throw 'Not implemented!'; |
| + } |
| + genericTransferIn(endpointNumber, length, timeout) { |
| + throw 'Not implemented!'; |
| + } |
| + genericTransferOut(endpointNumber, data, timeout) { |
| + throw 'Not implemented!'; |
| + } |
| + isochronousTransferIn(endpointNumber, numPackets, packetLength, |
| + timeout) { |
| + throw 'Not implemented!'; |
| + } |
| + isochronousTransferOut(endpointNumber, packets, timeout) { |
| + throw 'Not implemented!'; |
| + } |
| + }; |
| + |
| + class MockDeviceManager extends deviceManager.DeviceManager.stubClass { |
| + constructor() { |
| + super(); |
| + this.router_ = null; |
| + this.mockDevices_ = new Map(); |
| + this.addedDevices_ = []; |
| + this.removedDevices_ = []; |
| + this.deviceChangePromiseResolvers_ = []; |
| + } |
| + |
| + reset() { |
| + this.mockDevices_.forEach(device => { |
| + for (var handle of device.handles) |
| + core.close(handle.pipe_); |
| + this.removedDevices_.push(device.info); |
| + }); |
| + this.mockDevices_.clear(); |
| + this.maybeResolveDeviceChangePromise(); |
| + } |
| + |
| + addMockDevice(info) { |
| + let device = { |
| + info: info, |
| + handles: [] |
| + }; |
| + this.mockDevices_.set(info.guid, device); |
| + this.addedDevices_.push(info); |
| + this.maybeResolveDeviceChangePromise(); |
| + } |
| + |
| + removeMockDevice(info) { |
| + let device = this.mockDevices_.get(info.guid); |
| + for (var handle of device.handles) |
| + core.close(handle.pipe_); |
| + this.mockDevices_.delete(info.guid); |
| + this.removedDevices_.push(info); |
| + this.maybeResolveDeviceChangePromise(); |
| + } |
| + |
| + bindToPipe(pipe) { |
| + assert_equals(this.router_, null); |
| + this.router_ = new router.Router(pipe); |
| + this.router_.setIncomingReceiver(this); |
| + } |
| + |
| + getDevices(options) { |
| + let devices = []; |
| + this.mockDevices_.forEach(device => { |
| + devices.push(device.info); |
| + }); |
| + return Promise.resolve({ results: devices }); |
| + } |
| + |
| + getDeviceChanges() { |
| + let promise = new Promise((resolve, reject) => { |
| + this.deviceChangePromiseResolvers_.push(resolve); |
| + }); |
| + this.maybeResolveDeviceChangePromise(); |
| + return promise; |
| + } |
| + |
| + maybeResolveDeviceChangePromise() { |
| + if (this.addedDevices_.length == 0 && |
| + this.removedDevices_.length == 0) { |
| + return; |
| + } |
| + |
| + let resolve = this.deviceChangePromiseResolvers_.shift(); |
| + if (resolve === undefined) |
| + return; |
| + |
| + resolve({ |
| + changes: { |
| + devices_added: this.addedDevices_, |
| + devices_removed: this.removedDevices_ |
| + } |
| + }); |
| + this.addedDevices_ = []; |
| + this.removedDevices_ = []; |
| + } |
| + |
| + getDevice(guid, pipe) { |
| + let device = this.mockDevices_.get(guid); |
| + if (device !== undefined) { |
| + var mock = new MockDevice(device.info, pipe); |
| + device.handles.push(mock); |
| + } |
| + } |
| + } |
| + |
| + let mockDeviceManager = new MockDeviceManager; |
| + |
| + serviceRegistry.addServiceOverrideForTesting( |
| + deviceManager.DeviceManager.name, |
| + pipe => { |
| + mockDeviceManager.bindToPipe(pipe); |
| + }); |
| + |
| + serviceRegistry.addServiceOverrideForTesting( |
| + permissionProvider.PermissionProvider.name, |
| + pipe => { |
| + console.log('Connected to PermissionProvider!'); |
| + }); |
| + |
| + fakeUsbDevices.then(fakeDevices => { |
| + resolve({ |
| + DeviceManager: deviceManager.DeviceManager, |
| + Device: device.Device, |
| + PermissionProvider: permissionProvider.PermissionProvider, |
| + mockDeviceManager: mockDeviceManager, |
| + fakeDevices: fakeDevices, |
| + assertDeviceInfoEquals: assertDeviceInfoEquals, |
| + assertConfigurationInfoEquals: assertConfigurationInfoEquals, |
| + }); |
| + }); |
| + }); |
| +}); |
| + |
| +function usb_test(func, name, properties) { |
| + mojo_test(mojo => { |
| + usbMocks.then(usb => { |
|
Ken Rockot(use gerrit already)
2016/02/23 23:12:48
and then you can just call:
usbMocks(mojo).then(u
Reilly Grant (use Gerrit)
2016/02/24 20:18:47
Done.
|
| + let result = new Promise((resolve, reject) => { |
| + try { |
| + resolve(func(usb)); |
| + } catch (e) { |
| + reject(e); |
| + } |
| + }); |
| + let cleanUp = () => usb.mockDeviceManager.reset(); |
| + result.then(cleanUp, cleanUp); |
| + return result; |
|
Ken Rockot(use gerrit already)
2016/02/23 23:12:49
nit: You can return result.then(cleanUp, cleanUp)
Reilly Grant (use Gerrit)
2016/02/24 20:18:47
Done.
|
| + }); |
| + }, name, properties); |
| +} |