diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index ab080dfc81a2..d27980ccc4cd 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -583,6 +583,15 @@ XPCOMUtils.defineLazyPreferenceGetter( } ); +/* Import aboutWelcomeFeature from Nimbus Experiment API + to access experiment values */ +XPCOMUtils.defineLazyGetter(this, "aboutWelcomeFeature", () => { + const { ExperimentFeature } = ChromeUtils.import( + "resource://messaging-system/experiments/ExperimentAPI.jsm" + ); + return new ExperimentFeature("aboutwelcome"); +}); + customElements.setElementCreationCallback("translation-notification", () => { Services.scriptloader.loadSubScript( "chrome://browser/content/translation-notification.js", @@ -2199,7 +2208,16 @@ var gBrowserInit = { let shouldRemoveFocusedAttribute = true; this._callWithURIToLoad(uriToLoad => { - if (isBlankPageURL(uriToLoad) || uriToLoad == "about:privatebrowsing") { + // Check if user is enrolled in an aboutWelcome experiment that has skipFocus + // property set to true, if yes remove focus from urlbar for about:welcome + const aboutWelcomeSkipUrlBarFocus = + uriToLoad == "about:welcome" && + aboutWelcomeFeature.getValue()?.skipFocus; + + if ( + (isBlankPageURL(uriToLoad) && !aboutWelcomeSkipUrlBarFocus) || + uriToLoad == "about:privatebrowsing" + ) { gURLBar.select(); shouldRemoveFocusedAttribute = false; return; diff --git a/browser/components/newtab/test/browser/browser.ini b/browser/components/newtab/test/browser/browser.ini index 57c27ad7f183..451e2c927181 100644 --- a/browser/components/newtab/test/browser/browser.ini +++ b/browser/components/newtab/test/browser/browser.ini @@ -20,6 +20,7 @@ prefs = [browser_aboutwelcome_actors.js] [browser_aboutwelcome_simplified.js] [browser_aboutwelcome_multistage.js] +[browser_aboutwelcome_skipFocus.js] [browser_aboutwelcome_rtamo.js] [browser_aboutwelcome_attribution.js] skip-if = (os == "linux") # Test setup only implemented for OSX and Windows diff --git a/browser/components/newtab/test/browser/browser_aboutwelcome_skipFocus.js b/browser/components/newtab/test/browser/browser_aboutwelcome_skipFocus.js new file mode 100644 index 000000000000..e8e75c4bd26a --- /dev/null +++ b/browser/components/newtab/test/browser/browser_aboutwelcome_skipFocus.js @@ -0,0 +1,101 @@ +"use strict"; + +const ABOUT_WELCOME_OVERRIDE_CONTENT_PREF = + "browser.aboutwelcome.overrideContent"; + +const TEST_MULTISTAGE_JSON = { + id: "multi-stage-welcome", + template: "multistage", + screens: [ + { + id: "AW_STEP1", + order: 0, + content: { + title: "Step 1", + }, + }, + ], +}; + +async function setAboutWelcomeOverrideContent(value) { + return pushPrefs([ABOUT_WELCOME_OVERRIDE_CONTENT_PREF, value]); +} + +async function openAboutWelcomeBrowserWindow() { + let win = window.openDialog( + AppConstants.BROWSER_CHROME_URL, + "_blank", + "chrome,all,dialog=no", + "about:welcome" + ); + + await BrowserTestUtils.waitForEvent(win, "DOMContentLoaded"); + + let promises = [ + BrowserTestUtils.firstBrowserLoaded(win, false), + BrowserTestUtils.browserStopped( + win.gBrowser.selectedBrowser, + "about:welcome" + ), + ]; + + await Promise.all(promises); + + await new Promise(resolve => { + // 10 is an arbitrary value here, it needs to be at least 2 to avoid + // races with code initializing itself using idle callbacks. + (function waitForIdle(count = 10) { + if (!count) { + resolve(); + return; + } + Services.tm.idleDispatchToMainThread(() => { + waitForIdle(count - 1); + }); + })(); + }); + return win; +} + +add_task(async function test_multistage_default() { + let win = await openAboutWelcomeBrowserWindow(); + Assert.ok(win.gURLBar.focused, "Focus should be on awesome bar"); + Assert.ok( + win.gURLBar.hasAttribute("focused"), + "Has focused attribute on urlBar" + ); + + registerCleanupFunction(async () => { + await BrowserTestUtils.closeWindow(win); + }); +}); + +add_task(async function test_multistage_without_skipFocus() { + await setAboutWelcomeOverrideContent(JSON.stringify(TEST_MULTISTAGE_JSON)); + let win = await openAboutWelcomeBrowserWindow(); + Assert.ok(win.gURLBar.focused, "Focus should be on awesome bar"); + Assert.ok( + win.gURLBar.hasAttribute("focused"), + "Has focused attribute on urlBar" + ); + + registerCleanupFunction(async () => { + await BrowserTestUtils.closeWindow(win); + }); +}); + +add_task(async function test_multistage_with_skipFocus() { + await setAboutWelcomeOverrideContent( + JSON.stringify({ ...TEST_MULTISTAGE_JSON, skipFocus: true }) + ); + let win = await openAboutWelcomeBrowserWindow(); + Assert.ok(!win.gURLBar.focused, "Focus should not be on awesome bar"); + Assert.ok( + !win.gURLBar.hasAttribute("focused"), + "No focused attribute on urlBar" + ); + + registerCleanupFunction(async () => { + await BrowserTestUtils.closeWindow(win); + }); +});