Bug 1962683 - Avoid missed event in browser_ext_tabs_group_ungroup.js r=zombie

This patch moves the tabs API calls after the tabGroups event call,
which increases the likelihood that the tabGroups implementation loads
(with the event registered) before tabs.group() is called, to address
https://bugzilla.mozilla.org/show_bug.cgi?id=1962683#c3

Differential Revision: https://phabricator.services.mozilla.com/D246779
This commit is contained in:
Rob Wu
2025-04-29 13:14:56 +00:00
parent 85992a9d15
commit e6e595260e

View File

@@ -19,13 +19,10 @@ add_task(async function group_ungroup_and_index() {
"tab3.htm": "<title>tab3.html</title>",
},
async background() {
const { id: tabId1 } = await browser.tabs.create({ url: "tab1.htm" });
const { id: tabId2 } = await browser.tabs.create({ url: "tab2.htm" });
const { id: tabId3 } = await browser.tabs.create({ url: "tab3.htm" });
let eventIds = [];
let expected = [];
let allEvents = Promise.withResolvers();
const EXPECTED_EVENT_COUNT = 16;
browser.tabGroups.onCreated.addListener(group => {
eventIds.push(group.id);
@@ -34,13 +31,22 @@ add_task(async function group_ungroup_and_index() {
browser.tabGroups.onRemoved.addListener(group => {
eventIds.push(-group.id);
browser.test.log(`Events so far (${eventIds.length}): ${eventIds}`);
if (eventIds.length === 16) {
if (eventIds.length === EXPECTED_EVENT_COUNT) {
allEvents.resolve();
}
if (eventIds.length > 16) {
if (eventIds.length > EXPECTED_EVENT_COUNT) {
browser.fail("Extra event received: " + group.id);
}
});
browser.tabGroups.onMoved.addListener(group => {
// We do not expect any tabGroups.onMoved. Logging for visibility.
eventIds.push(`moved${group.id}`);
browser.test.log(`Events so far (${eventIds.length}): ${eventIds}`);
});
const { id: tabId1 } = await browser.tabs.create({ url: "tab1.htm" });
const { id: tabId2 } = await browser.tabs.create({ url: "tab2.htm" });
const { id: tabId3 } = await browser.tabs.create({ url: "tab3.htm" });
async function assertAllTabExpectations(expectations, desc) {
const tabs = await Promise.all([
@@ -193,16 +199,18 @@ add_task(async function group_ungroup_and_index() {
expected.push(-groupId8, -groupId9);
// TODO bug 1962683: Re-enable when events are no longer missing
// await allEvents.promise;
//
// browser.test.assertEq(
// eventIds.join(),
// expected.join(),
// "Received expected onCreated events"
// );
browser.test.log(`Expect: ${eventIds.join()}`);
browser.test.log(`Actual: ${expected.join()}`);
// If the test gets stuck here, see bug 1962683.
// If we miss the first tabGroups.onCreated event, a work-around could be
// to call and await a tabGroups method before the first tabs.group()
// call above, like other work-arounds for bug 1300234.
info(`Waiting for events ${expected.length}/${EXPECTED_EVENT_COUNT}`);
await allEvents.promise;
browser.test.assertEq(
eventIds.join(),
expected.join(),
"Received expected onCreated events"
);
browser.test.sendMessage("done");
},