Index: third_party/WebKit/LayoutTests/usb/usbDevice.html |
diff --git a/third_party/WebKit/LayoutTests/usb/usbDevice.html b/third_party/WebKit/LayoutTests/usb/usbDevice.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9f3b6af16402ecc45ea33ded0205bbb93f0e2b88 |
--- /dev/null |
+++ b/third_party/WebKit/LayoutTests/usb/usbDevice.html |
@@ -0,0 +1,297 @@ |
+<!DOCTYPE html> |
+<script src="../resources/testharness.js"></script> |
+<script src="../resources/testharnessreport.js"></script> |
+<script src="../resources/mojo-helpers.js"></script> |
+<script src="resources/fake-devices.js"></script> |
+<script src="resources/usb-helpers.js"></script> |
+<script> |
+'use strict'; |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open().then(() => device.close()); |
+ }); |
+}, 'a device can be opened and closed'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(1)) |
+ .then(() => device.getConfiguration()) |
+ .then(config => { |
+ usb.assertConfigurationInfoEquals( |
+ config, usb.fakeDevices[0].configurations[0]); |
+ }) |
+ .then(() => device.close()); |
+ }); |
+}, 'device configuration can be set and queried'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.getConfiguration() |
+ .then(() => { |
+ assert_unreached('getConfiguration should reject'); |
+ }) |
+ .catch(error => { |
+ assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
+ return Promise.resolve(); |
+ })) |
+ .then(() => device.close()); |
+ }); |
+}, 'querying an unset configuration raises NotFoundError'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(1)) |
+ .then(() => device.claimInterface(0)) |
+ .then(() => device.releaseInterface(0)) |
+ .then(() => device.close()); |
+ }); |
+}, 'an interface can be claimed and released'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.claimInterface(0) |
+ .then(() => { |
+ assert_unreached('claimInterface should reject'); |
+ }) |
+ .catch(error => { |
+ // TODO(reillyg): This should be INVALID_STATE_ERR. |
+ assert_equals(error.code, DOMException.NETWORK_ERR); |
+ return Promise.resolve(); |
+ })) |
+ .then(() => device.close()); |
+ }); |
+}, 'claiming an interface fails without an active configuration'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(2)) |
+ .then(() => device.claimInterface(0)) |
+ .then(() => device.setInterface(0, 1)) |
+ .then(() => device.close()); |
+ }); |
+}, 'can select an alternate interface'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.controlTransferIn({ |
+ requestType: 'vendor', |
+ recipient: 'device', |
+ request: 0x42, |
+ value: 0x1234, |
+ index: 0x5678 |
+ }, 7)) |
+ .then(result => { |
+ assert_true(result instanceof USBInTransferResult); |
+ assert_equals(result.status, 'ok'); |
+ assert_equals(result.data.byteLength, 7); |
+ assert_equals(result.data.getUint16(0), 0x07); |
+ assert_equals(result.data.getUint8(2), 0x42); |
+ assert_equals(result.data.getUint16(3), 0x1234); |
+ assert_equals(result.data.getUint16(5), 0x5678); |
+ return device.close(); |
+ }); |
+ }); |
+}, 'can issue IN control transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.controlTransferOut({ |
+ requestType: 'vendor', |
+ recipient: 'device', |
+ request: 0x42, |
+ value: 0x1234, |
+ index: 0x5678 |
+ }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))) |
+ .then(result => { |
+ assert_true(result instanceof USBOutTransferResult); |
+ assert_equals(result.status, 'ok'); |
+ assert_equals(result.bytesWritten, 8); |
+ return device.close(); |
+ }) |
+ }); |
+}, 'can issue OUT control transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(1)) |
+ .then(() => device.claimInterface(0)) |
+ .then(() => device.clearHalt(1)) |
+ .then(() => device.close()); |
+ }); |
+}, 'can clear a halt condition'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(1)) |
+ .then(() => device.claimInterface(0)) |
+ .then(() => device.transferIn(1, 8)) |
+ .then(result => { |
+ assert_true(result instanceof USBInTransferResult); |
+ assert_equals(result.status, 'ok'); |
+ assert_equals(result.data.byteLength, 8); |
+ for (let i = 0; i < 8; ++i) |
+ assert_equals(result.data.getUint8(i), i, 'mismatch at byte ' + i); |
+ return device.close(); |
+ }); |
+ }); |
+}, 'can issue IN interrupt transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(1)) |
+ .then(() => device.claimInterface(1)) |
+ .then(() => device.transferIn(2, 1024)) |
+ .then(result => { |
+ assert_true(result instanceof USBInTransferResult); |
+ assert_equals(result.status, 'ok'); |
+ assert_equals(result.data.byteLength, 1024); |
+ for (let i = 0; i < 1024; ++i) |
+ assert_equals(result.data.getUint8(i), i & 0xff, |
+ 'mismatch at byte ' + i); |
+ return device.close(); |
+ }); |
+ }); |
+}, 'can issue IN bulk transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(1)) |
+ .then(() => device.claimInterface(1)) |
+ .then(() => { |
+ let data = new DataView(new ArrayBuffer(1024)); |
+ for (let i = 0; i < 1024; ++i) |
+ data.setUint8(i, i & 0xff); |
+ return device.transferOut(2, data); |
+ }) |
+ .then(result => { |
+ assert_true(result instanceof USBOutTransferResult); |
+ assert_equals(result.status, 'ok'); |
+ assert_equals(result.bytesWritten, 1024); |
+ return device.close(); |
+ }); |
+ }); |
+}, 'can issue OUT bulk transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(2)) |
+ .then(() => device.claimInterface(0)) |
+ .then(() => device.setInterface(0, 1)) |
+ .then(() => device.isochronousTransferIn( |
+ 1, [64, 64, 64, 64, 64, 64, 64, 64])) |
+ .then(result => { |
+ assert_true(result instanceof USBIsochronousInTransferResult); |
+ assert_equals(result.data.byteLength, 64 * 8, 'buffer size'); |
+ assert_equals(result.packets.length, 8, 'number of packets'); |
+ let byteOffset = 0; |
+ for (let i = 0; i < result.packets.length; ++i) { |
+ assert_true( |
+ result.packets[i] instanceof USBIsochronousInTransferPacket); |
+ assert_equals(result.packets[i].status, 'ok'); |
+ assert_equals(result.packets[i].data.byteLength, 64); |
+ assert_equals(result.packets[i].data.buffer, result.data.buffer); |
+ assert_equals(result.packets[i].data.byteOffset, byteOffset); |
+ for (let j = 0; j < 64; ++j) |
+ assert_equals(result.packets[i].data.getUint8(j), j & 0xff, |
+ 'mismatch at byte ' + j + ' of packet ' + i); |
+ byteOffset += result.packets[i].data.byteLength; |
+ } |
+ return device.close(); |
+ }); |
+ }); |
+}, 'can issue IN isochronous transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(devices.length, 1); |
+ let device = devices[0]; |
+ return device.open() |
+ .then(() => device.setConfiguration(2)) |
+ .then(() => device.claimInterface(0)) |
+ .then(() => device.setInterface(0, 1)) |
+ .then(() => { |
+ let data = new DataView(new ArrayBuffer(64 * 8)); |
+ for (let i = 0; i < 8; ++i) { |
+ for (let j = 0; j < 64; ++j) |
+ data.setUint8(i * j, j & 0xff); |
+ } |
+ return device.isochronousTransferOut( |
+ 1, data, [64, 64, 64, 64, 64, 64, 64, 64]); |
+ }) |
+ .then(result => { |
+ assert_true(result instanceof USBIsochronousOutTransferResult); |
+ assert_equals(result.packets.length, 8, 'number of packets'); |
+ let byteOffset = 0; |
+ for (let i = 0; i < result.packets.length; ++i) { |
+ assert_true( |
+ result.packets[i] instanceof USBIsochronousOutTransferPacket); |
+ assert_equals(result.packets[i].status, 'ok'); |
+ assert_equals(result.packets[i].bytesWritten, 64); |
+ } |
+ return device.close(); |
+ }); |
+ }); |
+}, 'can issue OUT isochronous transfer'); |
+ |
+usb_test(usb => { |
+ usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
+ return navigator.usb.getDevices().then(devices => { |
+ assert_equals(1, devices.length); |
+ let device = devices[0]; |
+ return device.open().then(() => device.reset()).then(() => device.close()); |
+ }); |
+}, 'can reset the device'); |
+</script> |