summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/modal-dialog-ancestor-is-inert.html
blob: c6bcb5d4caf357db85606638d8e11ffcd52cf02f (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.chromium.org/p/chromium/issues/detail?id=329407">
<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>

<style>
#ancestor {
  position: absolute;
  height: 50px;
  width: 50px;
  top: 200px;
  left: 100px;
  border: 1px solid;
}

dialog {
  height: 50px;
  width: 50px;
  top: 200px;
  left: 200px;
  margin: 0;
}

dialog::backdrop {
  display: none;
}
</style>

<div id="ancestor">
  <dialog></dialog>
</div>

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

  const div = document.querySelector('#ancestor');
  const dialog = document.querySelector('dialog');
  dialog.showModal();

  const handledEvent = {};
  document.addEventListener('click', function(event) {
    handledEvent['document'] = true;
  });

  document.body.addEventListener('click', function(event) {
    handledEvent['body'] = true;
    // body should get a event only via bubbling.
    if (event.target != dialog) {
      assert_unreached('body was targeted for an click event');
      div.style.backgroundColor = 'red';
    }
  });

  div.addEventListener('click', function(event) {
    handledEvent['div'] = true;
    // div should get a event only via bubbling.
    if (event.target != dialog) {
      assert_unreached('div was targeted for an click event');
      div.style.backgroundColor = 'red';
    }
  });

  dialog.addEventListener('click', function(event) {
    handledEvent['dialog'] = true;
    dialog.style.backgroundColor = 'green';
    if (event.target != dialog) {
      assert_unreached('dialog was not targeted for a click event');
      dialog.style.backgroundColor = 'red';
    }
  });

  const nodes = [ 'document', 'body', 'div', 'dialog' ];
  nodes.map(function(node) { handledEvent[node] = false; });
  await clickOn(div);
  assert_true(handledEvent.document, 'Clicking on ancestor.');
  assert_false(handledEvent.body, 'Clicking on ancestor.');
  assert_false(handledEvent.dialog, 'Clicking on ancestor.');
  assert_false(handledEvent.div, 'Clicking on ancestor.');
  handledEvent.document = false;

  await clickOn(dialog);
  assert_true(handledEvent.document, 'Clicking on dialog.');
  assert_true(handledEvent.body, 'Clicking on dialog.');
  assert_true(handledEvent.dialog, 'Clicking on dialog.');
  assert_true(handledEvent.div, 'Clicking on dialog.');
}, 'Test that ancestors of modal dialog are inert.');
</script>