summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/serial/resources
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/serial/resources')
-rw-r--r--testing/web-platform/tests/serial/resources/automation.js66
-rw-r--r--testing/web-platform/tests/serial/resources/common.js33
-rw-r--r--testing/web-platform/tests/serial/resources/manual.js38
-rw-r--r--testing/web-platform/tests/serial/resources/open-in-iframe.html33
-rw-r--r--testing/web-platform/tests/serial/resources/serial-allowed-by-permissions-policy-worker.js14
-rw-r--r--testing/web-platform/tests/serial/resources/serial-disabled-by-permissions-policy-worker.js17
6 files changed, 201 insertions, 0 deletions
diff --git a/testing/web-platform/tests/serial/resources/automation.js b/testing/web-platform/tests/serial/resources/automation.js
new file mode 100644
index 0000000000..e88fdb1a9d
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/automation.js
@@ -0,0 +1,66 @@
+'use strict';
+
+// These tests rely on the User Agent providing an implementation of the
+// FakeSerialService interface which replaces the platform-specific
+// implementation of the Web Serial API with one that can be automated from
+// Javascript for testing purposes.
+//
+// In Chromium-based browsers this implementation is provided by a polyfill
+// in order to reduce the amount of test-only code shipped to users. To enable
+// these tests the browser must be run with these options:
+//
+// --enable-blink-features=MojoJS,MojoJSTest
+
+let fakeSerialService = undefined;
+
+// Returns a SerialPort instance and associated FakeSerialPort instance.
+async function getFakeSerialPort(fake) {
+ let token = fake.addPort();
+ let fakePort = fake.getFakePort(token);
+
+ let ports = await navigator.serial.getPorts();
+ assert_equals(ports.length, 1);
+
+ let port = ports[0];
+ assert_true(port instanceof SerialPort);
+
+ return { port, fakePort };
+}
+
+function serial_test(func, name, properties) {
+ promise_test(async (test) => {
+ assert_implements(navigator.serial, 'missing navigator.serial');
+ if (fakeSerialService === undefined) {
+ // Try loading a polyfill for the fake serial service.
+ if (isChromiumBased) {
+ const fakes = await import('/resources/chromium/fake-serial.js');
+ fakeSerialService = fakes.fakeSerialService;
+ }
+ }
+ assert_implements(fakeSerialService, 'missing fakeSerialService after initialization');
+
+ fakeSerialService.start();
+ try {
+ await func(test, fakeSerialService);
+ } finally {
+ fakeSerialService.stop();
+ fakeSerialService.reset();
+ }
+ }, name, properties);
+}
+
+function trustedClick() {
+ return new Promise(resolve => {
+ let button = document.createElement('button');
+ button.textContent = 'click to continue test';
+ button.style.display = 'block';
+ button.style.fontSize = '20px';
+ button.style.padding = '10px';
+ button.onclick = () => {
+ document.body.removeChild(button);
+ resolve();
+ };
+ document.body.appendChild(button);
+ test_driver.click(button);
+ });
+}
diff --git a/testing/web-platform/tests/serial/resources/common.js b/testing/web-platform/tests/serial/resources/common.js
new file mode 100644
index 0000000000..5177f83a86
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/common.js
@@ -0,0 +1,33 @@
+// Compare two Uint8Arrays.
+function compareArrays(actual, expected) {
+ assert_true(actual instanceof Uint8Array, 'actual is Uint8Array');
+ assert_true(expected instanceof Uint8Array, 'expected is Uint8Array');
+ assert_equals(actual.byteLength, expected.byteLength, 'lengths equal');
+ for (let i = 0; i < expected.byteLength; ++i)
+ assert_equals(actual[i], expected[i], `Mismatch at position ${i}.`);
+}
+
+// Reads from |reader| until at least |targetLength| is read or the stream is
+// closed. The data is returned as a combined Uint8Array.
+async function readWithLength(reader, targetLength) {
+ const chunks = [];
+ let actualLength = 0;
+
+ while (true) {
+ let {value, done} = await reader.read();
+ chunks.push(value);
+ actualLength += value.byteLength;
+
+ if (actualLength >= targetLength || done) {
+ // It would be better to allocate |buffer| up front with the number of
+ // of bytes expected but this is the best that can be done without a
+ // BYOB reader to control the amount of data read.
+ const buffer = new Uint8Array(actualLength);
+ chunks.reduce((offset, chunk) => {
+ buffer.set(chunk, offset);
+ return offset + chunk.byteLength;
+ }, 0);
+ return buffer;
+ }
+ }
+}
diff --git a/testing/web-platform/tests/serial/resources/manual.js b/testing/web-platform/tests/serial/resources/manual.js
new file mode 100644
index 0000000000..ac19136b35
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/manual.js
@@ -0,0 +1,38 @@
+let manualTestPort = null;
+
+navigator.serial.addEventListener('disconnect', (e) => {
+ if (e.target === manualTestPort) {
+ manualTestPort = null;
+ }
+})
+
+async function getPortForManualTest() {
+ if (manualTestPort) {
+ return manualTestPort;
+ }
+
+ 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);
+ });
+
+ manualTestPort = await navigator.serial.requestPort({filters: []});
+ assert_true(manualTestPort instanceof SerialPort);
+
+ return manualTestPort;
+}
+
+function manual_serial_test(func, name, properties) {
+ promise_test(async (test) => {
+ await func(test, await getPortForManualTest());
+ }, name, properties);
+}
diff --git a/testing/web-platform/tests/serial/resources/open-in-iframe.html b/testing/web-platform/tests/serial/resources/open-in-iframe.html
new file mode 100644
index 0000000000..9bf8beb66a
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/open-in-iframe.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+
+<script src="/resources/testdriver.js"></script>
+<script src="/resources/testdriver-vendor.js"></script>
+
+<body>
+ <button>Fake user gesture</button>
+</body>
+
+<script>
+ 'use strict';
+
+ test_driver.set_test_context(parent);
+
+ window.onmessage = messageEvent => {
+ switch (messageEvent.data.type) {
+ case 'GetPorts':
+ navigator.serial.getPorts()
+ .then(ports => parent.postMessage('Success', '*'))
+ .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
+ break;
+ case 'RequestPort':
+ test_driver.click(document.getElementsByTagName('button')[0])
+ .then(() => navigator.serial.requestPort({filters: []}))
+ .then(port => parent.postMessage('Success', '*'))
+ .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
+ break;
+ default:
+ parent.postMessage(
+ `FAIL: Bad message type: ${messageEvent.data}`, '*');
+ };
+ };
+</script>
diff --git a/testing/web-platform/tests/serial/resources/serial-allowed-by-permissions-policy-worker.js b/testing/web-platform/tests/serial/resources/serial-allowed-by-permissions-policy-worker.js
new file mode 100644
index 0000000000..cef0aacdfc
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/serial-allowed-by-permissions-policy-worker.js
@@ -0,0 +1,14 @@
+'use strict';
+
+importScripts('/resources/testharness.js');
+
+let workerType;
+
+if (typeof postMessage === 'function') {
+ workerType = 'dedicated';
+}
+
+promise_test(() => navigator.serial.getPorts(),
+ `Inherited header permissions policy allows ${workerType} workers.`);
+
+done();
diff --git a/testing/web-platform/tests/serial/resources/serial-disabled-by-permissions-policy-worker.js b/testing/web-platform/tests/serial/resources/serial-disabled-by-permissions-policy-worker.js
new file mode 100644
index 0000000000..afac442974
--- /dev/null
+++ b/testing/web-platform/tests/serial/resources/serial-disabled-by-permissions-policy-worker.js
@@ -0,0 +1,17 @@
+'use strict';
+
+importScripts('/resources/testharness.js');
+
+const header = 'Permissions-Policy header serial=()';
+let workerType;
+
+if (typeof postMessage === 'function') {
+ workerType = 'dedicated';
+}
+
+promise_test(() => navigator.serial.getPorts().then(
+ () => assert_unreached('expected promise to reject with SecurityError'),
+ error => assert_equals(error.name, 'SecurityError')),
+ `Inherited ${header} disallows ${workerType} workers.`);
+
+done();