test_unicode_input_on_windows_with_emulation.html (5732B)
1 <!doctype html> 2 <head> 3 <meta charset="utf-8"> 4 <title>Test event sequence of Unicode character input of Windows builtin IME</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script src="/tests/SimpleTest/EventUtils.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"> 8 <script> 9 "use strict"; 10 11 SimpleTest.waitForExplicitFinish(); 12 SimpleTest.waitForFocus(async () => { 13 await SpecialPowers.pushPrefEnv({ 14 set: [ 15 ["test.events.async.enabled", true], 16 ], 17 }); 18 const input = document.querySelector("input"); 19 input.focus(); 20 21 let events = []; 22 function handler(aEvent) { 23 events.push({ 24 type: aEvent.type, 25 key: aEvent.key, 26 code: aEvent.code, 27 altKey: aEvent.altKey, 28 defaultPrevented: aEvent.defaultPrevented, 29 }); 30 } 31 function stringifyEvents(aEvents) { 32 if (!aEvents.length) { 33 return "[]"; 34 } 35 function stringifyEvent(aEvent) { 36 return `{ type: "${aEvent.type}", key: "${aEvent.key}", code: ${aEvent.code}, defaultPrevented: ${ 37 aEvent.defaultPrevented 38 }`; 39 } 40 let result = ""; 41 for (const event of aEvents) { 42 if (result == "") { 43 result = "[ "; 44 } else { 45 result += ", "; 46 } 47 result += stringifyEvent(event); 48 } 49 return result + " ]"; 50 } 51 input.addEventListener("keydown", handler); 52 input.addEventListener("keypress", handler); 53 input.addEventListener("keyup", handler); 54 55 const waitForInput = new Promise(resolve => { 56 input.addEventListener("input", resolve, {once: true}); 57 }); 58 59 /** 60 * On Windows, users can enable a legacy Unicode input IME with adding a 61 * registry key, `EnableHexNumpad` whose type is `REG_SZ` and value is "1" 62 * under `HKEY_CURRENT_USER\Control Panel\Input Method`. Then, user can 63 * type a Unicode character with typing the code point in hex while holding 64 * Alt key and typing "+" in the numpad. Finally, when the Alt key is 65 * released, a Unicode character should be inserted into focused editable 66 * element. In this case, NativeKey class dispatches the typing "+" and 67 * hex values only with `keydown` and `keyup` events, then, dispatch only 68 * a "keypress" for the Unicode character. 69 * 70 * This test checks whether the events are dispatched as expected in a 71 * content process when it comes from the parent process. 72 */ 73 74 const nsITextInputProcessor = SpecialPowers.Ci.nsITextInputProcessor; 75 const TIP = SpecialPowers.Cc["@mozilla.org/text-input-processor;1"].createInstance(nsITextInputProcessor); 76 ok(TIP.beginInputTransactionForTests(window), "beginInputTransactionForTests should've succeeded"); 77 TIP.keydown(new KeyboardEvent("keydown", { key: "Alt", code: "AltLeft" })); 78 TIP.keydown(new KeyboardEvent("keydown", { key: "+", code : "NumpadAdd" }), nsITextInputProcessor.KEY_DEFAULT_PREVENTED); 79 TIP.keyup(new KeyboardEvent("keyup", { key: "+", code : "NumpadAdd" })); 80 TIP.keydown(new KeyboardEvent("keydown", { key: "2", code : "Numpad2" }), nsITextInputProcessor.KEY_DEFAULT_PREVENTED); 81 TIP.keyup(new KeyboardEvent("keyup", { key: "2", code : "Numpad2" })); 82 TIP.keydown(new KeyboardEvent("keydown", { key: "7", code : "Numpad7" }), nsITextInputProcessor.KEY_DEFAULT_PREVENTED); 83 TIP.keyup(new KeyboardEvent("keyup", { key: "7", code : "Numpad7" })); 84 TIP.keydown(new KeyboardEvent("keydown", { key: "4", code : "Numpad4" }), nsITextInputProcessor.KEY_DEFAULT_PREVENTED); 85 TIP.keyup(new KeyboardEvent("keyup", { key: "4", code : "Numpad4" })); 86 TIP.keydown(new KeyboardEvent("keydown", { key: "e", code : "KeyE" }), nsITextInputProcessor.KEY_DEFAULT_PREVENTED); 87 TIP.keyup(new KeyboardEvent("keyup", { key: "e", code : "KeyE" })); 88 TIP.keyup(new KeyboardEvent("keyup", { key: "Alt", code: "AltLeft" })); 89 TIP.insertTextWithKeyPress("\u274e", new KeyboardEvent("keyup", { key: "Alt", code: "AltLeft" })); 90 91 info("Waiting for input event..."); 92 await waitForInput; 93 94 is( 95 input.value, 96 "\u274e", 97 "Only the unicode character should be inserted" 98 ); 99 is( 100 stringifyEvents(events), 101 stringifyEvents([ 102 { type: "keydown", key: "Alt", code: "AltLeft", altKey: true, defaultPrevented: false }, 103 { type: "keydown", key: "+", code: "NumpadAdd", altKey: true, defaultPrevented: false }, 104 { type: "keyup", key: "+", code: "NumpadAdd", altKey: true, defaultPrevented: false }, 105 { type: "keydown", key: "2", code: "Numpad2", altKey: true, defaultPrevented: false }, 106 { type: "keyup", key: "2", code: "Numpad2", altKey: true, defaultPrevented: false }, 107 { type: "keydown", key: "7", code: "Numpad7", altKey: true, defaultPrevented: false }, 108 { type: "keyup", key: "7", code: "Numpad7", altKey: true, defaultPrevented: false }, 109 { type: "keydown", key: "4", code: "Numpad4", altKey: true, defaultPrevented: false }, 110 { type: "keyup", key: "4", code: "Numpad4", altKey: true, defaultPrevented: false }, 111 { type: "keydown", key: "e", code: "KeyE", altKey: true, defaultPrevented: false }, 112 { type: "keyup", key: "e", code: "KeyE", altKey: true, defaultPrevented: false }, 113 { type: "keyup", key: "Alt", code: "AltLeft", altKey: false, defaultPrevented: false }, 114 { type: "keypress", key: "\u274e", code: "AltLeft", altKey: false, defaultPrevented: false }, 115 ]), 116 "Typing the code point should not cause keypress events but defaultPrevented should be false, " + 117 "and finally, the Unicode character should be inserted with a keypress event" 118 ); 119 120 input.removeEventListener("keydown", handler); 121 input.removeEventListener("keypress", handler); 122 input.removeEventListener("keyup", handler); 123 124 SimpleTest.finish(); 125 }); 126 </script> 127 </head> 128 <body> 129 <input> 130 </body> 131 </html>