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 function assertRejectsWithNotFoundError(promise) { | 10 function assertRejectsWithNotFoundError(promise) { |
| 11 return assertRejectsWithError(promise, 'NotFoundError'); | 11 return assertRejectsWithError(promise, 'NotFoundError'); |
| 12 } | 12 } |
| 13 | 13 |
| 14 function assertRejectsWithNotOpenError(promise) { | 14 function assertRejectsWithNotOpenError(promise) { |
| 15 return assertRejectsWithError( | 15 return assertRejectsWithError( |
| 16 promise, 'InvalidStateError', 'The device must be opened first.') | 16 promise, 'InvalidStateError', 'The device must be opened first.') |
| 17 } | 17 } |
| 18 | 18 |
| 19 function assertRejectsWithNotConfiguredError(promise) { | 19 function assertRejectsWithNotConfiguredError(promise) { |
| 20 return assertRejectsWithError( | 20 return assertRejectsWithError( |
| 21 promise, 'InvalidStateError', | 21 promise, 'InvalidStateError', |
| 22 'The device must have a configuration selected.'); | 22 'The device must have a configuration selected.'); |
| 23 } | 23 } |
| 24 | 24 |
| 25 usb_test(() => { | 25 usb_test(() => { |
| 26 let addPromise = connectionEventPromise('connect'); | |
| 26 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 27 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 27 | 28 |
| 28 return navigator.usb.getDevices().then(devices => { | 29 return addPromise.then(device => { |
| 29 assert_equals(1, devices.length); | 30 let removePromise = connectionEventPromise('disconnect'); |
| 30 navigator.usb.test.removeFakeDevice(guid); | 31 navigator.usb.test.removeFakeDevice(guid); |
| 31 return assertRejectsWithNotFoundError(devices[0].open()); | 32 return removePromise.then(() => { |
| 33 return assertRejectsWithNotFoundError(device.open()); | |
| 34 }); | |
| 32 }); | 35 }); |
| 33 }, 'open rejects when called on a disconnected device'); | 36 }, 'open rejects when called on a disconnected device'); |
| 34 | 37 |
| 35 usb_test(() => { | 38 usb_test(() => { |
| 39 let addPromise = connectionEventPromise('connect'); | |
| 36 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 40 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 37 | 41 |
| 38 return navigator.usb.getDevices().then(devices => { | 42 return addPromise.then(device => { |
| 39 assert_equals(1, devices.length); | 43 let removePromise = connectionEventPromise('disconnect'); |
| 40 let promise = devices[0].open(); | |
| 41 navigator.usb.test.removeFakeDevice(guid); | 44 navigator.usb.test.removeFakeDevice(guid); |
| 42 return assertRejectsWithNotFoundError(promise) | 45 return device.open().then(() => { |
| 43 .then(() => runGarbageCollection()); | 46 // If the open wins then after the device has been removed it should be |
| 47 // closed. | |
| 48 return removePromise.then(() => { | |
| 49 assert_false(device.opened); | |
| 50 }); | |
| 51 }, error => { | |
| 52 // If the disconnection wins then the open should fail. | |
| 53 assert_equals(error.name, 'NotFoundError'); | |
| 54 }); | |
|
ortuno
2017/04/12 03:31:07
Seems like a the test that should be split into tw
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
The test API doesn't guarantee a way to inject a d
| |
| 44 }); | 55 }); |
| 45 }, 'open rejects when device disconnected during call'); | 56 }, 'race open against disconnection'); |
| 46 | 57 |
| 47 usb_test(() => { | 58 usb_test(() => { |
| 48 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 59 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 49 | |
| 50 return navigator.usb.getDevices().then(devices => { | |
| 51 assert_equals(1, devices.length); | |
| 52 let device = devices[0]; | |
| 53 assert_false(device.opened); | 60 assert_false(device.opened); |
| 54 return device.open().then(() => { | 61 return device.open().then(() => { |
| 55 assert_true(device.opened); | 62 assert_true(device.opened); |
| 56 return device.close().then(() => { | 63 return device.close().then(() => { |
| 57 assert_false(device.opened); | 64 assert_false(device.opened); |
| 58 }); | 65 }); |
| 59 }); | 66 }); |
| 60 }).then(() => runGarbageCollection()); | 67 }); |
| 61 }, 'a device can be opened and closed'); | 68 }, 'a device can be opened and closed'); |
| 62 | 69 |
| 63 usb_test(() => { | 70 usb_test(() => { |
| 64 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 71 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 65 | |
| 66 return navigator.usb.getDevices().then(devices => { | |
| 67 assert_equals(1, devices.length); | |
| 68 let device = devices[0]; | |
| 69 return device.open() | 72 return device.open() |
| 70 .then(() => device.open()) | 73 .then(() => device.open()) |
| 71 .then(() => device.open()) | 74 .then(() => device.open()) |
| 72 .then(() => device.open()) | 75 .then(() => device.open()) |
| 73 .then(() => device.close()) | 76 .then(() => device.close()) |
| 74 .then(() => device.close()) | 77 .then(() => device.close()) |
| 75 .then(() => device.close()) | 78 .then(() => device.close()) |
| 76 .then(() => device.close()); | 79 .then(() => device.close()); |
| 77 }); | 80 }); |
| 78 }, 'open and close can be called multiple times'); | 81 }, 'open and close can be called multiple times'); |
| 79 | 82 |
| 80 usb_test(() => { | 83 usb_test(() => { |
| 81 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 84 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 82 | |
| 83 return navigator.usb.getDevices().then(devices => { | |
| 84 assert_equals(1, devices.length); | |
| 85 let device = devices[0]; | |
| 86 const message = | 85 const message = |
| 87 'An operation that changes the device state is in progress.'; | 86 'An operation that changes the device state is in progress.'; |
| 88 return Promise.all([ | 87 return Promise.all([ |
| 89 device.open(), | 88 device.open(), |
| 90 assertRejectsWithError(device.open(), 'InvalidStateError', message), | 89 assertRejectsWithError(device.open(), 'InvalidStateError', message), |
| 91 assertRejectsWithError(device.close(), 'InvalidStateError', message), | 90 assertRejectsWithError(device.close(), 'InvalidStateError', message), |
| 92 ]).then(() => Promise.all([ | 91 ]).then(() => Promise.all([ |
| 93 device.close(), | 92 device.close(), |
| 94 assertRejectsWithError(device.open(), 'InvalidStateError', message), | 93 assertRejectsWithError(device.open(), 'InvalidStateError', message), |
| 95 assertRejectsWithError(device.close(), 'InvalidStateError', message), | 94 assertRejectsWithError(device.close(), 'InvalidStateError', message), |
| 96 ])); | 95 ])); |
| 97 }); | 96 }); |
| 98 }, 'open and close cannot be called again while open or close are in progress'); | 97 }, 'open and close cannot be called again while open or close are in progress'); |
| 99 | 98 |
| 100 usb_test(() => { | 99 usb_test(() => { |
| 100 let addPromise = connectionEventPromise('connect'); | |
|
ortuno
2017/04/12 03:31:08
Why not getConnectedDevice()?
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
I've added getFakeDeviceAndGuid() which handles th
| |
| 101 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 101 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 102 | 102 |
| 103 return navigator.usb.getDevices().then(devices => { | 103 return addPromise.then(device => { |
| 104 assert_equals(1, devices.length); | |
| 105 let device = devices[0]; | |
| 106 return device.open().then(() => { | 104 return device.open().then(() => { |
| 105 let removePromise = connectionEventPromise('disconnect'); | |
|
ortuno
2017/04/12 03:31:08
Seems like something you could abstract as well.
Reilly Grant (use Gerrit)
2017/04/12 22:47:07
Done.
| |
| 107 navigator.usb.test.removeFakeDevice(guid); | 106 navigator.usb.test.removeFakeDevice(guid); |
| 108 return assertRejectsWithNotFoundError(device.close()); | 107 return removePromise.then(() => { |
| 108 return assertRejectsWithNotFoundError(device.close()); | |
| 109 }); | |
| 109 }); | 110 }); |
| 110 }); | 111 }); |
| 111 }, 'close rejects when called on a disconnected device'); | 112 }, 'close rejects when called on a disconnected device'); |
| 112 | 113 |
| 113 usb_test(() => { | 114 usb_test(() => { |
| 115 let addPromise = connectionEventPromise('connect'); | |
| 114 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 116 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 115 | 117 |
| 116 return navigator.usb.getDevices().then(devices => { | 118 return addPromise.then(device => { |
| 117 assert_equals(1, devices.length); | 119 return device.open().then(() => { |
| 118 let device = devices[0]; | 120 let removePromise = connectionEventPromise('disconnect'); |
| 119 return device.open() | 121 navigator.usb.test.removeFakeDevice(guid); |
| 120 .then(() => { | 122 return removePromise.then(() => { |
| 121 navigator.usb.test.removeFakeDevice(guid); | |
| 122 return assertRejectsWithNotFoundError(device.selectConfiguration(1)); | 123 return assertRejectsWithNotFoundError(device.selectConfiguration(1)); |
| 123 }); | 124 }); |
| 125 }); | |
| 124 }); | 126 }); |
| 125 }, 'selectConfiguration rejects when called on a disconnected device'); | 127 }, 'selectConfiguration rejects when called on a disconnected device'); |
| 126 | 128 |
| 127 usb_test(() => { | 129 usb_test(() => { |
| 128 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 130 return getConnectedDevice(fakeDeviceInit).then(device => Promise.all([ |
| 129 | 131 assertRejectsWithNotOpenError(device.selectConfiguration(1)), |
| 130 return navigator.usb.getDevices().then(devices => { | 132 assertRejectsWithNotOpenError(device.claimInterface(0)), |
| 131 assert_equals(1, devices.length); | 133 assertRejectsWithNotOpenError(device.releaseInterface(0)), |
| 132 let device = devices[0]; | 134 assertRejectsWithNotOpenError(device.selectAlternateInterface(0, 1)), |
| 133 return Promise.all([ | 135 assertRejectsWithNotOpenError(device.controlTransferIn({ |
| 134 assertRejectsWithNotOpenError(device.selectConfiguration(1)), | 136 requestType: 'vendor', |
| 135 assertRejectsWithNotOpenError(device.claimInterface(0)), | 137 recipient: 'device', |
| 136 assertRejectsWithNotOpenError(device.releaseInterface(0)), | 138 request: 0x42, |
| 137 assertRejectsWithNotOpenError(device.selectAlternateInterface(0, 1)), | 139 value: 0x1234, |
| 138 assertRejectsWithNotOpenError(device.controlTransferIn({ | 140 index: 0x5678 |
| 139 requestType: 'vendor', | 141 }, 7)), |
| 140 recipient: 'device', | 142 assertRejectsWithNotOpenError(device.controlTransferOut({ |
| 141 request: 0x42, | 143 requestType: 'vendor', |
| 142 value: 0x1234, | 144 recipient: 'device', |
| 143 index: 0x5678 | 145 request: 0x42, |
| 144 }, 7)), | 146 value: 0x1234, |
| 145 assertRejectsWithNotOpenError(device.controlTransferOut({ | 147 index: 0x5678 |
| 146 requestType: 'vendor', | 148 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))), |
| 147 recipient: 'device', | 149 assertRejectsWithNotOpenError(device.clearHalt('in', 1)), |
| 148 request: 0x42, | 150 assertRejectsWithNotOpenError(device.transferIn(1, 8)), |
| 149 value: 0x1234, | 151 assertRejectsWithNotOpenError( |
| 150 index: 0x5678 | 152 device.transferOut(1, new ArrayBuffer(8))), |
| 151 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))), | 153 assertRejectsWithNotOpenError(device.isochronousTransferIn(1, [8])), |
| 152 assertRejectsWithNotOpenError(device.clearHalt('in', 1)), | 154 assertRejectsWithNotOpenError( |
| 153 assertRejectsWithNotOpenError(device.transferIn(1, 8)), | 155 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])), |
| 154 assertRejectsWithNotOpenError( | 156 assertRejectsWithNotOpenError(device.reset()) |
| 155 device.transferOut(1, new ArrayBuffer(8))), | 157 ])); |
| 156 assertRejectsWithNotOpenError(device.isochronousTransferIn(1, [8])), | |
| 157 assertRejectsWithNotOpenError( | |
| 158 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])), | |
| 159 assertRejectsWithNotOpenError(device.reset()) | |
| 160 ]); | |
| 161 }); | |
| 162 }, 'methods requiring it reject when the device is not open'); | 158 }, 'methods requiring it reject when the device is not open'); |
| 163 | 159 |
| 164 usb_test(() => { | 160 usb_test(() => { |
| 165 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 161 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 166 | |
| 167 return navigator.usb.getDevices().then(devices => { | |
| 168 assert_equals(1, devices.length); | |
| 169 let device = devices[0]; | |
| 170 assert_equals(device.configuration, null); | 162 assert_equals(device.configuration, null); |
| 171 return device.open() | 163 return device.open() |
| 172 .then(() => { | 164 .then(() => { |
| 173 assert_equals(device.configuration, null); | 165 assert_equals(device.configuration, null); |
| 174 return device.selectConfiguration(1); | 166 return device.selectConfiguration(1); |
| 175 }) | 167 }) |
| 176 .then(() => { | 168 .then(() => { |
| 177 assertDeviceInfoEquals( | 169 assertDeviceInfoEquals( |
| 178 device.configuration, fakeDeviceInit.configurations[0]); | 170 device.configuration, fakeDeviceInit.configurations[0]); |
| 179 }) | 171 }) |
| 180 .then(() => device.close()); | 172 .then(() => device.close()); |
| 181 }); | 173 }); |
| 182 }, 'device configuration can be set and queried'); | 174 }, 'device configuration can be set and queried'); |
| 183 | 175 |
| 184 usb_test(() => { | 176 usb_test(() => { |
| 185 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 177 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 186 | |
| 187 return navigator.usb.getDevices().then(devices => { | |
| 188 assert_equals(1, devices.length); | |
| 189 let device = devices[0]; | |
| 190 assert_equals(device.configuration, null); | 178 assert_equals(device.configuration, null); |
| 191 return device.open() | 179 return device.open() |
| 192 .then(() => assertRejectsWithError( | 180 .then(() => assertRejectsWithError( |
| 193 device.selectConfiguration(3), 'NotFoundError', | 181 device.selectConfiguration(3), 'NotFoundError', |
| 194 'The configuration value provided is not supported by the device.')) | 182 'The configuration value provided is not supported by the device.')) |
| 195 .then(() => device.close()); | 183 .then(() => device.close()); |
| 196 }); | 184 }); |
| 197 }, 'selectConfiguration rejects on invalid configurations'); | 185 }, 'selectConfiguration rejects on invalid configurations'); |
| 198 | 186 |
| 199 usb_test(() => { | 187 usb_test(() => { |
| 200 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 188 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 201 | |
| 202 return navigator.usb.getDevices().then(devices => { | |
| 203 assert_equals(1, devices.length); | |
| 204 let device = devices[0]; | |
| 205 assert_equals(device.configuration, null); | 189 assert_equals(device.configuration, null); |
| 206 return device.open().then(() => Promise.all([ | 190 return device.open().then(() => Promise.all([ |
| 207 assertRejectsWithNotConfiguredError(device.claimInterface(0)), | 191 assertRejectsWithNotConfiguredError(device.claimInterface(0)), |
| 208 assertRejectsWithNotConfiguredError(device.releaseInterface(0)), | 192 assertRejectsWithNotConfiguredError(device.releaseInterface(0)), |
| 209 assertRejectsWithNotConfiguredError(device.selectAlternateInterface(0, 1 )), | 193 assertRejectsWithNotConfiguredError(device.selectAlternateInterface(0, 1 )), |
| 210 assertRejectsWithNotConfiguredError(device.controlTransferIn({ | 194 assertRejectsWithNotConfiguredError(device.controlTransferIn({ |
| 211 requestType: 'vendor', | 195 requestType: 'vendor', |
| 212 recipient: 'device', | 196 recipient: 'device', |
| 213 request: 0x42, | 197 request: 0x42, |
| 214 value: 0x1234, | 198 value: 0x1234, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 227 device.transferOut(1, new ArrayBuffer(8))), | 211 device.transferOut(1, new ArrayBuffer(8))), |
| 228 assertRejectsWithNotConfiguredError( | 212 assertRejectsWithNotConfiguredError( |
| 229 device.isochronousTransferIn(1, [8])), | 213 device.isochronousTransferIn(1, [8])), |
| 230 assertRejectsWithNotConfiguredError( | 214 assertRejectsWithNotConfiguredError( |
| 231 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])), | 215 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])), |
| 232 ])).then(() => device.close()); | 216 ])).then(() => device.close()); |
| 233 }); | 217 }); |
| 234 }, 'methods requiring it reject when the device is unconfigured'); | 218 }, 'methods requiring it reject when the device is unconfigured'); |
| 235 | 219 |
| 236 usb_test(() => { | 220 usb_test(() => { |
| 237 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 221 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 238 | |
| 239 return navigator.usb.getDevices().then(devices => { | |
| 240 assert_equals(1, devices.length); | |
| 241 let device = devices[0]; | |
| 242 return device.open() | 222 return device.open() |
| 243 .then(() => device.selectConfiguration(1)) | 223 .then(() => device.selectConfiguration(1)) |
| 244 .then(() => device.claimInterface(0)) | 224 .then(() => device.claimInterface(0)) |
| 245 .then(() => { | 225 .then(() => { |
| 246 assert_true(device.configuration.interfaces[0].claimed); | 226 assert_true(device.configuration.interfaces[0].claimed); |
| 247 return device.releaseInterface(0); | 227 return device.releaseInterface(0); |
| 248 }) | 228 }) |
| 249 .then(() => { | 229 .then(() => { |
| 250 assert_false(device.configuration.interfaces[0].claimed); | 230 assert_false(device.configuration.interfaces[0].claimed); |
| 251 return device.close(); | 231 return device.close(); |
| 252 }); | 232 }); |
| 253 }); | 233 }); |
| 254 }, 'an interface can be claimed and released'); | 234 }, 'an interface can be claimed and released'); |
| 255 | 235 |
| 256 usb_test(() => { | 236 usb_test(() => { |
| 257 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 237 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 258 | |
| 259 return navigator.usb.getDevices().then(devices => { | |
| 260 assert_equals(1, devices.length); | |
| 261 let device = devices[0]; | |
| 262 return device.open() | 238 return device.open() |
| 263 .then(() => device.selectConfiguration(1)) | 239 .then(() => device.selectConfiguration(1)) |
| 264 .then(() => device.claimInterface(0)) | 240 .then(() => device.claimInterface(0)) |
| 265 .then(() => { | 241 .then(() => { |
| 266 assert_true(device.configuration.interfaces[0].claimed); | 242 assert_true(device.configuration.interfaces[0].claimed); |
| 267 return device.close(0); | 243 return device.close(0); |
| 268 }) | 244 }) |
| 269 .then(() => { | 245 .then(() => { |
| 270 assert_false(device.configuration.interfaces[0].claimed); | 246 assert_false(device.configuration.interfaces[0].claimed); |
| 271 }); | 247 }); |
| 272 }); | 248 }); |
| 273 }, 'interfaces are released on close'); | 249 }, 'interfaces are released on close'); |
| 274 | 250 |
| 275 usb_test(() => { | 251 usb_test(() => { |
| 276 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 252 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 277 | |
| 278 return navigator.usb.getDevices().then(devices => { | |
| 279 assert_equals(1, devices.length); | |
| 280 let device = devices[0]; | |
| 281 const message = 'The interface number provided is not supported by the ' + | 253 const message = 'The interface number provided is not supported by the ' + |
| 282 'device in its current configuration.'; | 254 'device in its current configuration.'; |
| 283 return device.open() | 255 return device.open() |
| 284 .then(() => device.selectConfiguration(1)) | 256 .then(() => device.selectConfiguration(1)) |
| 285 .then(() => Promise.all([ | 257 .then(() => Promise.all([ |
| 286 assertRejectsWithError( | 258 assertRejectsWithError( |
| 287 device.claimInterface(2), 'NotFoundError', message), | 259 device.claimInterface(2), 'NotFoundError', message), |
| 288 assertRejectsWithError( | 260 assertRejectsWithError( |
| 289 device.releaseInterface(2), 'NotFoundError', message), | 261 device.releaseInterface(2), 'NotFoundError', message), |
| 290 ])) | 262 ])) |
| 291 .then(() => device.close()); | 263 .then(() => device.close()); |
| 292 }); | 264 }); |
| 293 }, 'a non-existent interface cannot be claimed or released'); | 265 }, 'a non-existent interface cannot be claimed or released'); |
| 294 | 266 |
| 295 usb_test(() => { | 267 usb_test(() => { |
| 268 let addPromise = connectionEventPromise('connect'); | |
| 296 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 269 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 297 | 270 |
| 298 return navigator.usb.getDevices().then(devices => { | 271 return addPromise.then(device => { |
| 299 assert_equals(1, devices.length); | |
| 300 var device = devices[0]; | |
| 301 return device.open() | 272 return device.open() |
| 302 .then(() => device.selectConfiguration(1)) | 273 .then(() => device.selectConfiguration(1)) |
| 303 .then(() => { | 274 .then(() => { |
| 275 let removePromise = connectionEventPromise('disconnect'); | |
| 304 navigator.usb.test.removeFakeDevice(guid); | 276 navigator.usb.test.removeFakeDevice(guid); |
| 305 return assertRejectsWithNotFoundError(device.claimInterface(0)); | 277 return removePromise.then(() => { |
| 278 return assertRejectsWithNotFoundError(device.claimInterface(0)); | |
| 279 }); | |
| 306 }); | 280 }); |
| 307 }); | 281 }); |
| 308 }, 'claimInterface rejects when called on a disconnected device'); | 282 }, 'claimInterface rejects when called on a disconnected device'); |
| 309 | 283 |
| 310 usb_test(() => { | 284 usb_test(() => { |
| 285 let addPromise = connectionEventPromise('connect'); | |
| 311 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 286 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 312 | 287 |
| 313 return navigator.usb.getDevices().then(devices => { | 288 return addPromise.then(device => { |
| 314 assert_equals(1, devices.length); | |
| 315 var device = devices[0]; | |
| 316 return device.open() | 289 return device.open() |
| 317 .then(() => device.selectConfiguration(1)) | 290 .then(() => device.selectConfiguration(1)) |
| 318 .then(() => device.claimInterface(0)) | 291 .then(() => device.claimInterface(0)) |
| 319 .then(() => { | 292 .then(() => { |
| 293 let removePromise = connectionEventPromise('disconnect'); | |
| 320 navigator.usb.test.removeFakeDevice(guid); | 294 navigator.usb.test.removeFakeDevice(guid); |
| 321 return assertRejectsWithNotFoundError(device.releaseInterface(0)); | 295 return removePromise.then(() => { |
| 296 return assertRejectsWithNotFoundError(device.releaseInterface(0)); | |
| 297 }); | |
| 322 }); | 298 }); |
| 323 }); | 299 }); |
| 324 }, 'releaseInterface rejects when called on a disconnected device'); | 300 }, 'releaseInterface rejects when called on a disconnected device'); |
| 325 | 301 |
| 326 usb_test(() => { | 302 usb_test(() => { |
| 327 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 303 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 328 | |
| 329 return navigator.usb.getDevices().then(devices => { | |
| 330 assert_equals(1, devices.length); | |
| 331 let device = devices[0]; | |
| 332 return device.open() | 304 return device.open() |
| 333 .then(() => device.selectConfiguration(2)) | 305 .then(() => device.selectConfiguration(2)) |
| 334 .then(() => device.claimInterface(0)) | 306 .then(() => device.claimInterface(0)) |
| 335 .then(() => device.selectAlternateInterface(0, 1)) | 307 .then(() => device.selectAlternateInterface(0, 1)) |
| 336 .then(() => device.close()); | 308 .then(() => device.close()); |
| 337 }); | 309 }); |
| 338 }, 'can select an alternate interface'); | 310 }, 'can select an alternate interface'); |
| 339 | 311 |
| 340 usb_test(() => { | 312 usb_test(() => { |
| 341 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 313 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 342 | |
| 343 return navigator.usb.getDevices().then(devices => { | |
| 344 assert_equals(1, devices.length); | |
| 345 let device = devices[0]; | |
| 346 return device.open() | 314 return device.open() |
| 347 .then(() => device.selectConfiguration(2)) | 315 .then(() => device.selectConfiguration(2)) |
| 348 .then(() => device.claimInterface(0)) | 316 .then(() => device.claimInterface(0)) |
| 349 .then(() => assertRejectsWithError( | 317 .then(() => assertRejectsWithError( |
| 350 device.selectAlternateInterface(0, 2), 'NotFoundError', | 318 device.selectAlternateInterface(0, 2), 'NotFoundError', |
| 351 'The alternate setting provided is not supported by the device in ' + | 319 'The alternate setting provided is not supported by the device in ' + |
| 352 'its current configuration.')) | 320 'its current configuration.')) |
| 353 .then(() => device.close()); | 321 .then(() => device.close()); |
| 354 }); | 322 }); |
| 355 }, 'cannot select a non-existent alternate interface'); | 323 }, 'cannot select a non-existent alternate interface'); |
| 356 | 324 |
| 357 usb_test(() => { | 325 usb_test(() => { |
| 326 let addPromise = connectionEventPromise('connect'); | |
| 358 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 327 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 359 | 328 |
| 360 return navigator.usb.getDevices().then(devices => { | 329 return addPromise.then(device => { |
| 361 assert_equals(1, devices.length); | |
| 362 var device = devices[0]; | |
| 363 return device.open() | 330 return device.open() |
| 364 .then(() => device.selectConfiguration(2)) | 331 .then(() => device.selectConfiguration(2)) |
| 365 .then(() => device.claimInterface(0)) | 332 .then(() => device.claimInterface(0)) |
| 366 .then(() => { | 333 .then(() => { |
| 334 let removePromise = connectionEventPromise('disconnect'); | |
| 367 navigator.usb.test.removeFakeDevice(guid); | 335 navigator.usb.test.removeFakeDevice(guid); |
| 368 return assertRejectsWithNotFoundError(device.selectAlternateInterface(0, 1)); | 336 return removePromise.then(() => { |
| 337 return assertRejectsWithNotFoundError(device.selectAlternateInterface( 0, 1)); | |
| 338 }); | |
| 369 }); | 339 }); |
| 370 }); | 340 }); |
| 371 }, 'selectAlternateInterface rejects when called on a disconnected device'); | 341 }, 'selectAlternateInterface rejects when called on a disconnected device'); |
| 372 | 342 |
| 373 usb_test(() => { | 343 usb_test(() => { |
| 374 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 344 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 375 | |
| 376 return navigator.usb.getDevices().then(devices => { | |
| 377 assert_equals(1, devices.length); | |
| 378 let device = devices[0]; | |
| 379 return device.open() | 345 return device.open() |
| 380 .then(() => device.selectConfiguration(1)) | 346 .then(() => device.selectConfiguration(1)) |
| 381 .then(() => device.controlTransferIn({ | 347 .then(() => device.controlTransferIn({ |
| 382 requestType: 'vendor', | 348 requestType: 'vendor', |
| 383 recipient: 'device', | 349 recipient: 'device', |
| 384 request: 0x42, | 350 request: 0x42, |
| 385 value: 0x1234, | 351 value: 0x1234, |
| 386 index: 0x5678 | 352 index: 0x5678 |
| 387 }, 7)) | 353 }, 7)) |
| 388 .then(result => { | 354 .then(result => { |
| 389 assert_true(result instanceof USBInTransferResult); | 355 assert_true(result instanceof USBInTransferResult); |
| 390 assert_equals(result.status, 'ok'); | 356 assert_equals(result.status, 'ok'); |
| 391 assert_equals(result.data.byteLength, 7); | 357 assert_equals(result.data.byteLength, 7); |
| 392 assert_equals(result.data.getUint16(0), 0x07); | 358 assert_equals(result.data.getUint16(0), 0x07); |
| 393 assert_equals(result.data.getUint8(2), 0x42); | 359 assert_equals(result.data.getUint8(2), 0x42); |
| 394 assert_equals(result.data.getUint16(3), 0x1234); | 360 assert_equals(result.data.getUint16(3), 0x1234); |
| 395 assert_equals(result.data.getUint16(5), 0x5678); | 361 assert_equals(result.data.getUint16(5), 0x5678); |
| 396 return device.close(); | 362 return device.close(); |
| 397 }); | 363 }); |
| 398 }); | 364 }); |
| 399 }, 'can issue IN control transfer'); | 365 }, 'can issue IN control transfer'); |
| 400 | 366 |
| 401 usb_test(() => { | 367 usb_test(() => { |
| 368 let addPromise = connectionEventPromise('connect'); | |
| 402 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 369 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 403 | 370 |
| 404 return navigator.usb.getDevices().then(devices => { | 371 return addPromise.then(device => { |
| 405 assert_equals(1, devices.length); | |
| 406 let device = devices[0]; | |
| 407 return device.open() | 372 return device.open() |
| 408 .then(() => device.selectConfiguration(1)) | 373 .then(() => device.selectConfiguration(1)) |
| 409 .then(() => { | 374 .then(() => { |
| 375 let removePromise = connectionEventPromise('disconnect'); | |
| 410 navigator.usb.test.removeFakeDevice(guid); | 376 navigator.usb.test.removeFakeDevice(guid); |
| 411 return assertRejectsWithNotFoundError(device.controlTransferIn({ | 377 return removePromise.then(() => { |
| 412 requestType: 'vendor', | 378 return assertRejectsWithNotFoundError(device.controlTransferIn({ |
| 413 recipient: 'device', | 379 requestType: 'vendor', |
| 414 request: 0x42, | 380 recipient: 'device', |
| 415 value: 0x1234, | 381 request: 0x42, |
| 416 index: 0x5678 | 382 value: 0x1234, |
| 417 }, 7)); | 383 index: 0x5678 |
| 384 }, 7)); | |
| 385 }); | |
| 418 }); | 386 }); |
| 419 }); | 387 }); |
| 420 }, 'controlTransferIn rejects when called on a disconnected device'); | 388 }, 'controlTransferIn rejects when called on a disconnected device'); |
| 421 | 389 |
| 422 usb_test(() => { | 390 usb_test(() => { |
| 423 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 391 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 424 | |
| 425 return navigator.usb.getDevices().then(devices => { | |
| 426 assert_equals(1, devices.length); | |
| 427 let device = devices[0]; | |
| 428 return device.open() | 392 return device.open() |
| 429 .then(() => device.selectConfiguration(1)) | 393 .then(() => device.selectConfiguration(1)) |
| 430 .then(() => device.controlTransferOut({ | 394 .then(() => device.controlTransferOut({ |
| 431 requestType: 'vendor', | 395 requestType: 'vendor', |
| 432 recipient: 'device', | 396 recipient: 'device', |
| 433 request: 0x42, | 397 request: 0x42, |
| 434 value: 0x1234, | 398 value: 0x1234, |
| 435 index: 0x5678 | 399 index: 0x5678 |
| 436 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))) | 400 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))) |
| 437 .then(result => { | 401 .then(result => { |
| 438 assert_true(result instanceof USBOutTransferResult); | 402 assert_true(result instanceof USBOutTransferResult); |
| 439 assert_equals(result.status, 'ok'); | 403 assert_equals(result.status, 'ok'); |
| 440 assert_equals(result.bytesWritten, 8); | 404 assert_equals(result.bytesWritten, 8); |
| 441 return device.close(); | 405 return device.close(); |
| 442 }) | 406 }) |
| 443 }); | 407 }); |
| 444 }, 'can issue OUT control transfer'); | 408 }, 'can issue OUT control transfer'); |
| 445 | 409 |
| 446 usb_test(() => { | 410 usb_test(() => { |
| 411 let addPromise = connectionEventPromise('connect'); | |
| 447 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 412 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 448 | 413 |
| 449 return navigator.usb.getDevices().then(devices => { | 414 return addPromise.then(device => { |
| 450 assert_equals(1, devices.length); | |
| 451 let device = devices[0]; | |
| 452 return device.open() | 415 return device.open() |
| 453 .then(() => device.selectConfiguration(1)) | 416 .then(() => device.selectConfiguration(1)) |
| 454 .then(() => { | 417 .then(() => { |
| 418 let removePromise = connectionEventPromise('disconnect'); | |
| 455 navigator.usb.test.removeFakeDevice(guid); | 419 navigator.usb.test.removeFakeDevice(guid); |
| 456 return assertRejectsWithNotFoundError(device.controlTransferOut({ | 420 return removePromise.then(() => { |
| 457 requestType: 'vendor', | 421 return assertRejectsWithNotFoundError(device.controlTransferOut({ |
| 458 recipient: 'device', | 422 requestType: 'vendor', |
| 459 request: 0x42, | 423 recipient: 'device', |
| 460 value: 0x1234, | 424 request: 0x42, |
| 461 index: 0x5678 | 425 value: 0x1234, |
| 462 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))); | 426 index: 0x5678 |
| 427 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))); | |
| 428 }); | |
| 463 }); | 429 }); |
| 464 }); | 430 }); |
| 465 }, 'controlTransferOut rejects when called on a disconnected device'); | 431 }, 'controlTransferOut rejects when called on a disconnected device'); |
| 466 | 432 |
| 467 usb_test(() => { | 433 usb_test(() => { |
| 468 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 434 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 469 | |
| 470 return navigator.usb.getDevices().then(devices => { | |
| 471 assert_equals(1, devices.length); | |
| 472 let device = devices[0]; | |
| 473 let interfaceRequest = { | 435 let interfaceRequest = { |
| 474 requestType: 'vendor', | 436 requestType: 'vendor', |
| 475 recipient: 'interface', | 437 recipient: 'interface', |
| 476 request: 0x42, | 438 request: 0x42, |
| 477 value: 0x1234, | 439 value: 0x1234, |
| 478 index: 0x5600 // Last byte of index is interface number. | 440 index: 0x5600 // Last byte of index is interface number. |
| 479 }; | 441 }; |
| 480 let endpointRequest = { | 442 let endpointRequest = { |
| 481 requestType: 'vendor', | 443 requestType: 'vendor', |
| 482 recipient: 'endpoint', | 444 recipient: 'endpoint', |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 522 assert_equals(result.data.getUint16(5), 0x5681); | 484 assert_equals(result.data.getUint16(5), 0x5681); |
| 523 }), | 485 }), |
| 524 device.controlTransferOut(interfaceRequest, data), | 486 device.controlTransferOut(interfaceRequest, data), |
| 525 device.controlTransferOut(endpointRequest, data), | 487 device.controlTransferOut(endpointRequest, data), |
| 526 ])) | 488 ])) |
| 527 .then(() => device.close()); | 489 .then(() => device.close()); |
| 528 }); | 490 }); |
| 529 }, 'requests to interfaces and endpoint require an interface claim'); | 491 }, 'requests to interfaces and endpoint require an interface claim'); |
| 530 | 492 |
| 531 usb_test(() => { | 493 usb_test(() => { |
| 532 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 494 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 533 | |
| 534 return navigator.usb.getDevices().then(devices => { | |
| 535 assert_equals(devices.length, 1); | |
| 536 let device = devices[0]; | |
| 537 return device.open() | 495 return device.open() |
| 538 .then(() => device.selectConfiguration(1)) | 496 .then(() => device.selectConfiguration(1)) |
| 539 .then(() => device.claimInterface(0)) | 497 .then(() => device.claimInterface(0)) |
| 540 .then(() => device.clearHalt('in', 1)) | 498 .then(() => device.clearHalt('in', 1)) |
| 541 .then(() => device.close()); | 499 .then(() => device.close()); |
| 542 }); | 500 }); |
| 543 }, 'can clear a halt condition'); | 501 }, 'can clear a halt condition'); |
| 544 | 502 |
| 545 usb_test(() => { | 503 usb_test(() => { |
| 504 let addPromise = connectionEventPromise('connect'); | |
| 546 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 505 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 547 | 506 |
| 548 return navigator.usb.getDevices().then(devices => { | 507 return addPromise.then(device => { |
| 549 assert_equals(devices.length, 1); | |
| 550 let device = devices[0]; | |
| 551 return device.open() | 508 return device.open() |
| 552 .then(() => device.selectConfiguration(1)) | 509 .then(() => device.selectConfiguration(1)) |
| 553 .then(() => device.claimInterface(0)) | 510 .then(() => device.claimInterface(0)) |
| 554 .then(() => { | 511 .then(() => { |
| 512 let removePromise = connectionEventPromise('disconnect'); | |
| 555 navigator.usb.test.removeFakeDevice(guid); | 513 navigator.usb.test.removeFakeDevice(guid); |
| 556 return assertRejectsWithNotFoundError(device.clearHalt('in', 1)); | 514 return removePromise.then(() => { |
| 515 return assertRejectsWithNotFoundError(device.clearHalt('in', 1)); | |
| 516 }); | |
| 557 }); | 517 }); |
| 558 }); | 518 }); |
| 559 }, 'clearHalt rejects when called on a disconnected device'); | 519 }, 'clearHalt rejects when called on a disconnected device'); |
| 560 | 520 |
| 561 usb_test(() => { | 521 usb_test(() => { |
| 562 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 522 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 563 | |
| 564 return navigator.usb.getDevices().then(devices => { | |
| 565 assert_equals(devices.length, 1); | |
| 566 let device = devices[0]; | |
| 567 let data = new DataView(new ArrayBuffer(1024)); | 523 let data = new DataView(new ArrayBuffer(1024)); |
| 568 for (let i = 0; i < 1024; ++i) | 524 for (let i = 0; i < 1024; ++i) |
| 569 data.setUint8(i, i & 0xff); | 525 data.setUint8(i, i & 0xff); |
| 570 const notFoundMessage = 'The specified endpoint is not part of a claimed ' + | 526 const notFoundMessage = 'The specified endpoint is not part of a claimed ' + |
| 571 'and selected alternate interface.'; | 527 'and selected alternate interface.'; |
| 572 const rangeError = 'The specified endpoint number is out of range.'; | 528 const rangeError = 'The specified endpoint number is out of range.'; |
| 573 return device.open() | 529 return device.open() |
| 574 .then(() => device.selectConfiguration(1)) | 530 .then(() => device.selectConfiguration(1)) |
| 575 .then(() => device.claimInterface(0)) | 531 .then(() => device.claimInterface(0)) |
| 576 .then(() => Promise.all([ | 532 .then(() => Promise.all([ |
| 577 assertRejectsWithError(device.transferIn(2, 8), | 533 assertRejectsWithError(device.transferIn(2, 8), |
| 578 'NotFoundError', notFoundMessage), // Unclaimed | 534 'NotFoundError', notFoundMessage), // Unclaimed |
| 579 assertRejectsWithError(device.transferIn(3, 8), 'NotFoundError', | 535 assertRejectsWithError(device.transferIn(3, 8), 'NotFoundError', |
| 580 notFoundMessage), // Non-existent | 536 notFoundMessage), // Non-existent |
| 581 assertRejectsWithError( | 537 assertRejectsWithError( |
| 582 device.transferIn(16, 8), 'IndexSizeError', rangeError), | 538 device.transferIn(16, 8), 'IndexSizeError', rangeError), |
| 583 assertRejectsWithError(device.transferOut(2, data), | 539 assertRejectsWithError(device.transferOut(2, data), |
| 584 'NotFoundError', notFoundMessage), // Unclaimed | 540 'NotFoundError', notFoundMessage), // Unclaimed |
| 585 assertRejectsWithError(device.transferOut(3, data), 'NotFoundError', | 541 assertRejectsWithError(device.transferOut(3, data), 'NotFoundError', |
| 586 notFoundMessage), // Non-existent | 542 notFoundMessage), // Non-existent |
| 587 assertRejectsWithError( | 543 assertRejectsWithError( |
| 588 device.transferOut(16, data), 'IndexSizeError', rangeError), | 544 device.transferOut(16, data), 'IndexSizeError', rangeError), |
| 589 ])); | 545 ])); |
| 590 }); | 546 }); |
| 591 }, 'transfers to unavailable endpoints are rejected'); | 547 }, 'transfers to unavailable endpoints are rejected'); |
| 592 | 548 |
| 593 usb_test(() => { | 549 usb_test(() => { |
| 594 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 550 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 595 | |
| 596 return navigator.usb.getDevices().then(devices => { | |
| 597 assert_equals(devices.length, 1); | |
| 598 let device = devices[0]; | |
| 599 return device.open() | 551 return device.open() |
| 600 .then(() => device.selectConfiguration(1)) | 552 .then(() => device.selectConfiguration(1)) |
| 601 .then(() => device.claimInterface(0)) | 553 .then(() => device.claimInterface(0)) |
| 602 .then(() => device.transferIn(1, 8)) | 554 .then(() => device.transferIn(1, 8)) |
| 603 .then(result => { | 555 .then(result => { |
| 604 assert_true(result instanceof USBInTransferResult); | 556 assert_true(result instanceof USBInTransferResult); |
| 605 assert_equals(result.status, 'ok'); | 557 assert_equals(result.status, 'ok'); |
| 606 assert_equals(result.data.byteLength, 8); | 558 assert_equals(result.data.byteLength, 8); |
| 607 for (let i = 0; i < 8; ++i) | 559 for (let i = 0; i < 8; ++i) |
| 608 assert_equals(result.data.getUint8(i), i, 'mismatch at byte ' + i); | 560 assert_equals(result.data.getUint8(i), i, 'mismatch at byte ' + i); |
| 609 return device.close(); | 561 return device.close(); |
| 610 }); | 562 }); |
| 611 }); | 563 }); |
| 612 }, 'can issue IN interrupt transfer'); | 564 }, 'can issue IN interrupt transfer'); |
| 613 | 565 |
| 614 usb_test(() => { | 566 usb_test(() => { |
| 615 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 567 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 616 | |
| 617 return navigator.usb.getDevices().then(devices => { | |
| 618 assert_equals(devices.length, 1); | |
| 619 let device = devices[0]; | |
| 620 return device.open() | 568 return device.open() |
| 621 .then(() => device.selectConfiguration(1)) | 569 .then(() => device.selectConfiguration(1)) |
| 622 .then(() => device.claimInterface(1)) | 570 .then(() => device.claimInterface(1)) |
| 623 .then(() => device.transferIn(2, 1024)) | 571 .then(() => device.transferIn(2, 1024)) |
| 624 .then(result => { | 572 .then(result => { |
| 625 assert_true(result instanceof USBInTransferResult); | 573 assert_true(result instanceof USBInTransferResult); |
| 626 assert_equals(result.status, 'ok'); | 574 assert_equals(result.status, 'ok'); |
| 627 assert_equals(result.data.byteLength, 1024); | 575 assert_equals(result.data.byteLength, 1024); |
| 628 for (let i = 0; i < 1024; ++i) | 576 for (let i = 0; i < 1024; ++i) |
| 629 assert_equals(result.data.getUint8(i), i & 0xff, | 577 assert_equals(result.data.getUint8(i), i & 0xff, |
| 630 'mismatch at byte ' + i); | 578 'mismatch at byte ' + i); |
| 631 return device.close(); | 579 return device.close(); |
| 632 }); | 580 }); |
| 633 }); | 581 }); |
| 634 }, 'can issue IN bulk transfer'); | 582 }, 'can issue IN bulk transfer'); |
| 635 | 583 |
| 636 usb_test(() => { | 584 usb_test(() => { |
| 585 let addPromise = connectionEventPromise('connect'); | |
| 637 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 586 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 638 | 587 |
| 639 return navigator.usb.getDevices().then(devices => { | 588 return addPromise.then(device => { |
| 640 assert_equals(devices.length, 1); | |
| 641 let device = devices[0]; | |
| 642 return device.open() | 589 return device.open() |
| 643 .then(() => device.selectConfiguration(1)) | 590 .then(() => device.selectConfiguration(1)) |
| 644 .then(() => device.claimInterface(1)) | 591 .then(() => device.claimInterface(1)) |
| 645 .then(() => { | 592 .then(() => { |
| 593 let removePromise = connectionEventPromise('disconnect'); | |
| 646 navigator.usb.test.removeFakeDevice(guid); | 594 navigator.usb.test.removeFakeDevice(guid); |
| 647 return assertRejectsWithNotFoundError(device.transferIn(2, 1024)); | 595 return removePromise.then(() => { |
| 596 return assertRejectsWithNotFoundError(device.transferIn(2, 1024)); | |
| 597 }); | |
| 648 }); | 598 }); |
| 649 }); | 599 }); |
| 650 }, 'transferIn rejects if called on a disconnected device'); | 600 }, 'transferIn rejects if called on a disconnected device'); |
| 651 | 601 |
| 652 usb_test(() => { | 602 usb_test(() => { |
| 653 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 603 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 654 | |
| 655 return navigator.usb.getDevices().then(devices => { | |
| 656 assert_equals(devices.length, 1); | |
| 657 let device = devices[0]; | |
| 658 return device.open() | 604 return device.open() |
| 659 .then(() => device.selectConfiguration(1)) | 605 .then(() => device.selectConfiguration(1)) |
| 660 .then(() => device.claimInterface(1)) | 606 .then(() => device.claimInterface(1)) |
| 661 .then(() => { | 607 .then(() => { |
| 662 let data = new DataView(new ArrayBuffer(1024)); | 608 let data = new DataView(new ArrayBuffer(1024)); |
| 663 for (let i = 0; i < 1024; ++i) | 609 for (let i = 0; i < 1024; ++i) |
| 664 data.setUint8(i, i & 0xff); | 610 data.setUint8(i, i & 0xff); |
| 665 return device.transferOut(2, data); | 611 return device.transferOut(2, data); |
| 666 }) | 612 }) |
| 667 .then(result => { | 613 .then(result => { |
| 668 assert_true(result instanceof USBOutTransferResult); | 614 assert_true(result instanceof USBOutTransferResult); |
| 669 assert_equals(result.status, 'ok'); | 615 assert_equals(result.status, 'ok'); |
| 670 assert_equals(result.bytesWritten, 1024); | 616 assert_equals(result.bytesWritten, 1024); |
| 671 return device.close(); | 617 return device.close(); |
| 672 }); | 618 }); |
| 673 }); | 619 }); |
| 674 }, 'can issue OUT bulk transfer'); | 620 }, 'can issue OUT bulk transfer'); |
| 675 | 621 |
| 676 usb_test(() => { | 622 usb_test(() => { |
| 623 let addPromise = connectionEventPromise('connect'); | |
| 677 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 624 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 678 | 625 |
| 679 return navigator.usb.getDevices().then(devices => { | 626 return addPromise.then(device => { |
| 680 assert_equals(devices.length, 1); | |
| 681 let device = devices[0]; | |
| 682 return device.open() | 627 return device.open() |
| 683 .then(() => device.selectConfiguration(1)) | 628 .then(() => device.selectConfiguration(1)) |
| 684 .then(() => device.claimInterface(1)) | 629 .then(() => device.claimInterface(1)) |
| 685 .then(() => { | 630 .then(() => { |
| 686 let data = new DataView(new ArrayBuffer(1024)); | 631 let data = new DataView(new ArrayBuffer(1024)); |
| 687 for (let i = 0; i < 1024; ++i) | 632 for (let i = 0; i < 1024; ++i) |
| 688 data.setUint8(i, i & 0xff); | 633 data.setUint8(i, i & 0xff); |
| 634 let removePromise = connectionEventPromise('disconnect'); | |
| 689 navigator.usb.test.removeFakeDevice(guid); | 635 navigator.usb.test.removeFakeDevice(guid); |
| 690 return assertRejectsWithNotFoundError(device.transferOut(2, data)); | 636 return removePromise.then(() => { |
| 637 return assertRejectsWithNotFoundError(device.transferOut(2, data)); | |
| 638 }); | |
| 691 }); | 639 }); |
| 692 }); | 640 }); |
| 693 }, 'transferOut rejects if called on a disconnected device'); | 641 }, 'transferOut rejects if called on a disconnected device'); |
| 694 | 642 |
| 695 usb_test(() => { | 643 usb_test(() => { |
| 696 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 644 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 697 | |
| 698 return navigator.usb.getDevices().then(devices => { | |
| 699 assert_equals(devices.length, 1); | |
| 700 let device = devices[0]; | |
| 701 return device.open() | 645 return device.open() |
| 702 .then(() => device.selectConfiguration(2)) | 646 .then(() => device.selectConfiguration(2)) |
| 703 .then(() => device.claimInterface(0)) | 647 .then(() => device.claimInterface(0)) |
| 704 .then(() => device.selectAlternateInterface(0, 1)) | 648 .then(() => device.selectAlternateInterface(0, 1)) |
| 705 .then(() => device.isochronousTransferIn( | 649 .then(() => device.isochronousTransferIn( |
| 706 1, [64, 64, 64, 64, 64, 64, 64, 64])) | 650 1, [64, 64, 64, 64, 64, 64, 64, 64])) |
| 707 .then(result => { | 651 .then(result => { |
| 708 assert_true(result instanceof USBIsochronousInTransferResult); | 652 assert_true(result instanceof USBIsochronousInTransferResult); |
| 709 assert_equals(result.data.byteLength, 64 * 8, 'buffer size'); | 653 assert_equals(result.data.byteLength, 64 * 8, 'buffer size'); |
| 710 assert_equals(result.packets.length, 8, 'number of packets'); | 654 assert_equals(result.packets.length, 8, 'number of packets'); |
| 711 let byteOffset = 0; | 655 let byteOffset = 0; |
| 712 for (let i = 0; i < result.packets.length; ++i) { | 656 for (let i = 0; i < result.packets.length; ++i) { |
| 713 assert_true( | 657 assert_true( |
| 714 result.packets[i] instanceof USBIsochronousInTransferPacket); | 658 result.packets[i] instanceof USBIsochronousInTransferPacket); |
| 715 assert_equals(result.packets[i].status, 'ok'); | 659 assert_equals(result.packets[i].status, 'ok'); |
| 716 assert_equals(result.packets[i].data.byteLength, 64); | 660 assert_equals(result.packets[i].data.byteLength, 64); |
| 717 assert_equals(result.packets[i].data.buffer, result.data.buffer); | 661 assert_equals(result.packets[i].data.buffer, result.data.buffer); |
| 718 assert_equals(result.packets[i].data.byteOffset, byteOffset); | 662 assert_equals(result.packets[i].data.byteOffset, byteOffset); |
| 719 for (let j = 0; j < 64; ++j) | 663 for (let j = 0; j < 64; ++j) |
| 720 assert_equals(result.packets[i].data.getUint8(j), j & 0xff, | 664 assert_equals(result.packets[i].data.getUint8(j), j & 0xff, |
| 721 'mismatch at byte ' + j + ' of packet ' + i); | 665 'mismatch at byte ' + j + ' of packet ' + i); |
| 722 byteOffset += result.packets[i].data.byteLength; | 666 byteOffset += result.packets[i].data.byteLength; |
| 723 } | 667 } |
| 724 return device.close(); | 668 return device.close(); |
| 725 }); | 669 }); |
| 726 }); | 670 }); |
| 727 }, 'can issue IN isochronous transfer'); | 671 }, 'can issue IN isochronous transfer'); |
| 728 | 672 |
| 729 usb_test(() => { | 673 usb_test(() => { |
| 674 let addPromise = connectionEventPromise('connect'); | |
| 730 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 675 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 731 | 676 |
| 732 return navigator.usb.getDevices().then(devices => { | 677 return addPromise.then(device => { |
| 733 assert_equals(devices.length, 1); | |
| 734 let device = devices[0]; | |
| 735 return device.open() | 678 return device.open() |
| 736 .then(() => device.selectConfiguration(2)) | 679 .then(() => device.selectConfiguration(2)) |
| 737 .then(() => device.claimInterface(0)) | 680 .then(() => device.claimInterface(0)) |
| 738 .then(() => device.selectAlternateInterface(0, 1)) | 681 .then(() => device.selectAlternateInterface(0, 1)) |
| 739 .then(() => { | 682 .then(() => { |
| 683 let removePromise = connectionEventPromise('disconnect'); | |
| 740 navigator.usb.test.removeFakeDevice(guid); | 684 navigator.usb.test.removeFakeDevice(guid); |
| 741 return assertRejectsWithNotFoundError(device.isochronousTransferIn( | 685 return removePromise.then(() => { |
| 742 1, [64, 64, 64, 64, 64, 64, 64, 64])); | 686 return assertRejectsWithNotFoundError(device.isochronousTransferIn( |
| 687 1, [64, 64, 64, 64, 64, 64, 64, 64])); | |
| 688 }); | |
| 743 }); | 689 }); |
| 744 }); | 690 }); |
| 745 }, 'isochronousTransferIn rejects when called on a disconnected device'); | 691 }, 'isochronousTransferIn rejects when called on a disconnected device'); |
| 746 | 692 |
| 747 usb_test(() => { | 693 usb_test(() => { |
| 748 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 694 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 749 | |
| 750 return navigator.usb.getDevices().then(devices => { | |
| 751 assert_equals(devices.length, 1); | |
| 752 let device = devices[0]; | |
| 753 return device.open() | 695 return device.open() |
| 754 .then(() => device.selectConfiguration(2)) | 696 .then(() => device.selectConfiguration(2)) |
| 755 .then(() => device.claimInterface(0)) | 697 .then(() => device.claimInterface(0)) |
| 756 .then(() => device.selectAlternateInterface(0, 1)) | 698 .then(() => device.selectAlternateInterface(0, 1)) |
| 757 .then(() => { | 699 .then(() => { |
| 758 let data = new DataView(new ArrayBuffer(64 * 8)); | 700 let data = new DataView(new ArrayBuffer(64 * 8)); |
| 759 for (let i = 0; i < 8; ++i) { | 701 for (let i = 0; i < 8; ++i) { |
| 760 for (let j = 0; j < 64; ++j) | 702 for (let j = 0; j < 64; ++j) |
| 761 data.setUint8(i * j, j & 0xff); | 703 data.setUint8(i * j, j & 0xff); |
| 762 } | 704 } |
| 763 return device.isochronousTransferOut( | 705 return device.isochronousTransferOut( |
| 764 1, data, [64, 64, 64, 64, 64, 64, 64, 64]); | 706 1, data, [64, 64, 64, 64, 64, 64, 64, 64]); |
| 765 }) | 707 }) |
| 766 .then(result => { | 708 .then(result => { |
| 767 assert_true(result instanceof USBIsochronousOutTransferResult); | 709 assert_true(result instanceof USBIsochronousOutTransferResult); |
| 768 assert_equals(result.packets.length, 8, 'number of packets'); | 710 assert_equals(result.packets.length, 8, 'number of packets'); |
| 769 let byteOffset = 0; | 711 let byteOffset = 0; |
| 770 for (let i = 0; i < result.packets.length; ++i) { | 712 for (let i = 0; i < result.packets.length; ++i) { |
| 771 assert_true( | 713 assert_true( |
| 772 result.packets[i] instanceof USBIsochronousOutTransferPacket); | 714 result.packets[i] instanceof USBIsochronousOutTransferPacket); |
| 773 assert_equals(result.packets[i].status, 'ok'); | 715 assert_equals(result.packets[i].status, 'ok'); |
| 774 assert_equals(result.packets[i].bytesWritten, 64); | 716 assert_equals(result.packets[i].bytesWritten, 64); |
| 775 } | 717 } |
| 776 return device.close(); | 718 return device.close(); |
| 777 }); | 719 }); |
| 778 }); | 720 }); |
| 779 }, 'can issue OUT isochronous transfer'); | 721 }, 'can issue OUT isochronous transfer'); |
| 780 | 722 |
| 781 usb_test(() => { | 723 usb_test(() => { |
| 724 let addPromise = connectionEventPromise('connect'); | |
| 782 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 725 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 783 | 726 |
| 784 return navigator.usb.getDevices().then(devices => { | 727 return addPromise.then(device => { |
| 785 assert_equals(devices.length, 1); | |
| 786 let device = devices[0]; | |
| 787 return device.open() | 728 return device.open() |
| 788 .then(() => device.selectConfiguration(2)) | 729 .then(() => device.selectConfiguration(2)) |
| 789 .then(() => device.claimInterface(0)) | 730 .then(() => device.claimInterface(0)) |
| 790 .then(() => device.selectAlternateInterface(0, 1)) | 731 .then(() => device.selectAlternateInterface(0, 1)) |
| 791 .then(() => { | 732 .then(() => { |
| 792 let data = new DataView(new ArrayBuffer(64 * 8)); | 733 let data = new DataView(new ArrayBuffer(64 * 8)); |
| 793 for (let i = 0; i < 8; ++i) { | 734 for (let i = 0; i < 8; ++i) { |
| 794 for (let j = 0; j < 64; ++j) | 735 for (let j = 0; j < 64; ++j) |
| 795 data.setUint8(i * j, j & 0xff); | 736 data.setUint8(i * j, j & 0xff); |
| 796 } | 737 } |
| 738 let removePromise = connectionEventPromise('disconnect'); | |
| 797 navigator.usb.test.removeFakeDevice(guid); | 739 navigator.usb.test.removeFakeDevice(guid); |
| 798 return assertRejectsWithNotFoundError(device.isochronousTransferOut( | 740 return removePromise.then(() => { |
| 799 1, data, [64, 64, 64, 64, 64, 64, 64, 64])); | 741 return assertRejectsWithNotFoundError(device.isochronousTransferOut( |
| 742 1, data, [64, 64, 64, 64, 64, 64, 64, 64])); | |
| 743 }); | |
| 800 }); | 744 }); |
| 801 }); | 745 }); |
| 802 }, 'isochronousTransferOut rejects when called on a disconnected device'); | 746 }, 'isochronousTransferOut rejects when called on a disconnected device'); |
| 803 | 747 |
| 804 usb_test(() => { | 748 usb_test(() => { |
| 805 navigator.usb.test.addFakeDevice(fakeDeviceInit); | 749 return getConnectedDevice(fakeDeviceInit).then(device => { |
| 806 | |
| 807 return navigator.usb.getDevices().then(devices => { | |
| 808 assert_equals(1, devices.length); | |
| 809 let device = devices[0]; | |
| 810 return device.open().then(() => device.reset()).then(() => device.close()); | 750 return device.open().then(() => device.reset()).then(() => device.close()); |
| 811 }); | 751 }); |
| 812 }, 'can reset the device'); | 752 }, 'can reset the device'); |
| 813 | 753 |
| 814 usb_test(() => { | 754 usb_test(() => { |
| 755 let addPromise = connectionEventPromise('connect'); | |
| 815 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); | 756 let guid = navigator.usb.test.addFakeDevice(fakeDeviceInit); |
| 816 | 757 |
| 817 return navigator.usb.getDevices().then(devices => { | 758 return addPromise.then(device => { |
| 818 assert_equals(1, devices.length); | |
| 819 let device = devices[0]; | |
| 820 return device.open().then(() => { | 759 return device.open().then(() => { |
| 760 let removePromise = connectionEventPromise('disconnect'); | |
| 821 navigator.usb.test.removeFakeDevice(guid); | 761 navigator.usb.test.removeFakeDevice(guid); |
| 822 return assertRejectsWithNotFoundError(device.reset()); | 762 return removePromise.then(() => { |
| 763 return assertRejectsWithNotFoundError(device.reset()); | |
| 764 }); | |
| 823 }); | 765 }); |
| 824 }); | 766 }); |
| 825 }, 'resetDevice rejects when called on a disconnected device'); | 767 }, 'resetDevice rejects when called on a disconnected device'); |
| 826 </script> | 768 </script> |
| OLD | NEW |