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