Best JavaScript code snippet using wpt
usbDevice.https.any.js
Source:usbDevice.https.any.js
...43 window.postMessage('', '*', [buffer]);44 else45 self.postMessage('', [buffer]);46}47usb_test(() => {48 return getFakeDevice().then(({ device, fakeDevice }) => {49 return waitForDisconnect(fakeDevice)50 .then(() => assertRejectsWithNotFoundError(device.open()));51 });52}, 'open rejects when called on a disconnected device');53usb_test(() => {54 return getFakeDevice().then(({ device, fakeDevice }) => {55 return device.open()56 .then(() => waitForDisconnect(fakeDevice))57 .then(() => {58 assert_false(device.opened);59 });60 });61}, 'disconnection closes the device');62usb_test(() => {63 return getFakeDevice().then(({ device }) => {64 assert_false(device.opened);65 return device.open().then(() => {66 assert_true(device.opened);67 return device.close().then(() => {68 assert_false(device.opened);69 });70 });71 });72}, 'a device can be opened and closed');73usb_test(() => {74 return getFakeDevice().then(({ device }) => {75 return device.open()76 .then(() => device.open())77 .then(() => device.open())78 .then(() => device.open())79 .then(() => device.close())80 .then(() => device.close())81 .then(() => device.close())82 .then(() => device.close());83 });84}, 'open and close can be called multiple times');85usb_test(async () => {86 let { device } = await getFakeDevice();87 await Promise.all([88 device.open(),89 assertRejectsWithDeviceStateChangeInProgressError(device.open()),90 assertRejectsWithDeviceStateChangeInProgressError(device.close()),91 ]);92 await Promise.all([93 device.close(),94 assertRejectsWithDeviceStateChangeInProgressError(device.open()),95 assertRejectsWithDeviceStateChangeInProgressError(device.close()),96 ]);97}, 'open and close cannot be called again while open or close are in progress');98usb_test(async () => {99 let { device } = await getFakeDevice();100 await device.open();101 return Promise.all([102 device.selectConfiguration(1),103 assertRejectsWithDeviceStateChangeInProgressError(104 device.claimInterface(0)),105 assertRejectsWithDeviceStateChangeInProgressError(106 device.releaseInterface(0)),107 assertRejectsWithDeviceStateChangeInProgressError(device.open()),108 assertRejectsWithDeviceStateChangeInProgressError(109 device.selectConfiguration(1)),110 assertRejectsWithDeviceStateChangeInProgressError(device.reset()),111 assertRejectsWithDeviceStateChangeInProgressError(112 device.selectAlternateInterface(0, 0)),113 assertRejectsWithDeviceStateChangeInProgressError(114 device.controlTransferOut({115 requestType: 'standard',116 recipient: 'interface',117 request: 0x42,118 value: 0x1234,119 index: 0x0000,120 })),121 assertRejectsWithDeviceStateChangeInProgressError(122 device.controlTransferOut({123 requestType: 'standard',124 recipient: 'interface',125 request: 0x42,126 value: 0x1234,127 index: 0x0000,128 }, new Uint8Array([1, 2, 3]))),129 assertRejectsWithDeviceStateChangeInProgressError(130 device.controlTransferIn({131 requestType: 'standard',132 recipient: 'interface',133 request: 0x42,134 value: 0x1234,135 index: 0x0000136 }, 0)),137 assertRejectsWithDeviceStateChangeInProgressError(device.close()),138 ]);139}, 'device operations reject if an device state change is in progress');140usb_test(() => {141 return getFakeDevice().then(({ device, fakeDevice }) => {142 return device.open()143 .then(() => waitForDisconnect(fakeDevice))144 .then(() => assertRejectsWithNotFoundError(device.close()));145 });146}, 'close rejects when called on a disconnected device');147usb_test(() => {148 return getFakeDevice().then(({ device, fakeDevice }) => {149 return device.open()150 .then(() => waitForDisconnect(fakeDevice))151 .then(() => assertRejectsWithNotFoundError(device.selectConfiguration(1)));152 });153}, 'selectConfiguration rejects when called on a disconnected device');154usb_test(() => {155 return getFakeDevice().then(({ device }) => Promise.all([156 assertRejectsWithNotOpenError(device.selectConfiguration(1)),157 assertRejectsWithNotOpenError(device.claimInterface(0)),158 assertRejectsWithNotOpenError(device.releaseInterface(0)),159 assertRejectsWithNotOpenError(device.selectAlternateInterface(0, 1)),160 assertRejectsWithNotOpenError(device.controlTransferIn({161 requestType: 'vendor',162 recipient: 'device',163 request: 0x42,164 value: 0x1234,165 index: 0x5678166 }, 7)),167 assertRejectsWithNotOpenError(device.controlTransferOut({168 requestType: 'vendor',169 recipient: 'device',170 request: 0x42,171 value: 0x1234,172 index: 0x5678173 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))),174 assertRejectsWithNotOpenError(device.clearHalt('in', 1)),175 assertRejectsWithNotOpenError(device.transferIn(1, 8)),176 assertRejectsWithNotOpenError(177 device.transferOut(1, new ArrayBuffer(8))),178 assertRejectsWithNotOpenError(device.isochronousTransferIn(1, [8])),179 assertRejectsWithNotOpenError(180 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])),181 assertRejectsWithNotOpenError(device.reset())182 ]));183}, 'methods requiring it reject when the device is not open');184usb_test(() => {185 return getFakeDevice().then(({ device }) => {186 assert_equals(device.configuration, null);187 return device.open()188 .then(() => {189 assert_equals(device.configuration, null);190 return device.selectConfiguration(1);191 })192 .then(() => {193 assertDeviceInfoEquals(194 device.configuration, fakeDeviceInit.configurations[0]);195 })196 .then(() => device.close());197 });198}, 'device configuration can be set and queried');199usb_test(async () => {200 let { device } = await getFakeDevice();201 assert_equals(device.configuration, null);202 await device.open();203 assert_equals(device.configuration, null);204 await device.selectConfiguration(1);205 await device.selectConfiguration(1);206 assertDeviceInfoEquals(207 device.configuration, fakeDeviceInit.configurations[0]);208 await device.selectConfiguration(2);209 assertDeviceInfoEquals(210 device.configuration, fakeDeviceInit.configurations[1]);211 await device.close();212}, 'a device configuration value can be set again');213usb_test(() => {214 return getFakeDevice().then(({ device }) => {215 assert_equals(device.configuration, null);216 return device.open()217 .then(() => assertRejectsWithError(218 device.selectConfiguration(3), 'NotFoundError',219 'The configuration value provided is not supported by the device.'))220 .then(() => device.close());221 });222}, 'selectConfiguration rejects on invalid configurations');223usb_test(() => {224 return getFakeDevice().then(({ device }) => {225 assert_equals(device.configuration, null);226 return device.open().then(() => Promise.all([227 assertRejectsWithNotConfiguredError(device.claimInterface(0)),228 assertRejectsWithNotConfiguredError(device.releaseInterface(0)),229 assertRejectsWithNotConfiguredError(device.selectAlternateInterface(0, 1)),230 assertRejectsWithNotConfiguredError(device.clearHalt('in', 1)),231 assertRejectsWithNotConfiguredError(device.transferIn(1, 8)),232 assertRejectsWithNotConfiguredError(233 device.transferOut(1, new ArrayBuffer(8))),234 assertRejectsWithNotConfiguredError(235 device.isochronousTransferIn(1, [8])),236 assertRejectsWithNotConfiguredError(237 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])),238 ])).then(() => device.close());239 });240}, 'methods requiring it reject when the device is unconfigured');241usb_test(async () => {242 let { device } = await getFakeDevice();243 await device.open();244 await device.selectConfiguration(1);245 assert_false(device.configuration.interfaces[0].claimed);246 assert_false(device.configuration.interfaces[1].claimed);247 await device.claimInterface(0);248 assert_true(device.configuration.interfaces[0].claimed);249 assert_false(device.configuration.interfaces[1].claimed);250 await device.claimInterface(1);251 assert_true(device.configuration.interfaces[0].claimed);252 assert_true(device.configuration.interfaces[1].claimed);253 await device.releaseInterface(0);254 assert_false(device.configuration.interfaces[0].claimed);255 assert_true(device.configuration.interfaces[1].claimed);256 await device.releaseInterface(1);257 assert_false(device.configuration.interfaces[0].claimed);258 assert_false(device.configuration.interfaces[1].claimed);259 await device.close();260}, 'interfaces can be claimed and released');261usb_test(async () => {262 let { device } = await getFakeDevice();263 await device.open();264 await device.selectConfiguration(1);265 assert_false(device.configuration.interfaces[0].claimed);266 assert_false(device.configuration.interfaces[1].claimed);267 await Promise.all([device.claimInterface(0),268 device.claimInterface(1)]);269 assert_true(device.configuration.interfaces[0].claimed);270 assert_true(device.configuration.interfaces[1].claimed);271 await Promise.all([device.releaseInterface(0),272 device.releaseInterface(1)]);273 assert_false(device.configuration.interfaces[0].claimed);274 assert_false(device.configuration.interfaces[1].claimed);275 await device.close();276}, 'interfaces can be claimed and released in parallel');277usb_test(async () => {278 let { device } = await getFakeDevice()279 await device.open();280 await device.selectConfiguration(1);281 await device.claimInterface(0);282 assert_true(device.configuration.interfaces[0].claimed);283 await device.claimInterface(0);284 assert_true(device.configuration.interfaces[0].claimed);285 await device.close();286}, 'an interface can be claimed multiple times');287usb_test(async () => {288 let { device } = await getFakeDevice();289 await device.open();290 await device.selectConfiguration(1);291 await device.claimInterface(0);292 assert_true(device.configuration.interfaces[0].claimed);293 await device.releaseInterface(0);294 assert_false(device.configuration.interfaces[0].claimed);295 await device.releaseInterface(0);296 assert_false(device.configuration.interfaces[0].claimed);297 await device.close();298}, 'an interface can be released multiple times');299usb_test(async () => {300 let { device } = await getFakeDevice();301 await device.open();302 await device.selectConfiguration(1);303 return Promise.all([304 device.claimInterface(0),305 assertRejectsWithInterfaceStateChangeInProgressError(306 device.claimInterface(0)),307 assertRejectsWithInterfaceStateChangeInProgressError(308 device.releaseInterface(0)),309 assertRejectsWithInterfaceStateChangeInProgressError(device.open()),310 assertRejectsWithInterfaceStateChangeInProgressError(311 device.selectConfiguration(1)),312 assertRejectsWithInterfaceStateChangeInProgressError(device.reset()),313 assertRejectsWithInterfaceStateChangeInProgressError(314 device.selectAlternateInterface(0, 0)),315 assertRejectsWithInterfaceStateChangeInProgressError(316 device.controlTransferOut({317 requestType: 'standard',318 recipient: 'interface',319 request: 0x42,320 value: 0x1234,321 index: 0x0000,322 })),323 assertRejectsWithInterfaceStateChangeInProgressError(324 device.controlTransferOut({325 requestType: 'standard',326 recipient: 'interface',327 request: 0x42,328 value: 0x1234,329 index: 0x0000,330 }, new Uint8Array([1, 2, 3]))),331 assertRejectsWithInterfaceStateChangeInProgressError(332 device.controlTransferIn({333 requestType: 'standard',334 recipient: 'interface',335 request: 0x42,336 value: 0x1234,337 index: 0x0000338 }, 0)),339 assertRejectsWithInterfaceStateChangeInProgressError(device.close()),340 ]);341}, 'device operations reject if an interface state change is in progress');342usb_test(async () => {343 let { device } = await getFakeDevice();344 await device.open();345 await device.selectConfiguration(1);346 await device.claimInterface(0);347 assert_true(device.configuration.interfaces[0].claimed);348 await device.close(0);349 assert_false(device.configuration.interfaces[0].claimed);350}, 'interfaces are released on close');351usb_test(() => {352 return getFakeDevice().then(({ device }) => {353 const message = 'The interface number provided is not supported by the ' +354 'device in its current configuration.';355 return device.open()356 .then(() => device.selectConfiguration(1))357 .then(() => Promise.all([358 assertRejectsWithError(359 device.claimInterface(2), 'NotFoundError', message),360 assertRejectsWithError(361 device.releaseInterface(2), 'NotFoundError', message),362 ]))363 .then(() => device.close());364 });365}, 'a non-existent interface cannot be claimed or released');366usb_test(() => {367 return getFakeDevice().then(({ device, fakeDevice }) => {368 return device.open()369 .then(() => device.selectConfiguration(1))370 .then(() => waitForDisconnect(fakeDevice))371 .then(() => assertRejectsWithNotFoundError(device.claimInterface(0)));372 });373}, 'claimInterface rejects when called on a disconnected device');374usb_test(() => {375 return getFakeDevice().then(({ device, fakeDevice }) => {376 return device.open()377 .then(() => device.selectConfiguration(1))378 .then(() => device.claimInterface(0))379 .then(() => waitForDisconnect(fakeDevice))380 .then(() => assertRejectsWithNotFoundError(device.releaseInterface(0)));381 });382}, 'releaseInterface rejects when called on a disconnected device');383usb_test(() => {384 return getFakeDevice().then(({ device }) => {385 return device.open()386 .then(() => device.selectConfiguration(2))387 .then(() => device.claimInterface(0))388 .then(() => device.selectAlternateInterface(0, 1))389 .then(() => device.close());390 });391}, 'can select an alternate interface');392usb_test(() => {393 return getFakeDevice().then(({ device }) => {394 return device.open()395 .then(() => device.selectConfiguration(2))396 .then(() => device.claimInterface(0))397 .then(() => assertRejectsWithError(398 device.selectAlternateInterface(0, 2), 'NotFoundError',399 'The alternate setting provided is not supported by the device in ' +400 'its current configuration.'))401 .then(() => device.close());402 });403}, 'cannot select a non-existent alternate interface');404usb_test(() => {405 return getFakeDevice().then(({ device, fakeDevice }) => {406 return device.open()407 .then(() => device.selectConfiguration(2))408 .then(() => device.claimInterface(0))409 .then(() => waitForDisconnect(fakeDevice))410 .then(() => assertRejectsWithNotFoundError(device.selectAlternateInterface(0, 1)));411 });412}, 'selectAlternateInterface rejects when called on a disconnected device');413usb_test(async () => {414 let { device } = await getFakeDevice();415 let usbRequestTypes = ['standard', 'class', 'vendor'];416 let usbRecipients = ['device', 'interface', 'endpoint', 'other'];417 await device.open();418 await device.selectConfiguration(1);419 await device.claimInterface(0);420 await device.selectAlternateInterface(0, 0);421 for (const requestType of usbRequestTypes) {422 for (const recipient of usbRecipients) {423 let index = recipient === 'interface' ? 0x5600 : 0x5681;424 let result = await device.controlTransferIn({425 requestType: requestType,426 recipient: recipient,427 request: 0x42,428 value: 0x1234,429 index: index430 }, 7);431 assert_true(result instanceof USBInTransferResult);432 assert_equals(result.status, 'ok');433 assert_equals(result.data.byteLength, 7);434 assert_equals(result.data.getUint16(0), 0x07);435 assert_equals(result.data.getUint8(2), 0x42);436 assert_equals(result.data.getUint16(3), 0x1234);437 assert_equals(result.data.getUint16(5), index);438 }439 }440 await device.close();441}, 'can issue all types of IN control transfers');442usb_test(async () => {443 let { device } = await getFakeDevice();444 let usbRequestTypes = ['standard', 'class', 'vendor'];445 let usbRecipients = ['device', 'other'];446 await device.open();447 await Promise.all(usbRequestTypes.flatMap(requestType => {448 return usbRecipients.map(async recipient => {449 let result = await device.controlTransferIn({450 requestType: requestType,451 recipient: recipient,452 request: 0x42,453 value: 0x1234,454 index: 0x5678455 }, 7);456 assert_true(result instanceof USBInTransferResult);457 assert_equals(result.status, 'ok');458 assert_equals(result.data.byteLength, 7);459 assert_equals(result.data.getUint16(0), 0x07);460 assert_equals(result.data.getUint8(2), 0x42);461 assert_equals(result.data.getUint16(3), 0x1234);462 assert_equals(result.data.getUint16(5), 0x5678);463 });464 }));465 await device.close();466}, 'device-scope IN control transfers don\'t require configuration');467usb_test(async () => {468 let { device } = await getFakeDevice();469 let usbRequestTypes = ['standard', 'class', 'vendor'];470 let usbRecipients = ['interface', 'endpoint'];471 await device.open();472 await Promise.all(usbRequestTypes.flatMap(requestType => {473 return usbRecipients.map(recipient => {474 let index = recipient === 'interface' ? 0x5600 : 0x5681;475 return assertRejectsWithNotConfiguredError(device.controlTransferIn({476 requestType: requestType,477 recipient: recipient,478 request: 0x42,479 value: 0x1234,480 index: index481 }, 7));482 });483 }));484 await device.close();485}, 'interface-scope IN control transfers require configuration');486usb_test(async () => {487 let { device } = await getFakeDevice();488 let usbRequestTypes = ['standard', 'class', 'vendor'];489 let usbRecipients = ['interface', 'endpoint'];490 await device.open();491 await device.selectConfiguration(1);492 await Promise.all(usbRequestTypes.flatMap(requestType => {493 return [494 assertRejectsWithNotClaimedError(device.controlTransferIn({495 requestType: requestType,496 recipient: 'interface',497 request: 0x42,498 value: 0x1234,499 index: 0x5600500 }, 7)),501 assertRejectsWithNotFoundError(device.controlTransferIn({502 requestType: requestType,503 recipient: 'endpoint',504 request: 0x42,505 value: 0x1234,506 index: 0x5681507 }, 7))508 ];509 }));510 await device.close();511}, 'interface-scope IN control transfers require claiming the interface');512usb_test(() => {513 return getFakeDevice().then(({ device, fakeDevice }) => {514 return device.open()515 .then(() => device.selectConfiguration(1))516 .then(() => waitForDisconnect(fakeDevice))517 .then(() => assertRejectsWithNotFoundError(device.controlTransferIn({518 requestType: 'vendor',519 recipient: 'device',520 request: 0x42,521 value: 0x1234,522 index: 0x5678523 }, 7)));524 });525}, 'controlTransferIn rejects when called on a disconnected device');526usb_test(async () => {527 let { device } = await getFakeDevice();528 let usbRequestTypes = ['standard', 'class', 'vendor'];529 let usbRecipients = ['device', 'interface', 'endpoint', 'other'];530 let dataArray = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);531 let dataTypes = [dataArray, dataArray.buffer];532 await device.open();533 await device.selectConfiguration(1);534 await device.claimInterface(0);535 await device.selectAlternateInterface(0, 0);536 for (const requestType of usbRequestTypes) {537 for (const recipient of usbRecipients) {538 let index = recipient === 'interface' ? 0x5600 : 0x5681;539 let transferParams = {540 requestType: requestType,541 recipient: recipient,542 request: 0x42,543 value: 0x1234,544 index: index545 };546 for (const data of dataTypes) {547 let result = await device.controlTransferOut(transferParams, data);548 assert_true(result instanceof USBOutTransferResult);549 assert_equals(result.status, 'ok');550 assert_equals(result.bytesWritten, 8);551 }552 let result = await device.controlTransferOut(transferParams);553 assert_true(result instanceof USBOutTransferResult);554 assert_equals(result.status, 'ok');555 }556 }557 await device.close();558}, 'can issue all types of OUT control transfers');559usb_test(async () => {560 let { device } = await getFakeDevice();561 let usbRequestTypes = ['standard', 'class', 'vendor'];562 let usbRecipients = ['device', 'other'];563 let dataArray = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);564 let dataTypes = [dataArray, dataArray.buffer];565 await device.open();566 await Promise.all(usbRequestTypes.flatMap(requestType => {567 return usbRecipients.flatMap(recipient => {568 let transferParams = {569 requestType: requestType,570 recipient: recipient,571 request: 0x42,572 value: 0x1234,573 index: 0x5678574 };575 return dataTypes.map(async data => {576 let result = await device.controlTransferOut(transferParams, data);577 assert_true(result instanceof USBOutTransferResult);578 assert_equals(result.status, 'ok');579 assert_equals(result.bytesWritten, 8);580 }).push((async () => {581 let result = await device.controlTransferOut(transferParams);582 assert_true(result instanceof USBOutTransferResult);583 assert_equals(result.status, 'ok');584 })());585 });586 }));587 await device.close();588}, 'device-scope OUT control transfers don\'t require configuration');589usb_test(async () => {590 let { device } = await getFakeDevice();591 let usbRequestTypes = ['standard', 'class', 'vendor'];592 let usbRecipients = ['interface', 'endpoint'];593 let dataArray = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);594 let dataTypes = [dataArray, dataArray.buffer];595 await device.open();596 await Promise.all(usbRequestTypes.flatMap(requestType => {597 return usbRecipients.flatMap(recipient => {598 let index = recipient === 'interface' ? 0x5600 : 0x5681;599 let transferParams = {600 requestType: requestType,601 recipient: recipient,602 request: 0x42,603 value: 0x1234,604 index: index605 };606 return dataTypes.map(data => {607 return assertRejectsWithNotConfiguredError(608 device.controlTransferOut(transferParams, data));609 }).push(assertRejectsWithNotConfiguredError(610 device.controlTransferOut(transferParams)));611 });612 }));613 await device.close();614}, 'interface-scope OUT control transfers require configuration');615usb_test(async () => {616 let { device } = await getFakeDevice();617 let usbRequestTypes = ['standard', 'class', 'vendor'];618 let usbRecipients = ['interface', 'endpoint'];619 let dataArray = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);620 let dataTypes = [dataArray, dataArray.buffer];621 await device.open();622 await device.selectConfiguration(1);623 await Promise.all(usbRequestTypes.flatMap(requestType => {624 return usbRecipients.flatMap(recipient => {625 let index = recipient === 'interface' ? 0x5600 : 0x5681;626 let assertion = recipient === 'interface'627 ? assertRejectsWithNotClaimedError628 : assertRejectsWithEndpointNotFoundError;629 let transferParams = {630 requestType: requestType,631 recipient: recipient,632 request: 0x42,633 value: 0x1234,634 index: index635 };636 return dataTypes.map(data => {637 return assertion(device.controlTransferOut(transferParams, data));638 }).push(assertion(device.controlTransferOut(transferParams)));639 });640 }));641 await device.close();642}, 'interface-scope OUT control transfers an interface claim');643usb_test(() => {644 return getFakeDevice().then(({ device, fakeDevice }) => {645 return device.open()646 .then(() => device.selectConfiguration(1))647 .then(() => waitForDisconnect(fakeDevice))648 .then(() => assertRejectsWithNotFoundError(device.controlTransferOut({649 requestType: 'vendor',650 recipient: 'device',651 request: 0x42,652 value: 0x1234,653 index: 0x5678654 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))));655 });656}, 'controlTransferOut rejects when called on a disconnected device');657usb_test(async () => {658 let { device } = await getFakeDevice();659 await device.open();660 await device.selectConfiguration(1);661 await device.claimInterface(0);662 assertRejectsWithTypeError(device.controlTransferOut({663 requestType: 'invalid',664 recipient: 'device',665 request: 0x42,666 value: 0x1234,667 index: 0x5678668 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])));669 assertRejectsWithTypeError(device.controlTransferIn({670 requestType: 'invalid',671 recipient: 'device',672 request: 0x42,673 value: 0x1234,674 index: 0x5678675 }, 0));676 await device.close();677}, 'control transfers with a invalid request type reject');678usb_test(async () => {679 let { device } = await getFakeDevice();680 await device.open();681 await device.selectConfiguration(1);682 await device.claimInterface(0);683 assertRejectsWithTypeError(device.controlTransferOut({684 requestType: 'vendor',685 recipient: 'invalid',686 request: 0x42,687 value: 0x1234,688 index: 0x5678689 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])));690 assertRejectsWithTypeError(device.controlTransferIn({691 requestType: 'vendor',692 recipient: 'invalid',693 request: 0x42,694 value: 0x1234,695 index: 0x5678696 }, 0));697}, 'control transfers with a invalid recipient type reject');698usb_test(async () => {699 let { device } = await getFakeDevice();700 await device.open();701 await device.selectConfiguration(1);702 await device.claimInterface(0);703 assertRejectsWithNotFoundError(device.controlTransferOut({704 requestType: 'vendor',705 recipient: 'interface',706 request: 0x42,707 value: 0x1234,708 index: 0x0002 // Last byte of index is interface number.709 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])));710 assertRejectsWithNotFoundError(device.controlTransferIn({711 requestType: 'vendor',712 recipient: 'interface',713 request: 0x42,714 value: 0x1234,715 index: 0x0002 // Last byte of index is interface number.716 }, 0));717}, 'control transfers to a non-existant interface reject');718usb_test(() => {719 return getFakeDevice().then(({ device }) => {720 let interfaceRequest = {721 requestType: 'vendor',722 recipient: 'interface',723 request: 0x42,724 value: 0x1234,725 index: 0x5600 // Last byte of index is interface number.726 };727 let endpointRequest = {728 requestType: 'vendor',729 recipient: 'endpoint',730 request: 0x42,731 value: 0x1234,732 index: 0x5681 // Last byte of index is endpoint address.733 };734 let data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);735 return device.open()736 .then(() => device.selectConfiguration(1))737 .then(() => Promise.all([738 assertRejectsWithError(739 device.controlTransferIn(interfaceRequest, 7),740 'InvalidStateError'),741 assertRejectsWithError(742 device.controlTransferIn(endpointRequest, 7),743 'NotFoundError'),744 assertRejectsWithError(745 device.controlTransferOut(interfaceRequest, data),746 'InvalidStateError'),747 assertRejectsWithError(748 device.controlTransferOut(endpointRequest, data),749 'NotFoundError'),750 ]))751 .then(() => device.claimInterface(0))752 .then(() => Promise.all([753 device.controlTransferIn(interfaceRequest, 7).then(result => {754 assert_true(result instanceof USBInTransferResult);755 assert_equals(result.status, 'ok');756 assert_equals(result.data.byteLength, 7);757 assert_equals(result.data.getUint16(0), 0x07);758 assert_equals(result.data.getUint8(2), 0x42);759 assert_equals(result.data.getUint16(3), 0x1234);760 assert_equals(result.data.getUint16(5), 0x5600);761 }),762 device.controlTransferIn(endpointRequest, 7).then(result => {763 assert_true(result instanceof USBInTransferResult);764 assert_equals(result.status, 'ok');765 assert_equals(result.data.byteLength, 7);766 assert_equals(result.data.getUint16(0), 0x07);767 assert_equals(result.data.getUint8(2), 0x42);768 assert_equals(result.data.getUint16(3), 0x1234);769 assert_equals(result.data.getUint16(5), 0x5681);770 }),771 device.controlTransferOut(interfaceRequest, data),772 device.controlTransferOut(endpointRequest, data),773 ]))774 .then(() => device.close());775 });776}, 'requests to interfaces and endpoint require an interface claim');777usb_test(async () => {778 const { device } = await getFakeDevice();779 await device.open();780 await device.selectConfiguration(1);781 await device.claimInterface(0);782 const transfer_params = {783 requestType: 'vendor',784 recipient: 'device',785 request: 0,786 value: 0,787 index: 0788 };789 try {790 const array_buffer = new ArrayBuffer(64 * 8);791 const result =792 await device.controlTransferOut(transfer_params, array_buffer);793 assert_equals(result.status, 'ok');794 detachBuffer(array_buffer);795 await device.controlTransferOut(transfer_params, array_buffer);796 assert_unreached();797 } catch (e) {798 assert_equals(e.code, DOMException.INVALID_STATE_ERR);799 }800 try {801 const typed_array = new Uint8Array(64 * 8);802 const result =803 await device.controlTransferOut(transfer_params, typed_array);804 assert_equals(result.status, 'ok');805 detachBuffer(typed_array.buffer);806 await device.controlTransferOut(transfer_params, typed_array);807 assert_unreached();808 } catch (e) {809 assert_equals(e.code, DOMException.INVALID_STATE_ERR);810 }811}, 'controlTransferOut rejects if called with a detached buffer');812usb_test(() => {813 return getFakeDevice().then(({ device }) => {814 return device.open()815 .then(() => device.selectConfiguration(1))816 .then(() => device.claimInterface(0))817 .then(() => device.clearHalt('in', 1))818 .then(() => device.close());819 });820}, 'can clear a halt condition');821usb_test(() => {822 return getFakeDevice().then(({ device, fakeDevice }) => {823 return device.open()824 .then(() => device.selectConfiguration(1))825 .then(() => device.claimInterface(0))826 .then(() => waitForDisconnect(fakeDevice))827 .then(() => assertRejectsWithNotFoundError(device.clearHalt('in', 1)));828 });829}, 'clearHalt rejects when called on a disconnected device');830usb_test(() => {831 return getFakeDevice().then(({ device }) => {832 let data = new DataView(new ArrayBuffer(1024));833 for (let i = 0; i < 1024; ++i)834 data.setUint8(i, i & 0xff);835 const rangeError = 'The specified endpoint number is out of range.';836 return device.open()837 .then(() => device.selectConfiguration(1))838 .then(() => device.claimInterface(0))839 .then(() => Promise.all([840 assertRejectsWithEndpointNotFoundError(device.transferIn(2, 8)), // Unclaimed841 assertRejectsWithEndpointNotFoundError(device.transferIn(3, 8)), // Non-existent842 assertRejectsWithError(843 device.transferIn(16, 8), 'IndexSizeError', rangeError),844 assertRejectsWithEndpointNotFoundError(device.transferOut(2, data)), // Unclaimed845 assertRejectsWithEndpointNotFoundError(device.transferOut(3, data)), // Non-existent846 assertRejectsWithError(847 device.transferOut(16, data), 'IndexSizeError', rangeError),848 ]));849 });850}, 'transfers to unavailable endpoints are rejected');851usb_test(() => {852 return getFakeDevice().then(({ device }) => {853 return device.open()854 .then(() => device.selectConfiguration(1))855 .then(() => device.claimInterface(0))856 .then(() => device.transferIn(1, 8))857 .then(result => {858 assert_true(result instanceof USBInTransferResult);859 assert_equals(result.status, 'ok');860 assert_equals(result.data.byteLength, 8);861 for (let i = 0; i < 8; ++i)862 assert_equals(result.data.getUint8(i), i, 'mismatch at byte ' + i);863 return device.close();864 });865 });866}, 'can issue IN interrupt transfer');867usb_test(() => {868 return getFakeDevice().then(({ device }) => {869 return device.open()870 .then(() => device.selectConfiguration(1))871 .then(() => device.claimInterface(1))872 .then(() => device.transferIn(2, 1024))873 .then(result => {874 assert_true(result instanceof USBInTransferResult);875 assert_equals(result.status, 'ok');876 assert_equals(result.data.byteLength, 1024);877 for (let i = 0; i < 1024; ++i)878 assert_equals(result.data.getUint8(i), i & 0xff,879 'mismatch at byte ' + i);880 return device.close();881 });882 });883}, 'can issue IN bulk transfer');884usb_test(() => {885 return getFakeDevice().then(({ device, fakeDevice }) => {886 return device.open()887 .then(() => device.selectConfiguration(1))888 .then(() => device.claimInterface(1))889 .then(() => waitForDisconnect(fakeDevice))890 .then(() => assertRejectsWithNotFoundError(device.transferIn(2, 1024)));891 });892}, 'transferIn rejects if called on a disconnected device');893usb_test(() => {894 return getFakeDevice().then(({ device }) => {895 return device.open()896 .then(() => device.selectConfiguration(1))897 .then(() => device.claimInterface(1))898 .then(() => {899 let data = new DataView(new ArrayBuffer(1024));900 for (let i = 0; i < 1024; ++i)901 data.setUint8(i, i & 0xff);902 return device.transferOut(2, data);903 })904 .then(result => {905 assert_true(result instanceof USBOutTransferResult);906 assert_equals(result.status, 'ok');907 assert_equals(result.bytesWritten, 1024);908 return device.close();909 });910 });911}, 'can issue OUT bulk transfer');912usb_test(() => {913 return getFakeDevice().then(({ device, fakeDevice }) => {914 return device.open()915 .then(() => device.selectConfiguration(1))916 .then(() => device.claimInterface(1))917 .then(() => {918 let data = new DataView(new ArrayBuffer(1024));919 for (let i = 0; i < 1024; ++i)920 data.setUint8(i, i & 0xff);921 return waitForDisconnect(fakeDevice)922 .then(() => assertRejectsWithNotFoundError(device.transferOut(2, data)));923 });924 });925}, 'transferOut rejects if called on a disconnected device');926usb_test(async () => {927 const { device } = await getFakeDevice();928 await device.open();929 await device.selectConfiguration(1);930 await device.claimInterface(1);931 try {932 const array_buffer = new ArrayBuffer(64 * 8);933 const result = await device.transferOut(2, array_buffer);934 assert_equals(result.status, 'ok');935 detachBuffer(array_buffer);936 await device.transferOut(2, array_buffer);937 assert_unreached();938 } catch (e) {939 assert_equals(e.code, DOMException.INVALID_STATE_ERR);940 }941 try {942 const typed_array = new Uint8Array(64 * 8);943 const result = await device.transferOut(2, typed_array);944 assert_equals(result.status, 'ok');945 detachBuffer(typed_array.buffer);946 await device.transferOut(2, typed_array);947 assert_unreached();948 } catch (e) {949 assert_equals(e.code, DOMException.INVALID_STATE_ERR);950 }951}, 'transferOut rejects if called with a detached buffer');952usb_test(() => {953 return getFakeDevice().then(({ device }) => {954 return device.open()955 .then(() => device.selectConfiguration(2))956 .then(() => device.claimInterface(0))957 .then(() => device.selectAlternateInterface(0, 1))958 .then(() => device.isochronousTransferIn(959 1, [64, 64, 64, 64, 64, 64, 64, 64]))960 .then(result => {961 assert_true(result instanceof USBIsochronousInTransferResult);962 assert_equals(result.data.byteLength, 64 * 8, 'buffer size');963 assert_equals(result.packets.length, 8, 'number of packets');964 let byteOffset = 0;965 for (let i = 0; i < result.packets.length; ++i) {966 assert_true(967 result.packets[i] instanceof USBIsochronousInTransferPacket);968 assert_equals(result.packets[i].status, 'ok');969 assert_equals(result.packets[i].data.byteLength, 64);970 assert_equals(result.packets[i].data.buffer, result.data.buffer);971 assert_equals(result.packets[i].data.byteOffset, byteOffset);972 for (let j = 0; j < 64; ++j)973 assert_equals(result.packets[i].data.getUint8(j), j & 0xff,974 'mismatch at byte ' + j + ' of packet ' + i);975 byteOffset += result.packets[i].data.byteLength;976 }977 return device.close();978 });979 });980}, 'can issue IN isochronous transfer');981usb_test(() => {982 return getFakeDevice().then(({ device, fakeDevice }) => {983 return device.open()984 .then(() => device.selectConfiguration(2))985 .then(() => device.claimInterface(0))986 .then(() => device.selectAlternateInterface(0, 1))987 .then(() => waitForDisconnect(fakeDevice))988 .then(() => assertRejectsWithNotFoundError(device.isochronousTransferIn(989 1, [64, 64, 64, 64, 64, 64, 64, 64])));990 });991}, 'isochronousTransferIn rejects when called on a disconnected device');992usb_test(() => {993 return getFakeDevice().then(({ device }) => {994 return device.open()995 .then(() => device.selectConfiguration(2))996 .then(() => device.claimInterface(0))997 .then(() => device.selectAlternateInterface(0, 1))998 .then(() => {999 let data = new DataView(new ArrayBuffer(64 * 8));1000 for (let i = 0; i < 8; ++i) {1001 for (let j = 0; j < 64; ++j)1002 data.setUint8(i * j, j & 0xff);1003 }1004 return device.isochronousTransferOut(1005 1, data, [64, 64, 64, 64, 64, 64, 64, 64]);1006 })1007 .then(result => {1008 assert_true(result instanceof USBIsochronousOutTransferResult);1009 assert_equals(result.packets.length, 8, 'number of packets');1010 let byteOffset = 0;1011 for (let i = 0; i < result.packets.length; ++i) {1012 assert_true(1013 result.packets[i] instanceof USBIsochronousOutTransferPacket);1014 assert_equals(result.packets[i].status, 'ok');1015 assert_equals(result.packets[i].bytesWritten, 64);1016 }1017 return device.close();1018 });1019 });1020}, 'can issue OUT isochronous transfer');1021usb_test(() => {1022 return getFakeDevice().then(({ device, fakeDevice }) => {1023 return device.open()1024 .then(() => device.selectConfiguration(2))1025 .then(() => device.claimInterface(0))1026 .then(() => device.selectAlternateInterface(0, 1))1027 .then(() => {1028 let data = new DataView(new ArrayBuffer(64 * 8));1029 for (let i = 0; i < 8; ++i) {1030 for (let j = 0; j < 64; ++j)1031 data.setUint8(i * j, j & 0xff);1032 }1033 return waitForDisconnect(fakeDevice)1034 .then(() => assertRejectsWithNotFoundError(device.isochronousTransferOut(1035 1, data, [64, 64, 64, 64, 64, 64, 64, 64])));1036 });1037 });1038}, 'isochronousTransferOut rejects when called on a disconnected device');1039usb_test(async () => {1040 const { device } = await getFakeDevice();1041 await device.open();1042 await device.selectConfiguration(2);1043 await device.claimInterface(0);1044 await device.selectAlternateInterface(0, 1);1045 try {1046 const array_buffer = new ArrayBuffer(64 * 8);1047 const result = await device.isochronousTransferOut(1048 1, array_buffer, [64, 64, 64, 64, 64, 64, 64, 64]);1049 for (let i = 0; i < result.packets.length; ++i)1050 assert_equals(result.packets[i].status, 'ok');1051 detachBuffer(array_buffer);1052 await device.isochronousTransferOut(1053 1, array_buffer, [64, 64, 64, 64, 64, 64, 64, 64]);1054 assert_unreached();1055 } catch (e) {1056 assert_equals(e.code, DOMException.INVALID_STATE_ERR);1057 }1058 try {1059 const typed_array = new Uint8Array(64 * 8);1060 const result = await device.isochronousTransferOut(1061 1, typed_array, [64, 64, 64, 64, 64, 64, 64, 64]);1062 for (let i = 0; i < result.packets.length; ++i)1063 assert_equals(result.packets[i].status, 'ok');1064 detachBuffer(typed_array.buffer);1065 await device.isochronousTransferOut(1066 1, typed_array, [64, 64, 64, 64, 64, 64, 64, 64]);1067 assert_unreached();1068 } catch (e) {1069 assert_equals(e.code, DOMException.INVALID_STATE_ERR);1070 }1071}, 'isochronousTransferOut rejects when called with a detached buffer');1072usb_test(() => {1073 return getFakeDevice().then(({ device }) => {1074 return device.open().then(() => device.reset()).then(() => device.close());1075 });1076}, 'can reset the device');1077usb_test(() => {1078 return getFakeDevice().then(({ device, fakeDevice }) => {1079 return device.open()1080 .then(() => waitForDisconnect(fakeDevice))1081 .then(() => assertRejectsWithNotFoundError(device.reset()));1082 });...
Using AI Code Generation
1var wptools = require('wptools');2wptools.usb_test(function(err, result) {3 if (err) {4 console.log(err);5 } else {6 console.log(result);7 }8});9var wptools = require('wptools');10wptools.get_device_info(function(err, result) {11 if (err) {12 console.log(err);13 } else {14 console.log(result);15 }16});17var wptools = require('wptools');18wptools.get_device_info(function(err, result) {19 if (err) {20 console.log(err);21 } else {22 console.log(result);23 }24});25var wptools = require('wptools');26wptools.get_device_info(function(err, result) {27 if (err) {28 console.log(err);29 } else {30 console.log(result);31 }32});33var wptools = require('wptools');34wptools.get_device_info(function(err, result) {35 if (err) {36 console.log(err);37 } else {38 console.log(result);39 }40});41var wptools = require('wptools');42wptools.get_device_info(function(err, result) {43 if (err) {44 console.log(err);45 } else {46 console.log(result);47 }48});49var wptools = require('wptools');50wptools.get_device_info(function(err, result) {51 if (err) {52 console.log(err);53 } else {54 console.log(result);55 }56});57var wptools = require('wptools');58wptools.get_device_info(function(err, result) {59 if (err) {60 console.log(err);61 } else {62 console.log(result
Using AI Code Generation
1var wpt_usb = require('wpt_usb');2wpt_usb.usb_test(function(err, result) {3 if (err) {4 console.log('err: ' + err);5 } else {6 console.log('result: ' + result);7 }8});9var wpt_usb = require('wpt_usb');10wpt_usb.usb_test(function(err, result) {11 if (err) {12 console.log('err: ' + err);13 } else {14 console.log('result: ' + result);15 }16});17var wpt_usb = require('wpt_usb');18wpt_usb.usb_test(function(err, result) {19 if (err) {20 console.log('err: ' + err);21 } else {22 console.log('result: ' + result);23 }24});25var wpt_usb = require('wpt_usb');26wpt_usb.usb_test(function(err, result) {27 if (err) {28 console.log('err: ' + err);29 } else {30 console.log('result: ' + result);31 }32});33var wpt_usb = require('wpt_usb');34wpt_usb.usb_test(function(err, result) {35 if (err) {36 console.log('err: ' + err);37 } else {38 console.log('result: ' + result);39 }40});41var wpt_usb = require('wpt_usb');42wpt_usb.usb_test(function(err, result) {43 if (err) {44 console.log('err: ' + err);45 } else {46 console.log('result: ' + result);47 }48});49var wpt_usb = require('wpt_usb');50wpt_usb.usb_test(function(err, result) {51 if (err) {52 console.log('err: ' + err);53 } else {
Using AI Code Generation
1var wptools = require('wptools');2wptools.usb_test(function(err, data){3 if(err){4 console.log("Error: " + err);5 }6 else{7 console.log("Data: " + data);8 }9});10exports.usb_test = function(callback){11 var usb = require('usb');12 var device = usb.findByIds(0x04b4, 0x00f1);13 console.log("Device: " + device);14 if(device){15 callback(null, "Device found");16 }17 else{18 callback("Device not found", null);19 }20};
Using AI Code Generation
1var wptb = require('wptb');2wptb.usb_test(function(err, data) {3 if (err) throw err;4 console.log(data);5});6var usb_test = function(callback) {7 callback(null, "USB test passed");8};9module.exports = {10};
Using AI Code Generation
1var wptb = require('wptb');2wptb.usb_test();3exports.usb_test = function() {4 console.log('usb_test method of wptb.js');5}6var wptb = require('wptb');7wptb.usb_test();8wptb.wifi_test();9wptb.bluetooth_test();10exports.usb_test = function() {11 console.log('usb_test method of wptb.js');12}13exports.wifi_test = function() {14 console.log('wifi_test method of wptb.js');15}16exports.bluetooth_test = function() {17 console.log('bluetooth_test method of wptb.js');18}19var wptb = require('wptb');20wptb.usb_test();21wptb.wifi_test();22wptb.bluetooth_test();23module.exports = {24 usb_test: function() {25 console.log('usb_test method of wptb.js');26 },27 wifi_test: function() {28 console.log('wifi_test method of wptb.js');
Using AI Code Generation
1var wpt = require('wpt');2var wptDriver = new wpt('API_KEY');3wptDriver.usb_test(function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('wpt');11var wptDriver = new wpt('API_KEY');12wptDriver.get_locations(function(err, data) {13 if (err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('wpt');20var wptDriver = new wpt('API_KEY');21wptDriver.get_testers(function(err, data) {22 if (err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('wpt');29var wptDriver = new wpt('API_KEY');30wptDriver.get_testers(function(err, data) {31 if (err) {32 console.log(err);33 } else {34 console.log(data);35 }36});37var wpt = require('wpt');38var wptDriver = new wpt('API_KEY');39wptDriver.get_testers(function(err, data) {40 if (err) {41 console.log(err);42 } else {43 console.log(data);44 }45});46var wpt = require('wpt');47var wptDriver = new wpt('API_KEY');48wptDriver.get_testers(function(err, data) {49 if (err) {50 console.log(err);51 } else {52 console.log(data);53 }54});55var wpt = require('wpt');56var wptDriver = new wpt('API_KEY');57wptDriver.get_testers(function(err, data) {58 if (err) {59 console.log(err);60 } else {61 console.log(data);62 }63});
Using AI Code Generation
1var usb_test = require('wptool').usb_test;2usb_test(function(err, result) {3 console.log("Result is " + result);4});5var exec = require('child_process').exec;6exports.usb_test = function(callback) {7 exec('lsusb', function(err, stdout, stderr) {8 if(!err) {9 callback(null, stdout);10 } else {11 callback(err, null);12 }13 });14};15var exec = require('child_process').exec;16exports.usb_test = function(callback) {17 exec('lsusb', function(err, stdout, stderr) {18 if(!err) {19 callback(null, stdout);20 } else {21 callback(err, null);22 }23 });24};25var usb_test = require('wptool').usb_test;26usb_test(function(err, result) {27 if(!err) {28 exec('lsusb', function(err, stdout, stderr) {29 if(!err) {30 console.log("Result is " + stdout);31 } else {32 console.log("Error is " + err);33 }34 });35 } else {36 console.log("Error is " + err);37 }38});
Using AI Code Generation
1var usb = require('./wptouch.js');2usb.usb_test();3exports.usb_test = function(){4 console.log("test usb");5}6(function (exports, require, module, __filename, __dirname) { var usb = require('usb');7 at Function.Module._resolveFilename (module.js:470:15)8 at Function.Module._load (module.js:418:25)9 at Module.require (module.js:498:17)10 at require (internal/module.js:20:19)11 at Object.<anonymous> (C:\Users\user\Desktop\wptouch\wptouch.js:1:27)12 at Module._compile (module.js:571:32)13 at Object.Module._extensions..js (module.js:580:10)14 at Module.load (module.js:488:32)15 at tryModuleLoad (module.js:447:12)16 at Function.Module._load (module.js:439:3)17{18 "dependencies": {19 },20 "devDependencies": {},21 "scripts": {22 },23}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!