OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
ortuno
2017/04/06 01:14:06
I don't think the "-manual" suffix pattern will sc
Reilly Grant (use Gerrit)
2017/04/06 01:52:32
I can get away with it in these tests because the
| |
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> | |
5 <script src="resources/fake-devices.js"></script> | 4 <script src="resources/fake-devices.js"></script> |
6 <script src="resources/usb-helpers.js"></script> | 5 <script src="resources/usb-helpers.js"></script> |
7 <script> | 6 <script> |
8 'use strict'; | 7 'use strict'; |
9 | 8 |
10 // Returns a promise that is resolved when the next USBConnectionEvent of the | 9 // Returns a promise that is resolved when the next USBConnectionEvent of the |
11 // given type is received. | 10 // given type is received. |
12 function connectionEventPromise(eventType) { | 11 function connectionEventPromise(eventType) { |
13 return new Promise(resolve => { | 12 return new Promise(resolve => { |
14 let eventHandler = e => { | 13 let eventHandler = e => { |
15 assert_true(e instanceof USBConnectionEvent); | 14 assert_true(e instanceof USBConnectionEvent); |
16 navigator.usb.removeEventListener(eventType, eventHandler); | 15 navigator.usb.removeEventListener(eventType, eventHandler); |
17 resolve(e.device); | 16 resolve(e.device); |
18 }; | 17 }; |
19 navigator.usb.addEventListener(eventType, eventHandler); | 18 navigator.usb.addEventListener(eventType, eventHandler); |
20 }); | 19 }); |
21 } | 20 } |
22 | 21 |
23 usb_test(usb => { | 22 usb_test(() => { |
24 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 23 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
ortuno
2017/04/06 01:14:06
In the vein of designing our tests APIs in a way t
Reilly Grant (use Gerrit)
2017/04/06 01:52:32
This call doesn't guarantee that anything happens
ortuno
2017/04/06 02:51:47
Interesting, then how do you guarantee that the de
Reilly Grant (use Gerrit)
2017/04/06 06:53:13
Indeed, you're right, if I were really using this
| |
25 | 24 |
26 return navigator.usb.getDevices().then(devices => { | 25 return navigator.usb.getDevices().then(devices => { |
27 assert_equals(devices.length, 1); | 26 assert_equals(devices.length, 1); |
28 usb.assertDeviceInfoEquals(devices[0], usb.fakeDevices[0]); | 27 assertDeviceInfoEquals(devices[0], fakeDeviceInit); |
29 }); | 28 }); |
30 }, 'getDevices returns devices exposed by the DeviceManager service'); | 29 }, 'getDevices returns devices exposed by the DeviceManager service'); |
31 | 30 |
32 usb_test(usb => { | 31 usb_test(() => { |
33 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 32 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
34 | 33 |
35 return navigator.usb.getDevices().then(devicesFirstTime => { | 34 return navigator.usb.getDevices().then(devicesFirstTime => { |
36 assert_equals(devicesFirstTime.length, 1); | 35 assert_equals(devicesFirstTime.length, 1); |
37 return navigator.usb.getDevices().then(devicesSecondTime => { | 36 return navigator.usb.getDevices().then(devicesSecondTime => { |
38 assert_array_equals(devicesSecondTime, devicesFirstTime); | 37 assert_array_equals(devicesSecondTime, devicesFirstTime); |
39 }); | 38 }); |
40 }); | 39 }); |
41 }, 'getDevices returns the same objects for each USB device'); | 40 }, 'getDevices returns the same objects for each USB device'); |
42 | 41 |
43 usb_test(usb => { | 42 usb_test(() => { |
44 return navigator.usb.requestDevice({ filters: [] }) | 43 return navigator.usb.requestDevice({ filters: [] }) |
45 .then(device => { | 44 .then(device => { |
46 assert_unreachable('requestDevice should reject without a user gesture'); | 45 assert_unreachable('requestDevice should reject without a user gesture'); |
47 }) | 46 }) |
48 .catch(error => { | 47 .catch(error => { |
49 assert_equals(error.code, DOMException.SECURITY_ERR); | 48 assert_equals(error.code, DOMException.SECURITY_ERR); |
50 }); | 49 }); |
51 }, 'requestDevice rejects when called without a user gesture'); | 50 }, 'requestDevice rejects when called without a user gesture'); |
52 | 51 |
53 usb_test(usb => { | 52 usb_test(() => { |
54 usb.mockChooserService.setChosenDevice(null); | 53 return callWithTrustedClick(() => navigator.usb.requestDevice({ filters: [] }) |
ortuno
2017/04/06 01:14:06
hmm callWithTrustedClick just adds a button and se
Reilly Grant (use Gerrit)
2017/04/06 01:52:32
I include wpt_automation/fullscreen/auto-click.js
ortuno
2017/04/06 02:51:47
Wow that's so subtle specially since there is nowh
| |
55 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | |
56 .then(device => { | 54 .then(device => { |
57 assert_unreachable('requestDevice should reject when no device selected'); | 55 assert_unreachable('requestDevice should reject when no device selected'); |
58 }) | 56 }) |
59 .catch(error => { | 57 .catch(error => { |
60 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 58 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
61 }) | 59 }) |
62 ); | 60 ); |
63 }, 'requestDevice rejects when no device is chosen'); | 61 }, 'requestDevice rejects when no device is chosen'); |
64 | 62 |
65 usb_test(usb => { | 63 usb_test(() => { |
66 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 64 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
67 usb.mockChooserService.setChosenDevice(usb.fakeDevices[0]); | 65 navigator.usb.test.chosenDevice = guid; |
68 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 66 |
67 return callWithTrustedClick(() => navigator.usb.requestDevice({ filters: [] }) | |
69 .then(device => { | 68 .then(device => { |
70 usb.assertDeviceInfoEquals(device, usb.fakeDevices[0]); | 69 assertDeviceInfoEquals(device, fakeDeviceInit); |
71 }) | 70 }) |
72 ); | 71 ); |
73 }, 'requestDevice returns the device chosen by the user'); | 72 }, 'requestDevice returns the device chosen by the user'); |
74 | 73 |
75 usb_test(usb => { | 74 usb_test(() => { |
76 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 75 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
77 usb.mockChooserService.setChosenDevice(usb.fakeDevices[0]); | 76 navigator.usb.test.chosenDevice = guid; |
78 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: [] }) | 77 |
78 return callWithTrustedClick(() => navigator.usb.requestDevice({ filters: [] }) | |
79 .then(device => navigator.usb.getDevices().then(devices => { | 79 .then(device => navigator.usb.getDevices().then(devices => { |
80 assert_equals(devices.length, 1); | 80 assert_equals(devices.length, 1); |
81 assert_equals(devices[0], device); | 81 assert_equals(devices[0], device); |
82 })) | 82 })) |
83 ); | 83 ); |
84 }, 'getDevices returns the same object as requestDevice'); | 84 }, 'getDevices returns the same object as requestDevice'); |
85 | 85 |
86 usb_test(usb => { | 86 usb_test(() => { |
87 const expectedFilters = [ | 87 const expectedFilters = [ |
88 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, | 88 { vendorId: 1234, classCode: 0xFF, serialNumber: "123ABC" }, |
89 { vendorId: 5678, productId: 0xF00F } | 89 { vendorId: 5678, productId: 0xF00F } |
90 ]; | 90 ]; |
91 usb.mockChooserService.setChosenDevice(null); | 91 |
92 return callWithKeyDown(() => navigator.usb.requestDevice({ filters: expectedFi lters }) | 92 return callWithTrustedClick(() => navigator.usb.requestDevice({ filters: expec tedFilters }) |
93 .then(device => { | 93 .then(device => { |
94 assert_unreachable('requestDevice should reject because no device selected '); | 94 assert_unreachable('requestDevice should reject because no device selected '); |
95 }) | 95 }) |
96 .catch(error => { | 96 .catch(error => { |
97 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 97 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
98 let actualFilters = usb.mockChooserService.getLastFilters(); | 98 let actualFilters = navigator.usb.test.lastFilters; |
99 assert_equals(actualFilters.length, expectedFilters.length); | 99 assert_equals(actualFilters.length, expectedFilters.length); |
100 assert_true(actualFilters[0].has_vendor_id); | 100 for (var i = 0; i < actualFilters.length; ++i) |
101 assert_equals(actualFilters[0].vendor_id, expectedFilters[0].vendorId); | 101 assert_object_equals(actualFilters[i], expectedFilters[i]); |
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); | |
115 }) | 102 }) |
116 ); | 103 ); |
117 }, 'filters are sent correctly'); | 104 }, 'filters are sent correctly'); |
118 | 105 |
119 usb_test(usb => { | 106 usb_test(() => { |
120 let promise = connectionEventPromise('connect'); | 107 let promise = connectionEventPromise('connect'); |
121 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 108 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
109 | |
122 return promise.then(device => { | 110 return promise.then(device => { |
123 usb.assertDeviceInfoEquals(device, usb.fakeDevices[0]); | 111 assertDeviceInfoEquals(device, fakeDeviceInit); |
124 return device.open().then(() => device.close()); | 112 return device.open().then(() => device.close()); |
125 }); | 113 }); |
126 }, 'onconnect event is trigged by adding a device'); | 114 }, 'onconnect event is trigged by adding a device'); |
127 | 115 |
128 usb_test(usb => { | 116 usb_test(usb => { |
129 let promise = connectionEventPromise('connect'); | 117 let promise = connectionEventPromise('connect'); |
130 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 118 navigator.usb.test.addFakeDevice(fakeDeviceInit); |
119 | |
131 return promise | 120 return promise |
132 .then(device => navigator.usb.getDevices().then(devices => { | 121 .then(device => navigator.usb.getDevices().then(devices => { |
133 assert_equals(devices.length, 1); | 122 assert_equals(devices.length, 1); |
134 assert_equals(devices[0], device); | 123 assert_equals(devices[0], device); |
135 })); | 124 })); |
136 }, 'getDevices returns the same object as sent in the onconnect event'); | 125 }, 'getDevices returns the same object as sent in the onconnect event'); |
137 | 126 |
138 usb_test(usb => { | 127 usb_test(usb => { |
139 let deviceAdded = connectionEventPromise('connect'); | 128 let deviceAdded = connectionEventPromise('connect'); |
140 usb.mockDeviceManager.addMockDevice(usb.fakeDevices[0]); | 129 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
130 | |
141 return deviceAdded.then(device => { | 131 return deviceAdded.then(device => { |
142 let deviceRemoved = connectionEventPromise('disconnect'); | 132 let deviceRemoved = connectionEventPromise('disconnect'); |
143 usb.mockDeviceManager.removeMockDevice(usb.fakeDevices[0]); | 133 navigator.usb.test.removeFakeDevice(guid); |
144 return deviceRemoved.then(removedDevice => { | 134 return deviceRemoved.then(removedDevice => { |
145 usb.assertDeviceInfoEquals(removedDevice, usb.fakeDevices[0]); | 135 assertDeviceInfoEquals(removedDevice, fakeDeviceInit); |
146 assert_equals(removedDevice, device); | 136 assert_equals(removedDevice, device); |
147 return removedDevice.open().then(() => { | 137 return removedDevice.open().then(() => { |
148 assert_unreachable('should not be able to open a disconnected device'); | 138 assert_unreachable('should not be able to open a disconnected device'); |
149 }, error => { | 139 }, error => { |
150 assert_equals(error.code, DOMException.NOT_FOUND_ERR); | 140 assert_equals(error.code, DOMException.NOT_FOUND_ERR); |
151 }); | 141 }); |
152 }); | 142 }); |
153 }); | 143 }); |
154 }, 'ondisconnect event is triggered by removing a device'); | 144 }, 'ondisconnect event is triggered by removing a device'); |
155 </script> | 145 </script> |
OLD | NEW |