summaryrefslogtreecommitdiffstats
path: root/dom/html/test/dialog/test_cancelDialogByEscape.html
blob: d0c8d80385e4414bc002db1ca48e45d1180f131e (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1322947
-->
<head>
  <title>Test for Bug 1322947</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="SimpleTest.waitForFocus(runTest)">
  <a target="_blank"
    href="https://bugzilla.mozilla.org/show_bug.cgi?id=1322947"> Test dialog modal is closed by escape key</a>
<p id="display"></p>
<dialog id="dialog">
  <p>Hello World</p>
</dialog>

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

<pre id="test">
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();

/* 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() {
    ok(dialog.close, "dialog with input autofocused is closed");
    done();
  });
  dialog.showModal();
  ok(input == document.activeElement, "input element should be focused");

  synthesizeKey("VK_ESCAPE", {}, window);
}

function runTest() {
  const dialog = document.getElementById("dialog");

  dialog.addEventListener("close", function() {
    ok(dialog.close, "dialog closed");
    setTimeout(function(){
      runTestCancelWhenInputFocused();
    }, 0);
  });

  dialog.showModal();
  synthesizeKey("VK_ESCAPE", {}, window);
}

function done() {
  SimpleTest.finish();
}
</script>
</pre>
</body>
</html>