OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <script src="../resources/testharness.js"></script> | |
3 <script src="../resources/testharnessreport.js"></script> | |
4 <script src="resources/fake-devices.js"></script> | |
5 <script src="resources/usb-helpers.js"></script> | |
6 <script src="resources/webusb-test.js"></script> | |
7 <script> | |
8 'use strict'; | |
9 | |
10 usb_test(() => { | |
11 return getFakeDevice().then(({ device }) => { | |
12 return navigator.usb.getDevices().then(devices => { | |
13 assert_equals(devices.length, 1); | |
14 assert_equals(device, devices[0]); | |
15 assertDeviceInfoEquals(devices[0], fakeDeviceInit); | |
16 }); | |
17 }); | |
18 }, 'getDevices returns devices that are connected'); | |
19 | |
20 usb_test(() => { | |
21 return getFakeDevice().then(() => { | |
22 return navigator.usb.getDevices().then(devicesFirstTime => { | |
23 assert_equals(devicesFirstTime.length, 1); | |
24 return navigator.usb.getDevices().then(devicesSecondTime => { | |
25 assert_array_equals(devicesSecondTime, devicesFirstTime); | |
26 }); | |
27 }); | |
28 }); | |
29 }, 'getDevices returns the same objects for each USB device'); | |
30 | |
31 usb_test(() => { | |
32 return navigator.usb.requestDevice({ filters: [] }) | |
33 .then(device => { | |
34 assert_unreachable('requestDevice should reject without a user gesture'); | |
35 }) | |
36 .catch(error => { | |
37 assert_equals(error.code, DOMException.SECURITY_ERR); | |
38 }); | |
39 }, 'requestDevice rejects when called without a user gesture'); | |
40 | |
41 usb_test(() => { | |
42 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | |
43 .then(device => { | |
44 assert_unreachable('requestDevice should reject when no device selected'); | |
45 }) | |
46 .catch(error => { | |
47 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | |
48 }) | |
49 ); | |
50 }, 'requestDevice rejects when no device is chosen'); | |
51 | |
52 usb_test(() => { | |
53 return getFakeDevice().then(({ device, fakeDevice }) => { | |
54 navigator.usb.test.chosenDevice = fakeDevice; | |
55 return callWithKeyDown(() => { | |
56 return navigator.usb.requestDevice({ filters: [] }).then(chosenDevice => { | |
57 assert_equals(chosenDevice, device); | |
58 }); | |
59 }); | |
60 }); | |
61 }, 'requestDevice returns the device chosen by the user'); | |
62 | |
63 usb_test(() => { | |
64 return getFakeDevice().then(({ device, fakeDevice }) => { | |
65 navigator.usb.test.chosenDevice = fakeDevice; | |
66 return callWithKeyDown(() => { | |
67 return navigator.usb.requestDevice({ filters: [] }).then(chosenDevice => { | |
68 assert_equals(chosenDevice, device); | |
69 return navigator.usb.getDevices().then(devices => { | |
70 assert_equals(devices.length, 1); | |
71 assert_equals(devices[0], chosenDevice); | |
72 }); | |
73 }); | |
74 }); | |
75 }); | |
76 }, 'getDevices returns the same object as requestDevice'); | |
77 | |
78 usb_test(() => { | |
79 const expectedFilters = [ | |
80 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, | |
81 { vendorId: 5678, productId: 0xF00F } | |
82 ]; | |
83 | |
84 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: expectedFi
lters }) | |
85 .then(device => { | |
86 assert_unreachable('requestDevice should reject because no device selected
'); | |
87 }) | |
88 .catch(error => { | |
89 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | |
90 let actualFilters = navigator.usb.test.lastFilters; | |
91 assert_equals(actualFilters.length, expectedFilters.length); | |
92 for (var i = 0; i < actualFilters.length; ++i) | |
93 assert_object_equals(actualFilters[i], expectedFilters[i]); | |
94 }) | |
95 ); | |
96 }, 'filters are sent correctly'); | |
97 | |
98 usb_test(() => { | |
99 return getFakeDevice().then(({ device }) => { | |
100 assertDeviceInfoEquals(device, fakeDeviceInit); | |
101 return device.open().then(() => device.close()); | |
102 }); | |
103 }, 'onconnect event is trigged by adding a device'); | |
104 | |
105 usb_test(usb => { | |
106 return getFakeDevice().then(({ device, fakeDevice }) => { | |
107 return waitForDisconnect(fakeDevice).then(removedDevice => { | |
108 assertDeviceInfoEquals(removedDevice, fakeDeviceInit); | |
109 assert_equals(removedDevice, device); | |
110 return removedDevice.open().then(() => { | |
111 assert_unreachable('should not be able to open a disconnected device'); | |
112 }, error => { | |
113 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | |
114 }); | |
115 }); | |
116 }); | |
117 }, 'ondisconnect event is triggered by removing a device'); | |
118 </script> | |
OLD | NEW |