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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
|
<html>
<head>
<title>Test for key event handler of text editor</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css"
href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<div id="display">
<input type="text" id="inputField">
<input type="password" id="passwordField">
<textarea id="textarea"></textarea>
</div>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests, window);
var inputField = document.getElementById("inputField");
var passwordField = document.getElementById("passwordField");
var textarea = document.getElementById("textarea");
const kIsMac = navigator.platform.includes("Mac");
const kIsWin = navigator.platform.includes("Win");
const kIsLinux = navigator.platform.includes("Linux");
async function runTests() {
var fm = SpecialPowers.Services.focus;
var capturingPhase = { fired: false, prevented: false };
var bubblingPhase = { fired: false, prevented: false };
var listener = {
handleEvent: function _hv(aEvent) {
is(aEvent.type, "keypress", "unexpected event is handled");
switch (aEvent.eventPhase) {
case aEvent.CAPTURING_PHASE:
capturingPhase.fired = true;
capturingPhase.prevented = aEvent.defaultPrevented;
break;
case aEvent.BUBBLING_PHASE:
bubblingPhase.fired = true;
bubblingPhase.prevented = aEvent.defaultPrevented;
aEvent.preventDefault(); // prevent the browser default behavior
break;
default:
ok(false, "event is handled in unexpected phase");
}
},
};
function check(aDescription,
aFiredOnCapture, aFiredOnBubbling, aPreventedOnBubbling) {
function getDesciption(aExpected) {
return aDescription + (aExpected ? " wasn't " : " was ");
}
is(capturingPhase.fired, aFiredOnCapture,
getDesciption(aFiredOnCapture) + "fired on capture phase");
is(bubblingPhase.fired, aFiredOnBubbling,
getDesciption(aFiredOnBubbling) + "fired on bubbling phase");
// If the event is fired on bubbling phase and it was already prevented
// on capture phase, it must be prevented on bubbling phase too.
if (capturingPhase.prevented) {
todo(false, aDescription +
" was consumed already, so, we cannot test the editor behavior actually");
aPreventedOnBubbling = true;
}
is(bubblingPhase.prevented, aPreventedOnBubbling,
getDesciption(aPreventedOnBubbling) + "prevented on bubbling phase");
}
var parentElement = document.getElementById("display");
SpecialPowers.wrap(parentElement).addEventListener("keypress", listener, { capture: true, mozSystemGroup: true });
SpecialPowers.wrap(parentElement).addEventListener("keypress", listener, { capture: false, mozSystemGroup: true });
async function doTest(aElement, aDescription, aIsSingleLine, aIsReadonly) {
function reset(aText) {
capturingPhase.fired = false;
capturingPhase.prevented = false;
bubblingPhase.fired = false;
bubblingPhase.prevented = false;
aElement.value = aText;
}
if (document.activeElement) {
document.activeElement.blur();
}
aDescription += ": ";
aElement.focus();
is(SpecialPowers.unwrap(fm.focusedElement), aElement, aDescription + "failed to move focus");
// Backspace key:
// If native key bindings map the key combination to something, it's consumed.
// If editor is readonly, it doesn't consume.
// If editor is editable, it consumes backspace and shift+backspace.
// Otherwise, editor doesn't consume the event but the native key
// bindings on nsTextControlFrame may consume it.
reset("");
synthesizeKey("KEY_Backspace");
check(aDescription + "Backspace", true, true, true);
reset("");
synthesizeKey("KEY_Backspace", {shiftKey: true});
check(aDescription + "Shift+Backspace", true, true, true);
reset("");
synthesizeKey("KEY_Backspace", {ctrlKey: true});
// Win: cmd_deleteWordBackward
check(aDescription + "Ctrl+Backspace",
true, true, aIsReadonly || kIsWin);
reset("");
synthesizeKey("KEY_Backspace", {altKey: true});
// Win: cmd_undo
// Mac: cmd_deleteWordBackward
check(aDescription + "Alt+Backspace",
true, true, aIsReadonly || kIsWin || kIsMac);
reset("");
synthesizeKey("KEY_Backspace", {metaKey: true});
check(aDescription + "Meta+Backspace", true, true, aIsReadonly || kIsMac);
// Delete key:
// If native key bindings map the key combination to something, it's consumed.
// If editor is readonly, it doesn't consume.
// If editor is editable, delete is consumed.
// Otherwise, editor doesn't consume the event but the native key
// bindings on nsTextControlFrame may consume it.
reset("");
synthesizeKey("KEY_Delete");
// Linux: native handler
// Mac: cmd_deleteCharForward
check(aDescription + "Delete",
true, true, !aIsReadonly || kIsLinux || kIsMac);
reset("");
// Win: cmd_cutOrDelete
// Linux: cmd_cut
// Mac: cmd_deleteCharForward
synthesizeKey("KEY_Delete", {shiftKey: true});
check(aDescription + "Shift+Delete",
true, true, true);
reset("");
synthesizeKey("KEY_Delete", {ctrlKey: true});
// Win: cmd_deleteWordForward
// Linux: cmd_copy
check(aDescription + "Ctrl+Delete",
true, true, kIsWin || kIsLinux);
reset("");
synthesizeKey("KEY_Delete", {altKey: true});
// Mac: cmd_deleteWordForward
check(aDescription + "Alt+Delete",
true, true, kIsMac);
reset("");
synthesizeKey("KEY_Delete", {metaKey: true});
// Linux: native handler consumed.
check(aDescription + "Meta+Delete",
true, true, kIsLinux);
// XXX input.value returns "\n" when it's empty, so, we should use dummy
// value ("a") for the following tests.
// Return key:
// If editor is readonly, it doesn't consume.
// If editor is editable and not single line editor, it consumes Return
// and Shift+Return.
// Otherwise, editor doesn't consume the event.
reset("a");
synthesizeKey("KEY_Enter");
check(aDescription + "Return",
true, true, !aIsSingleLine && !aIsReadonly);
is(aElement.value, !aIsSingleLine && !aIsReadonly ? "a\n" : "a",
aDescription + "Return");
reset("a");
synthesizeKey("KEY_Enter", {shiftKey: true});
check(aDescription + "Shift+Return",
true, true, !aIsSingleLine && !aIsReadonly);
is(aElement.value, !aIsSingleLine && !aIsReadonly ? "a\n" : "a",
aDescription + "Shift+Return");
reset("a");
synthesizeKey("KEY_Enter", {ctrlKey: true});
check(aDescription + "Ctrl+Return", true, true, false);
is(aElement.value, "a", aDescription + "Ctrl+Return");
reset("a");
synthesizeKey("KEY_Enter", {altKey: true});
check(aDescription + "Alt+Return", true, true, false);
is(aElement.value, "a", aDescription + "Alt+Return");
reset("a");
synthesizeKey("KEY_Enter", {metaKey: true});
check(aDescription + "Meta+Return", true, true, false);
is(aElement.value, "a", aDescription + "Meta+Return");
// Tab key:
// Editor consumes tab key event unless any modifier keys are pressed.
reset("a");
synthesizeKey("KEY_Tab");
check(aDescription + "Tab", true, true, false);
is(aElement.value, "a", aDescription + "Tab");
is(SpecialPowers.unwrap(fm.focusedElement), aElement,
aDescription + "focus moved unexpectedly (Tab)");
aElement.focus();
reset("a");
synthesizeKey("KEY_Tab", {shiftKey: true});
check(aDescription + "Shift+Tab", true, true, false);
is(aElement.value, "a", aDescription + "Shift+Tab");
is(SpecialPowers.unwrap(fm.focusedElement), aElement,
aDescription + "focus moved unexpectedly (Shift+Tab)");
// Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
// event should never be fired.
reset("a");
synthesizeKey("KEY_Tab", {ctrlKey: true});
check(aDescription + "Ctrl+Tab", false, false, false);
is(aElement.value, "a", aDescription + "Ctrl+Tab");
is(SpecialPowers.unwrap(fm.focusedElement), aElement,
aDescription + "focus moved unexpectedly (Ctrl+Tab)");
reset("a");
synthesizeKey("KEY_Tab", {altKey: true});
check(aDescription + "Alt+Tab", true, true, false);
is(aElement.value, "a", aDescription + "Alt+Tab");
is(SpecialPowers.unwrap(fm.focusedElement), aElement,
aDescription + "focus moved unexpectedly (Alt+Tab)");
reset("a");
synthesizeKey("KEY_Tab", {metaKey: true});
check(aDescription + "Meta+Tab", true, true, false);
is(aElement.value, "a", aDescription + "Meta+Tab");
is(SpecialPowers.unwrap(fm.focusedElement), aElement,
aDescription + "focus moved unexpectedly (Meta+Tab)");
// Esc key:
// In all cases, esc key events are not consumed
reset("abc");
synthesizeKey("KEY_Escape");
check(aDescription + "Esc", true, true, false);
reset("abc");
synthesizeKey("KEY_Escape", {shiftKey: true});
check(aDescription + "Shift+Esc", true, true, false);
reset("abc");
synthesizeKey("KEY_Escape", {ctrlKey: true});
check(aDescription + "Ctrl+Esc", true, true, false);
reset("abc");
synthesizeKey("KEY_Escape", {altKey: true});
check(aDescription + "Alt+Esc", true, true, false);
reset("abc");
synthesizeKey("KEY_Escape", {metaKey: true});
check(aDescription + "Meta+Esc", true, true, false);
// typical typing tests:
reset("");
sendString("M");
check(aDescription + "M", true, true, !aIsReadonly);
sendString("o");
check(aDescription + "o", true, true, !aIsReadonly);
sendString("z");
check(aDescription + "z", true, true, !aIsReadonly);
sendString("i");
check(aDescription + "i", true, true, !aIsReadonly);
sendString("l");
check(aDescription + "l", true, true, !aIsReadonly);
sendString("l");
check(aDescription + "l", true, true, !aIsReadonly);
sendString("a");
check(aDescription + "a", true, true, !aIsReadonly);
sendString(" ");
check(aDescription + "' '", true, true, !aIsReadonly);
is(aElement.value, !aIsReadonly ? "Mozilla " : "",
aDescription + "typed \"Mozilla \"");
// typing non-BMP character:
async function test_typing_surrogate_pair(
aTestPerSurrogateKeyPress,
aTestIllFormedUTF16KeyValue = false
) {
await SpecialPowers.pushPrefEnv({
set: [
["dom.event.keypress.dispatch_once_per_surrogate_pair", !aTestPerSurrogateKeyPress],
["dom.event.keypress.key.allow_lone_surrogate", aTestIllFormedUTF16KeyValue],
],
});
reset("");
let events = [];
function pushIntoEvents(aEvent) {
events.push(aEvent);
}
function getEventData(aKeyboardEventOrInputEvent) {
if (!aKeyboardEventOrInputEvent) {
return "{}";
}
switch (aKeyboardEventOrInputEvent.type) {
case "keydown":
case "keypress":
case "keyup":
return `{ type: "${aKeyboardEventOrInputEvent.type}", key="${
aKeyboardEventOrInputEvent.key
}", charCode=0x${
aKeyboardEventOrInputEvent.charCode.toString(16).toUpperCase()
} }`;
default:
return `{ type: "${aKeyboardEventOrInputEvent.type}", inputType="${
aKeyboardEventOrInputEvent.inputType
}", data="${aKeyboardEventOrInputEvent.data}" }`;
}
}
function getEventArrayData(aEvents) {
if (!aEvents.length) {
return "[]";
}
let result = "[\n";
for (const e of aEvents) {
result += ` ${getEventData(e)}\n`;
}
return result + "]";
}
aElement.addEventListener("keydown", pushIntoEvents);
aElement.addEventListener("keypress", pushIntoEvents);
aElement.addEventListener("keyup", pushIntoEvents);
aElement.addEventListener("beforeinput", pushIntoEvents);
aElement.addEventListener("input", pushIntoEvents);
synthesizeKey("\uD842\uDFB7");
aElement.removeEventListener("keydown", pushIntoEvents);
aElement.removeEventListener("keypress", pushIntoEvents);
aElement.removeEventListener("keyup", pushIntoEvents);
aElement.removeEventListener("beforeinput", pushIntoEvents);
aElement.removeEventListener("input", pushIntoEvents);
const settingDescription =
`aTestPerSurrogateKeyPress=${
aTestPerSurrogateKeyPress
}, aTestIllFormedUTF16KeyValue=${aTestIllFormedUTF16KeyValue}`;
const allowIllFormedUTF16 =
aTestPerSurrogateKeyPress && aTestIllFormedUTF16KeyValue;
check(`${aDescription}, ${settingDescription}a surrogate pair`, true, true, !aIsReadonly);
is(
aElement.value,
!aIsReadonly ? "\uD842\uDFB7" : "",
`${aDescription}, ${
settingDescription
}, The typed surrogate pair should've been inserted`
);
if (aIsReadonly) {
is(
getEventArrayData(events),
getEventArrayData(
// eslint-disable-next-line no-nested-ternary
aTestPerSurrogateKeyPress
? (
allowIllFormedUTF16
? [
{ type: "keydown", key: "\uD842\uDFB7", charCode: 0 },
{ type: "keypress", key: "\uD842", charCode: 0xD842 },
{ type: "keypress", key: "\uDFB7", charCode: 0xDFB7 },
{ type: "keyup", key: "\uD842\uDFB7", charCode: 0 },
]
: [
{ type: "keydown", key: "\uD842\uDFB7", charCode: 0 },
{ type: "keypress", key: "\uD842\uDFB7", charCode: 0xD842 },
{ type: "keypress", key: "", charCode: 0xDFB7 },
{ type: "keyup", key: "\uD842\uDFB7", charCode: 0 },
]
)
: [
{ type: "keydown", key: "\uD842\uDFB7", charCode: 0 },
{ type: "keypress", key: "\uD842\uDFB7", charCode: 0x20BB7 },
{ type: "keyup", key: "\uD842\uDFB7", charCode: 0 },
]
),
`${aDescription}, ${
settingDescription
}, Typing a surrogate pair in readonly editor should not cause input events`
);
} else {
is(
getEventArrayData(events),
getEventArrayData(
// eslint-disable-next-line no-nested-ternary
aTestPerSurrogateKeyPress
? (
allowIllFormedUTF16
? [
{ type: "keydown", key: "\uD842\uDFB7", charCode: 0 },
{ type: "keypress", key: "\uD842", charCode: 0xD842 },
{ type: "beforeinput", data: "\uD842", inputType: "insertText" },
{ type: "input", data: "\uD842", inputType: "insertText" },
{ type: "keypress", key: "\uDFB7", charCode: 0xDFB7 },
{ type: "beforeinput", data: "\uDFB7", inputType: "insertText" },
{ type: "input", data: "\uDFB7", inputType: "insertText" },
{ type: "keyup", key: "\uD842\uDFB7", charCode: 0 },
]
: [
{ type: "keydown", key: "\uD842\uDFB7", charCode: 0 },
{ type: "keypress", key: "\uD842\uDFB7", charCode: 0xD842 },
{ type: "beforeinput", data: "\uD842\uDFB7", inputType: "insertText" },
{ type: "input", data: "\uD842\uDFB7", inputType: "insertText" },
{ type: "keypress", key: "", charCode: 0xDFB7 },
{ type: "keyup", key: "\uD842\uDFB7", charCode: 0 },
]
)
: [
{ type: "keydown", key: "\uD842\uDFB7", charCode: 0 },
{ type: "keypress", key: "\uD842\uDFB7", charCode: 0x20BB7 },
{ type: "beforeinput", data: "\uD842\uDFB7", inputType: "insertText" },
{ type: "input", data: "\uD842\uDFB7", inputType: "insertText" },
{ type: "keyup", key: "\uD842\uDFB7", charCode: 0 },
]
),
`${aDescription}, ${
settingDescription
}, Typing a surrogate pair in editor should cause input events`
);
}
}
await test_typing_surrogate_pair(true, true);
await test_typing_surrogate_pair(true, false);
await test_typing_surrogate_pair(false);
}
await doTest(inputField, "<input type=\"text\">", true, false);
inputField.setAttribute("readonly", "readonly");
await doTest(inputField, "<input type=\"text\" readonly>", true, true);
await doTest(passwordField, "<input type=\"password\">", true, false);
passwordField.setAttribute("readonly", "readonly");
await doTest(passwordField, "<input type=\"password\" readonly>", true, true);
await doTest(textarea, "<textarea>", false, false);
textarea.setAttribute("readonly", "readonly");
await doTest(textarea, "<textarea readonly>", false, true);
SpecialPowers.wrap(parentElement).removeEventListener("keypress", listener, { capture: true, mozSystemGroup: true });
SpecialPowers.wrap(parentElement).removeEventListener("keypress", listener, { capture: false, mozSystemGroup: true });
SimpleTest.finish();
}
</script>
</body>
</html>
|