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 --- .../shell/test/browser_setDefaultBrowser.js | 146 +++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 browser/components/shell/test/browser_setDefaultBrowser.js (limited to 'browser/components/shell/test/browser_setDefaultBrowser.js') diff --git a/browser/components/shell/test/browser_setDefaultBrowser.js b/browser/components/shell/test/browser_setDefaultBrowser.js new file mode 100644 index 0000000000..ecf7fcb303 --- /dev/null +++ b/browser/components/shell/test/browser_setDefaultBrowser.js @@ -0,0 +1,146 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +ChromeUtils.defineESModuleGetters(this, { + ExperimentAPI: "resource://nimbus/ExperimentAPI.sys.mjs", + ExperimentFakes: "resource://testing-common/NimbusTestUtils.sys.mjs", + NimbusFeatures: "resource://nimbus/ExperimentAPI.sys.mjs", + sinon: "resource://testing-common/Sinon.sys.mjs", +}); + +const _handleWDBAResultStub = sinon + .stub(ShellService, "_handleWDBAResult") + .callsFake(async () => { + throw new Error("from _handleWDBAResultStub"); + }); + +const _callExternalDefaultBrowserAgentStub = sinon + .stub(ShellService, "_callExternalDefaultBrowserAgent") + .callsFake(async () => ({ + async wait() { + return { exitCode: 0 }; + }, + })); + +const _userChoiceImpossibleTelemetryResultStub = sinon + .stub(ShellService, "_userChoiceImpossibleTelemetryResult") + .callsFake(() => null); + +const userChoiceStub = sinon + .stub(ShellService, "setAsDefaultUserChoice") + .resolves(); +const setDefaultStub = sinon.stub(); +const shellStub = sinon + .stub(ShellService, "shellService") + .value({ setDefaultBrowser: setDefaultStub }); + +registerCleanupFunction(() => { + _handleWDBAResultStub.restore(); + _callExternalDefaultBrowserAgentStub.restore(); + _userChoiceImpossibleTelemetryResultStub.restore(); + userChoiceStub.restore(); + shellStub.restore(); + + ExperimentAPI._store._deleteForTests("shellService"); +}); + +let defaultUserChoice; +add_task(async function need_user_choice() { + ShellService.setDefaultBrowser(); + defaultUserChoice = userChoiceStub.called; + + Assert.ok( + defaultUserChoice !== undefined, + "Decided which default browser method to use" + ); + Assert.equal( + setDefaultStub.notCalled, + defaultUserChoice, + "Only one default behavior was used" + ); +}); + +add_task(async function remote_disable() { + if (defaultUserChoice === false) { + info("Default behavior already not user choice, so nothing to test"); + return; + } + + userChoiceStub.resetHistory(); + setDefaultStub.resetHistory(); + let doCleanup = await ExperimentFakes.enrollWithRollout({ + featureId: NimbusFeatures.shellService.featureId, + value: { + setDefaultBrowserUserChoice: false, + enabled: true, + }, + }); + + ShellService.setDefaultBrowser(); + + Assert.ok( + userChoiceStub.notCalled, + "Set default with user choice disabled via nimbus" + ); + Assert.ok(setDefaultStub.called, "Used plain set default insteead"); + + await doCleanup(); +}); + +add_task(async function restore_default() { + if (defaultUserChoice === undefined) { + info("No default user choice behavior set, so nothing to test"); + return; + } + + userChoiceStub.resetHistory(); + setDefaultStub.resetHistory(); + ExperimentAPI._store._deleteForTests("shellService"); + + ShellService.setDefaultBrowser(); + + Assert.equal( + userChoiceStub.called, + defaultUserChoice, + "Set default with user choice restored to original" + ); + Assert.equal( + setDefaultStub.notCalled, + defaultUserChoice, + "Plain set default behavior restored to original" + ); +}); + +add_task(async function ensure_fallback() { + if (!AppConstants.isPlatformAndVersionAtLeast("win", "10")) { + info("Nothing to test on non-Windows"); + return; + } + + let userChoicePromise = Promise.resolve(); + userChoiceStub.callsFake(function (...args) { + return (userChoicePromise = userChoiceStub.wrappedMethod.apply(this, args)); + }); + userChoiceStub.resetHistory(); + setDefaultStub.resetHistory(); + let doCleanup = await ExperimentFakes.enrollWithRollout({ + featureId: NimbusFeatures.shellService.featureId, + value: { + setDefaultBrowserUserChoice: true, + setDefaultPDFHandler: false, + enabled: true, + }, + }); + + ShellService.setDefaultBrowser(); + + Assert.ok(userChoiceStub.called, "Set default with user choice called"); + + let thrown = false; + await userChoicePromise.catch(() => (thrown = true)); + + Assert.ok(thrown, "Set default with user choice threw an error"); + Assert.ok(setDefaultStub.called, "Fallbacked to plain set default"); + + await doCleanup(); +}); -- cgit v1.2.3