summaryrefslogtreecommitdiffstats
path: root/browser/components/shell/test/browser_setDefaultBrowser.js
blob: ecf7fcb303cd498c2e65f09c4ee0bbec98cac195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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();
});