manual.js (3264B)
1 let manualTestDevice = null; 2 3 navigator.usb.addEventListener('disconnect', (e) => { 4 if (e.device === manualTestDevice) { 5 manualTestDevice = null; 6 } 7 }) 8 9 async function getDeviceForManualTest() { 10 if (manualTestDevice) { 11 return manualTestDevice; 12 } 13 14 const button = document.createElement('button'); 15 button.textContent = 'Click to select a device'; 16 button.style.display = 'block'; 17 button.style.fontSize = '20px'; 18 button.style.padding = '10px'; 19 20 await new Promise((resolve) => { 21 button.onclick = () => { 22 document.body.removeChild(button); 23 resolve(); 24 }; 25 document.body.appendChild(button); 26 }); 27 28 manualTestDevice = await navigator.usb.requestDevice({filters: []}); 29 assert_true(manualTestDevice instanceof USBDevice); 30 31 return manualTestDevice; 32 } 33 34 function manual_usb_test(func, name, properties) { 35 promise_test(async (test) => { 36 await func(test, await getDeviceForManualTest()); 37 }, name, properties); 38 } 39 40 function manual_usb_serial_test(func, name, properties) { 41 promise_test(async (test) => { 42 const device = await getDeviceForManualTest(); 43 await device.open(); 44 test.add_cleanup(async () => { 45 if (device.opened) { 46 await device.close(); 47 } 48 }); 49 50 await device.selectConfiguration(1); 51 52 let controlInterface = undefined; 53 for (const iface of device.configuration.interfaces) { 54 const alternate = iface.alternates[0]; 55 if (alternate.interfaceClass == 2 && 56 alternate.interfaceSubclass == 2 && 57 alternate.interfaceProtocol == 0) { 58 controlInterface = iface; 59 break; 60 } 61 } 62 assert_not_equals(controlInterface, undefined, 63 'No control interface found.'); 64 65 let dataInterface = undefined; 66 for (const iface of device.configuration.interfaces) { 67 const alternate = iface.alternates[0]; 68 if (alternate.interfaceClass == 10 && 69 alternate.interfaceSubclass == 0 && 70 alternate.interfaceProtocol == 0) { 71 dataInterface = iface; 72 break; 73 } 74 } 75 assert_not_equals(dataInterface, undefined, 'No data interface found.'); 76 77 await device.claimInterface(controlInterface.interfaceNumber); 78 await device.claimInterface(dataInterface.interfaceNumber); 79 80 let inEndpoint = undefined; 81 for (const endpoint of dataInterface.alternate.endpoints) { 82 if (endpoint.type == 'bulk' && endpoint.direction == 'in') { 83 inEndpoint = endpoint; 84 break; 85 } 86 } 87 assert_not_equals(inEndpoint, undefined, 'No IN endpoint found.'); 88 89 let outEndpoint = undefined; 90 for (const endpoint of dataInterface.alternate.endpoints) { 91 if (endpoint.type == 'bulk' && endpoint.direction == 'out') { 92 outEndpoint = endpoint; 93 break; 94 } 95 } 96 assert_not_equals(outEndpoint, undefined, 'No OUT endpoint found.'); 97 98 // Execute a SET_CONTROL_LINE_STATE command to let the device know the 99 // host is ready to transmit and receive data. 100 await device.controlTransferOut({ 101 requestType: 'class', 102 recipient: 'interface', 103 request: 0x22, 104 value: 0x01, 105 index: controlInterface.interfaceNumber, 106 }); 107 108 await func(test, device, inEndpoint, outEndpoint); 109 }, name, properties); 110 }