summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_defaultbrowser_alwayscheck.js
blob: 1e1a096d3187084a58eec8de35e5c22924bf8ef9 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"use strict";

const CHECK_DEFAULT_INITIAL = Services.prefs.getBoolPref(
  "browser.shell.checkDefaultBrowser"
);

add_task(async function clicking_make_default_checks_alwaysCheck_checkbox() {
  await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:preferences");

  await test_with_mock_shellservice({ isDefault: false }, async function() {
    let checkDefaultBrowserState = isDefault => {
      let isDefaultPane = content.document.getElementById("isDefaultPane");
      let isNotDefaultPane = content.document.getElementById(
        "isNotDefaultPane"
      );
      Assert.equal(
        ContentTaskUtils.is_hidden(isDefaultPane),
        !isDefault,
        "The 'browser is default' pane should be hidden when browser is not default"
      );
      Assert.equal(
        ContentTaskUtils.is_hidden(isNotDefaultPane),
        isDefault,
        "The 'make default' pane should be hidden when browser is default"
      );
    };

    checkDefaultBrowserState(false);

    let alwaysCheck = content.document.getElementById("alwaysCheckDefault");
    Assert.ok(!alwaysCheck.checked, "Always Check is unchecked by default");
    Assert.ok(
      !Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
      "alwaysCheck pref should be false by default in test runs"
    );

    let setDefaultButton = content.document.getElementById("setDefaultButton");
    setDefaultButton.click();
    content.window.gMainPane.updateSetDefaultBrowser();

    await ContentTaskUtils.waitForCondition(
      () => alwaysCheck.checked,
      "'Always Check' checkbox should get checked after clicking the 'Set Default' button"
    );

    Assert.ok(
      alwaysCheck.checked,
      "Clicking 'Make Default' checks the 'Always Check' checkbox"
    );
    Assert.ok(
      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
      "Checking the checkbox should set the pref to true"
    );
    Assert.ok(
      alwaysCheck.disabled,
      "'Always Check' checkbox is locked with default browser and alwaysCheck=true"
    );
    checkDefaultBrowserState(true);
    Assert.ok(
      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
      "checkDefaultBrowser pref is now enabled"
    );
  });

  gBrowser.removeCurrentTab();
  Services.prefs.clearUserPref("browser.shell.checkDefaultBrowser");
});

add_task(async function clicking_make_default_checks_alwaysCheck_checkbox() {
  Services.prefs.lockPref("browser.shell.checkDefaultBrowser");
  await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:preferences");

  await test_with_mock_shellservice({ isDefault: false }, async function() {
    let isDefaultPane = content.document.getElementById("isDefaultPane");
    let isNotDefaultPane = content.document.getElementById("isNotDefaultPane");
    Assert.ok(
      ContentTaskUtils.is_hidden(isDefaultPane),
      "The 'browser is default' pane should be hidden when not default"
    );
    Assert.ok(
      ContentTaskUtils.is_visible(isNotDefaultPane),
      "The 'make default' pane should be visible when not default"
    );

    let alwaysCheck = content.document.getElementById("alwaysCheckDefault");
    Assert.ok(alwaysCheck.disabled, "Always Check is disabled when locked");
    Assert.ok(
      alwaysCheck.checked,
      "Always Check is checked because defaultPref is true and pref is locked"
    );
    Assert.ok(
      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
      "alwaysCheck pref should ship with 'true' by default"
    );

    let setDefaultButton = content.document.getElementById("setDefaultButton");
    setDefaultButton.click();
    content.window.gMainPane.updateSetDefaultBrowser();

    await ContentTaskUtils.waitForCondition(
      () => ContentTaskUtils.is_visible(isDefaultPane),
      "Browser is now default"
    );

    Assert.ok(
      alwaysCheck.checked,
      "'Always Check' is still checked because it's locked"
    );
    Assert.ok(
      alwaysCheck.disabled,
      "'Always Check is disabled because it's locked"
    );
    Assert.ok(
      Services.prefs.getBoolPref("browser.shell.checkDefaultBrowser"),
      "The pref is locked and so doesn't get changed"
    );
  });

  Services.prefs.unlockPref("browser.shell.checkDefaultBrowser");
  gBrowser.removeCurrentTab();
});

add_task(async function make_default_disabled_until_prefs_are_loaded() {
  // Testcase with Firefox not set as the default browser
  await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:preferences");
  await test_with_mock_shellservice({ isDefault: false }, async function() {
    let alwaysCheck = content.document.getElementById("alwaysCheckDefault");
    Assert.ok(
      !alwaysCheck.disabled,
      "'Always Check' is enabled after default browser updated"
    );
  });
  gBrowser.removeCurrentTab();

  // Testcase with Firefox set as the default browser
  await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:preferences");
  await test_with_mock_shellservice({ isDefault: true }, async function() {
    let alwaysCheck = content.document.getElementById("alwaysCheckDefault");
    Assert.ok(
      alwaysCheck.disabled,
      "'Always Check' is still disabled after default browser updated"
    );
  });
  gBrowser.removeCurrentTab();
});

registerCleanupFunction(function() {
  Services.prefs.unlockPref("browser.shell.checkDefaultBrowser");
  Services.prefs.setBoolPref(
    "browser.shell.checkDefaultBrowser",
    CHECK_DEFAULT_INITIAL
  );
});

async function test_with_mock_shellservice(options, testFn) {
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [options], async function(
    contentOptions
  ) {
    let doc = content.document;
    let win = doc.defaultView;
    win.oldShellService = win.getShellService();
    let mockShellService = {
      _isDefault: false,
      isDefaultBrowser() {
        return this._isDefault;
      },
      setDefaultBrowser() {
        this._isDefault = true;
      },
    };
    win.getShellService = function() {
      return mockShellService;
    };
    mockShellService._isDefault = contentOptions.isDefault;
    win.gMainPane.updateSetDefaultBrowser();
  });

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], testFn);

  Services.prefs.setBoolPref(
    "browser.shell.checkDefaultBrowser",
    CHECK_DEFAULT_INITIAL
  );
}