summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webusb/resources/open-in-iframe.html
blob: ad0e12d3717dc6096ec4b2ef7afc8c16a756e5e7 (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
<!DOCTYPE html>
<!-- Intentionally use relative paths here because this file is also used by blink/web_tests/usb/usbDevice-iframe.html. -->
<script src="../../resources/test-only-api.js"></script>
<script src="usb-helpers.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>

<body>
  <button>Fake user gesture</button>
</body>

<script>
  'use strict';
  test_driver.set_test_context(parent);
  window.onmessage = messageEvent => {
    switch (messageEvent.data) {
      case 'ConnectEvent':
        navigator.usb.addEventListener('connect', connectEvent => {
          connectEvent.device.open()
              .then(() => parent.postMessage('Success', '*'))
              .catch(err =>
                  parent.postMessage(`FAIL: open rejected ${err}`, '*'));
        });
        parent.postMessage('Ready', '*');
        break;
      case 'GetDevices':
        navigator.usb.getDevices()
            .then(devices => parent.postMessage('Success', '*'))
            .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
        break;
      case 'RequestDevice':
        test_driver.click(document.getElementsByTagName('button')[0])
            .then(() => navigator.usb.requestDevice({filters: []}))
            .then(device => parent.postMessage('Success', '*'))
            .catch(err => parent.postMessage(`FAIL: ${err}`, '*'));
        break;
      default:
        parent.postMessage(
            `FAIL: Bad message type: ${messageEvent.data}`, '*');
    }
  };
</script>