summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/popovers/light-dismiss-event-ordering.html
blob: be39050ac6bab9f149a30032f20f086378974b38 (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
<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=help href="https://chromium-review.googlesource.com/c/chromium/src/+/4023021">
<link rel=help href="https://github.com/whatwg/html/pull/8221#discussion_r1041135388">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="resources/popover-utils.js"></script>

<button id=target>target</button>
<div id=popover popover=auto>popover</div>

<script>
for (const capture of [true, false]) {
  for (const eventName of ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'click']) {
    promise_test(async t => {
      t.add_cleanup(() => {
        try {
          popover.hidePopover();
        } catch {}
      });

      popover.showPopover();
      document.addEventListener(eventName, event => {
        event.preventDefault();
      }, {capture, once: true});
      // Click away from the popover to activate light dismiss.
      await clickOn(target);
      assert_equals(document.querySelectorAll(':popover-open').length, 0,
        'The popover should be closed via light dismiss even when preventDefault is called.');

      popover.showPopover();
      document.addEventListener(eventName, event => {
        event.stopPropagation();
      }, {capture, once: true});
      // Click away from the popover to activate light dismiss.
      await clickOn(target);
      assert_equals(document.querySelectorAll(':popover-open').length, 0,
        'The popover should be closed via light dismiss even when stopPropagation is called.');

    }, `Tests the interactions between popover light dismiss and pointer/mouse events. eventName: ${eventName}, capture: ${capture}`);
  }
}

promise_test(async t => {
  t.add_cleanup(() => {
    try {
      popover.hidePopover();
    } catch {}
  });
  popover.showPopover();

  const expectedEvents = [
    'pointerdown',
    'mousedown',
    'beforetoggle newState: closed',
    'pointerup',
    'mouseup',
    'click'
  ];
  const events = [];

  for (const eventName of ['pointerdown', 'pointerup', 'mousedown', 'mouseup', 'click']) {
    document.addEventListener(eventName, () => events.push(eventName));
  }
  popover.addEventListener('beforetoggle', event => {
    events.push('beforetoggle newState: ' + event.newState);
  });

  // Click away from the popover to activate light dismiss.
  await clickOn(target);

  assert_array_equals(events, expectedEvents,
    'pointer and popover events should be fired in the correct order.');

  assert_equals(document.querySelectorAll(':popover-open').length, 0,
    'The popover should be closed via light dismiss.');

}, 'Tests the order of pointer/mouse events during popover light dismiss.');
</script>