test_quicksuggest_defaultPrefs.js (6790B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Tests that Suggest is enabled by default for appropriate region-locales and 6 // disabled by default everywhere else. 7 8 "use strict"; 9 10 ChromeUtils.defineESModuleGetters(this, { 11 Preferences: "resource://gre/modules/Preferences.sys.mjs", 12 }); 13 14 const EN_LOCALES = ["en-CA", "en-GB", "en-US", "en-ZA"]; 15 16 // Expected prefs when Suggest is disabled. 17 const EXPECTED_PREFS_SUGGEST_DISABLED = { 18 "quicksuggest.enabled": false, 19 "quicksuggest.online.available": false, 20 "quicksuggest.online.enabled": true, 21 "quicksuggest.settingsUi": QuickSuggest.SETTINGS_UI.NONE, 22 "suggest.quicksuggest.all": false, 23 "suggest.quicksuggest.sponsored": false, 24 "addons.featureGate": false, 25 "amp.featureGate": false, 26 "importantDates.featureGate": false, 27 "mdn.featureGate": false, 28 "weather.featureGate": false, 29 "wikipedia.featureGate": false, 30 "yelp.featureGate": false, 31 }; 32 33 // Expected prefs for native locales in EU countries (e.g., `de` locale in 34 // Germany). 35 const EXPECTED_PREFS_EU_NATIVE = { 36 ...EXPECTED_PREFS_SUGGEST_DISABLED, 37 "quicksuggest.enabled": true, 38 "quicksuggest.settingsUi": QuickSuggest.SETTINGS_UI.OFFLINE_ONLY, 39 "suggest.quicksuggest.all": true, 40 "suggest.quicksuggest.sponsored": true, 41 "importantDates.featureGate": true, 42 "weather.featureGate": true, 43 }; 44 45 // Expected prefs for `en` locales in EU countries (e.g., `en-US` locale in 46 // Germany). 47 const EXPECTED_PREFS_EU_EN = { 48 ...EXPECTED_PREFS_SUGGEST_DISABLED, 49 "quicksuggest.enabled": true, 50 "importantDates.featureGate": true, 51 }; 52 53 // Base set of expected prefs for countries where an `en` locale is native 54 // (e.g., US, UK). These countries will have slightly different actual expected 55 // prefs, which is why this is a base set. 56 const EXPECTED_PREFS_BASE_EN_NATIVE = { 57 ...EXPECTED_PREFS_SUGGEST_DISABLED, 58 "quicksuggest.enabled": true, 59 "quicksuggest.settingsUi": QuickSuggest.SETTINGS_UI.OFFLINE_ONLY, 60 "suggest.quicksuggest.all": true, 61 "suggest.quicksuggest.sponsored": true, 62 "amp.featureGate": true, 63 "importantDates.featureGate": true, 64 "weather.featureGate": true, 65 "wikipedia.featureGate": true, 66 }; 67 68 // Region -> locale -> expected prefs when Suggest is enabled 69 const EXPECTED_PREFS_BY_LOCALE_BY_REGION = { 70 DE: { 71 de: EXPECTED_PREFS_EU_NATIVE, 72 ...Object.fromEntries( 73 EN_LOCALES.map(locale => [locale, EXPECTED_PREFS_EU_EN]) 74 ), 75 }, 76 FR: { 77 fr: EXPECTED_PREFS_EU_NATIVE, 78 ...Object.fromEntries( 79 EN_LOCALES.map(locale => [locale, EXPECTED_PREFS_EU_EN]) 80 ), 81 }, 82 GB: Object.fromEntries( 83 EN_LOCALES.map(locale => [locale, EXPECTED_PREFS_BASE_EN_NATIVE]) 84 ), 85 IT: { 86 it: EXPECTED_PREFS_EU_NATIVE, 87 ...Object.fromEntries( 88 EN_LOCALES.map(locale => [locale, EXPECTED_PREFS_EU_EN]) 89 ), 90 }, 91 US: Object.fromEntries( 92 EN_LOCALES.map(locale => [ 93 locale, 94 { 95 ...EXPECTED_PREFS_BASE_EN_NATIVE, 96 "addons.featureGate": true, 97 "mdn.featureGate": true, 98 "yelp.featureGate": true, 99 }, 100 ]) 101 ), 102 }; 103 104 add_setup(async () => { 105 await UrlbarTestUtils.initNimbusFeature(); 106 }); 107 108 add_task(async function test() { 109 let tests = [ 110 // Regions/locales where Suggest should be enabled to some extent 111 { region: "DE", locale: "de" }, 112 { region: "DE", locale: "en-GB" }, 113 { region: "DE", locale: "en-US" }, 114 115 { region: "FR", locale: "fr" }, 116 { region: "FR", locale: "en-GB" }, 117 { region: "FR", locale: "en-US" }, 118 119 { region: "GB", locale: "en-US" }, 120 { region: "GB", locale: "en-CA" }, 121 { region: "GB", locale: "en-GB" }, 122 123 { region: "IT", locale: "it" }, 124 { region: "IT", locale: "en-GB" }, 125 { region: "IT", locale: "en-US" }, 126 127 { region: "US", locale: "en-US" }, 128 { region: "US", locale: "en-CA" }, 129 { region: "US", locale: "en-GB" }, 130 131 // Regions/locales where Suggest should be completely disabled 132 { region: "CA", locale: "en-US" }, 133 { region: "CA", locale: "en-CA" }, 134 { region: "GB", locale: "de" }, 135 { region: "US", locale: "de" }, 136 ]; 137 138 for (let { locale, region } of tests) { 139 await doTest({ locale, region }); 140 } 141 }); 142 143 /** 144 * Sets the app's locale and region, reinitializes Suggest, and asserts that the 145 * pref values are correct. 146 * 147 * @param {object} options 148 * Options object. 149 * @param {string} options.locale 150 * The locale to simulate. 151 * @param {string} options.region 152 * The "home" region to simulate. 153 */ 154 async function doTest({ locale, region }) { 155 let expectedPrefs = 156 EXPECTED_PREFS_BY_LOCALE_BY_REGION[region]?.[locale] ?? 157 EXPECTED_PREFS_SUGGEST_DISABLED; 158 159 let defaultBranch = new Preferences({ 160 branch: "browser.urlbar.", 161 defaultBranch: true, 162 }); 163 let userBranch = new Preferences({ 164 branch: "browser.urlbar.", 165 defaultBranch: false, 166 }); 167 168 // Setup: Clear any user values and save original default-branch values. 169 let originalDefaults = {}; 170 for (let name of Object.keys(expectedPrefs)) { 171 userBranch.reset(name); 172 originalDefaults[name] = defaultBranch.get(name); 173 } 174 175 // Clear the migration version to simulate a new profile. `QuickSuggest` will 176 // perform a full migration process, applying each migration version in turn 177 // until it reaches the current version. 178 userBranch.reset("quicksuggest.migrationVersion"); 179 180 // Set the region and locale, call the function, check the pref values. 181 await QuickSuggestTestUtils.withRegionAndLocale({ 182 region, 183 locale, 184 callback: async () => { 185 for (let [name, value] of Object.entries(expectedPrefs)) { 186 // Check the default-branch value. 187 Assert.strictEqual( 188 defaultBranch.get(name), 189 value, 190 `Default pref value for ${name}, locale ${locale}, region ${region}` 191 ); 192 193 // For good measure, also check the return value of `UrlbarPrefs.get` 194 // since we use it everywhere. The value should be the same as the 195 // default-branch value. 196 UrlbarPrefs.get( 197 name, 198 value, 199 `UrlbarPrefs.get() value for ${name}, locale ${locale}, region ${region}` 200 ); 201 202 // Make sure migration didn't unexpectedly set any user-branch values. 203 Assert.ok( 204 !userBranch.isSet(name), 205 "Pref should not be set on the user branch: " + name 206 ); 207 } 208 }, 209 }); 210 211 // Teardown: Restore original default-branch values for the next task. 212 for (let [name, originalDefault] of Object.entries(originalDefaults)) { 213 if (originalDefault === undefined) { 214 Services.prefs.deleteBranch("browser.urlbar." + name); 215 } else { 216 defaultBranch.set(name, originalDefault); 217 } 218 } 219 }