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">
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>
19 <div id=
"content" style=
"display: none">
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});
35 document.addEventListener(
"keypress", (aKeyPressEvent) =
> {
36 keypress = aKeyPressEvent;
38 synthesizeNativeKey(aKeyboardLayout, aNativeKeyCode, aModifiers, aChars, aUnmodifiedChars);
43 async function runTests() {
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);
55 const kDescription = kTest.target +
": ";
57 let keypress = await testNativeKey(KEYBOARD_LAYOUT_EN_US, MAC_VK_ANSI_A, {},
"a",
"a");
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");
63 kDescription +
"'a' key press with shift key should cause firing keypress event");
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");
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");
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);
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");
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");
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");
97 kDescription +
"'7' key press with option key should not cause firing keypress event");
103 SimpleTest.waitForFocus(runTests);