summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webusb/resources/manual.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/webusb/resources/manual.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/webusb/resources/manual.js')
-rw-r--r--testing/web-platform/tests/webusb/resources/manual.js110
1 files changed, 110 insertions, 0 deletions
diff --git a/testing/web-platform/tests/webusb/resources/manual.js b/testing/web-platform/tests/webusb/resources/manual.js
new file mode 100644
index 0000000000..e8dc08a8bd
--- /dev/null
+++ b/testing/web-platform/tests/webusb/resources/manual.js
@@ -0,0 +1,110 @@
+let manualTestDevice = null;
+
+navigator.usb.addEventListener('disconnect', (e) => {
+ if (e.device === manualTestDevice) {
+ manualTestDevice = null;
+ }
+})
+
+async function getDeviceForManualTest() {
+ if (manualTestDevice) {
+ return manualTestDevice;
+ }
+
+ const button = document.createElement('button');
+ button.textContent = 'Click to select a device';
+ button.style.display = 'block';
+ button.style.fontSize = '20px';
+ button.style.padding = '10px';
+
+ await new Promise((resolve) => {
+ button.onclick = () => {
+ document.body.removeChild(button);
+ resolve();
+ };
+ document.body.appendChild(button);
+ });
+
+ manualTestDevice = await navigator.usb.requestDevice({filters: []});
+ assert_true(manualTestDevice instanceof USBDevice);
+
+ return manualTestDevice;
+}
+
+function manual_usb_test(func, name, properties) {
+ promise_test(async (test) => {
+ await func(test, await getDeviceForManualTest());
+ }, name, properties);
+}
+
+function manual_usb_serial_test(func, name, properties) {
+ promise_test(async (test) => {
+ const device = await getDeviceForManualTest();
+ await device.open();
+ test.add_cleanup(async () => {
+ if (device.opened) {
+ await device.close();
+ }
+ });
+
+ await device.selectConfiguration(1);
+
+ let controlInterface = undefined;
+ for (const iface of device.configuration.interfaces) {
+ const alternate = iface.alternates[0];
+ if (alternate.interfaceClass == 2 &&
+ alternate.interfaceSubclass == 2 &&
+ alternate.interfaceProtocol == 0) {
+ controlInterface = iface;
+ break;
+ }
+ }
+ assert_not_equals(controlInterface, undefined,
+ 'No control interface found.');
+
+ let dataInterface = undefined;
+ for (const iface of device.configuration.interfaces) {
+ const alternate = iface.alternates[0];
+ if (alternate.interfaceClass == 10 &&
+ alternate.interfaceSubclass == 0 &&
+ alternate.interfaceProtocol == 0) {
+ dataInterface = iface;
+ break;
+ }
+ }
+ assert_not_equals(dataInterface, undefined, 'No data interface found.');
+
+ await device.claimInterface(controlInterface.interfaceNumber);
+ await device.claimInterface(dataInterface.interfaceNumber);
+
+ let inEndpoint = undefined;
+ for (const endpoint of dataInterface.alternate.endpoints) {
+ if (endpoint.type == 'bulk' && endpoint.direction == 'in') {
+ inEndpoint = endpoint;
+ break;
+ }
+ }
+ assert_not_equals(inEndpoint, undefined, 'No IN endpoint found.');
+
+ let outEndpoint = undefined;
+ for (const endpoint of dataInterface.alternate.endpoints) {
+ if (endpoint.type == 'bulk' && endpoint.direction == 'out') {
+ outEndpoint = endpoint;
+ break;
+ }
+ }
+ assert_not_equals(outEndpoint, undefined, 'No OUT endpoint found.');
+
+ // Execute a SET_CONTROL_LINE_STATE command to let the device know the
+ // host is ready to transmit and receive data.
+ await device.controlTransferOut({
+ requestType: 'class',
+ recipient: 'interface',
+ request: 0x22,
+ value: 0x01,
+ index: controlInterface.interfaceNumber,
+ });
+
+ await func(test, device, inEndpoint, outEndpoint);
+ }, name, properties);
+} \ No newline at end of file