// Test helper to listen to the next key press for a given key, // returning a promise to help using Tasks. function once(shortcuts, key, listener) {
let called = false; returnnew Promise(done => { const onShortcut = event => {
shortcuts.off(key, onShortcut);
ok(!called, "once listener called only once (i.e. off() works)");
called = true;
listener(event);
done();
};
shortcuts.on(key, onShortcut);
});
}
async function testSimple(shortcuts) {
info("Test simple key shortcuts");
// Plus is special. It's keycode is the one for "=". That's because it requires // shift to be pressed and is behind "=" key. So it should be considered as a // character key
async function testPlusCharacter(shortcuts) {
info("Test 'Plus' key shortcuts");
// Dispatch the first shortcut and expect only this one to be notified
ok(!hitFirst, "First shortcut isn't notified before firing the key event");
EventUtils.synthesizeKey("0", {}, window);
await onFirstKey;
ok(hitFirst, "Got the first shortcut notified");
ok(!hitSecond, "No mixup, second shortcut is still not notified (1/2)");
// Wait an extra time, just to be sure this isn't racy
await new Promise(done => {
window.setTimeout(done, 0);
});
ok(!hitSecond, "No mixup, second shortcut is still not notified (2/2)");
// Finally dispatch the second shortcut
EventUtils.synthesizeKey("a", { altKey: true }, window);
await onSecondKey;
ok(hitSecond, "Got the second shortcut notified once it is actually fired");
}
// On azerty keyboard, digits are only available by pressing Shift/Capslock, // but we accept them even if we omit doing that.
async function testLooseDigits(shortcuts) {
info("Test Loose digits");
let onKey = once(shortcuts, "0", event => {
is(event.key, "à");
ok(!event.altKey);
ok(!event.ctrlKey);
ok(!event.metaKey);
ok(!event.shiftKey);
}); // Simulate a press on the "0" key, without shift pressed on a french // keyboard
EventUtils.synthesizeKey("à", { keyCode: 48 }, window);
await onKey;
// Test that shortcuts is notified only when the modifiers match exactly
async function testExactModifiers(shortcuts) {
info("Test exact modifiers match");
let hit = false; const onKey = once(shortcuts, "Alt+A", event => {
is(event.key, "a");
ok(event.altKey);
ok(!event.ctrlKey);
ok(!event.metaKey);
ok(!event.shiftKey);
hit = true;
});
// Wait an extra time to let a chance to call the listener
await new Promise(done => {
window.setTimeout(done, 0);
});
ok(!hit, "Listener isn't called when modifiers aren't exactly matching");
// Dispatch the expected modifiers
EventUtils.synthesizeKey( "a",
{ accelKey: false, altKey: true, shiftKey: false },
window
);
await onKey;
ok(hit, "Got shortcut notified once it is actually fired");
}
// Some keys are only accessible via shift and listener should also be called // even if the key didn't explicitely requested Shift modifier. // For example, `%` on french keyboards is only accessible via Shift. // Same thing for `@` on US keybords.
async function testLooseShiftModifier(shortcuts) {
info("Test Loose shift modifier");
let onKey = once(shortcuts, "%", event => {
is(event.key, "%");
ok(!event.altKey);
ok(!event.ctrlKey);
ok(!event.metaKey);
ok(event.shiftKey);
});
EventUtils.synthesizeKey( "%",
{ accelKey: false, altKey: false, ctrlKey: false, shiftKey: true },
window
);
await onKey;
function testInvalidShortcutString(shortcuts) {
info("Test wrong shortcut string");
const shortcut = KeyShortcuts.parseElectronKey("Cmmd+F");
ok(
!shortcut, "Passing a invalid shortcut string should return a null object"
);
shortcuts.on("Cmmd+F", function () {});
ok(true, "on() shouldn't throw when passing invalid shortcut string");
}
// Can happen on localized builds where the value of the localized string is // empty, eg `toolbox.elementPicker.key=`. See Bug 1569572. function testNullShortcut(shortcuts) {
info("Test null shortcut");
const shortcut = KeyShortcuts.parseElectronKey(null);
ok(!shortcut, "Passing a null object should return a null object");
const stringified = KeyShortcuts.stringify(shortcut);
is(stringified, "", "A null object should be stringified as an empty string");
shortcuts.on(null, function () {});
ok(true, "on() shouldn't throw when passing a null object");
}
/** * Shift+Alt+I generates ^ key (`event.key`) on OSX and KeyShortcuts module * must ensure that this doesn't interfere with shortcuts CmdOrCtrl+Alt+Shift+I * for opening the Browser Toolbox and CmdOrCtrl+Alt+I for toggling the Toolbox.
*/
async function testTabCharacterShortcut(shortcuts) { if (!isOSX) { return;
}
info("Test tab character shortcut");
once(shortcuts, "CmdOrCtrl+Alt+I", () => {
ok(false, "This handler must not be executed");
});
// Simulate `CmdOrCtrl+Alt+Shift+I` shortcut. Note that EventUtils doesn't // generate `^` like real keyboard, so we need to pass it explicitly // and use proper keyCode for `I` character.
EventUtils.synthesizeKey( "^",
{
code: "KeyI",
key: "^",
keyCode: 73,
shiftKey: true,
altKey: true,
metaKey: true,
},
window
);
await onKey;
}
¤ Dauer der Verarbeitung: 0.28 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.