summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/interactive-elements/the-dialog-element/dialog-cancel-with-input.html
blob: 153d434317e9f9b4cc2e0e6e63a899e6b37912b2 (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>
<html>
<head>
  <title>Test dialog modal is closed by escape key with input focused</title>
  <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>
  <link rel="help" href="https://bugs.webkit.org/show_bug.cgi?id=227534">
  <link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1322947">
</head>
<body>
<p>Test dialog modal is closed by escape key with input focused</p>
<dialog id="dialog">
  <p>Hello World</p>
</dialog>

<dialog id="dialogWithAutofocus">
  <input autofocus/>
</dialog>

<script>
  setup({ single_test: true });

  const triggerEscKey = () => {
    test_driver.send_keys(document.documentElement, "\uE00C"); // ESC key
  };

  /* Make sure we still cancel the dialog even if the input element is focused */
  function runTestCancelWhenInputFocused() {
    const dialog = document.getElementById("dialogWithAutofocus");
    const input = document.querySelector("input");

    dialog.addEventListener("close", function() {
      assert_false(dialog.open, "dialog with input autofocused is closed");
      done();
    });
    dialog.showModal();
    assert_true(input == document.activeElement, "input element should be focused");

    triggerEscKey();
  }

  const dialog = document.getElementById("dialog");

  dialog.addEventListener("close", function() {
    assert_false(dialog.open, "dialog closed");
    step_timeout(function() {
      runTestCancelWhenInputFocused();
    }, 0);
  });

  dialog.showModal();
  triggerEscKey();
</script>
</pre>
</body>
</html>