Bug 1438157 - part 1: Make EventUtils.synthesizeKey()'s second argument, aEvent, optional r?smaug
Most callers of EventUtils.synthesizeKey() call it only with the first argument,
aKey. Therefore, it should be optional argument.
This patch also fixes a bug of EventUtils.sendChar(). It sets shiftKey to
true even for "0" - "9" and " ". When I replace syntesizeKey() which just
type text with sendString() in the following patch, I hit this bug.
MozReview-Commit-ID: 9ndL9jLho2N
--- a/testing/mochitest/tests/SimpleTest/EventUtils.js
+++ b/testing/mochitest/tests/SimpleTest/EventUtils.js
@@ -273,17 +273,18 @@ function sendChar(aChar, aWindow) {
case "\"":
case "|":
case "<":
case ">":
case "?":
hasShift = true;
break;
default:
- hasShift = (aChar == aChar.toUpperCase());
+ hasShift = aChar.toLowerCase() != aChar.toUpperCase() &&
+ aChar == aChar.toUpperCase();
break;
}
synthesizeKey(aChar, { shiftKey: hasShift }, aWindow);
}
/**
* Send the string aStr to the focused element.
*
@@ -895,38 +896,40 @@ function _computeKeyCodeFromChar(aChar)
* Note that if some of these values are false, they are ignored (i.e.,
* not inactivated with this function).
* - keyCode: Must be 0 - 255 (0xFF). If this is specified explicitly,
* .keyCode value is initialized with this value.
*
* aWindow is optional, and defaults to the current window object.
* aCallback is optional, use the callback for receiving notifications of TIP.
*/
-function synthesizeKey(aKey, aEvent, aWindow = window, aCallback)
+function synthesizeKey(aKey, aEvent = undefined, aWindow = window, aCallback)
{
+ var event = aEvent === undefined || aEvent === null ? {} : aEvent;
+
var TIP = _getTIP(aWindow, aCallback);
if (!TIP) {
return;
}
var KeyboardEvent = _getKeyboardEvent(aWindow);
- var modifiers = _emulateToActivateModifiers(TIP, aEvent, aWindow);
- var keyEventDict = _createKeyboardEventDictionary(aKey, aEvent, aWindow);
+ var modifiers = _emulateToActivateModifiers(TIP, event, aWindow);
+ var keyEventDict = _createKeyboardEventDictionary(aKey, event, aWindow);
var keyEvent = new KeyboardEvent("", keyEventDict.dictionary);
var dispatchKeydown =
- !("type" in aEvent) || aEvent.type === "keydown" || !aEvent.type;
+ !("type" in event) || event.type === "keydown" || !event.type;
var dispatchKeyup =
- !("type" in aEvent) || aEvent.type === "keyup" || !aEvent.type;
+ !("type" in event) || event.type === "keyup" || !event.type;
try {
if (dispatchKeydown) {
TIP.keydown(keyEvent, keyEventDict.flags);
- if ("repeat" in aEvent && aEvent.repeat > 1) {
+ if ("repeat" in event && event.repeat > 1) {
keyEventDict.dictionary.repeat = true;
var repeatedKeyEvent = new KeyboardEvent("", keyEventDict.dictionary);
- for (var i = 1; i < aEvent.repeat; i++) {
+ for (var i = 1; i < event.repeat; i++) {
TIP.keydown(repeatedKeyEvent, keyEventDict.flags);
}
}
}
if (dispatchKeyup) {
TIP.keyup(keyEvent, keyEventDict.flags);
}
} finally {