summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/bluetooth/resources/health-thermometer-iframe.html
blob: f9f7a6f0d7d9cff3e2c24edba6751292a003f6de (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<body>
<button>Click me!</button>
<script>
let device, gatt;

test_driver.set_test_context(parent);

function requestDeviceWithOptionsAndConnect(options) {
  return test_driver.click(document.getElementsByTagName("button")[0])
      .then(() => navigator.bluetooth.requestDevice(options))
      .then(device => device.gatt.connect());
}

window.addEventListener('message', (messageEvent) => {
  switch (messageEvent.data.type) {
    case 'GetAvailability':
      navigator.bluetooth.getAvailability()
          .then(availability => parent.postMessage(availability, '*'))
          .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
      break;
    case 'GetDevices':
      navigator.bluetooth.getDevices()
          .then(devices => parent.postMessage('Success', '*'))
          .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
      break;
    case 'RequestDevice':
      test_driver.click(document.getElementsByTagName('button')[0])
          .then(
              () => navigator.bluetooth.requestDevice(
                  {filters: [{services: ['generic_access']}]}))
          .then(device => {
            if (device.constructor.name === 'BluetoothDevice') {
              parent.postMessage('Success', '*');
            } else {
              parent.postMessage(
                  `FAIL: requestDevice in iframe returned ${device.name}`, '*');
            }
          })
          .catch(err => parent.postMessage(`FAIL: ${err.name}: ${err.message}`, '*'));
      break;
    case 'RequestLEScan':
      test_driver.click(document.getElementsByTagName('button')[0])
          .then(
              () => navigator.bluetooth.requestLEScan(
                {filters: [{name: 'Health Thermometer'}]}))
          .then(leScan => {
              if (leScan.active) {
                parent.postMessage('Success', '*');
                leScan.stop();
              } else {
                parent.postMessage(`FAIL: the LE scan hasn't been initiated.`, '*');
              }
          })
          .catch(err => parent.postMessage(`FAIL: ${err.name}: ${err.message}`, '*'));
      break;
    case 'RequestAndConnect':
      requestDeviceWithOptionsAndConnect(messageEvent.data.options)
          .then(_ => {
            gatt = _;
            device = gatt.device;
            parent.postMessage('Connected', '*');
          })
          .catch(err => {
            parent.postMessage(`FAIL: ${err}`, '*');
          });
      break;
    case 'DiscoverServices':
      requestDeviceWithOptionsAndConnect(messageEvent.data.options)
          .then(gatt => gatt.getPrimaryServices())
          .then(() => parent.postMessage('DiscoveryComplete', '*'))
          .catch(err => {
            parent.postMessage(`FAIL: ${err}`, '*');
          });
      break;
    case 'GetService':
      if (typeof gatt === 'undefined') {
        parent.postMessage('FAIL: no GATT server', '*');
        break;
      }
      gatt.getPrimaryService(messageEvent.data.options)
          .then(() => parent.postMessage('ServiceReceived', '*'))
          .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
      break;
    default:
      parent.postMessage(
          `FAIL: Bad message type: ${messageEvent.data.type}`, '*');
  }
});
</script>