Bug 1924542 - Add test coverage for WebExtensions commands shortcuts using F1-F19 keys. r=willdurand

Depends on D230108

Differential Revision: https://phabricator.services.mozilla.com/D229813
This commit is contained in:
Luca Greco
2024-11-28 14:28:03 +00:00
parent e80bbc22d3
commit 5123d0ecd3
3 changed files with 144 additions and 14 deletions

View File

@@ -39,22 +39,28 @@ function disableAddon(addon) {
add_task(async function test_update_defined_command() {
let extension;
let updatedExtension;
registerCleanupFunction(async () => {
await extension.unload();
// updatedExtension might not have started up if we didn't make it that far.
if (updatedExtension) {
await updatedExtension.unload();
let needsCleanup = true;
const cleanup = async () => {
if (!needsCleanup) {
return;
}
needsCleanup = false;
const extensionId = extension.id;
await extension?.unload();
// updatedExtension might not have started up if we didn't make it that far.
await updatedExtension?.unload();
// Check that ESS is cleaned up on uninstall.
let storedCommands = ExtensionSettingsStore.getAllForExtension(
extension.id,
"commands"
);
is(storedCommands.length, 0, "There are no stored commands after unload");
});
if (extensionId) {
let storedCommands = ExtensionSettingsStore.getAllForExtension(
extensionId,
"commands"
);
is(storedCommands.length, 0, "There are no stored commands after unload");
}
};
registerCleanupFunction(cleanup);
extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
@@ -352,6 +358,8 @@ add_task(async function test_update_defined_command() {
await TestUtils.waitForCondition(() => extensionKeyset(extension.id));
// Shortcut is unchanged since it was previously updated.
checkNumericKey(extension.id, "9", "alt,shift");
await cleanup();
});
add_task(async function updateSidebarCommand() {
@@ -517,3 +525,59 @@ add_task(async function test_extension_sidebar_shortcuts() {
await SpecialPowers.popPrefEnv();
});
add_task(async function test_extended_function_keys() {
const extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
manifest: {
version: "1.0",
browser_specific_settings: { gecko: { id: "commands@mochi.test" } },
commands: {
foo: {
suggested_key: {
default: "Alt+Shift+F12",
},
description: "The foo command",
},
},
},
background() {
browser.test.onMessage.addListener(async (msg, data) => {
if (msg == "update") {
await browser.commands.update(data);
return browser.test.sendMessage("updateDone");
}
});
browser.commands.onCommand.addListener(name =>
browser.test.sendMessage("oncommand", name)
);
browser.test.sendMessage("bgpage:ready");
},
});
await extension.startup();
await extension.awaitMessage("bgpage:ready");
// Sanity check.
info("Verify command listener called on original manifest-assigned shortcut");
EventUtils.synthesizeKey("VK_F12", { altKey: true, shiftKey: true });
is(
await extension.awaitMessage("oncommand"),
"foo",
"Expect onCommand listener call for command foo on Alt+Shift+F12"
);
info("Update foo command shortcut to be set to Alt+Shift+F19");
extension.sendMessage("update", { name: "foo", shortcut: "Alt+Shift+F19" });
await extension.awaitMessage("updateDone");
info("Verify command listener called on extension-updated shortcut");
EventUtils.synthesizeKey("VK_F19", { altKey: true, shiftKey: true });
is(
await extension.awaitMessage("oncommand"),
"foo",
"Expect onCommand listener call for command foo on Alt+Shift+F19"
);
await extension.unload();
});

View File

@@ -103,3 +103,34 @@ add_task(async function test_action_version() {
`Manifest v2 with "action" key first warning is clear.`
);
});
// Verify that extended F13-F19 keys are invalid when specified in the
// extensions manifest.
//
// Other related tests:
// - browser_manage_shortcuts.js include a test that makes sure the F13-F19 keys are valid when
// assigned by a user through about:addons "Manage Shortcuts".
// - browser_ext_commands_update.js include a test that makes sure the F13-F19 keys
// are valid when dynamically assigned by the extension through
// browser.commands.update API calls.
add_task(async function test_invalid_extended_function_keys() {
info("Verify F13-19 are invalid when assigned from the extension manifest");
ExtensionTestUtils.failOnSchemaWarnings(false);
let normalized = await ExtensionTestUtils.normalizeManifest({
commands: {
invalidFnKeyCommand: {
suggested_key: {
default: "Alt+F13",
},
},
},
});
const EXPECTED_ERROR_REGEX = new RegExp(
`Value ".*" must not include extended F13-F19 keys. F13-F19 keys can only be used for user-defined keyboard shortcuts`
);
ExtensionTestUtils.failOnSchemaWarnings(true);
ok(
EXPECTED_ERROR_REGEX.test(normalized.error),
`Should have reported F13 as an invalid key manifest error, got: ${normalized.error}`
);
});

View File

@@ -54,6 +54,10 @@ add_task(async function testUpdatingCommands() {
description: "Command Two!",
suggested_key: { default: "Alt+4" },
},
commandThree: {
description: "Command Three!",
suggested_key: { default: "Alt+F12" },
},
_execute_browser_action: {
suggested_key: { default: "Shift+Alt+9" },
},
@@ -77,6 +81,7 @@ add_task(async function testUpdatingCommands() {
await extensionShortcutsReady(extension.id);
async function checkShortcut(name, key, modifiers) {
info(`Synthesize keyboard shortcut ${JSON.stringify({ key, modifiers })}`);
EventUtils.synthesizeKey(key, modifiers);
let message = await extension.awaitMessage("oncommand");
is(
@@ -103,6 +108,7 @@ add_task(async function testUpdatingCommands() {
// Check that the original shortcuts work.
await checkShortcut("commandOne", "7", { shiftKey: true, altKey: true });
await checkShortcut("commandTwo", "4", { altKey: true });
await checkShortcut("commandThree", "VK_F12", { altKey: true });
let doc = win.document;
@@ -119,7 +125,13 @@ add_task(async function testUpdatingCommands() {
let nameOrder = Array.from(inputs).map(input => input.getAttribute("name"));
Assert.deepEqual(
nameOrder,
["commandOne", "commandTwo", "_execute_browser_action", "commandZero"],
[
"commandOne",
"commandTwo",
"commandThree",
"_execute_browser_action",
"commandZero",
],
"commandZero should be last since it is unset"
);
@@ -156,6 +168,29 @@ add_task(async function testUpdatingCommands() {
);
}
// Test F1-F19 keys are all considered valid and trigger the command
// as expected.
const shortcutInput = Array.from(inputs)
.filter(input => input.getAttribute("name") != "_execute_browser_action")
.pop();
const shortcutInputCmdName = shortcutInput.getAttribute("name");
info(`Verify F1-F19 keys shortcuts on ${shortcutInputCmdName}`);
for (let i = 1; i <= 19; i++) {
const key = `F${i}`;
const synthesizeKey = `VK_${key}`;
shortcutInput.focus();
EventUtils.synthesizeKey(synthesizeKey, { shiftKey: true, altKey: true });
// Wait for the shortcut attribute to change.
await BrowserTestUtils.waitForCondition(
() => shortcutInput.getAttribute("shortcut") == `Alt+Shift+${key}`,
`Wait for shortcut to update to Alt+Shift+${key}`
);
await checkShortcut(shortcutInputCmdName, synthesizeKey, {
shiftKey: true,
altKey: true,
});
}
// Check that errors can be shown.
let input = inputs[0];
let error = doc.querySelector(".error-message");