Skip to content

Commit

Permalink
Bug 1924542 - Add test coverage for WebExtensions commands shortcuts …
Browse files Browse the repository at this point in the history
…using F1-F19 keys. r=willdurand

Depends on D230108

Differential Revision: https://phabricator.services.mozilla.com/D229813

UltraBlame original commit: 7711a1e3e9ae283378d663e435694845f35883c3
  • Loading branch information
marco-c committed Dec 1, 2024
1 parent dd2ae54 commit 1e7d009
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,28 @@ function disableAddon(addon) {
add_task(async function test_update_defined_command() {
let extension;
let updatedExtension;

registerCleanupFunction(async () => {
await extension.unload();
let needsCleanup = true;
const cleanup = async () => {
if (!needsCleanup) {
return;
}
needsCleanup = false;
const extensionId = extension.id;
await extension?.unload();

await updatedExtension?.unload();


if (updatedExtension) {
await updatedExtension.unload();
if (extensionId) {
let storedCommands = ExtensionSettingsStore.getAllForExtension(
extensionId,
"commands"
);
is(storedCommands.length, 0, "There are no stored commands after unload");
}
};


let storedCommands = ExtensionSettingsStore.getAllForExtension(
extension.id,
"commands"
);
is(storedCommands.length, 0, "There are no stored commands after unload");
});
registerCleanupFunction(cleanup);

extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
Expand Down Expand Up @@ -352,6 +358,8 @@ add_task(async function test_update_defined_command() {
await TestUtils.waitForCondition(() => extensionKeyset(extension.id));

checkNumericKey(extension.id, "9", "alt,shift");

await cleanup();
});

add_task(async function updateSidebarCommand() {
Expand Down Expand Up @@ -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: "[email protected]" } },
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");


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();
});
31 changes: 31 additions & 0 deletions browser/components/extensions/test/xpcshell/test_ext_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,34 @@ add_task(async function test_action_version() {
`Manifest v2 with "action" key first warning is clear.`
);
});










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}`
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
},
Expand All @@ -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(
Expand All @@ -103,6 +108,7 @@ add_task(async function testUpdatingCommands() {

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;

Expand All @@ -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"
);

Expand Down Expand Up @@ -157,6 +169,29 @@ add_task(async function testUpdatingCommands() {
}



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 });

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,
});
}


let input = inputs[0];
let error = doc.querySelector(".error-message");
let label = error.querySelector(".error-message-label");
Expand Down

0 comments on commit 1e7d009

Please sign in to comment.