Best JavaScript code snippet using wpt
usbDevice.https.any.js
Source:usbDevice.https.any.js
...27 'An operation that changes interface state is in progress.');28}29usb_test(() => {30 return getFakeDevice().then(({ device, fakeDevice }) => {31 return waitForDisconnect(fakeDevice)32 .then(() => assertRejectsWithNotFoundError(device.open()));33 });34}, 'open rejects when called on a disconnected device');35usb_test(() => {36 return getFakeDevice().then(({ device, fakeDevice }) => {37 return device.open()38 .then(() => waitForDisconnect(fakeDevice))39 .then(() => {40 assert_false(device.opened);41 });42 });43}, 'disconnection closes the device');44usb_test(() => {45 return getFakeDevice().then(({ device }) => {46 assert_false(device.opened);47 return device.open().then(() => {48 assert_true(device.opened);49 return device.close().then(() => {50 assert_false(device.opened);51 });52 });53 });54}, 'a device can be opened and closed');55usb_test(() => {56 return getFakeDevice().then(({ device }) => {57 return device.open()58 .then(() => device.open())59 .then(() => device.open())60 .then(() => device.open())61 .then(() => device.close())62 .then(() => device.close())63 .then(() => device.close())64 .then(() => device.close());65 });66}, 'open and close can be called multiple times');67usb_test(async () => {68 let { device } = await getFakeDevice();69 await Promise.all([70 device.open(),71 assertRejectsWithDeviceStateChangeInProgressError(device.open()),72 assertRejectsWithDeviceStateChangeInProgressError(device.close()),73 ]);74 await Promise.all([75 device.close(),76 assertRejectsWithDeviceStateChangeInProgressError(device.open()),77 assertRejectsWithDeviceStateChangeInProgressError(device.close()),78 ]);79}, 'open and close cannot be called again while open or close are in progress');80usb_test(async () => {81 let { device } = await getFakeDevice();82 await device.open();83 return Promise.all([84 device.selectConfiguration(1),85 assertRejectsWithDeviceStateChangeInProgressError(86 device.claimInterface(0)),87 assertRejectsWithDeviceStateChangeInProgressError(88 device.releaseInterface(0)),89 assertRejectsWithDeviceStateChangeInProgressError(device.open()),90 assertRejectsWithDeviceStateChangeInProgressError(91 device.selectConfiguration(1)),92 assertRejectsWithDeviceStateChangeInProgressError(device.reset()),93 assertRejectsWithDeviceStateChangeInProgressError(94 device.selectAlternateInterface(0, 0)),95 assertRejectsWithDeviceStateChangeInProgressError(96 device.controlTransferOut({97 requestType: 'standard',98 recipient: 'interface',99 request: 0x42,100 value: 0x1234,101 index: 0x0000,102 })),103 assertRejectsWithDeviceStateChangeInProgressError(104 device.controlTransferOut({105 requestType: 'standard',106 recipient: 'interface',107 request: 0x42,108 value: 0x1234,109 index: 0x0000,110 }, new Uint8Array([1, 2, 3]))),111 assertRejectsWithDeviceStateChangeInProgressError(112 device.controlTransferIn({113 requestType: 'standard',114 recipient: 'interface',115 request: 0x42,116 value: 0x1234,117 index: 0x0000118 }, 0)),119 assertRejectsWithDeviceStateChangeInProgressError(device.close()),120 ]);121}, 'device operations reject if an device state change is in progress');122usb_test(() => {123 return getFakeDevice().then(({ device, fakeDevice }) => {124 return device.open()125 .then(() => waitForDisconnect(fakeDevice))126 .then(() => assertRejectsWithNotFoundError(device.close()));127 });128}, 'close rejects when called on a disconnected device');129usb_test(() => {130 return getFakeDevice().then(({ device, fakeDevice }) => {131 return device.open()132 .then(() => waitForDisconnect(fakeDevice))133 .then(() => assertRejectsWithNotFoundError(device.selectConfiguration(1)));134 });135}, 'selectConfiguration rejects when called on a disconnected device');136usb_test(() => {137 return getFakeDevice().then(({ device }) => Promise.all([138 assertRejectsWithNotOpenError(device.selectConfiguration(1)),139 assertRejectsWithNotOpenError(device.claimInterface(0)),140 assertRejectsWithNotOpenError(device.releaseInterface(0)),141 assertRejectsWithNotOpenError(device.selectAlternateInterface(0, 1)),142 assertRejectsWithNotOpenError(device.controlTransferIn({143 requestType: 'vendor',144 recipient: 'device',145 request: 0x42,146 value: 0x1234,147 index: 0x5678148 }, 7)),149 assertRejectsWithNotOpenError(device.controlTransferOut({150 requestType: 'vendor',151 recipient: 'device',152 request: 0x42,153 value: 0x1234,154 index: 0x5678155 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))),156 assertRejectsWithNotOpenError(device.clearHalt('in', 1)),157 assertRejectsWithNotOpenError(device.transferIn(1, 8)),158 assertRejectsWithNotOpenError(159 device.transferOut(1, new ArrayBuffer(8))),160 assertRejectsWithNotOpenError(device.isochronousTransferIn(1, [8])),161 assertRejectsWithNotOpenError(162 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])),163 assertRejectsWithNotOpenError(device.reset())164 ]));165}, 'methods requiring it reject when the device is not open');166usb_test(() => {167 return getFakeDevice().then(({ device }) => {168 assert_equals(device.configuration, null);169 return device.open()170 .then(() => {171 assert_equals(device.configuration, null);172 return device.selectConfiguration(1);173 })174 .then(() => {175 assertDeviceInfoEquals(176 device.configuration, fakeDeviceInit.configurations[0]);177 })178 .then(() => device.close());179 });180}, 'device configuration can be set and queried');181usb_test(async () => {182 let { device } = await getFakeDevice();183 assert_equals(device.configuration, null);184 await device.open();185 assert_equals(device.configuration, null);186 await device.selectConfiguration(1);187 await device.selectConfiguration(1);188 assertDeviceInfoEquals(189 device.configuration, fakeDeviceInit.configurations[0]);190 await device.selectConfiguration(2);191 assertDeviceInfoEquals(192 device.configuration, fakeDeviceInit.configurations[1]);193 await device.close();194}, 'a device configuration value can be set again');195usb_test(() => {196 return getFakeDevice().then(({ device }) => {197 assert_equals(device.configuration, null);198 return device.open()199 .then(() => assertRejectsWithError(200 device.selectConfiguration(3), 'NotFoundError',201 'The configuration value provided is not supported by the device.'))202 .then(() => device.close());203 });204}, 'selectConfiguration rejects on invalid configurations');205usb_test(() => {206 return getFakeDevice().then(({ device }) => {207 assert_equals(device.configuration, null);208 return device.open().then(() => Promise.all([209 assertRejectsWithNotConfiguredError(device.claimInterface(0)),210 assertRejectsWithNotConfiguredError(device.releaseInterface(0)),211 assertRejectsWithNotConfiguredError(device.selectAlternateInterface(0, 1)),212 assertRejectsWithNotConfiguredError(device.controlTransferIn({213 requestType: 'vendor',214 recipient: 'device',215 request: 0x42,216 value: 0x1234,217 index: 0x5678218 }, 7)),219 assertRejectsWithNotConfiguredError(device.controlTransferOut({220 requestType: 'vendor',221 recipient: 'device',222 request: 0x42,223 value: 0x1234,224 index: 0x5678225 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))),226 assertRejectsWithNotConfiguredError(device.clearHalt('in', 1)),227 assertRejectsWithNotConfiguredError(device.transferIn(1, 8)),228 assertRejectsWithNotConfiguredError(229 device.transferOut(1, new ArrayBuffer(8))),230 assertRejectsWithNotConfiguredError(231 device.isochronousTransferIn(1, [8])),232 assertRejectsWithNotConfiguredError(233 device.isochronousTransferOut(1, new ArrayBuffer(8), [8])),234 ])).then(() => device.close());235 });236}, 'methods requiring it reject when the device is unconfigured');237usb_test(() => {238 return getFakeDevice().then(({ device }) => {239 return device.open()240 .then(() => device.selectConfiguration(1))241 .then(() => device.claimInterface(0))242 .then(() => {243 assert_true(device.configuration.interfaces[0].claimed);244 return device.releaseInterface(0);245 })246 .then(() => {247 assert_false(device.configuration.interfaces[0].claimed);248 return device.close();249 });250 });251}, 'an interface can be claimed and released');252usb_test(async () => {253 let { device } = await getFakeDevice()254 await device.open();255 await device.selectConfiguration(1);256 await device.claimInterface(0);257 assert_true(device.configuration.interfaces[0].claimed);258 await device.claimInterface(0);259 assert_true(device.configuration.interfaces[0].claimed);260 await device.close();261}, 'an interface can be claimed multiple times');262usb_test(async () => {263 let { device } = await getFakeDevice();264 await device.open();265 await device.selectConfiguration(1);266 await device.claimInterface(0);267 assert_true(device.configuration.interfaces[0].claimed);268 await device.releaseInterface(0);269 assert_false(device.configuration.interfaces[0].claimed);270 await device.releaseInterface(0);271 assert_false(device.configuration.interfaces[0].claimed);272 await device.close();273}, 'an interface can be released multiple times');274usb_test(async () => {275 let { device } = await getFakeDevice();276 await device.open();277 await device.selectConfiguration(1);278 return Promise.all([279 device.claimInterface(0),280 assertRejectsWithInterfaceStateChangeInProgressError(281 device.claimInterface(0)),282 assertRejectsWithInterfaceStateChangeInProgressError(283 device.releaseInterface(0)),284 assertRejectsWithInterfaceStateChangeInProgressError(device.open()),285 assertRejectsWithInterfaceStateChangeInProgressError(286 device.selectConfiguration(1)),287 assertRejectsWithInterfaceStateChangeInProgressError(device.reset()),288 assertRejectsWithInterfaceStateChangeInProgressError(289 device.selectAlternateInterface(0, 0)),290 assertRejectsWithInterfaceStateChangeInProgressError(291 device.controlTransferOut({292 requestType: 'standard',293 recipient: 'interface',294 request: 0x42,295 value: 0x1234,296 index: 0x0000,297 })),298 assertRejectsWithInterfaceStateChangeInProgressError(299 device.controlTransferOut({300 requestType: 'standard',301 recipient: 'interface',302 request: 0x42,303 value: 0x1234,304 index: 0x0000,305 }, new Uint8Array([1, 2, 3]))),306 assertRejectsWithInterfaceStateChangeInProgressError(307 device.controlTransferIn({308 requestType: 'standard',309 recipient: 'interface',310 request: 0x42,311 value: 0x1234,312 index: 0x0000313 }, 0)),314 assertRejectsWithInterfaceStateChangeInProgressError(device.close()),315 ]);316}, 'device operations reject if an interface state change is in progress');317usb_test(async () => {318 let { device } = await getFakeDevice();319 await device.open();320 await device.selectConfiguration(1);321 await device.claimInterface(0);322 assert_true(device.configuration.interfaces[0].claimed);323 await device.close(0);324 assert_false(device.configuration.interfaces[0].claimed);325}, 'interfaces are released on close');326usb_test(() => {327 return getFakeDevice().then(({ device }) => {328 const message = 'The interface number provided is not supported by the ' +329 'device in its current configuration.';330 return device.open()331 .then(() => device.selectConfiguration(1))332 .then(() => Promise.all([333 assertRejectsWithError(334 device.claimInterface(2), 'NotFoundError', message),335 assertRejectsWithError(336 device.releaseInterface(2), 'NotFoundError', message),337 ]))338 .then(() => device.close());339 });340}, 'a non-existent interface cannot be claimed or released');341usb_test(() => {342 return getFakeDevice().then(({ device, fakeDevice }) => {343 return device.open()344 .then(() => device.selectConfiguration(1))345 .then(() => waitForDisconnect(fakeDevice))346 .then(() => assertRejectsWithNotFoundError(device.claimInterface(0)));347 });348}, 'claimInterface rejects when called on a disconnected device');349usb_test(() => {350 return getFakeDevice().then(({ device, fakeDevice }) => {351 return device.open()352 .then(() => device.selectConfiguration(1))353 .then(() => device.claimInterface(0))354 .then(() => waitForDisconnect(fakeDevice))355 .then(() => assertRejectsWithNotFoundError(device.releaseInterface(0)));356 });357}, 'releaseInterface rejects when called on a disconnected device');358usb_test(() => {359 return getFakeDevice().then(({ device }) => {360 return device.open()361 .then(() => device.selectConfiguration(2))362 .then(() => device.claimInterface(0))363 .then(() => device.selectAlternateInterface(0, 1))364 .then(() => device.close());365 });366}, 'can select an alternate interface');367usb_test(() => {368 return getFakeDevice().then(({ device }) => {369 return device.open()370 .then(() => device.selectConfiguration(2))371 .then(() => device.claimInterface(0))372 .then(() => assertRejectsWithError(373 device.selectAlternateInterface(0, 2), 'NotFoundError',374 'The alternate setting provided is not supported by the device in ' +375 'its current configuration.'))376 .then(() => device.close());377 });378}, 'cannot select a non-existent alternate interface');379usb_test(() => {380 return getFakeDevice().then(({ device, fakeDevice }) => {381 return device.open()382 .then(() => device.selectConfiguration(2))383 .then(() => device.claimInterface(0))384 .then(() => waitForDisconnect(fakeDevice))385 .then(() => assertRejectsWithNotFoundError(device.selectAlternateInterface(0, 1)));386 });387}, 'selectAlternateInterface rejects when called on a disconnected device');388usb_test(async () => {389 let { device } = await getFakeDevice();390 let usbRequestTypes = ['standard', 'class', 'vendor'];391 let usbRecipients = ['device', 'interface', 'endpoint', 'other'];392 await device.open();393 await device.selectConfiguration(1);394 await device.claimInterface(0);395 await device.selectAlternateInterface(0, 0);396 for (const requestType of usbRequestTypes) {397 for (const recipient of usbRecipients) {398 let index = recipient === 'interface' ? 0x5600 : 0x5681;399 let result = await device.controlTransferIn({400 requestType: requestType,401 recipient: recipient,402 request: 0x42,403 value: 0x1234,404 index: index405 }, 7);406 assert_true(result instanceof USBInTransferResult);407 assert_equals(result.status, 'ok');408 assert_equals(result.data.byteLength, 7);409 assert_equals(result.data.getUint16(0), 0x07);410 assert_equals(result.data.getUint8(2), 0x42);411 assert_equals(result.data.getUint16(3), 0x1234);412 assert_equals(result.data.getUint16(5), index);413 }414 }415 await device.close();416}, 'can issue all types of IN control transfers');417usb_test(() => {418 return getFakeDevice().then(({ device, fakeDevice }) => {419 return device.open()420 .then(() => device.selectConfiguration(1))421 .then(() => waitForDisconnect(fakeDevice))422 .then(() => assertRejectsWithNotFoundError(device.controlTransferIn({423 requestType: 'vendor',424 recipient: 'device',425 request: 0x42,426 value: 0x1234,427 index: 0x5678428 }, 7)));429 });430}, 'controlTransferIn rejects when called on a disconnected device');431usb_test(async () => {432 let { device } = await getFakeDevice();433 let usbRequestTypes = ['standard', 'class', 'vendor'];434 let usbRecipients = ['device', 'interface', 'endpoint', 'other'];435 let dataArray = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);436 let dataTypes = [dataArray, dataArray.buffer];437 await device.open();438 await device.selectConfiguration(1);439 await device.claimInterface(0);440 await device.selectAlternateInterface(0, 0);441 for (const requestType of usbRequestTypes) {442 for (const recipient of usbRecipients) {443 let index = recipient === 'interface' ? 0x5600 : 0x5681;444 let transferParams = {445 requestType: requestType,446 recipient: recipient,447 request: 0x42,448 value: 0x1234,449 index: index450 };451 for (const data of dataTypes) {452 let result = await device.controlTransferOut(transferParams, data);453 assert_true(result instanceof USBOutTransferResult);454 assert_equals(result.status, 'ok');455 assert_equals(result.bytesWritten, 8);456 }457 let result = await device.controlTransferOut(transferParams);458 assert_true(result instanceof USBOutTransferResult);459 assert_equals(result.status, 'ok');460 }461 }462 await device.close();463}, 'can issue all types of OUT control transfers');464usb_test(() => {465 return getFakeDevice().then(({ device, fakeDevice }) => {466 return device.open()467 .then(() => device.selectConfiguration(1))468 .then(() => waitForDisconnect(fakeDevice))469 .then(() => assertRejectsWithNotFoundError(device.controlTransferOut({470 requestType: 'vendor',471 recipient: 'device',472 request: 0x42,473 value: 0x1234,474 index: 0x5678475 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))));476 });477}, 'controlTransferOut rejects when called on a disconnected device');478usb_test(async () => {479 let { device } = await getFakeDevice();480 await device.open();481 await device.selectConfiguration(1);482 await device.claimInterface(0);483 assertRejectsWithTypeError(device.controlTransferOut({484 requestType: 'invalid',485 recipient: 'device',486 request: 0x42,487 value: 0x1234,488 index: 0x5678489 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])));490 assertRejectsWithTypeError(device.controlTransferIn({491 requestType: 'invalid',492 recipient: 'device',493 request: 0x42,494 value: 0x1234,495 index: 0x5678496 }, 0));497 await device.close();498}, 'control transfers with a invalid request type reject');499usb_test(async () => {500 let { device } = await getFakeDevice();501 await device.open();502 await device.selectConfiguration(1);503 await device.claimInterface(0);504 assertRejectsWithTypeError(device.controlTransferOut({505 requestType: 'vendor',506 recipient: 'invalid',507 request: 0x42,508 value: 0x1234,509 index: 0x5678510 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])));511 assertRejectsWithTypeError(device.controlTransferIn({512 requestType: 'vendor',513 recipient: 'invalid',514 request: 0x42,515 value: 0x1234,516 index: 0x5678517 }, 0));518}, 'control transfers with a invalid recipient type reject');519usb_test(async () => {520 let { device } = await getFakeDevice();521 await device.open();522 await device.selectConfiguration(1);523 await device.claimInterface(0);524 assertRejectsWithNotFoundError(device.controlTransferOut({525 requestType: 'vendor',526 recipient: 'interface',527 request: 0x42,528 value: 0x1234,529 index: 0x0002 // Last byte of index is interface number.530 }, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])));531 assertRejectsWithNotFoundError(device.controlTransferIn({532 requestType: 'vendor',533 recipient: 'interface',534 request: 0x42,535 value: 0x1234,536 index: 0x0002 // Last byte of index is interface number.537 }, 0));538}, 'control transfers to a non-existant interface reject');539usb_test(() => {540 return getFakeDevice().then(({ device }) => {541 let interfaceRequest = {542 requestType: 'vendor',543 recipient: 'interface',544 request: 0x42,545 value: 0x1234,546 index: 0x5600 // Last byte of index is interface number.547 };548 let endpointRequest = {549 requestType: 'vendor',550 recipient: 'endpoint',551 request: 0x42,552 value: 0x1234,553 index: 0x5681 // Last byte of index is endpoint address.554 };555 let data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);556 return device.open()557 .then(() => device.selectConfiguration(1))558 .then(() => Promise.all([559 assertRejectsWithError(560 device.controlTransferIn(interfaceRequest, 7),561 'InvalidStateError'),562 assertRejectsWithError(563 device.controlTransferIn(endpointRequest, 7),564 'NotFoundError'),565 assertRejectsWithError(566 device.controlTransferOut(interfaceRequest, data),567 'InvalidStateError'),568 assertRejectsWithError(569 device.controlTransferOut(endpointRequest, data),570 'NotFoundError'),571 ]))572 .then(() => device.claimInterface(0))573 .then(() => Promise.all([574 device.controlTransferIn(interfaceRequest, 7).then(result => {575 assert_true(result instanceof USBInTransferResult);576 assert_equals(result.status, 'ok');577 assert_equals(result.data.byteLength, 7);578 assert_equals(result.data.getUint16(0), 0x07);579 assert_equals(result.data.getUint8(2), 0x42);580 assert_equals(result.data.getUint16(3), 0x1234);581 assert_equals(result.data.getUint16(5), 0x5600);582 }),583 device.controlTransferIn(endpointRequest, 7).then(result => {584 assert_true(result instanceof USBInTransferResult);585 assert_equals(result.status, 'ok');586 assert_equals(result.data.byteLength, 7);587 assert_equals(result.data.getUint16(0), 0x07);588 assert_equals(result.data.getUint8(2), 0x42);589 assert_equals(result.data.getUint16(3), 0x1234);590 assert_equals(result.data.getUint16(5), 0x5681);591 }),592 device.controlTransferOut(interfaceRequest, data),593 device.controlTransferOut(endpointRequest, data),594 ]))595 .then(() => device.close());596 });597}, 'requests to interfaces and endpoint require an interface claim');598usb_test(() => {599 return getFakeDevice().then(({ device }) => {600 return device.open()601 .then(() => device.selectConfiguration(1))602 .then(() => device.claimInterface(0))603 .then(() => device.clearHalt('in', 1))604 .then(() => device.close());605 });606}, 'can clear a halt condition');607usb_test(() => {608 return getFakeDevice().then(({ device, fakeDevice }) => {609 return device.open()610 .then(() => device.selectConfiguration(1))611 .then(() => device.claimInterface(0))612 .then(() => waitForDisconnect(fakeDevice))613 .then(() => assertRejectsWithNotFoundError(device.clearHalt('in', 1)));614 });615}, 'clearHalt rejects when called on a disconnected device');616usb_test(() => {617 return getFakeDevice().then(({ device }) => {618 let data = new DataView(new ArrayBuffer(1024));619 for (let i = 0; i < 1024; ++i)620 data.setUint8(i, i & 0xff);621 const notFoundMessage = 'The specified endpoint is not part of a claimed ' +622 'and selected alternate interface.';623 const rangeError = 'The specified endpoint number is out of range.';624 return device.open()625 .then(() => device.selectConfiguration(1))626 .then(() => device.claimInterface(0))627 .then(() => Promise.all([628 assertRejectsWithError(device.transferIn(2, 8),629 'NotFoundError', notFoundMessage), // Unclaimed630 assertRejectsWithError(device.transferIn(3, 8), 'NotFoundError',631 notFoundMessage), // Non-existent632 assertRejectsWithError(633 device.transferIn(16, 8), 'IndexSizeError', rangeError),634 assertRejectsWithError(device.transferOut(2, data),635 'NotFoundError', notFoundMessage), // Unclaimed636 assertRejectsWithError(device.transferOut(3, data), 'NotFoundError',637 notFoundMessage), // Non-existent638 assertRejectsWithError(639 device.transferOut(16, data), 'IndexSizeError', rangeError),640 ]));641 });642}, 'transfers to unavailable endpoints are rejected');643usb_test(() => {644 return getFakeDevice().then(({ device }) => {645 return device.open()646 .then(() => device.selectConfiguration(1))647 .then(() => device.claimInterface(0))648 .then(() => device.transferIn(1, 8))649 .then(result => {650 assert_true(result instanceof USBInTransferResult);651 assert_equals(result.status, 'ok');652 assert_equals(result.data.byteLength, 8);653 for (let i = 0; i < 8; ++i)654 assert_equals(result.data.getUint8(i), i, 'mismatch at byte ' + i);655 return device.close();656 });657 });658}, 'can issue IN interrupt transfer');659usb_test(() => {660 return getFakeDevice().then(({ device }) => {661 return device.open()662 .then(() => device.selectConfiguration(1))663 .then(() => device.claimInterface(1))664 .then(() => device.transferIn(2, 1024))665 .then(result => {666 assert_true(result instanceof USBInTransferResult);667 assert_equals(result.status, 'ok');668 assert_equals(result.data.byteLength, 1024);669 for (let i = 0; i < 1024; ++i)670 assert_equals(result.data.getUint8(i), i & 0xff,671 'mismatch at byte ' + i);672 return device.close();673 });674 });675}, 'can issue IN bulk transfer');676usb_test(() => {677 return getFakeDevice().then(({ device, fakeDevice }) => {678 return device.open()679 .then(() => device.selectConfiguration(1))680 .then(() => device.claimInterface(1))681 .then(() => waitForDisconnect(fakeDevice))682 .then(() => assertRejectsWithNotFoundError(device.transferIn(2, 1024)));683 });684}, 'transferIn rejects if called on a disconnected device');685usb_test(() => {686 return getFakeDevice().then(({ device }) => {687 return device.open()688 .then(() => device.selectConfiguration(1))689 .then(() => device.claimInterface(1))690 .then(() => {691 let data = new DataView(new ArrayBuffer(1024));692 for (let i = 0; i < 1024; ++i)693 data.setUint8(i, i & 0xff);694 return device.transferOut(2, data);695 })696 .then(result => {697 assert_true(result instanceof USBOutTransferResult);698 assert_equals(result.status, 'ok');699 assert_equals(result.bytesWritten, 1024);700 return device.close();701 });702 });703}, 'can issue OUT bulk transfer');704usb_test(() => {705 return getFakeDevice().then(({ device, fakeDevice }) => {706 return device.open()707 .then(() => device.selectConfiguration(1))708 .then(() => device.claimInterface(1))709 .then(() => {710 let data = new DataView(new ArrayBuffer(1024));711 for (let i = 0; i < 1024; ++i)712 data.setUint8(i, i & 0xff);713 return waitForDisconnect(fakeDevice)714 .then(() => assertRejectsWithNotFoundError(device.transferOut(2, data)));715 });716 });717}, 'transferOut rejects if called on a disconnected device');718usb_test(() => {719 return getFakeDevice().then(({ device }) => {720 return device.open()721 .then(() => device.selectConfiguration(2))722 .then(() => device.claimInterface(0))723 .then(() => device.selectAlternateInterface(0, 1))724 .then(() => device.isochronousTransferIn(725 1, [64, 64, 64, 64, 64, 64, 64, 64]))726 .then(result => {727 assert_true(result instanceof USBIsochronousInTransferResult);728 assert_equals(result.data.byteLength, 64 * 8, 'buffer size');729 assert_equals(result.packets.length, 8, 'number of packets');730 let byteOffset = 0;731 for (let i = 0; i < result.packets.length; ++i) {732 assert_true(733 result.packets[i] instanceof USBIsochronousInTransferPacket);734 assert_equals(result.packets[i].status, 'ok');735 assert_equals(result.packets[i].data.byteLength, 64);736 assert_equals(result.packets[i].data.buffer, result.data.buffer);737 assert_equals(result.packets[i].data.byteOffset, byteOffset);738 for (let j = 0; j < 64; ++j)739 assert_equals(result.packets[i].data.getUint8(j), j & 0xff,740 'mismatch at byte ' + j + ' of packet ' + i);741 byteOffset += result.packets[i].data.byteLength;742 }743 return device.close();744 });745 });746}, 'can issue IN isochronous transfer');747usb_test(() => {748 return getFakeDevice().then(({ device, fakeDevice }) => {749 return device.open()750 .then(() => device.selectConfiguration(2))751 .then(() => device.claimInterface(0))752 .then(() => device.selectAlternateInterface(0, 1))753 .then(() => waitForDisconnect(fakeDevice))754 .then(() => assertRejectsWithNotFoundError(device.isochronousTransferIn(755 1, [64, 64, 64, 64, 64, 64, 64, 64])));756 });757}, 'isochronousTransferIn rejects when called on a disconnected device');758usb_test(() => {759 return getFakeDevice().then(({ device }) => {760 return device.open()761 .then(() => device.selectConfiguration(2))762 .then(() => device.claimInterface(0))763 .then(() => device.selectAlternateInterface(0, 1))764 .then(() => {765 let data = new DataView(new ArrayBuffer(64 * 8));766 for (let i = 0; i < 8; ++i) {767 for (let j = 0; j < 64; ++j)768 data.setUint8(i * j, j & 0xff);769 }770 return device.isochronousTransferOut(771 1, data, [64, 64, 64, 64, 64, 64, 64, 64]);772 })773 .then(result => {774 assert_true(result instanceof USBIsochronousOutTransferResult);775 assert_equals(result.packets.length, 8, 'number of packets');776 let byteOffset = 0;777 for (let i = 0; i < result.packets.length; ++i) {778 assert_true(779 result.packets[i] instanceof USBIsochronousOutTransferPacket);780 assert_equals(result.packets[i].status, 'ok');781 assert_equals(result.packets[i].bytesWritten, 64);782 }783 return device.close();784 });785 });786}, 'can issue OUT isochronous transfer');787usb_test(() => {788 return getFakeDevice().then(({ device, fakeDevice }) => {789 return device.open()790 .then(() => device.selectConfiguration(2))791 .then(() => device.claimInterface(0))792 .then(() => device.selectAlternateInterface(0, 1))793 .then(() => {794 let data = new DataView(new ArrayBuffer(64 * 8));795 for (let i = 0; i < 8; ++i) {796 for (let j = 0; j < 64; ++j)797 data.setUint8(i * j, j & 0xff);798 }799 return waitForDisconnect(fakeDevice)800 .then(() => assertRejectsWithNotFoundError(device.isochronousTransferOut(801 1, data, [64, 64, 64, 64, 64, 64, 64, 64])));802 });803 });804}, 'isochronousTransferOut rejects when called on a disconnected device');805usb_test(() => {806 return getFakeDevice().then(({ device }) => {807 return device.open().then(() => device.reset()).then(() => device.close());808 });809}, 'can reset the device');810usb_test(() => {811 return getFakeDevice().then(({ device, fakeDevice }) => {812 return device.open()813 .then(() => waitForDisconnect(fakeDevice))814 .then(() => assertRejectsWithNotFoundError(device.reset()));815 });...
DiagnosticsRelay.js
Source:DiagnosticsRelay.js
1/**2 * This source code is licensed under the terms found in the LICENSE file in 3 * the root directory of this project.4 */5/******************************************************************************6 * Required Modules7 *****************************************************************************/8const PropertyListService = require("../PropertyListService");9const debug = require("debug")("libijs:services:DiagnosticsRelay");10/******************************************************************************11 * Consts12 *****************************************************************************/13const DiagnosticsRequestTypes = {14 All: "All",15 WiFi: "WiFi",16 GasGauge: "GasGauge",17 NAND: "NAND",18};19const ActionFlags = {20 WaitForDisconnect: 1,21 DisplayPass: 2,22 DisplayFail: 4,23};24/**25 * 26 * 27 * @class DiagnosticsRelay28 * @extends {PropertyListService}29 */30class DiagnosticsRelay extends PropertyListService {31 /**32 * 33 * 34 * @param {string} [requestType=DiagnosticsRequestTypes.All]35 * @returns {JarvisEmitter}36 * @memberof DiagnosticsRelay37 */38 requestDiagnostics(requestType = DiagnosticsRequestTypes.All) {39 return this.__performRequest(requestType, null, "Diagnostics");40 }41 /**42 *43 *44 * @returns {JarvisEmitter}45 * @memberof DiagnosticsRelay46 */47 sleep() {48 return this.__performRequest("Sleep");49 }50 /**51 *52 *53 * @returns {JarvisEmitter}54 * @memberof DiagnosticsRelay55 */56 goodbye() {57 return this.__performRequest("Goodbye");58 }59 /**60 *61 *62 * @param {number} flags63 * @returns {JarvisEmitter}64 * @memberof DiagnosticsRelay65 */66 restart(flags) {67 return this.__performRequest("Restart", flags);68 }69 /**70 *71 *72 * @param {number} flags73 * @returns {JarvisEmitter}74 * @memberof DiagnosticsRelay75 */76 shutdown(flags) {77 return this.__performRequest("Shutdown", flags);78 }79 /**80 *81 *82 * @param {string[]} keys83 * @returns {JarvisEmitter}84 * @memberof DiagnosticsRelay85 */86 queryMobilegestalt(keys) {87 const request = {88 Request: "MobileGestalt",89 MobileGestaltKeys: keys,90 };91 return this.__performRequestWithCustomRequest(request, null, "Diagnostics");92 }93 /**94 * Note that it seems that caller should provide either plane, entry name95 * or class name, but not more than one. It seems if more than one is present96 * in the request, mobile_diagnostics_relay will use the first one it encounters,97 * in the following order: plane -> entry name -> entry class.98 *99 * @param {any} [plane=null]100 * @param {any} [name=null]101 * @param {any} [className=null]102 * @returns {JarvisEmitter}103 * @memberof DiagnosticsRelay104 */105 queryIORegistry(plane = null, entryName = null, className = null) {106 const request = {107 Request: "IORegistry",108 };109 if (plane) {110 request.CurrentPlane = plane;111 }112 if (entryName) {113 request.EntryName = entryName;114 }115 if (className) {116 request.EntryClass = className;117 }118 return this.__performRequestWithCustomRequest(request, null, "Diagnostics");119 }120 /**121 *122 *123 * @readonly124 * @static125 * @memberof DiagnosticsRelay126 */127 static get DiagnosticsRequestTypes() { return DiagnosticsRequestTypes; }128 /**129 *130 *131 * @readonly132 * @static133 * @memberof DiagnosticsRelay134 */135 static get ActionFlags() { return ActionFlags; }136 /**137 *138 *139 * @param {string} requestType140 * @param {number} [flags=null]141 * @param {string} [responseField=null]142 * @returns {JarvisEmitter}143 * @memberof DiagnosticsRelay144 */145 __performRequest(requestType, flags = null, responseField = null) {146 const request = {147 Request: requestType,148 };149 return this.__performRequestWithCustomRequest(request, flags, responseField);150 }151 /**152 *153 *154 * @param {Object} request155 * @param {number} [flags=null]156 * @param {string} [responseField=null]157 * @returns {JarvisEmitter}158 * @memberof DiagnosticsRelay159 */160 __performRequestWithCustomRequest(request, flags = null, responseField = null) {161 if (flags) {162 if (flags & ActionFlags.WaitForDisconnect) {163 request.WaitForDisconnect = true;164 }165 if (flags & ActionFlags.DisplayPass) {166 request.DisplayPass = true;167 }168 if (flags & ActionFlags.DisplayFail) {169 request.DisplayFail = true;170 }171 }172 return this._writeXml(request, true)173 .done.middleware((next, response) => {174 // For requests which don't need a specific response field, we'll just return true\false175 if (this.__checkResponse(request, response)) {176 next(responseField ? response[responseField] : true);177 } else {178 // TODO: Call "error"179 next(responseField ? null : false);180 }181 });182 }183 /**184 *185 *186 * @param {Object} request187 * @param {Object} response188 * @returns {boolean}189 * @memberof DiagnosticsRelay190 */191 __checkResponse(request, response) {192 if (!response.Status) {193 debug(`Got an invalid response for "${request.Request}" request (missing status): ${JSON.stringify(response)}`);194 return false;195 }196 if ("Success" !== response.Status) {197 if (("Failure" === response.Status) || ("UnknownRequest" === response.Status)) {198 debug(`"${request.Request}" request has failed with status: ${JSON.stringify(response)}`);199 } else {200 debug(`"${request.Request}" request has returned an unknown status: ${JSON.stringify(response)}`);201 }202 return false;203 }204 return true;205 }206}...
Using AI Code Generation
1var wpt = require('webpagetest');2var util = require('util');3var wpt = new WebPageTest('www.webpagetest.org');4var options = {5};6wpt.runTest(options, function(err, data) {7 if (err) return console.error(err);8 console.log(util.inspect(data));9 wpt.waitForDisconnect(data.data.testId, 5, function(err, data) {10 if (err) return console.error(err);11 console.log(util.inspect(data));12 });13});14var wpt = require('webpagetest');15var util = require('util');16var wpt = new WebPageTest('www.webpagetest.org');17var options = {18};19wpt.runTest(options, function(err, data) {20 if (err) return console.error(err);21 console.log(util.inspect(data));22 wpt.waitForTestStart(data.data.testId, 5, function(err, data) {23 if (err) return console.error(err);24 console.log(util.inspect(data));25 });26});27var wpt = require('webpagetest');28var util = require('util');29var wpt = new WebPageTest('www.webpagetest.org');30var options = {31};32wpt.runTest(options, function(err, data) {33 if (err) return console.error(err);34 console.log(util.inspect(data));35 wpt.getTestStatus(data.data.testId, function(err, data) {36 if (err) return console.error(err);37 console.log(util.inspect(data));38 });39});
Using AI Code Generation
1var wptool = require('wptool');2wptool.waitForDisconnect(function(err, result) {3 if(err) {4 console.log("Error: " + err);5 } else {6 console.log("Result: " + result);7 }8});9var wptool = require('wptool');10wptool.waitForDisconnect(function(err, result) {11 if(err) {12 console.log("Error: " + err);13 } else {14 console.log("Result: " + result);15 }16});17var wptool = require('wptool');18wptool.waitForDisconnect(function(err, result) {19 if(err) {20 console.log("Error: " + err);21 } else {22 console.log("Result: " + result);23 }24});25var wptool = require('wptool');26wptool.waitForDisconnect(function(err, result) {27 if(err) {28 console.log("Error: " + err);29 } else {30 console.log("Result: " + result);31 }32});33var wptool = require('wptool');34wptool.waitForDisconnect(function(err, result) {35 if(err) {36 console.log("Error: " + err);37 } else {38 console.log("Result: " + result);39 }40});41var wptool = require('wptool');42wptool.waitForDisconnect(function(err, result) {43 if(err) {44 console.log("Error: " + err);45 } else {46 console.log("Result: " + result);47 }48});49var wptool = require('wptool');50wptool.waitForDisconnect(function(err, result) {51 if(err) {52 console.log("Error: " + err);53 } else {54 console.log("Result: " + result);55 }56});
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('API_KEY');3test.waitForDisconnect('Test ID', function(err, data) {4 if(err) {5 console.log(err);6 } else {7 console.log(data);8 }9});10var wpt = require('webpagetest');11var test = wpt('API_KEY');12test.getTesters('Location', function(err, data) {13 if(err) {14 console.log(err);15 } else {16 console.log(data);17 }18});19var wpt = require('webpagetest');20var test = wpt('API_KEY');21test.getLocations(function(err, data) {22 if(err) {23 console.log(err);24 } else {25 console.log(data);26 }27});28var wpt = require('webpagetest');29var test = wpt('API_KEY');30test.getTestStatus('Test ID', function(err, data) {31 if(err) {32 console.log(err);33 } else {34 console.log(data);35 }36});
Using AI Code Generation
1var wpt = require('webpagetest');2var webPageTest = new wpt('API_KEY');3var options = {4};5webPageTest.runTest(options.url, options, function(err, data) {6 if (err) return console.log(err);7 var testId = data.data.testId;8 console.log('Test ID: ' + testId);9 webPageTest.waitForTestComplete(testId, function(err, data) {10 if (err) return console.log(err);11 console.log(data);12 });13});14var wpt = require('webpagetest');15var webPageTest = new wpt('API_KEY');16var options = {17};18webPageTest.runTest(options.url, options, function(err, data) {19 if (err) return console.log(err);20 var testId = data.data.testId;21 console.log('Test ID: ' + testId);22 webPageTest.waitForTestComplete(testId, function(err, data) {23 if (err) return console.log(err);24 console.log(data);25 });26});27var wpt = require('webpagetest');28var webPageTest = new wpt('API_KEY');29var options = {30};31webPageTest.runTest(options.url, options, function(err, data) {32 if (err) return console.log(err);33 var testId = data.data.testId;34 console.log('Test ID: ' + testId);35 webPageTest.waitForTestComplete(testId, function(err, data) {36 if (err) return console.log(err);37 console.log(data);38 webPageTest.getTestResults(testId, function(err, data) {39 if (err) return console.log(err);40 console.log(data);41 });42 });43});
Using AI Code Generation
1var wptools = require('wptools');2function waitForDisconnect() {3 var options = {4 targetFilter: function(target) {5 return target.type === 'page';6 }7 };8 wptools.waitForDisconnect(options, function() {9 console.log('Chrome has disconnected');10 });11}12waitForDisconnect();13wptools.waitForTarget(options, filter, callback)14var wptools = require('wptools');15function waitForTarget() {16 var options = {17 targetFilter: function(target) {18 return target.type === 'page';19 }20 };21 wptools.waitForTarget(options, function(target) {22 console.log('New target found: ' + target.id);23 });24}25waitForTarget();26wptools.waitForTargetList(options, filter, callback)
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!!