Skip to content

[testdriver] Add some test_driver.bidi.bluetooth commands and event #52847

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/writing-tests/testdriver.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,12 @@ The module provides access to [Web Bluetooth](https://webbluetoothcg.github.io/w
```eval_rst
.. js:autofunction:: test_driver.bidi.bluetooth.handle_request_device_prompt
.. js:autofunction:: test_driver.bidi.bluetooth.simulate_adapter
.. js:autofunction:: test_driver.bidi.bluetooth.disable_simulation
.. js:autofunction:: test_driver.bidi.bluetooth.simulate_preconnected_peripheral
.. js:autofunction:: test_driver.bidi.bluetooth.simulate_gatt_connection_response
.. js:autofunction:: test_driver.bidi.bluetooth.simulate_gatt_disconnection
.. js:autofunction:: test_driver.bidi.bluetooth.request_device_prompt_updated
.. js:autofunction:: test_driver.bidi.bluetooth.gatt_connection_attempted
```

### Emulation ###
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[simulate_gatt_connection_response.https.html]
expected:
if product != "chrome": ERROR
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[simulate_gatt_disconnection.https.html]
expected:
if product != "chrome": ERROR
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,11 @@
<script src="resources/bidi-bluetooth-helper.js"></script>

<script>
const name = 'LE Device';
promise_setup(async () => {
await test_driver.bidi.bluetooth.simulate_adapter({
state: "powered-on"
});
await test_driver.bidi.bluetooth.simulate_preconnected_peripheral({
address: "09:09:09:09:09:09",
name: name,
manufacturerData: [],
knownServiceUuids: []
});
await test_driver.bidi.bluetooth.request_device_prompt_updated.subscribe();
});

promise_test(async (t) => {
bluetooth_test(async (t) => {
const handle_prompt_promise =
test_driver.bidi.bluetooth.request_device_prompt_updated.once().then(
(promptEvent) => {
Expand All @@ -36,10 +26,10 @@
const [device] = await Promise.all([requestDeviceWithTrustedClick({
acceptAllDevices: true
}), handle_prompt_promise]);
assert_equals(device.name, name);
assert_equals(device.name, DEVICE_NAME);
}, "accept upon request_device_prompt_updated event");

promise_test(async (t) => {
bluetooth_test(async (t) => {
const handle_prompt_promise =
test_driver.bidi.bluetooth.request_device_prompt_updated.once().then(
(promptEvent) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

const DEVICE_NAME = 'LE Device';
const DEVICE_ADDRESS = '09:09:09:09:09:09';

/**
* Waits until the document has finished loading.
* @returns {Promise<void>} Resolves if the document is already completely
Expand Down Expand Up @@ -49,7 +52,7 @@ async function callWithTrustedClick(callback) {
function selectFirstDeviceOnDevicePromptUpdated() {
return test_driver.bidi.bluetooth.request_device_prompt_updated.once().then(
(promptEvent) => {
assert_greater_than_equal(promptEvent.devices.length, 0);
assert_greater_than(promptEvent.devices.length, 0);
return test_driver.bidi.bluetooth.handle_request_device_prompt({
prompt: promptEvent.prompt,
accept: true,
Expand All @@ -58,6 +61,26 @@ function selectFirstDeviceOnDevicePromptUpdated() {
});
}

/**
* Create a GATT connection to the `device` by registering a one-time handler
* that simulate a successful GATT connection response upon a GATT connection
* attempted event and making a GATT connection to it.
* @returns {Promise} fulfilled after the GATT connection is created, or
* rejected if the operation fails.
*/
async function createGattConnection(device) {
const simulationProcessedPromise =
test_driver.bidi.bluetooth.gatt_connection_attempted.once().then(
(event) => {
return test_driver.bidi.bluetooth.simulate_gatt_connection_response({
address: event.address,
code: 0x0,
});
});
const connectPromise = device.gatt.connect();
await Promise.all([connectPromise, simulationProcessedPromise]);
}

/**
* Calls requestDevice() in a context that's 'allowed to show a popup'.
* @returns {Promise<BluetoothDevice>} Resolves with a Bluetooth device if
Expand All @@ -68,3 +91,30 @@ function requestDeviceWithTrustedClick(...args) {
() => navigator.bluetooth.requestDevice(...args));
}

/**
* This is a test helper to run promise_test with Bluetooth emulation setup
* before the test and teardown after the test.
* @param {function{*}: Promise<*>} test_function The test to run.
* @param {string} name The name or description of the test.
* @returns {Promise<void>} Resolves if the test ran successfully, or rejects if
* the test failed.
*/
function bluetooth_test(test_function, name) {
return promise_test(async (t) => {
assert_implements(navigator.bluetooth, 'missing navigator.bluetooth');
await test_driver.bidi.bluetooth.simulate_adapter({
state: "powered-on"
});
await test_driver.bidi.bluetooth.simulate_preconnected_peripheral({
address: DEVICE_ADDRESS,
name: DEVICE_NAME,
manufacturerData: [],
knownServiceUuids: []
});
try {
await test_function(t);
} finally {
await test_driver.bidi.bluetooth.disable_simulation();
}
}, name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

assert_equals(false, await navigator.bluetooth.getAvailability(),
"Assert bluetooth is not available");
await test_driver.bidi.bluetooth.disable_simulation();
}, "simulate bluetooth adapter 'absent'");

promise_test(async (t) => {
Expand All @@ -23,6 +24,7 @@

assert_equals(true, await navigator.bluetooth.getAvailability(),
"Assert bluetooth available");
await test_driver.bidi.bluetooth.disable_simulation();
}, "simulate bluetooth adapter 'powered-off'");

promise_test(async (t) => {
Expand All @@ -32,5 +34,6 @@

assert_equals(true, await navigator.bluetooth.getAvailability(),
"Assert bluetooth available");
await test_driver.bidi.bluetooth.disable_simulation();
}, "simulate bluetooth adapter 'powered-on'");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>TestDriver bidi.bluetooth.simulate_gatt_connection_response method</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js?feature=bidi"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/bidi-bluetooth-helper.js"></script>

<script>
promise_setup(async () => {
await test_driver.bidi.bluetooth.request_device_prompt_updated.subscribe();
await test_driver.bidi.bluetooth.gatt_connection_attempted.subscribe();
});

bluetooth_test(async (t) => {
const handle_prompt_promise = selectFirstDeviceOnDevicePromptUpdated();
const [device] = await Promise.all([requestDeviceWithTrustedClick({
acceptAllDevices: true
}), handle_prompt_promise]);

const simulationProcessedPromise =
test_driver.bidi.bluetooth.gatt_connection_attempted.once().then(
(event) => {
return test_driver.bidi.bluetooth.simulate_gatt_connection_response({
address: event.address,
code: 0x0,
});
});
const connectPromise = device.gatt.connect();
await Promise.all([connectPromise, simulationProcessedPromise]);
assert_true(device.gatt.connected);
}, "simulate a GATT connection response.");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>TestDriver bidi.bluetooth.simulate_gatt_disconnection method</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js?feature=bidi"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/bidi-bluetooth-helper.js"></script>

<script>
promise_setup(async () => {
await test_driver.bidi.bluetooth.request_device_prompt_updated.subscribe();
await test_driver.bidi.bluetooth.gatt_connection_attempted.subscribe();
});

bluetooth_test(async (t) => {
const handle_prompt_promise = selectFirstDeviceOnDevicePromptUpdated();
const [device] = await Promise.all([requestDeviceWithTrustedClick({
acceptAllDevices: true
}), handle_prompt_promise]);

await createGattConnection(device);
assert_true(device.gatt.connected);
await test_driver.bidi.bluetooth.simulate_gatt_disconnection({
address: DEVICE_ADDRESS,
});
assert_false(device.gatt.connected);
}, "simulate a GATT disconnection.");
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
});

promise_test(async (t) => {
const name = 'LE Device';
await test_driver.bidi.bluetooth.simulate_preconnected_peripheral({
address: "09:09:09:09:09:09",
name: name,
address: DEVICE_ADDRESS,
name: DEVICE_NAME,
manufacturerData: [],
knownServiceUuids: []
});
const handle_prompt_promise = selectFirstDeviceOnDevicePromptUpdated();
const [device] = await Promise.all([requestDeviceWithTrustedClick({
acceptAllDevices: true
}), handle_prompt_promise]);
assert_equals(device.name, name);
assert_equals(device.name, DEVICE_NAME);
await test_driver.bidi.bluetooth.disable_simulation();
}, "simulate a preconnected peripheral.");
</script>

Loading