summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/bluetooth/device/gattserverdisconnected-event/reconnect-during-disconnected-event.https.window.js
blob: bdaf47c66198ce70f9a1951358f27b38dcfac4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
// META: script=/bluetooth/resources/bluetooth-test.js
// META: script=/bluetooth/resources/bluetooth-fake-devices.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);