Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / widget / tests / test_keypress_event_with_alt_on_mac.html
blob01d4100f97849a876f77d645391cd4159f0ded6c
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Testing if keypress event is fired when alt key is pressed</title>
5 <script src="/tests/SimpleTest/SimpleTest.js"></script>
6 <script src="/tests/SimpleTest/EventUtils.js"></script>
7 <script src="/tests/SimpleTest/NativeKeyCodes.js"></script>
8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
9 </head>
10 <body>
11 <div id="display">
12 <input id="input">
13 <input id="password" type="password">
14 <input id="readonly-input" readonly>
15 <textarea id="textarea"></textarea>
16 <textarea id="readonly-textarea" readonly></textarea>
17 <button id="button">button</button>
18 </div>
19 <div id="content" style="display: none">
20 </div>
21 <pre id="test">
22 </pre>
24 <script class="testbody" type="application/javascript">
25 SimpleTest.waitForExplicitFinish();
27 async function testNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers,
28 aChars, aUnmodifiedChars) {
29 // XXX Need to listen keyup event here because synthesizeNativeKey() does not
30 // guarantee that its callback will be called after "keypress" and "keyup".
31 let waitForKeyUp = new Promise(resolve => {
32 document.addEventListener("keyup", resolve, {once: true});
33 });
34 let keypress;
35 document.addEventListener("keypress", (aKeyPressEvent) => {
36 keypress = aKeyPressEvent;
37 }, {once: true});
38 synthesizeNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers, aChars, aUnmodifiedChars);
39 await waitForKeyUp;
40 return keypress;
43 async function runTests() {
44 const kTests =
45 [ { target: "input", isEditable: true },
46 { target: "password", isEditable: true },
47 { target: "readonly-input", isEditable: false },
48 { target: "textarea", isEditable: true },
49 { target: "readonly-textarea", isEditable: false },
50 { target: "button", isEditable: false } ];
51 for (const kTest of kTests) {
52 let element = document.getElementById(kTest.target);
53 element.focus();
55 const kDescription = kTest.target + ": ";
57 let keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {}, "a", "a");
58 ok(keypress,
59 kDescription + "'a' key press should cause firing keypress event");
61 keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {shiftKey: true}, "A", "A");
62 ok(keypress,
63 kDescription + "'a' key press with shift key should cause firing keypress event");
64 ok(keypress.shiftKey,
65 kDescription + "shiftKey of 'a' key press with shift key should be true");
67 // When a key inputs a character with option key, we need to unset altKey for our editor.
68 // Otherwise, altKey should be true for compatibility with the other browsers.
69 keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {altKey: true}, "\u00E5", "a");
70 ok(keypress,
71 kDescription + "'a' key press with option key should cause firing keypress event");
72 is(keypress.altKey, !kTest.isEditable,
73 kDescription + "altKey of 'a' key press with option key should be " + !kTest.isEditable);
75 keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {altKey: true, shiftKey: true}, "\u00C5", "A");
76 ok(keypress,
77 kDescription + "'a' key press with option key and shift key should cause firing keypress event");
78 is(keypress.altKey, !kTest.isEditable,
79 kDescription + "altKey of 'a' key press with option key and shift key should be " + !kTest.isEditable);
80 ok(keypress.shiftKey,
81 kDescription + "shiftKey of 'a' key press with option key and shift key should be true");
83 keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {ctrlKey: true}, "\u0001", "a");
84 ok(!keypress,
85 kDescription + "'a' key press with control key should not cause firing keypress event");
87 keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {altKey: true, ctrlKey: true}, "\u0001", "a");
88 ok(!keypress,
89 kDescription + "'a' key press with option key and control key should not cause firing keypress event");
91 // XXX Cannot test with command key for now since keyup event won't be fired due to macOS's limitation.
93 // Some keys of Arabic - PC keyboard layout do not input any character with option key.
94 // In such case, we shouldn't dispatch keypress event.
95 keypress = await testNativeKey(KEYBOARD_LAYOUT_ARABIC_PC, MAC_VK_ANSI_7, {altKey: true}, "", "\u0667");
96 ok(!keypress,
97 kDescription + "'7' key press with option key should not cause firing keypress event");
100 SimpleTest.finish();
103 SimpleTest.waitForFocus(runTests);
104 </script>
105 </body>
106 </html>