Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../resources/testharness.js"></script> | 2 <script src="../resources/testharness.js"></script> |
| 3 <script src="../resources/testharnessreport.js"></script> | 3 <script src="../resources/testharnessreport.js"></script> |
| 4 <script src="resources/fake-devices.js"></script> | 4 <script src="resources/fake-devices.js"></script> |
| 5 <script src="resources/usb-helpers.js"></script> | 5 <script src="resources/usb-helpers.js"></script> |
| 6 <script src="resources/webusb-test.js"></script> | 6 <script src="resources/webusb-test.js"></script> |
| 7 <script> | 7 <script> |
| 8 'use strict'; | 8 'use strict'; |
| 9 | 9 |
| 10 // Returns a promise that is resolved when the next USBConnectionEvent of the | 10 usb_test(() => { |
| 11 // given type is received. | 11 let promise = connectionEventPromise('connect'); |
|
ortuno
2017/04/12 03:31:07
How high do you think this event is in the priorit
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
As discussed offline I gave this a try but it requ
| |
| 12 function connectionEventPromise(eventType) { | 12 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 13 return new Promise(resolve => { | 13 |
| 14 let eventHandler = e => { | 14 return promise.then(device => { |
|
ortuno
2017/04/12 03:31:07
Why are you nesting the promises? Why not:
return
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
I need |device| to available in the scope of the h
| |
| 15 assert_true(e instanceof USBConnectionEvent); | 15 return navigator.usb.getDevices().then(devices => { |
| 16 navigator.usb.removeEventListener(eventType, eventHandler); | 16 assert_equals(devices.length, 1); |
| 17 resolve(e.device); | 17 assert_equals(device, devices[0]); |
| 18 }; | 18 assertDeviceInfoEquals(devices[0], fakeDeviceInit); |
| 19 navigator.usb.addEventListener(eventType, eventHandler); | 19 }); |
| 20 }); | 20 }); |
| 21 } | 21 }, 'getDevices returns devices that are connected'); |
| 22 | 22 |
| 23 usb_test(() => { | 23 usb_test(() => { |
| 24 let promise = connectionEventPromise('connect'); | |
|
ortuno
2017/04/12 03:31:07
Why not getConnectedDevice()?
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
Done.
| |
| 24 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 25 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 25 | 26 |
| 26 return navigator.usb.getDevices().then(devices => { | 27 return promise.then(device => { |
|
ortuno
2017/04/12 03:31:07
nit: You don't need `device`. You can replace it w
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
Done.
| |
| 27 assert_equals(devices.length, 1); | 28 return navigator.usb.getDevices().then(devicesFirstTime => { |
| 28 assertDeviceInfoEquals(devices[0], fakeDeviceInit); | 29 assert_equals(devicesFirstTime.length, 1); |
| 29 }); | 30 return navigator.usb.getDevices().then(devicesSecondTime => { |
| 30 }, 'getDevices returns devices exposed by the DeviceManager service'); | 31 assert_array_equals(devicesSecondTime, devicesFirstTime); |
| 31 | 32 }); |
| 32 usb_test(() => { | |
| 33 navigator.usb.test.addFakeDevice(fakeDeviceInit); | |
| 34 | |
| 35 return navigator.usb.getDevices().then(devicesFirstTime => { | |
| 36 assert_equals(devicesFirstTime.length, 1); | |
| 37 return navigator.usb.getDevices().then(devicesSecondTime => { | |
| 38 assert_array_equals(devicesSecondTime, devicesFirstTime); | |
| 39 }); | 33 }); |
| 40 }); | 34 }); |
| 41 }, 'getDevices returns the same objects for each USB device'); | 35 }, 'getDevices returns the same objects for each USB device'); |
| 42 | 36 |
| 43 usb_test(() => { | 37 usb_test(() => { |
| 44 return navigator.usb.requestDevice({ filters: [] }) | 38 return navigator.usb.requestDevice({ filters: [] }) |
| 45 .then(device => { | 39 .then(device => { |
| 46 assert_unreachable('requestDevice should reject without a user gesture'); | 40 assert_unreachable('requestDevice should reject without a user gesture'); |
| 47 }) | 41 }) |
| 48 .catch(error => { | 42 .catch(error => { |
| 49 assert_equals(error.code, DOMException.SECURITY_ERR); | 43 assert_equals(error.code, DOMException.SECURITY_ERR); |
| 50 }); | 44 }); |
| 51 }, 'requestDevice rejects when called without a user gesture'); | 45 }, 'requestDevice rejects when called without a user gesture'); |
| 52 | 46 |
| 53 usb_test(() => { | 47 usb_test(() => { |
| 54 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 48 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) |
| 55 .then(device => { | 49 .then(device => { |
| 56 assert_unreachable('requestDevice should reject when no device selected'); | 50 assert_unreachable('requestDevice should reject when no device selected'); |
| 57 }) | 51 }) |
| 58 .catch(error => { | 52 .catch(error => { |
| 59 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 53 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
| 60 }) | 54 }) |
| 61 ); | 55 ); |
| 62 }, 'requestDevice rejects when no device is chosen'); | 56 }, 'requestDevice rejects when no device is chosen'); |
| 63 | 57 |
| 64 usb_test(() => { | 58 usb_test(() => { |
| 59 let promise = connectionEventPromise('connect'); | |
| 65 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 60 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 66 navigator.usb.test.chosenDevice = guid; | 61 navigator.usb.test.chosenDevice = guid; |
| 67 | 62 |
| 68 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 63 return promise.then(device => { |
| 69 .then(device => { | 64 return callWithKeyDown(() => { |
| 70 assertDeviceInfoEquals(device, fakeDeviceInit); | 65 return navigator.usb.requestDevice({ filters: [] }).then(chosenDevice => { |
| 71 }) | 66 assert_equals(chosenDevice, device); |
| 72 ); | 67 }); |
| 68 }); | |
| 69 }); | |
| 73 }, 'requestDevice returns the device chosen by the user'); | 70 }, 'requestDevice returns the device chosen by the user'); |
| 74 | 71 |
| 75 usb_test(() => { | 72 usb_test(() => { |
| 73 let promise = connectionEventPromise('connect'); | |
| 76 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 74 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 77 navigator.usb.test.chosenDevice = guid; | 75 navigator.usb.test.chosenDevice = guid; |
| 78 | 76 |
| 79 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 77 return promise.then(device => { |
| 80 .then(device => navigator.usb.getDevices().then(devices => { | 78 return callWithKeyDown(() => { |
| 81 assert_equals(devices.length, 1); | 79 return navigator.usb.requestDevice({ filters: [] }).then(chosenDevice => { |
| 82 assert_equals(devices[0], device); | 80 assert_equals(chosenDevice, device); |
| 83 })) | 81 return navigator.usb.getDevices().then(devices => { |
| 84 ); | 82 assert_equals(devices.length, 1); |
| 83 assert_equals(devices[0], chosenDevice); | |
| 84 }); | |
| 85 }); | |
| 86 }); | |
| 87 }); | |
| 85 }, 'getDevices returns the same object as requestDevice'); | 88 }, 'getDevices returns the same object as requestDevice'); |
| 86 | 89 |
| 87 usb_test(() => { | 90 usb_test(() => { |
| 88 const expectedFilters = [ | 91 const expectedFilters = [ |
| 89 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, | 92 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, |
| 90 { vendorId: 5678, productId: 0xF00F } | 93 { vendorId: 5678, productId: 0xF00F } |
| 91 ]; | 94 ]; |
| 92 | 95 |
| 93 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: expectedFi lters }) | 96 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: expectedFi lters }) |
| 94 .then(device => { | 97 .then(device => { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 108 let promise = connectionEventPromise('connect'); | 111 let promise = connectionEventPromise('connect'); |
| 109 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 112 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 110 | 113 |
| 111 return promise.then(device => { | 114 return promise.then(device => { |
| 112 assertDeviceInfoEquals(device, fakeDeviceInit); | 115 assertDeviceInfoEquals(device, fakeDeviceInit); |
| 113 return device.open().then(() => device.close()); | 116 return device.open().then(() => device.close()); |
| 114 }); | 117 }); |
| 115 }, 'onconnect event is trigged by adding a device'); | 118 }, 'onconnect event is trigged by adding a device'); |
| 116 | 119 |
| 117 usb_test(usb => { | 120 usb_test(usb => { |
| 118 let promise = connectionEventPromise('connect'); | |
| 119 navigator.usb.test.addFakeDevice(fakeDeviceInit); | |
| 120 | |
| 121 return promise | |
| 122 .then(device => navigator.usb.getDevices().then(devices => { | |
| 123 assert_equals(devices.length, 1); | |
| 124 assert_equals(devices[0], device); | |
| 125 })); | |
| 126 }, 'getDevices returns the same object as sent in the onconnect event'); | |
| 127 | |
| 128 usb_test(usb => { | |
| 129 let deviceAdded = connectionEventPromise('connect'); | 121 let deviceAdded = connectionEventPromise('connect'); |
| 130 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 122 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 131 | 123 |
| 132 return deviceAdded.then(device => { | 124 return deviceAdded.then(device => { |
| 133 let deviceRemoved = connectionEventPromise('disconnect'); | 125 let deviceRemoved = connectionEventPromise('disconnect'); |
| 134 navigator.usb.test.removeFakeDevice(guid); | 126 navigator.usb.test.removeFakeDevice(guid); |
| 135 return deviceRemoved.then(removedDevice => { | 127 return deviceRemoved.then(removedDevice => { |
| 136 assertDeviceInfoEquals(removedDevice, fakeDeviceInit); | 128 assertDeviceInfoEquals(removedDevice, fakeDeviceInit); |
| 137 assert_equals(removedDevice, device); | 129 assert_equals(removedDevice, device); |
| 138 return removedDevice.open().then(() => { | 130 return removedDevice.open().then(() => { |
| 139 assert_unreachable('should not be able to open a disconnected device'); | 131 assert_unreachable('should not be able to open a disconnected device'); |
| 140 }, error => { | 132 }, error => { |
| 141 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 133 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
| 142 }); | 134 }); |
| 143 }); | 135 }); |
| 144 }); | 136 }); |
| 145 }, 'ondisconnect event is triggered by removing a device'); | 137 }, 'ondisconnect event is triggered by removing a device'); |
| 146 </script> | 138 </script> |
| OLD | NEW |