blob: 5abed2167dd909be9abda20c04f000e924a7ff9c [file] [log] [blame]
<!DOCTYPE html>
<body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../resources/testdriver.js"></script>
<script src="../resources/testdriver-vendor.js"></script>
<script src="file:///gen/layout_test_data/mojo/public/js/mojo_bindings.js"></script>
<script src="file:///gen/mojo/public/mojom/base/unguessable_token.mojom.js"></script>
<script src="file:///gen/third_party/blink/public/mojom/serial/serial.mojom.js"></script>
<script src="resources/serial-test-utils.js"></script>
<script>
promise_test(async () => {
try {
let port = await navigator.serial.requestPort({});
assert_unreached();
} catch (e) {
assert_equals(e.code, DOMException.SECURITY_ERR);
}
}, 'requestPort() rejects without a user gesture');
promise_test(async () => {
let interceptor =
new MojoInterfaceInterceptor(blink.mojom.SerialService.name);
interceptor.oninterfacerequest = e => e.handle.close();
interceptor.start();
try {
await trustedClick();
let port = await navigator.serial.requestPort({});
assert_unreached();
} catch (e) {
assert_equals(e.code, DOMException.NOT_FOUND_ERR);
} finally {
interceptor.stop();
}
}, 'requestPort() rejects if Mojo service connection fails');
serial_test(async (fake) => {
try {
await trustedClick();
let port = await navigator.serial.requestPort({});
assert_unreached();
} catch (e) {
assert_equals(e.code, DOMException.NOT_FOUND_ERR);
}
}, 'requestPort() rejects if no port has been selected');
serial_test(async (fake) => {
let guid = fake.addPort();
fake.setSelectedPort(guid);
await trustedClick();
let port = await navigator.serial.requestPort({});
assert_true(port instanceof SerialPort);
// TODO: Assert that product IDs (if provided) are passed through.
}, 'requestPort() returns the selected port');
serial_test(async (fake) => {
let guid = fake.addPort();
fake.setSelectedPort(guid);
await trustedClick();
let firstPort = await navigator.serial.requestPort({});
assert_true(firstPort instanceof SerialPort);
let secondPort = await navigator.serial.requestPort({});
assert_true(secondPort instanceof SerialPort);
assert_true(firstPort === secondPort);
}, 'requestPort() returns the same port object every time');
</script>
</body>