summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webusb/usbDevice-iframe.https.html
blob: 5a6dd0565f51e8af904ed544dc63cb5f1cf6efea (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/test-only-api.js"></script>
<script src="resources/fake-devices.js"></script>
<script src="resources/usb-helpers.js"></script>
<script>
'use strict';

async function connectInIframe() {
  let iframe = document.createElement('iframe');
  let opened = false;

  iframe.src = 'resources/open-in-iframe.html';
  document.body.appendChild(iframe);

  await navigator.usb.test.attachToContext(iframe);
  function nextIFrameMessage() {
    return new Promise(resolve => window.addEventListener(
        'message', e => resolve(e.data)));
  }
  iframe.contentWindow.postMessage('Ready', '*');

  assert_equals('Ready', (await nextIFrameMessage()));
  let fakeDevice = navigator.usb.test.addFakeDevice(fakeDeviceInit);
  let closedPromise = new Promise(resolve => fakeDevice.onclose = resolve)
      .then(() => assert_true(opened));

  assert_equals('Success', (await nextIFrameMessage()));
  opened = true;
  return { iframe, closedPromise };
}

usb_test(async () => {
  let { iframe, closedPromise } = await connectInIframe();
  document.body.removeChild(iframe);
  await closedPromise;
}, 'detaching iframe disconnects device.');

usb_test(async () => {
  let { iframe, closedPromise } = await connectInIframe();
  iframe.src = 'about:blank';
  await closedPromise;
}, 'navigating iframe disconnects device.');
</script>