summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/modal-dialog-blocks-mouse-events.html
blob: f6c0ec0ccb8c62925bcf1e8351b5554bf07098f3 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<link rel=author href="mailto:jarhar@chromium.org">
<link rel=author href="mailto:falken@chromium.org">
<link rel=help href="https://html.spec.whatwg.org/multipage/interactive-elements.html#the-dialog-element">
<link rel=help href="https://bugs.webkit.org/show_bug.cgi?id=110952">
<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>

<p>
To test manually, move the mouse to the blue box, click, and then move the
mouse outside. Then repeat for the red box. The test succeeds if both boxes
turn green
</p>

<style>
#inert-div {
  height: 100px;
  width: 100px;
  background: blue;
}

dialog {
  width: 100px;
}

dialog::backdrop {
  display: none;
}

#dialog-div {
  height: 100px;
  width: 100px;
  background: red;
}
</style>

<div id="inert-div"></div>
<dialog id="dialog">
  <div id="dialog-div"></div>
</dialog>

<script>
promise_test(async () => {
  async function clickOn(element) {
    const rect = element.getBoundingClientRect();
    const actions = new test_driver.Actions()
      .pointerMove(
        Math.floor(rect.left + rect.width / 2),
        Math.floor(rect.top + rect.height / 2))
      .pointerDown()
      .pointerUp()
      .pointerMove(0, 0);
    await actions.send();
  }

  dialog.showModal();

  inertDivHandledEvent = false;
  inertDiv = document.getElementById('inert-div');
  eventFiredOnInertNode = function(event) {
    inertDivHandledEvent = true;
    inertDiv.style.backgroundColor = 'red';
  };

  events =  ['mousedown', 'mouseup', 'click', 'mousemove', 'mouseover', 'mouseout'];
  dialogDiv = document.getElementById('dialog-div');
  handledEvents = {};
  handledEvents.dialogDiv = {};
  eventFiredOnDialog = function(event) {
    handledEvents.dialogDiv[event.type] = true;
    if (Object.keys(handledEvents.dialogDiv).length == events.length)
      dialogDiv.style.backgroundColor = 'green';
  };

  handledEvents.document = {};
  expectedEventCountForDocument = events.length - 1; // document won't get 'mouseout'
  eventFiredOnDocument = function(event) {
    handledEvents.document[event.type] = true;
    if (Object.keys(handledEvents.document).length == document.expectedEventCount && !inertDivHandledEvent) {
      inertDiv.style.backgroundColor = 'green';
    }
  };

  for (let i = 0; i < events.length; ++i) {
    inertDiv.addEventListener(events[i], eventFiredOnInertNode);
    dialogDiv.addEventListener(events[i], eventFiredOnDialog);
    document.addEventListener(events[i], eventFiredOnDocument);
  }

  await clickOn(inertDiv);
  assert_false(inertDivHandledEvent, 'Clicking on inert box');
  assert_equals(Object.keys(handledEvents.document).length, expectedEventCountForDocument, 'Clicking on inert box');

  await clickOn(dialogDiv);
  assert_false(inertDivHandledEvent, 'Clicking on non-inert box');
  assert_equals(Object.keys(handledEvents.dialogDiv).length, events.length, 'Clicking on non-inert box');
}, 'Ensure that mouse events are not dispatched to an inert node.');
</script>