Bug 1758502 - [remote] Add TabManager.getBrowsingContextById API r=webdriver-reviewers,whimboo

Differential Revision: https://phabricator.services.mozilla.com/D141944
This commit is contained in:
Julian Descottes 2022-03-27 08:58:10 +00:00
parent e55ce25a0a
commit 45df9472d7
5 changed files with 61 additions and 9 deletions

View File

@ -135,7 +135,7 @@ var TabManager = {
},
/**
* Retrieve a the browser element corresponding to the provided unique id,
* Retrieve the browser element corresponding to the provided unique id,
* previously generated via getIdForBrowser.
*
* TODO: To avoid creating strong references on browser elements and
@ -163,6 +163,23 @@ var TabManager = {
return null;
},
/**
* Retrieve the browsing context corresponding to the provided unique id.
*
* @param {String} id
* A browsing context unique id (created by getIdForBrowsingContext).
* @return {BrowsingContext=}
* The browsing context found for this id, null if none was found.
*/
getBrowsingContextById(id) {
const browser = this.getBrowserById(id);
if (browser) {
return browser.browsingContext;
}
return BrowsingContext.get(id);
},
/**
* Retrieve the unique id for the given xul browser element. The id is a
* dynamically generated uuid associated with the permanentKey property of the

View File

@ -6,6 +6,7 @@ BROWSER_CHROME_MANIFESTS += [
"listeners/test/browser/browser.ini",
"messagehandler/test/browser/broadcast/browser.ini",
"messagehandler/test/browser/browser.ini",
"test/browser/browser.ini",
]
XPCSHELL_TESTS_MANIFESTS += [

View File

@ -0,0 +1,5 @@
[DEFAULT]
tags = remote
subsuite = remote
[browser_TabManager.js]

View File

@ -0,0 +1,35 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { TabManager } = ChromeUtils.import(
"chrome://remote/content/shared/TabManager.jsm"
);
const FRAME_URL = "https://example.com/document-builder.sjs?html=frame";
const FRAME_MARKUP = `<iframe src="${encodeURI(FRAME_URL)}"></iframe>`;
const TEST_URL = `https://example.com/document-builder.sjs?html=${encodeURI(
FRAME_MARKUP
)}`;
add_task(async function test_getBrowsingContextById() {
const browser = gBrowser.selectedBrowser;
is(TabManager.getBrowsingContextById(null), null);
is(TabManager.getBrowsingContextById(undefined), null);
is(TabManager.getBrowsingContextById("wrong-id"), null);
info(`Navigate to ${TEST_URL}`);
const loaded = BrowserTestUtils.browserLoaded(browser);
BrowserTestUtils.loadURI(browser, TEST_URL);
await loaded;
const contexts = browser.browsingContext.getAllBrowsingContextsInSubtree();
is(contexts.length, 2, "Top context has 1 child");
const topContextId = TabManager.getIdForBrowsingContext(contexts[0]);
is(TabManager.getBrowsingContextById(topContextId), contexts[0]);
const childContextId = TabManager.getIdForBrowsingContext(contexts[1]);
is(TabManager.getBrowsingContextById(childContextId), contexts[1]);
});

View File

@ -107,13 +107,7 @@ class BrowsingContextModule extends Module {
`Expected "parent" to be a string, got ${parentId}`
);
// If the parent id is for a top-level browsing context get the browsing
// context via the unique id and the related content browser.
const browser = TabManager.getBrowserById(parentId);
contexts =
browser !== null
? [browser.browsingContext]
: [this.#getBrowsingContext(parentId)];
contexts = [this.#getBrowsingContext(parentId)];
} else {
// Return all top-level browsing contexts.
contexts = TabManager.browsers.map(browser => browser.browsingContext);
@ -143,7 +137,7 @@ class BrowsingContextModule extends Module {
return null;
}
const context = BrowsingContext.get(contextId);
const context = TabManager.getBrowsingContextById(contextId);
if (context === null) {
throw new error.NoSuchFrameError(
`Browsing Context with id ${contextId} not found`