Bug 1812530 - Fix openInBrowser context menu. r=mkmelin

Differential Revision: https://phabricator.services.mozilla.com/D167916

--HG--
extra : moz-landing-system : lando
This commit is contained in:
John Bieling 2023-02-01 22:43:39 +00:00
Родитель 7b4d432b8f
Коммит 08e85322ae
4 изменённых файлов: 166 добавлений и 8 удалений

Просмотреть файл

@ -527,18 +527,14 @@ class nsContextMenu {
this.showItem("browserContext-stop", notOnSpecialItem);
this.showItem("browserContext-reload", notOnSpecialItem);
let loadedProtocol = "";
if (this.target && this.target.ownerGlobal?.top.location) {
loadedProtocol = this.target.ownerGlobal?.top.location.protocol;
}
// Only show open in browser if we're not on a special item and we're not
// on an about: or chrome: protocol - for these protocols the browser is
// unlikely to show the same thing as we do (if at all), so therefore don't
// offer the option.
this.showItem(
"browserContext-openInBrowser",
notOnSpecialItem && ["http:", "https:"].includes(loadedProtocol)
notOnSpecialItem &&
["http", "https"].includes(this.contentData?.documentURIObject?.scheme)
);
// Only show browserContext-openLinkInBrowser if we're on a link and it isn't
@ -886,7 +882,10 @@ class nsContextMenu {
}
openInBrowser() {
let url = this.target.ownerGlobal?.top.location.href;
let url = this.contentData?.documentURIObject?.spec;
if (!url) {
return;
}
PlacesUtils.history
.insert({
url,

Просмотреть файл

@ -16,6 +16,9 @@ tags = webextensions
[browser_ext_addressBooksUI.js]
tags = addrbook
[browser_ext_bug1812530.js]
support-files = data/content.html
tags = contextmenu
[browser_ext_browserAction.js]
skip-if = true # TODO
[browser_ext_browserAction_popup_click.js]

Просмотреть файл

@ -0,0 +1,156 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/. *
*/
// Load subscript shared with all menu tests.
Services.scriptloader.loadSubScript(
new URL("head_menus.js", gTestPath).href,
this
);
let { MockRegistrar } = ChromeUtils.importESModule(
"resource://testing-common/MockRegistrar.sys.mjs"
);
/** @implements {nsIExternalProtocolService} */
let mockExternalProtocolService = {
_loadedURLs: [],
externalProtocolHandlerExists(protocolScheme) {},
getApplicationDescription(scheme) {},
getProtocolHandlerInfo(protocolScheme) {},
getProtocolHandlerInfoFromOS(protocolScheme, found) {},
isExposedProtocol(protocolScheme) {},
loadURI(uri, windowContext) {
this._loadedURLs.push(uri.spec);
},
setProtocolHandlerDefaults(handlerInfo, osHandlerExists) {},
urlLoaded(url) {
return this._loadedURLs.includes(url);
},
QueryInterface: ChromeUtils.generateQI(["nsIExternalProtocolService"]),
};
let mockExternalProtocolServiceCID = MockRegistrar.register(
"@mozilla.org/uriloader/external-protocol-service;1",
mockExternalProtocolService
);
registerCleanupFunction(() => {
MockRegistrar.unregister(mockExternalProtocolServiceCID);
});
const subtest_clickOpenInBrowserContextMenu = async (extension, getBrowser) => {
async function contextClick(elementId, browser) {
if (
browser.webProgress?.isLoadingDocument ||
!browser.currentURI ||
browser.currentURI?.spec == "about:blank"
) {
await BrowserTestUtils.browserLoaded(
browser,
undefined,
url => url != "about:blank"
);
}
let menuId = browser.getAttribute("context");
let menu = browser.ownerDocument.getElementById(menuId);
let hiddenPromise = BrowserTestUtils.waitForEvent(menu, "popuphidden");
await rightClickOnContent(menu, elementId, browser);
Assert.ok(
menu.querySelector("#browserContext-openInBrowser"),
"menu item should exist"
);
menu.activateItem(menu.querySelector("#browserContext-openInBrowser"));
await hiddenPromise;
}
await extension.startup();
// Wait for click on #description
{
let { elementId, url } = await extension.awaitMessage("contextClick");
Assert.equal(
"#description",
elementId,
`Test should click on the correct element.`
);
Assert.equal(
"https://example.org/browser/comm/mail/components/extensions/test/browser/data/content.html",
url,
`Test should open the correct page.`
);
await contextClick(elementId, getBrowser());
Assert.ok(
mockExternalProtocolService.urlLoaded(url),
`Page should have correctly been opened in external browser.`
);
await extension.sendMessage();
}
await extension.awaitFinish();
await extension.unload();
};
add_task(async function test_tabs() {
let extension = ExtensionTestUtils.loadExtension({
files: {
"utils.js": await getUtilsJS(),
"background.js": async () => {
// Open remote file and re-open it in the browser.
const url =
"https://example.org/browser/comm/mail/components/extensions/test/browser/data/content.html";
const elementId = "#description";
let testTab = await browser.tabs.create({ url });
await window.sendMessage("contextClick", { elementId, url });
await browser.tabs.remove(testTab.id);
browser.test.notifyPass();
},
},
manifest: {
background: {
scripts: ["utils.js", "background.js"],
},
permissions: ["tabs"],
},
});
await subtest_clickOpenInBrowserContextMenu(
extension,
() => document.getElementById("tabmail").currentTabInfo.browser
);
});
add_task(async function test_windows() {
let extension = ExtensionTestUtils.loadExtension({
files: {
"utils.js": await getUtilsJS(),
"background.js": async () => {
// Open remote file and re-open it in the browser.
const url =
"https://example.org/browser/comm/mail/components/extensions/test/browser/data/content.html";
const elementId = "#description";
let testWindow = await browser.windows.create({ type: "popup", url });
await window.sendMessage("contextClick", { elementId, url });
await browser.windows.remove(testWindow.id);
browser.test.notifyPass();
},
},
manifest: {
background: {
scripts: ["utils.js", "background.js"],
},
permissions: ["tabs"],
},
});
await subtest_clickOpenInBrowserContextMenu(
extension,
() => Services.wm.getMostRecentWindow("mail:extensionPopup").browser
);
});

Просмотреть файл

@ -5,7 +5,7 @@
<title>A test document</title>
</head>
<body>
<p>This is text.</p>
<p id="description">This is text.</p>
<p><a href="http://mochi.test:8888/">This is a link with text.</a></p>
<p><img src="http://mochi.test:8888/browser/comm/mail/components/extensions/test/browser/data/tb-logo.png" width="304" height="84" /></p>
</body>