Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>pageAction.onClicked test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<script type="text/javascript">
"use strict";
function createExtension(background = {}) {
return ExtensionTestUtils.loadExtension({
manifest: {
background,
page_action: { show_matches: ["<all_urls>"] },
},
async background() {
async function checkIsHandlingUserInput() {
try {
// permissions.request is declared with requireUserInput,
// so it would reject if inputHandling is false.
let granted = await browser.permissions.request({});
// We haven't requested any permissions, so the API call grants the
// requested permissions without actually prompting the user.
browser.test.assertTrue(granted, "empty permissions granted");
return true;
} catch (e) {
browser.test.assertEq(
e?.message,
"permissions.request may only be called from a user input handler",
"Expected error when permissions.request rejects"
);
return false;
}
}
browser.pageAction.onClicked.addListener(async () => {
browser.test.assertTrue(
await checkIsHandlingUserInput(),
"pageAction.onClicked is handling user input"
);
browser.test.notifyPass("action-clicked");
});
// Sanity check: Verify that pageAction is shown (because it needs to be
// in order to trigger pageAction.onClicked).
let [tab] = await browser.tabs.query({
active: true,
currentWindow: true,
});
browser.test.assertTrue(
await browser.pageAction.isShown({ tabId: tab.id }),
"pageAction should be visible (due to page_action.show_matches)"
);
browser.test.assertFalse(
await checkIsHandlingUserInput(),
"not handling user input by default"
);
browser.test.sendMessage("background-ready");
},
});
}
add_task(async function test_pageAction_onClicked_and_inputHandling() {
const extension = createExtension();
await extension.startup();
await extension.awaitMessage("background-ready");
await AppTestDelegate.clickPageAction(window, extension);
await extension.awaitFinish("action-clicked");
await AppTestDelegate.closePageAction(window, extension);
await extension.unload();
});
add_task(async function test_pageAction_onClicked_persistent_event() {
const extension = createExtension({ persistent: false });
await extension.startup();
await extension.awaitMessage("background-ready");
assertPersistentListeners(extension, "pageAction", ["onClicked"], {
primed: false,
});
await extension.terminateBackground();
assertPersistentListeners(extension, "pageAction", ["onClicked"], {
primed: true,
});
await AppTestDelegate.clickPageAction(window, extension);
// Background script will run again.
await extension.awaitMessage("background-ready");
await extension.awaitFinish("action-clicked");
await AppTestDelegate.closePageAction(window, extension);
await extension.unload();
});
</script>
</body>
</html>