From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../test/browser/browser_privatebrowsing_about.js | 266 +++++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 browser/components/privatebrowsing/test/browser/browser_privatebrowsing_about.js (limited to 'browser/components/privatebrowsing/test/browser/browser_privatebrowsing_about.js') diff --git a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_about.js b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_about.js new file mode 100644 index 0000000000..f9b935cf79 --- /dev/null +++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_about.js @@ -0,0 +1,266 @@ +/* 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/. */ + +ChromeUtils.defineESModuleGetters(this, { + UrlbarUtils: "resource:///modules/UrlbarUtils.sys.mjs", +}); + +XPCOMUtils.defineLazyGetter(this, "UrlbarTestUtils", () => { + const { UrlbarTestUtils: module } = ChromeUtils.importESModule( + "resource://testing-common/UrlbarTestUtils.sys.mjs" + ); + module.init(this); + return module; +}); + +/** + * Clicks the given link and checks this opens the given URI in the new tab. + * + * This function does not return to the previous page. + */ +async function testLinkOpensUrl({ win, tab, elementId, expectedUrl }) { + let loadedPromise = BrowserTestUtils.waitForNewTab(win.gBrowser, url => + url.startsWith(expectedUrl) + ); + await SpecialPowers.spawn(tab, [elementId], async function (elemId) { + content.document.getElementById(elemId).click(); + }); + await loadedPromise; + is( + win.gBrowser.selectedBrowser.currentURI.spec, + expectedUrl, + `Clicking ${elementId} opened ${expectedUrl} in the same tab.` + ); +} + +let expectedEngineAlias; +let expectedIconURL; + +add_setup(async function () { + await SpecialPowers.pushPrefEnv({ + set: [ + ["browser.search.separatePrivateDefault", true], + // Enable suggestions in this test. Otherwise, the behaviour of the + // content search box changes. + ["browser.search.suggest.enabled", true], + ], + }); + + const originalPrivateDefault = await Services.search.getDefaultPrivate(); + // We have to use a built-in engine as we are currently hard-coding the aliases. + const privateEngine = await Services.search.getEngineByName("DuckDuckGo"); + await Services.search.setDefaultPrivate( + privateEngine, + Ci.nsISearchService.CHANGE_REASON_UNKNOWN + ); + expectedEngineAlias = privateEngine.aliases[0]; + expectedIconURL = privateEngine.iconURI.spec; + + registerCleanupFunction(async () => { + await Services.search.setDefaultPrivate( + originalPrivateDefault, + Ci.nsISearchService.CHANGE_REASON_UNKNOWN + ); + }); +}); + +/** + * Tests the private-browsing-myths link in "about:privatebrowsing". + */ +add_task(async function test_myths_link() { + Services.prefs.setCharPref("app.support.baseURL", "https://example.com/"); + registerCleanupFunction(function () { + Services.prefs.clearUserPref("app.support.baseURL"); + }); + + let { win, tab } = await openAboutPrivateBrowsing(); + + await testLinkOpensUrl({ + win, + tab, + elementId: "private-browsing-myths", + expectedUrl: "https://example.com/private-browsing-myths", + }); + + await BrowserTestUtils.closeWindow(win); +}); + +function urlBarHasHiddenFocus(win) { + return win.gURLBar.focused && !win.gURLBar.hasAttribute("focused"); +} + +function urlBarHasNormalFocus(win) { + return win.gURLBar.hasAttribute("focused"); +} + +/** + * Tests that we have the correct icon displayed. + */ +add_task(async function test_search_icon() { + let { win, tab } = await openAboutPrivateBrowsing(); + + await SpecialPowers.spawn(tab, [expectedIconURL], async function (iconURL) { + is( + content.document.body.getAttribute("style"), + `--newtab-search-icon: url(${iconURL});`, + "Should have the correct icon URL for the logo" + ); + }); + + await BrowserTestUtils.closeWindow(win); +}); + +/** + * Tests the search hand-off on character keydown in "about:privatebrowsing". + */ +add_task(async function test_search_handoff_on_keydown() { + let { win, tab } = await openAboutPrivateBrowsing(); + + await SpecialPowers.spawn(tab, [], async function () { + let btn = content.document.getElementById("search-handoff-button"); + btn.click(); + ok(btn.classList.contains("focused"), "in-content search has focus styles"); + }); + ok(urlBarHasHiddenFocus(win), "Urlbar has hidden focus"); + + // Expect two searches, one to enter search mode and then another in search + // mode. + let searchPromise = UrlbarTestUtils.promiseSearchComplete(win); + + await new Promise(r => EventUtils.synthesizeKey("f", {}, win, r)); + await SpecialPowers.spawn(tab, [], async function () { + ok( + content.document + .getElementById("search-handoff-button") + .classList.contains("disabled"), + "in-content search is disabled" + ); + }); + await searchPromise; + ok(urlBarHasNormalFocus(win), "Urlbar has normal focus"); + is(win.gURLBar.value, "f", "url bar has search text"); + + // Close the popup. + await UrlbarTestUtils.promisePopupClose(win); + + // Hitting ESC should reshow the in-content search + await new Promise(r => EventUtils.synthesizeKey("KEY_Escape", {}, win, r)); + await SpecialPowers.spawn(tab, [], async function () { + ok( + !content.document + .getElementById("search-handoff-button") + .classList.contains("disabled"), + "in-content search is not disabled" + ); + }); + + await BrowserTestUtils.closeWindow(win); +}); + +/** + * Tests the search hand-off on composition start in "about:privatebrowsing". + */ +add_task(async function test_search_handoff_on_composition_start() { + let { win, tab } = await openAboutPrivateBrowsing(); + + await SpecialPowers.spawn(tab, [], async function () { + content.document.getElementById("search-handoff-button").click(); + }); + ok(urlBarHasHiddenFocus(win), "Urlbar has hidden focus"); + await new Promise(r => + EventUtils.synthesizeComposition({ type: "compositionstart" }, win, r) + ); + ok(urlBarHasNormalFocus(win), "Urlbar has normal focus"); + + await BrowserTestUtils.closeWindow(win); +}); + +/** + * Tests the search hand-off on paste in "about:privatebrowsing". + */ +add_task(async function test_search_handoff_on_paste() { + let { win, tab } = await openAboutPrivateBrowsing(); + + await SpecialPowers.spawn(tab, [], async function () { + content.document.getElementById("search-handoff-button").click(); + }); + ok(urlBarHasHiddenFocus(win), "Urlbar has hidden focus"); + var helper = SpecialPowers.Cc[ + "@mozilla.org/widget/clipboardhelper;1" + ].getService(SpecialPowers.Ci.nsIClipboardHelper); + helper.copyString("words"); + + // Expect two searches, one to enter search mode and then another in search + // mode. + let searchPromise = UrlbarTestUtils.promiseSearchComplete(win); + + await new Promise(r => + EventUtils.synthesizeKey("v", { accelKey: true }, win, r) + ); + + await searchPromise; + + ok(urlBarHasNormalFocus(win), "Urlbar has normal focus"); + is(win.gURLBar.value, "words", "Urlbar has search text"); + + await BrowserTestUtils.closeWindow(win); +}); + +/** + * Tests that handoff enters search mode when suggestions are disabled. + */ +add_task(async function test_search_handoff_search_mode() { + await SpecialPowers.pushPrefEnv({ + set: [["browser.urlbar.suggest.searches", false]], + }); + + let { win, tab } = await openAboutPrivateBrowsing(); + + await SpecialPowers.spawn(tab, [], async function () { + let btn = content.document.getElementById("search-handoff-button"); + btn.click(); + ok(btn.classList.contains("focused"), "in-content search has focus styles"); + }); + ok(urlBarHasHiddenFocus(win), "Urlbar has hidden focus"); + + // Expect two searches, one to enter search mode and then another in search + // mode. + let searchPromise = UrlbarTestUtils.promiseSearchComplete(win); + + await new Promise(r => EventUtils.synthesizeKey("f", {}, win, r)); + await SpecialPowers.spawn(tab, [], async function () { + ok( + content.document + .getElementById("search-handoff-button") + .classList.contains("disabled"), + "in-content search is disabled" + ); + }); + await searchPromise; + ok(urlBarHasNormalFocus(win), "Urlbar has normal focus"); + await UrlbarTestUtils.assertSearchMode(win, { + engineName: "DuckDuckGo", + source: UrlbarUtils.RESULT_SOURCE.SEARCH, + entry: "handoff", + }); + is(win.gURLBar.value, "f", "url bar has search text"); + + // Close the popup. + await UrlbarTestUtils.exitSearchMode(win); + await UrlbarTestUtils.promisePopupClose(win); + + // Hitting ESC should reshow the in-content search + await new Promise(r => EventUtils.synthesizeKey("KEY_Escape", {}, win, r)); + await SpecialPowers.spawn(tab, [], async function () { + ok( + !content.document + .getElementById("search-handoff-button") + .classList.contains("disabled"), + "in-content search is not disabled" + ); + }); + + await BrowserTestUtils.closeWindow(win); + await SpecialPowers.popPrefEnv(); +}); -- cgit v1.2.3