Skip to content

Commit 00d4986

Browse files
committed
Bug 1944147 - Option to turn off newtab for automated tests and other automation. r=mconley
Differential Revision: https://phabricator.services.mozilla.com/D269682
1 parent 8043a6d commit 00d4986

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

browser/modules/AboutNewTab.sys.mjs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const PREF_ACTIVITY_STREAM_DEBUG = "browser.newtabpage.activity-stream.debug";
2121
// AboutHomeStartupCache needs us in "quit-application", so stay alive longer.
2222
// TODO: We could better have a shared async shutdown blocker?
2323
const TOPIC_APP_QUIT = "profile-before-change";
24+
const PREF_SHOULD_INITIALIZE = "browser.newtabpage.shouldInitialize";
2425

2526
export const AboutNewTab = {
2627
QueryInterface: ChromeUtils.generateQI([
@@ -50,6 +51,16 @@ export const AboutNewTab = {
5051
return;
5152
}
5253

54+
// For tests/automation: when false, newtab won't initialize in this session.
55+
// Flipping after initialization has no effect on the current session.
56+
const shouldInitialize = Services.prefs.getBoolPref(
57+
PREF_SHOULD_INITIALIZE,
58+
true
59+
);
60+
if (!shouldInitialize) {
61+
return;
62+
}
63+
5364
Services.obs.addObserver(this, TOPIC_APP_QUIT);
5465
if (!AppConstants.RELEASE_OR_BETA) {
5566
XPCOMUtils.defineLazyPreferenceGetter(
@@ -238,11 +249,16 @@ export const AboutNewTab = {
238249
this.activityStream.uninit();
239250
this.activityStream = null;
240251
}
241-
Services.obs.removeObserver(this, TOPIC_APP_QUIT);
242-
Services.obs.removeObserver(
243-
this,
244-
lazy.TelemetryReportingPolicy.TELEMETRY_TOU_ACCEPTED_OR_INELIGIBLE
245-
);
252+
try {
253+
Services.obs.removeObserver(this, TOPIC_APP_QUIT);
254+
Services.obs.removeObserver(
255+
this,
256+
lazy.TelemetryReportingPolicy.TELEMETRY_TOU_ACCEPTED_OR_INELIGIBLE
257+
);
258+
} catch (e) {
259+
// If init failed before registering these observers, removeObserver may throw.
260+
// Safe to ignore during shutdown.
261+
}
246262

247263
this.initialized = false;
248264
},

browser/modules/test/browser/browser.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ https_first_disabled = true
7777
["browser_UsageTelemetry_uniqueOriginsVisitedInPast24Hours.js"]
7878
https_first_disabled = true
7979

80+
["browser_aboutnewtab_init_gate.js"]
81+
8082
["browser_preloading_tab_moving.js"]
8183

8284
["browser_taskbar_preview.js"]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
http://creativecommons.org/publicdomain/zero/1.0/ */
3+
4+
"use strict";
5+
6+
const PREF = "browser.newtabpage.shouldInitialize";
7+
8+
add_task(async function test_aboutnewtab_does_not_init_when_pref_false() {
9+
await SpecialPowers.pushPrefEnv({ set: [[PREF, false]] });
10+
11+
// Reset for a clean start just in case.
12+
AboutNewTab.uninit();
13+
14+
// Call init(); with pref=false, we should bail out early.
15+
AboutNewTab.init();
16+
17+
ok(
18+
!AboutNewTab.initialized,
19+
"AboutNewTab should not be initialized when pref is false"
20+
);
21+
22+
// Cleanup just in case, also shouldn't throw or hang.
23+
AboutNewTab.uninit();
24+
ok(
25+
!AboutNewTab.initialized,
26+
"AboutNewTab should still be uninitialized after uninit()"
27+
);
28+
});
29+
30+
add_task(async function test_aboutnewtab_initializes_by_default() {
31+
await SpecialPowers.pushPrefEnv({ set: [[PREF, true]] });
32+
33+
// Reset for a clean start just in case.
34+
AboutNewTab.uninit();
35+
36+
// Call init(); with pref=true, we should initialize.
37+
AboutNewTab.init();
38+
39+
ok(
40+
AboutNewTab.initialized,
41+
"AboutNewTab should initialize when pref is true"
42+
);
43+
44+
AboutNewTab.uninit();
45+
ok(
46+
!AboutNewTab.initialized,
47+
"AboutNewTab should be uninitialized after uninit()"
48+
);
49+
});

0 commit comments

Comments
 (0)