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