summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/bluetooth/getDevices/granted-devices-with-services.https.window.js
blob: 3228543617decdc9a213883d650ba33ffd9b90c1 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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 = 'getDevices() resolves with permitted devices that can be ' +
    'GATT connected to.';

bluetooth_test(async () => {
  // Set up two connectable Bluetooth devices with their services discovered.
  // One device is a Health Thermometer device with the 'health_thermometer'
  // service while the other is a Heart Rate device with the 'heart_rate'
  // service. Both devices contain the 'generic_access' service.
  let fake_peripherals = await setUpHealthThermometerAndHeartRateDevices();
  for (let fake_peripheral of fake_peripherals) {
    await fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS});
    await fake_peripheral.addFakeService({uuid: 'generic_access'});
    if (fake_peripheral.address === '09:09:09:09:09:09')
      await fake_peripheral.addFakeService({uuid: 'health_thermometer'});
    else
      await fake_peripheral.addFakeService({uuid: 'heart_rate'});
    await fake_peripheral.setNextGATTDiscoveryResponse({code: HCI_SUCCESS});
  }

  // Request the Health Thermometer device with access to its 'generic_access'
  // service.
  await requestDeviceWithTrustedClick(
      {filters: [{name: 'Health Thermometer', services: ['generic_access']}]});
  let devices = await navigator.bluetooth.getDevices();
  assert_equals(
      devices.length, 1,
      `getDevices() should return the 'Health Thermometer' device.`);

  // Only the 'generic_access' service can be accessed.
  try {
    await devices[0].gatt.connect();
    await devices[0].gatt.getPrimaryService('generic_access');
    assert_promise_rejects_with_message(
        devices[0].gatt.getPrimaryService('health_thermometer'),
        {name: 'SecurityError'});
  } catch (err) {
    assert_unreached(`${err.name}: ${err.message}`);
  }

  // Request the Heart Rate device with access to both of its services.
  await requestDeviceWithTrustedClick({
    filters: [{name: 'Heart Rate', services: ['generic_access', 'heart_rate']}]
  });
  devices = await navigator.bluetooth.getDevices();
  assert_equals(
      devices.length, 2,
      `getDevices() should return the 'Health Thermometer' and 'Health ` +
          `Monitor' devices`);

  // All of Heart Rate device's services can be accessed, while only the
  // 'generic_access' service can be accessed on Health Thermometer.
  try {
    for (let device of devices) {
      await device.gatt.connect();
      await device.gatt.getPrimaryService('generic_access');
      if (device.name === 'Heart Rate') {
        await device.gatt.getPrimaryService('heart_rate');
      } else {
        assert_promise_rejects_with_message(
            devices[0].gatt.getPrimaryService('health_thermometer'),
            {name: 'SecurityError'});
      }
    }
  } catch (err) {
    assert_unreached(`${err.name}: ${err.message}`);
  }
}, test_desc);