summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/popovers/popover-invoking-attribute.html
blob: 8e312e90d7af1050dd055cedac9af243bac7f035 (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
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Popover invoking attribute</title>
<link rel="author" href="mailto:masonf@chromium.org">
<link rel=help href="https://open-ui.org/components/popover.research.explainer">
<link rel=help href="https://html.spec.whatwg.org/multipage/popover.html">
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/popover-utils.js"></script>
<script src="resources/popover-invoking-attribute.js"></script>

<body>
<script>
runPopoverInvokerTests(["auto","manual"]);
</script>

<button popovertarget=p1>Toggle Popover 1</button>
<div popover id=p1 style="border: 5px solid red;top: 100px;left: 100px;">This is popover #1</div>

<script>
function clickOn(element) {
  const actions = new test_driver.Actions();
  return actions.pointerMove(0, 0, {origin: element})
    .pointerDown({button: actions.ButtonType.LEFT})
    .pointerUp({button: actions.ButtonType.LEFT})
    .send();
}

const popover = document.querySelector('[popover]');
const button = document.querySelector('button');
let showCount = 0;
let hideCount = 0;
popover.addEventListener('beforetoggle',(e) => {
  if (e.newState === "open")
      ++showCount;
    else
      ++hideCount;
  });

async function assertState(expectOpen,expectShow,expectHide) {
  assert_equals(popover.matches(':popover-open'),expectOpen,'Popover open state is incorrect');
  await new Promise(resolve => requestAnimationFrame(resolve));
  assert_equals(showCount,expectShow,'Show count is incorrect');
  assert_equals(hideCount,expectHide,'Hide count is incorrect');
}

window.addEventListener('load', () => {
  promise_test(async () => {
    showCount = hideCount = 0;
    await assertState(false,0,0);
    await clickOn(button);
    await assertState(true,1,0);
    popover.hidePopover();
    await assertState(false,1,1);
    button.click();
    await assertState(true,2,1);
    popover.hidePopover();
    await assertState(false,2,2);
  }, "Clicking a popovertarget button opens a closed popover (also check event counts)");

  promise_test(async () => {
    showCount = hideCount = 0;
    await assertState(false,0,0);
    await clickOn(button);
    await assertState(true,1,0);
    await clickOn(button);
    await assertState(false,1,1);
  }, "Clicking a popovertarget button closes an open popover (also check event counts)");
});
</script>