Bug 1761445 - [remote] TabManager.getBrowsingContextById should not return closed browsing contexts. r=jdescottes,webdriver-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D225303
This commit is contained in:
Temidayo
2025-04-03 12:14:34 +00:00
parent d203ef1855
commit 6df8482da8
2 changed files with 31 additions and 3 deletions

View File

@@ -231,15 +231,22 @@ class TabManagerClass {
* @param {string} id * @param {string} id
* A browsing context unique id (created by getIdForBrowsingContext). * A browsing context unique id (created by getIdForBrowsingContext).
* @returns {BrowsingContext=} * @returns {BrowsingContext=}
* The browsing context found for this id, null if none was found. * The browsing context found for this id, null if none was found or
* browsing context is discarded.
*/ */
getBrowsingContextById(id) { getBrowsingContextById(id) {
const browser = this.getBrowserById(id); const browser = this.getBrowserById(id);
let browsingContext;
if (browser) { if (browser) {
return browser.browsingContext; browsingContext = browser.browsingContext;
} else {
browsingContext = BrowsingContext.get(id);
} }
return BrowsingContext.get(id); if (!browsingContext || browsingContext.isDiscarded) {
return null;
}
return browsingContext;
} }
/** /**

View File

@@ -144,6 +144,27 @@ add_task(async function test_getBrowsingContextById() {
is(TabManager.getBrowsingContextById(childContextId), contexts[1]); is(TabManager.getBrowsingContextById(childContextId), contexts[1]);
}); });
add_task(async function test_getDiscardedBrowsingContextById() {
const tab = await TabManager.addTab();
const browser = tab.linkedBrowser;
const browsingContext = browser.browsingContext;
const contextId = TabManager.getIdForBrowsingContext(browsingContext);
is(
TabManager.getBrowsingContextById(contextId),
browsingContext,
"Browsing context is accessible by its ID"
);
gBrowser.removeTab(tab);
is(
TabManager.getBrowsingContextById(contextId),
null,
"Browsing context is no longer accessible after the tab is removed"
);
});
add_task(async function test_getIdForBrowsingContext() { add_task(async function test_getIdForBrowsingContext() {
const browser = gBrowser.selectedBrowser; const browser = gBrowser.selectedBrowser;