Skip to content

Commit

Permalink
Bug 1815314 - Tests for pageAction and browserAction persistent event…
Browse files Browse the repository at this point in the history
…s r=willdurand

Differential Revision: https://phabricator.services.mozilla.com/D189876
  • Loading branch information
zombie committed Oct 3, 2023
1 parent a3ea8ff commit c2e7116
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
38 changes: 38 additions & 0 deletions toolkit/components/extensions/test/mochitest/head.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,41 @@ function waitForState(sw, state) {
});
});
}

/* exported assertPersistentListeners */
async function assertPersistentListeners(
extWrapper,
apiNs,
apiEvents,
expected
) {
const stringErr = await SpecialPowers.spawnChrome(
[extWrapper.id, apiNs, apiEvents, expected],
async (id, apiNs, apiEvents, expected) => {
try {
const { ExtensionTestCommon } = ChromeUtils.importESModule(
"resource://testing-common/ExtensionTestCommon.sys.mjs"
);
const ext = { id };
for (const event of apiEvents) {
ExtensionTestCommon.testAssertions.assertPersistentListeners(
ext,
apiNs,
event,
{
primed: expected.primed,
persisted: expected.persisted,
primedListenersCount: expected.primedListenersCount,
}
);
}
} catch (err) {
return String(err);
}
}
);
ok(
stringErr == undefined,
stringErr ? stringErr : `Found expected primed and persistent listeners`
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
<script type="text/javascript">
"use strict";

add_task(async function test_browserAction_onClicked_and_inputHandling() {
const extension = ExtensionTestUtils.loadExtension({
function createExtension(background = {}) {
return ExtensionTestUtils.loadExtension({
manifest: {
browser_action: {},
background,
},
async background() {
async function checkIsHandlingUserInput() {
Expand Down Expand Up @@ -52,6 +53,10 @@
browser.test.sendMessage("background-ready");
},
});
}

add_task(async function test_browserAction_onClicked_and_inputHandling() {
const extension = createExtension();

await extension.startup();
await extension.awaitMessage("background-ready");
Expand All @@ -63,6 +68,32 @@
await extension.unload();
});

add_task(async function test_browserAction_onClicked_persistent_event() {
const extension = createExtension({ persistent: false });

await extension.startup();
await extension.awaitMessage("background-ready");

assertPersistentListeners(extension, "browserAction", ["onClicked"], {
primed: false,
});

await extension.terminateBackground();
assertPersistentListeners(extension, "browserAction", ["onClicked"], {
primed: true,
});

await AppTestDelegate.clickBrowserAction(window, extension);

// Background script will run again.
await extension.awaitMessage("background-ready");
await extension.awaitFinish("action-clicked");

await AppTestDelegate.closeBrowserAction(window, extension);

await extension.unload();
});

</script>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
<script type="text/javascript">
"use strict";

add_task(async function test_pageAction_onClicked_and_inputHandling() {
const extension = ExtensionTestUtils.loadExtension({
function createExtension(background = {}) {
return ExtensionTestUtils.loadExtension({
manifest: {
background,
page_action: { show_matches: ["<all_urls>"] },
},
async background() {
Expand Down Expand Up @@ -63,6 +64,10 @@
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");
Expand All @@ -74,6 +79,32 @@
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>
Expand Down

0 comments on commit c2e7116

Please sign in to comment.