OLD | NEW |
(Empty) | |
| 1 'use strict'; |
| 2 |
| 3 function usb_test(func, name, properties) { |
| 4 mojo_test(mojo => { |
| 5 return new Promise((resolve, reject) => { |
| 6 define('USB layout test: ' + name, [ |
| 7 'device/devices_app/usb/public/interfaces/device_manager.mojom', |
| 8 'device/devices_app/usb/public/interfaces/device.mojom', |
| 9 'device/devices_app/usb/public/interfaces/permission_provider.mojom' |
| 10 ], (deviceManager, device, permissionProvider) => { |
| 11 class MockDevice extends device.Device.stubClass { |
| 12 constructor(info, pipe) { |
| 13 super(); |
| 14 this.info_ = info; |
| 15 this.router_ = new mojo.router.Router(pipe); |
| 16 this.router_.setIncomingReceiver(this); |
| 17 } |
| 18 |
| 19 getDeviceInfo() { return { info: this.info_ }; } |
| 20 getConfiguration() { throw 'Not implemented!'; } |
| 21 open() { throw 'Not implemented!'; } |
| 22 close() { throw 'Not implemented!'; } |
| 23 setConfiguration(value) { throw 'Not implemented!'; } |
| 24 claimInterface(interfaceNumber) { throw 'Not implemented!'; } |
| 25 releaseInterface(interfaceNumber) { throw 'Not implemented!'; } |
| 26 setInterfaceAlternateSetting(interfaceNumber, alternateSetting) { |
| 27 throw 'Not implemented!'; |
| 28 } |
| 29 reset() { throw 'Not implemented!'; } |
| 30 clearHalt(endpoint) { throw 'Not implemented!'; } |
| 31 controlTransferIn(params, length, timeout) { |
| 32 throw 'Not implemented!'; |
| 33 } |
| 34 controlTransferOut(params, data, timeout) { |
| 35 throw 'Not implemented!'; |
| 36 } |
| 37 genericTransferIn(endpointNumber, length, timeout) { |
| 38 throw 'Not implemented!'; |
| 39 } |
| 40 genericTransferOut(endpointNumber, data, timeout) { |
| 41 throw 'Not implemented!'; |
| 42 } |
| 43 isochronousTransferIn(endpointNumber, numPackets, packetLength, |
| 44 timeout) { |
| 45 throw 'Not implemented!'; |
| 46 } |
| 47 isochronousTransferOut(endpointNumber, packets, timeout) { |
| 48 throw 'Not implemented!'; |
| 49 } |
| 50 }; |
| 51 |
| 52 class MockDeviceManager extends deviceManager.DeviceManager.stubClass { |
| 53 constructor() { |
| 54 super(); |
| 55 this.router_ = null; |
| 56 this.lastSeenDevices_ = {}; |
| 57 this.mockDevices_ = {}; |
| 58 } |
| 59 |
| 60 addMockDevice(info) { |
| 61 this.mockDevices_[info.guid] = info; |
| 62 } |
| 63 |
| 64 bindToPipe(pipe) { |
| 65 assert_equals(this.router_, null); |
| 66 this.router_ = new mojo.router.Router(pipe); |
| 67 this.router_.setIncomingReceiver(this); |
| 68 } |
| 69 |
| 70 getDevices(options) { |
| 71 return Promise.resolve({ |
| 72 results: Object.keys(this.mockDevices_).map( |
| 73 guid => this.mockDevices_[guid]), |
| 74 }); |
| 75 } |
| 76 |
| 77 getDeviceChanges() { |
| 78 let additions = []; |
| 79 for (let guid in this.mockDevices_) |
| 80 if (!(guid in this.lastSeenDevices_)) |
| 81 additions.push(this.mockDevices_[guid]); |
| 82 |
| 83 let removals = []; |
| 84 for (let guid in this.lastSeenDevices_) |
| 85 if (!(guid in this.mockDevices_)) |
| 86 removals.push(this.mockDevices_[guid]); |
| 87 |
| 88 this.lastSeenDevices_ = {}; |
| 89 for (let guid in this.mockDevices_) |
| 90 this.lastSeenDevices_[guid] = this.mockDevices_[guid]; |
| 91 return Promise.resolve({ |
| 92 changes: { devices_added: additions, devices_removed: removals } |
| 93 }); |
| 94 } |
| 95 |
| 96 getDevice(guid, pipe) { |
| 97 if (guid in this.mockDevices_) |
| 98 new MockDevice(this.mockDevices_[guid], pipe); |
| 99 } |
| 100 } |
| 101 |
| 102 let mockDeviceManager = new MockDeviceManager; |
| 103 |
| 104 mojo.serviceRegistry.addServiceOverrideForTesting( |
| 105 deviceManager.DeviceManager.name, |
| 106 pipe => { |
| 107 mockDeviceManager.bindToPipe(pipe); |
| 108 }); |
| 109 |
| 110 mojo.serviceRegistry.addServiceOverrideForTesting( |
| 111 permissionProvider.PermissionProvider.name, |
| 112 pipe => { |
| 113 console.log('Connected to PermissionProvider!'); |
| 114 }); |
| 115 |
| 116 try { |
| 117 resolve(func({ |
| 118 DeviceManager: deviceManager.DeviceManager, |
| 119 Device: device.Device, |
| 120 PermissionProvider: permissionProvider.PermissionProvider, |
| 121 mockDeviceManager: mockDeviceManager, |
| 122 })); |
| 123 } catch (e) { |
| 124 reject(e); |
| 125 } |
| 126 }); |
| 127 }); |
| 128 }, name, properties); |
| 129 } |
| 130 |
| 131 function assert_device_info_equals(device, info) { |
| 132 assert_equals(device.guid, info.guid); |
| 133 assert_equals(device.usbVersionMajor, info.usb_version_major); |
| 134 assert_equals(device.usbVersionMinor, info.usb_version_minor); |
| 135 assert_equals(device.usbVersionSubminor, info.usb_version_subminor); |
| 136 assert_equals(device.deviceClass, info.class_code); |
| 137 assert_equals(device.deviceSubclass, info.subclass_code); |
| 138 assert_equals(device.deviceProtocol, info.protocol_code); |
| 139 assert_equals(device.vendorId, info.vendor_id); |
| 140 assert_equals(device.productId, info.product_id); |
| 141 assert_equals(device.deviceVersionMajor, info.device_version_major); |
| 142 assert_equals(device.deviceVersionMinor, info.device_version_minor); |
| 143 assert_equals(device.deviceVersionSubminor, info.device_version_subminor); |
| 144 assert_equals(device.manufacturerName, info.manufacturer_name); |
| 145 assert_equals(device.productName, info.product_name); |
| 146 assert_equals(device.serialNumber, info.serial_number); |
| 147 assert_equals(device.configurations.length, info.configurations.length); |
| 148 }; |
OLD | NEW |