summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/portals/portal-activate-default.html
blob: b1a8feb1f4de4936f3c8b5b21a9629a5da02a0a2 (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
<!DOCTYPE html>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/open-blank-host.js"></script>
<script>
promise_test(async t => {
  assert_implements("HTMLPortalElement" in self);
  const w = await openBlankPortalHost();
  try {
    const bc = new BroadcastChannel('click-activate');
    const portal = w.document.createElement('portal');
    portal.src = new URL(`resources/portal-activate-broadcastchannel.html?bc=${bc.name}`, location.href);
    w.document.body.appendChild(portal);
    await new Promise(resolve => portal.onload = resolve);
    let activated = new Promise(resolve => bc.onmessage = e => resolve(e.data));
    portal.click();
    let {event, data} = await activated;
    assert_equals(event, 'portalactivate');
    assert_equals(data, undefined);
  } finally {
    w.close();
  }
}, "Clicking should activate with undefined data.");

promise_test(async t => {
  assert_implements("HTMLPortalElement" in self);
  const w = await openBlankPortalHost();
  try {
    const bc = new BroadcastChannel('prevent-no-activate');
    const portal = w.document.createElement('portal');
    portal.src = new URL(`resources/portal-activate-broadcastchannel.html?bc=${bc.name}`, location.href);
    portal.onclick = e => e.preventDefault();
    w.document.body.appendChild(portal);
    await new Promise(resolve => portal.onload = resolve);
    bc.onmessage = t.unreached_func('activation should not occur');
    portal.click();
    await new Promise(resolve => t.step_timeout(resolve, 3000));
  } finally {
    w.close();
  }
}, "Clicking shouldn't activate if prevented.");

// Script didn't create the promise so it shouldn't observe one.
// This forecloses a naive implementation of this behavior that simply calls the WebIDL operation.
promise_test(async t => {
  assert_implements("HTMLPortalElement" in self);
  const w = await openBlankPortalHost();
  try {
    const portal = w.document.createElement('portal');
    w.onunhandledrejection = t.unreached_func('unhandledrejection event should not fire');
    portal.click();
    await new Promise(resolve => t.step_timeout(resolve, 3000));
  } finally {
    w.close();
  }
}, "Failed activation should not surface as an unhandled promise rejection.");
</script>