commit 9ebbf095db508deaf82885c404436a5c3c37babf
parent 65a9e94015b6f0f7c9577baf2f10b13bfcbe1890
Author: scottdowne <sdowne@mozilla.com>
Date: Tue, 28 Oct 2025 15:24:28 +0000
Bug 1944147 - Option to turn off newtab for automated tests and other automation. r=mconley
Differential Revision: https://phabricator.services.mozilla.com/D269682
Diffstat:
3 files changed, 51 insertions(+), 5 deletions(-)
diff --git a/browser/modules/AboutNewTab.sys.mjs b/browser/modules/AboutNewTab.sys.mjs
@@ -21,6 +21,7 @@ const PREF_ACTIVITY_STREAM_DEBUG = "browser.newtabpage.activity-stream.debug";
// AboutHomeStartupCache needs us in "quit-application", so stay alive longer.
// TODO: We could better have a shared async shutdown blocker?
const TOPIC_APP_QUIT = "profile-before-change";
+const PREF_SHOULD_INITIALIZE = "browser.newtabpage.shouldInitialize";
export const AboutNewTab = {
QueryInterface: ChromeUtils.generateQI([
@@ -50,6 +51,16 @@ export const AboutNewTab = {
return;
}
+ // For tests/automation: when false, newtab won't initialize in this session.
+ // Flipping after initialization has no effect on the current session.
+ const shouldInitialize = Services.prefs.getBoolPref(
+ PREF_SHOULD_INITIALIZE,
+ true
+ );
+ if (!shouldInitialize) {
+ return;
+ }
+
Services.obs.addObserver(this, TOPIC_APP_QUIT);
if (!AppConstants.RELEASE_OR_BETA) {
XPCOMUtils.defineLazyPreferenceGetter(
@@ -238,11 +249,16 @@ export const AboutNewTab = {
this.activityStream.uninit();
this.activityStream = null;
}
- Services.obs.removeObserver(this, TOPIC_APP_QUIT);
- Services.obs.removeObserver(
- this,
- lazy.TelemetryReportingPolicy.TELEMETRY_TOU_ACCEPTED_OR_INELIGIBLE
- );
+ try {
+ Services.obs.removeObserver(this, TOPIC_APP_QUIT);
+ Services.obs.removeObserver(
+ this,
+ lazy.TelemetryReportingPolicy.TELEMETRY_TOU_ACCEPTED_OR_INELIGIBLE
+ );
+ } catch (e) {
+ // If init failed before registering these observers, removeObserver may throw.
+ // Safe to ignore during shutdown.
+ }
this.initialized = false;
},
diff --git a/browser/modules/test/browser/browser.toml b/browser/modules/test/browser/browser.toml
@@ -77,6 +77,8 @@ https_first_disabled = true
["browser_UsageTelemetry_uniqueOriginsVisitedInPast24Hours.js"]
https_first_disabled = true
+["browser_aboutnewtab_init_gate.js"]
+
["browser_preloading_tab_moving.js"]
["browser_taskbar_preview.js"]
diff --git a/browser/modules/test/browser/browser_aboutnewtab_init_gate.js b/browser/modules/test/browser/browser_aboutnewtab_init_gate.js
@@ -0,0 +1,28 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const PREF = "browser.newtabpage.shouldInitialize";
+
+add_task(async function test_aboutnewtab_init_gate() {
+ ok(AboutNewTab.initialized, "AboutNewTab should initialize by default");
+
+ await SpecialPowers.pushPrefEnv({ set: [[PREF, false]] });
+
+ // Reset for a clean start.
+ AboutNewTab.uninit();
+
+ // Call init(); with pref=false, we should bail out early.
+ AboutNewTab.init();
+
+ ok(
+ !AboutNewTab.initialized,
+ "AboutNewTab should not be initialized when pref is false"
+ );
+
+ // Cleanup and reset.
+ await SpecialPowers.popPrefEnv();
+ AboutNewTab.init();
+ await AboutNewTab.onBrowserReady();
+});