commit 00d49864ddaf2301fa40f3cbf3a76a048e7e2a79
parent 8043a6d9d4bfbdb04f96f75c91a1aac208e3eab6
Author: scottdowne <sdowne@mozilla.com>
Date: Fri, 24 Oct 2025 15:16:18 +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, 72 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,49 @@
+/* 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_does_not_init_when_pref_false() {
+ await SpecialPowers.pushPrefEnv({ set: [[PREF, false]] });
+
+ // Reset for a clean start just in case.
+ 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 just in case, also shouldn't throw or hang.
+ AboutNewTab.uninit();
+ ok(
+ !AboutNewTab.initialized,
+ "AboutNewTab should still be uninitialized after uninit()"
+ );
+});
+
+add_task(async function test_aboutnewtab_initializes_by_default() {
+ await SpecialPowers.pushPrefEnv({ set: [[PREF, true]] });
+
+ // Reset for a clean start just in case.
+ AboutNewTab.uninit();
+
+ // Call init(); with pref=true, we should initialize.
+ AboutNewTab.init();
+
+ ok(
+ AboutNewTab.initialized,
+ "AboutNewTab should initialize when pref is true"
+ );
+
+ AboutNewTab.uninit();
+ ok(
+ !AboutNewTab.initialized,
+ "AboutNewTab should be uninitialized after uninit()"
+ );
+});