summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_offlineDefault.js
blob: 7330dd4fd5e7a853c7d7a4833eb77c2d17701f69 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Tests `UrlbarPrefs.updateFirefoxSuggestScenario` in isolation under the
// assumption that the offline scenario should be enabled by default for US en.

"use strict";

ChromeUtils.defineESModuleGetters(this, {
  Region: "resource://gre/modules/Region.sys.mjs",
});

// All the prefs that `updateFirefoxSuggestScenario` sets along with the
// expected default-branch values when offline is enabled and when it's not
// enabled.
const PREFS = [
  {
    name: "browser.urlbar.quicksuggest.enabled",
    get: "getBoolPref",
    set: "setBoolPref",
    expectedOfflineValue: true,
    expectedOtherValue: false,
  },
  {
    name: "browser.urlbar.quicksuggest.shouldShowOnboardingDialog",
    get: "getBoolPref",
    set: "setBoolPref",
    expectedOfflineValue: false,
    expectedOtherValue: true,
  },
  {
    name: "browser.urlbar.suggest.quicksuggest.nonsponsored",
    get: "getBoolPref",
    set: "setBoolPref",
    expectedOfflineValue: true,
    expectedOtherValue: false,
  },
  {
    name: "browser.urlbar.suggest.quicksuggest.sponsored",
    get: "getBoolPref",
    set: "setBoolPref",
    expectedOfflineValue: true,
    expectedOtherValue: false,
  },
];

add_task(async function init() {
  await UrlbarTestUtils.initNimbusFeature();
});

add_task(async function test() {
  let tests = [
    { locale: "en-US", home: "US", expectedOfflineDefault: true },
    { locale: "en-US", home: "CA", expectedOfflineDefault: false },
    { locale: "en-CA", home: "US", expectedOfflineDefault: true },
    { locale: "en-CA", home: "CA", expectedOfflineDefault: false },
    { locale: "en-GB", home: "US", expectedOfflineDefault: true },
    { locale: "en-GB", home: "GB", expectedOfflineDefault: false },
    { locale: "de", home: "US", expectedOfflineDefault: false },
    { locale: "de", home: "DE", expectedOfflineDefault: false },
  ];
  for (let { locale, home, expectedOfflineDefault } of tests) {
    await doTest({ locale, home, expectedOfflineDefault });
  }
});

/**
 * Sets the app's locale and region, calls
 * `UrlbarPrefs.updateFirefoxSuggestScenario`, and asserts that the pref values
 * are correct.
 *
 * @param {object} options
 *   Options object.
 * @param {string} options.locale
 *   The locale to simulate.
 * @param {string} options.home
 *   The "home" region to simulate.
 * @param {boolean} options.expectedOfflineDefault
 *   The expected value of whether offline should be enabled by default given
 *   the locale and region.
 */
async function doTest({ locale, home, expectedOfflineDefault }) {
  // Setup: Clear any user values and save original default-branch values.
  for (let pref of PREFS) {
    Services.prefs.clearUserPref(pref.name);
    pref.originalDefault = Services.prefs
      .getDefaultBranch(pref.name)
      [pref.get]("");
  }

  // Set the region and locale, call the function, check the pref values.
  Region._setHomeRegion(home, false);
  await QuickSuggestTestUtils.withLocales([locale], async () => {
    await UrlbarPrefs.updateFirefoxSuggestScenario();
    for (let { name, get, expectedOfflineValue, expectedOtherValue } of PREFS) {
      let expectedValue = expectedOfflineDefault
        ? expectedOfflineValue
        : expectedOtherValue;

      // Check the default-branch value.
      Assert.strictEqual(
        Services.prefs.getDefaultBranch(name)[get](""),
        expectedValue,
        `Default pref value for ${name}, locale ${locale}, home ${home}`
      );

      // For good measure, also check the return value of `UrlbarPrefs.get`
      // since we use it everywhere. The value should be the same as the
      // default-branch value.
      UrlbarPrefs.get(
        name.replace("browser.urlbar.", ""),
        expectedValue,
        `UrlbarPrefs.get() value for ${name}, locale ${locale}, home ${home}`
      );
    }
  });

  // Teardown: Restore original default-branch values for the next task.
  for (let { name, originalDefault, set } of PREFS) {
    if (originalDefault === undefined) {
      Services.prefs.deleteBranch(name);
    } else {
      Services.prefs.getDefaultBranch(name)[set]("", originalDefault);
    }
  }
}