Commit 22217063 authored by Ovidio Henriquez's avatar Ovidio Henriquez Committed by Commit Bot

bluetooth: disconnect evt to standalone tests

This change updates the gattserverdisconnected-event tests in wpt/ to be
standalone window tests. This change allows the tests to be formatted
with clang-format.

Bug: 994756
Change-Id: Id08fd9bdef1fc8b37d8ad192b23e11a77a99f4c4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1761071
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarMatt Reynolds <mattreynolds@chromium.org>
Auto-Submit: Ovidio de Jesús Ruiz-Henríquez <odejesush@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689743}
parent 422a57e0
<!DOCTYPE html>
<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="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
const test_desc = 'A device disconnecting while connected should fire the ' +
'gattserverdisconnected event.';
bluetooth_test(() => getConnectedHealthThermometerDevice()
.then(({device, fake_peripheral}) => {
fake_peripheral.simulateGATTDisconnection();
return eventPromise(device, 'gattserverdisconnected');
})
.then(e => assert_true(e.bubbles)),
test_desc);
</script>
// META: script=/resources/testharness.js
// META: script=/resources/testharnessreport.js
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/bluetooth/resources/bluetooth-helpers.js
'use strict';
const test_desc = 'A device disconnecting while connected should fire the ' +
'gattserverdisconnected event.';
bluetooth_test(async () => {
const {device, fake_peripheral} = await getConnectedHealthThermometerDevice();
const disconnectPromise = eventPromise(device, 'gattserverdisconnected');
await fake_peripheral.simulateGATTDisconnection();
let disconnectEvent = await disconnectPromise;
assert_true(disconnectEvent.bubbles);
}, test_desc);
<!DOCTYPE html>
<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="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
// META: script=/resources/testharness.js
// META: script=/resources/testharnessreport.js
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/bluetooth/resources/bluetooth-helpers.js
'use strict';
const test_desc = 'A device disconnecting after the BluetoothDevice object ' +
'has been GC\'ed should not access freed memory.';
bluetooth_test(() => getConnectedHealthThermometerDevice()
.then(({fake_peripheral}) => {
// 1. Disconnect.
fake_peripheral.simulateGATTDisconnection();
// 2. Run garbage collection.
fake_peripheral = undefined;
runGarbageCollection();
})
bluetooth_test(async () => {
let {fake_peripheral} = await getConnectedHealthThermometerDevice();
// 1. Disconnect.
await fake_peripheral.simulateGATTDisconnection();
// 2. Run garbage collection.
fake_peripheral = undefined;
await runGarbageCollection();
// 3. Wait 50ms after the GC runs for the disconnection event to come back.
// There's nothing to assert other than that only valid memory is used.
.then(() => new Promise(resolve => step_timeout(resolve, 50))),
test_desc);
</script>
await new Promise(resolve => step_timeout(resolve, 50));
}, test_desc);
<!DOCTYPE html>
<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="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
// META: script=/resources/testharness.js
// META: script=/resources/testharnessreport.js
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/bluetooth/resources/bluetooth-helpers.js
'use strict';
const test_desc = 'If a site disconnects from a device while the platform is ' +
'disconnecting that device, only one gattserverdisconnected event should ' +
'fire.';
let device, fake_peripheral;
let num_events = 0;
bluetooth_test(() => getConnectedHealthThermometerDevice()
.then(_ => ({device, fake_peripheral} = _))
bluetooth_test(async () => {
const {device, fake_peripheral} = await getConnectedHealthThermometerDevice();
let num_events = 0;
// 1. Listen for disconnections.
.then(() =>
device.addEventListener('gattserverdisconnected', () => num_events++))
device.addEventListener('gattserverdisconnected', () => num_events++);
// 2. Disconnect several times.
.then(() => Promise.all([
await Promise.all([
eventPromise(device, 'gattserverdisconnected'),
fake_peripheral.simulateGATTDisconnection(),
device.gatt.disconnect(),
device.gatt.disconnect(),
]))
]);
// 3. Wait to catch disconnect events.
.then(() => new Promise(resolve => step_timeout(resolve, 50)))
await new Promise(resolve => step_timeout(resolve, 50));
// 4. Ensure there is exactly 1 disconnection recorded.
.then(() => assert_equals(num_events, 1)),
test_desc);
</script>
assert_equals(num_events, 1);
}, test_desc);
<!DOCTYPE html>
<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="/bluetooth/resources/bluetooth-helpers.js"></script>
<script>
'use strict';
let test_desc = 'A device that reconnects during the gattserverdisconnected ' +
'event should still receive gattserverdisconnected events after ' +
're-connection.';
let device, fake_peripheral;
bluetooth_test(() => getConnectedHealthThermometerDevice()
.then(_ => ({device, fake_peripheral} = _))
// 1. Disconnect.
.then(() => new Promise(resolve => {
fake_peripheral.simulateGATTDisconnection();
device.addEventListener(
'gattserverdisconnected', function onDisconnected() {
device.removeEventListener('gattserverdisconnected', onDisconnected);
// 2. Reconnect.
fake_peripheral.setNextGATTConnectionResponse({
code: HCI_SUCCESS,
})
.then(() => device.gatt.connect())
.then(() => resolve());
});
}))
// 3. Disconnect after reconnecting.
.then(() => {
fake_peripheral.simulateGATTDisconnection();
return eventPromise(device, 'gattserverdisconnected')
}), test_desc);
</script>
// META: script=/resources/testharness.js
// META: script=/resources/testharnessreport.js
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/bluetooth/resources/bluetooth-helpers.js
'use strict';
const test_desc = 'A device that reconnects during the ' +
'gattserverdisconnected event should still receive ' +
'gattserverdisconnected events after re-connection.';
bluetooth_test(async () => {
const {device, fake_peripheral} = await getConnectedHealthThermometerDevice();
const reconnectPromise = new Promise(async (resolve) => {
device.addEventListener('gattserverdisconnected', async () => {
// 2. Reconnect.
await fake_peripheral.setNextGATTConnectionResponse({
code: HCI_SUCCESS,
});
await device.gatt.connect();
// 3. Disconnect after reconnecting.
const disconnectPromise = eventPromise(device, 'gattserverdisconnected');
fake_peripheral.simulateGATTDisconnection();
resolve(disconnectPromise);
}, {once: true});
});
// 1. Disconnect.
await fake_peripheral.simulateGATTDisconnection();
await reconnectPromise;
}, test_desc);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment