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/mojo-helpers.js"></script> |
4 <script src="resources/fake-devices.js"></script> | 5 <script src="resources/fake-devices.js"></script> |
5 <script src="resources/usb-helpers.js"></script> | 6 <script src="resources/usb-helpers.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 // Returns a promise that is resolved when the next USBConnectionEvent of the |
11 // given type is received. | 11 // given type is received. |
12 function connectionEventPromise(eventType) { | 12 function connectionEventPromise(eventType) { |
13 return new Promise(resolve => { | 13 return new Promise(resolve => { |
14 let eventHandler = e => { | 14 let eventHandler = e => { |
15 assert_true(e instanceof USBConnectionEvent); | 15 assert_true(e instanceof USBConnectionEvent); |
16 navigator.usb.removeEventListener(eventType, eventHandler); | 16 navigator.usb.removeEventListener(eventType, eventHandler); |
17 resolve(e.device); | 17 resolve(e.device); |
18 }; | 18 }; |
19 navigator.usb.addEventListener(eventType, eventHandler); | 19 navigator.usb.addEventListener(eventType, eventHandler); |
20 }); | 20 }); |
21 } | 21 } |
22 | 22 |
23 usb_test(() => { | 23 usb_test(usb => { |
24 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 24 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
25 | 25 |
26 return navigator.usb.getDevices().then(devices => { | 26 return navigator.usb.getDevices().then(devices => { |
27 assert_equals(devices.length, 1); | 27 assert_equals(devices.length, 1); |
28 assertDeviceInfoEquals(devices[0], fakeDeviceInit); | 28 usb.assertDeviceInfoEquals(devices[0], usb.fakeDevices[0]); |
29 }); | 29 }); |
30 }, 'getDevices returns devices exposed by the DeviceManager service'); | 30 }, 'getDevices returns devices exposed by the DeviceManager service'); |
31 | 31 |
32 usb_test(() => { | 32 usb_test(usb => { |
33 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 33 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
34 | 34 |
35 return navigator.usb.getDevices().then(devicesFirstTime => { | 35 return navigator.usb.getDevices().then(devicesFirstTime => { |
36 assert_equals(devicesFirstTime.length, 1); | 36 assert_equals(devicesFirstTime.length, 1); |
37 return navigator.usb.getDevices().then(devicesSecondTime => { | 37 return navigator.usb.getDevices().then(devicesSecondTime => { |
38 assert_array_equals(devicesSecondTime, devicesFirstTime); | 38 assert_array_equals(devicesSecondTime, devicesFirstTime); |
39 }); | 39 }); |
40 }); | 40 }); |
41 }, 'getDevices returns the same objects for each USB device'); | 41 }, 'getDevices returns the same objects for each USB device'); |
42 | 42 |
43 usb_test(() => { | 43 usb_test(usb => { |
44 return navigator.usb.requestDevice({ filters: [] }) | 44 return navigator.usb.requestDevice({ filters: [] }) |
45 .then(device => { | 45 .then(device => { |
46 assert_unreachable('requestDevice should reject without a user gesture'); | 46 assert_unreachable('requestDevice should reject without a user gesture'); |
47 }) | 47 }) |
48 .catch(error => { | 48 .catch(error => { |
49 assert_equals(error.code, DOMException.SECURITY_ERR); | 49 assert_equals(error.code, DOMException.SECURITY_ERR); |
50 }); | 50 }); |
51 }, 'requestDevice rejects when called without a user gesture'); | 51 }, 'requestDevice rejects when called without a user gesture'); |
52 | 52 |
53 usb_test(() => { | 53 usb_test(usb => { |
| 54 usb.mockChooserService.setChosenDevice(null); |
54 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 55 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) |
55 .then(device => { | 56 .then(device => { |
56 assert_unreachable('requestDevice should reject when no device selected'); | 57 assert_unreachable('requestDevice should reject when no device selected'); |
57 }) | 58 }) |
58 .catch(error => { | 59 .catch(error => { |
59 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 60 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
60 }) | 61 }) |
61 ); | 62 ); |
62 }, 'requestDevice rejects when no device is chosen'); | 63 }, 'requestDevice rejects when no device is chosen'); |
63 | 64 |
64 usb_test(() => { | 65 usb_test(usb => { |
65 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 66 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
66 navigator.usb.test.chosenDevice = guid; | 67 usb.mockChooserService.setChosenDevice(usb.fakeDevices[0]); |
67 | |
68 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 68 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) |
69 .then(device => { | 69 .then(device => { |
70 assertDeviceInfoEquals(device, fakeDeviceInit); | 70 usb.assertDeviceInfoEquals(device, usb.fakeDevices[0]); |
71 }) | 71 }) |
72 ); | 72 ); |
73 }, 'requestDevice returns the device chosen by the user'); | 73 }, 'requestDevice returns the device chosen by the user'); |
74 | 74 |
75 usb_test(() => { | 75 usb_test(usb => { |
76 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 76 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
77 navigator.usb.test.chosenDevice = guid; | 77 usb.mockChooserService.setChosenDevice(usb.fakeDevices[0]); |
78 | |
79 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 78 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) |
80 .then(device => navigator.usb.getDevices().then(devices => { | 79 .then(device => navigator.usb.getDevices().then(devices => { |
81 assert_equals(devices.length, 1); | 80 assert_equals(devices.length, 1); |
82 assert_equals(devices[0], device); | 81 assert_equals(devices[0], device); |
83 })) | 82 })) |
84 ); | 83 ); |
85 }, 'getDevices returns the same object as requestDevice'); | 84 }, 'getDevices returns the same object as requestDevice'); |
86 | 85 |
87 usb_test(() => { | 86 usb_test(usb => { |
88 const expectedFilters = [ | 87 const expectedFilters = [ |
89 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, | 88 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, |
90 { vendorId: 5678, productId: 0xF00F } | 89 { vendorId: 5678, productId: 0xF00F } |
91 ]; | 90 ]; |
92 | 91 usb.mockChooserService.setChosenDevice(null); |
93 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: expectedFi
lters }) | 92 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: expectedFi
lters }) |
94 .then(device => { | 93 .then(device => { |
95 assert_unreachable('requestDevice should reject because no device selected
'); | 94 assert_unreachable('requestDevice should reject because no device selected
'); |
96 }) | 95 }) |
97 .catch(error => { | 96 .catch(error => { |
98 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 97 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
99 let actualFilters = navigator.usb.test.lastFilters; | 98 let actualFilters = usb.mockChooserService.getLastFilters(); |
100 assert_equals(actualFilters.length, expectedFilters.length); | 99 assert_equals(actualFilters.length, expectedFilters.length); |
101 for (var i = 0; i < actualFilters.length; ++i) | 100 assert_true(actualFilters[0].has_vendor_id); |
102 assert_object_equals(actualFilters[i], expectedFilters[i]); | 101 assert_equals(actualFilters[0].vendor_id, expectedFilters[0].vendorId); |
| 102 assert_false(actualFilters[0].has_product_id); |
| 103 assert_true(actualFilters[0].has_class_code, expectedFilters[0].classCode)
; |
| 104 assert_false(actualFilters[0].has_subclass_code); |
| 105 assert_false(actualFilters[0].has_protocol_code); |
| 106 assert_equals(actualFilters[0].serial_number, expectedFilters[0].serialNum
ber); |
| 107 assert_true(actualFilters[1].has_vendor_id); |
| 108 assert_equals(actualFilters[1].vendor_id, expectedFilters[1].vendorId); |
| 109 assert_true(actualFilters[1].has_product_id); |
| 110 assert_equals(actualFilters[1].product_id, expectedFilters[1].productId); |
| 111 assert_false(actualFilters[1].has_class_code); |
| 112 assert_false(actualFilters[1].has_subclass_code); |
| 113 assert_false(actualFilters[1].has_protocol_code); |
| 114 assert_equals(actualFilters[1].serial_number, null); |
103 }) | 115 }) |
104 ); | 116 ); |
105 }, 'filters are sent correctly'); | 117 }, 'filters are sent correctly'); |
106 | 118 |
107 usb_test(() => { | 119 usb_test(usb => { |
108 let promise = connectionEventPromise('connect'); | 120 let promise = connectionEventPromise('connect'); |
109 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 121 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
110 | |
111 return promise.then(device => { | 122 return promise.then(device => { |
112 assertDeviceInfoEquals(device, fakeDeviceInit); | 123 usb.assertDeviceInfoEquals(device, usb.fakeDevices[0]); |
113 return device.open().then(() => device.close()); | 124 return device.open().then(() => device.close()); |
114 }); | 125 }); |
115 }, 'onconnect event is trigged by adding a device'); | 126 }, 'onconnect event is trigged by adding a device'); |
116 | 127 |
117 usb_test(usb => { | 128 usb_test(usb => { |
118 let promise = connectionEventPromise('connect'); | 129 let promise = connectionEventPromise('connect'); |
119 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 130 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
120 | |
121 return promise | 131 return promise |
122 .then(device => navigator.usb.getDevices().then(devices => { | 132 .then(device => navigator.usb.getDevices().then(devices => { |
123 assert_equals(devices.length, 1); | 133 assert_equals(devices.length, 1); |
124 assert_equals(devices[0], device); | 134 assert_equals(devices[0], device); |
125 })); | 135 })); |
126 }, 'getDevices returns the same object as sent in the onconnect event'); | 136 }, 'getDevices returns the same object as sent in the onconnect event'); |
127 | 137 |
128 usb_test(usb => { | 138 usb_test(usb => { |
129 let deviceAdded = connectionEventPromise('connect'); | 139 let deviceAdded = connectionEventPromise('connect'); |
130 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 140 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); |
131 | |
132 return deviceAdded.then(device => { | 141 return deviceAdded.then(device => { |
133 let deviceRemoved = connectionEventPromise('disconnect'); | 142 let deviceRemoved = connectionEventPromise('disconnect'); |
134 navigator.usb.test.removeFakeDevice(guid); | 143 usb.mockDeviceManager.removeMockDevice(usb.fakeDevices[0]); |
135 return deviceRemoved.then(removedDevice => { | 144 return deviceRemoved.then(removedDevice => { |
136 assertDeviceInfoEquals(removedDevice, fakeDeviceInit); | 145 usb.assertDeviceInfoEquals(removedDevice, usb.fakeDevices[0]); |
137 assert_equals(removedDevice, device); | 146 assert_equals(removedDevice, device); |
138 return removedDevice.open().then(() => { | 147 return removedDevice.open().then(() => { |
139 assert_unreachable('should not be able to open a disconnected device'); | 148 assert_unreachable('should not be able to open a disconnected device'); |
140 }, error => { | 149 }, error => { |
141 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 150 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
142 }); | 151 }); |
143 }); | 152 }); |
144 }); | 153 }); |
145 }, 'ondisconnect event is triggered by removing a device'); | 154 }, 'ondisconnect event is triggered by removing a device'); |
146 </script> | 155 </script> |
OLD | NEW |