test_quicksuggest_exposures_locales.js (7434B)
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 // Checks that exposure suggestions can be enabled in Nimbus experiments 6 // regardless of region and locale, even for regions and locales where Suggest 7 // is normally disabled. 8 9 "use strict"; 10 11 const REMOTE_SETTINGS_RECORDS = [ 12 { 13 type: "dynamic-suggestions", 14 suggestion_type: "test-exposure-aaa", 15 score: 1.0, 16 attachment: [ 17 { 18 keywords: ["aaa keyword", "aaa bbb keyword", "amp", "wikipedia"], 19 data: { 20 result: { 21 isHiddenExposure: true, 22 payload: { 23 rsSuggestionType: "test-exposure-aaa", 24 }, 25 }, 26 }, 27 }, 28 ], 29 }, 30 { 31 type: "dynamic-suggestions", 32 suggestion_type: "test-exposure-bbb", 33 score: 1.0, 34 attachment: { 35 keywords: ["bbb keyword", "aaa bbb keyword", "amp", "wikipedia"], 36 data: { 37 result: { 38 isHiddenExposure: true, 39 payload: { 40 rsSuggestionType: "test-exposure-bbb", 41 }, 42 }, 43 }, 44 }, 45 }, 46 { 47 collection: QuickSuggestTestUtils.RS_COLLECTION.AMP, 48 type: QuickSuggestTestUtils.RS_TYPE.AMP, 49 attachment: [QuickSuggestTestUtils.ampRemoteSettings()], 50 }, 51 { 52 type: QuickSuggestTestUtils.RS_TYPE.WIKIPEDIA, 53 attachment: [QuickSuggestTestUtils.wikipediaRemoteSettings()], 54 }, 55 ]; 56 57 add_setup(async function () { 58 await QuickSuggestTestUtils.ensureQuickSuggestInit({ 59 remoteSettingsRecords: REMOTE_SETTINGS_RECORDS, 60 }); 61 62 // `ensureQuickSuggestInit()` enabled Suggest, but we want to start with it 63 // disabled so that when we change locales, we can verify Suggest is properly 64 // disabled or enabled depending on the locale. 65 UrlbarPrefs.clear("quicksuggest.enabled"); 66 }); 67 68 add_task(async function suggestEnabledLocales() { 69 let tests = [ 70 { 71 homeRegion: "US", 72 locales: ["en-US", "en-CA", "en-GB"], 73 expectedQuickSuggestEnabled: true, 74 queries: [ 75 { 76 query: "amp", 77 expectedResults: [ 78 QuickSuggestTestUtils.ampResult({ suggestedIndex: -1 }), 79 makeExpectedExposureResult("test-exposure-bbb"), 80 makeExpectedExposureResult("test-exposure-aaa"), 81 ], 82 }, 83 { 84 query: "wikipedia", 85 expectedResults: [ 86 QuickSuggestTestUtils.wikipediaResult(), 87 makeExpectedExposureResult("test-exposure-bbb"), 88 makeExpectedExposureResult("test-exposure-aaa"), 89 ], 90 }, 91 { 92 query: "aaa keyword", 93 expectedResults: [makeExpectedExposureResult("test-exposure-aaa")], 94 }, 95 { 96 query: "aaa bbb keyword", 97 expectedResults: [ 98 makeExpectedExposureResult("test-exposure-bbb"), 99 makeExpectedExposureResult("test-exposure-aaa"), 100 ], 101 }, 102 ], 103 }, 104 ]; 105 106 for (let test of tests) { 107 await doLocaleTest(test); 108 } 109 }); 110 111 add_task(async function suggestDisabledLocales() { 112 let queries = [ 113 { 114 query: "amp", 115 expectedResults: [ 116 // No AMP result! 117 makeExpectedExposureResult("test-exposure-bbb"), 118 makeExpectedExposureResult("test-exposure-aaa"), 119 ], 120 }, 121 { 122 query: "wikipedia", 123 expectedResults: [ 124 // No Wikipedia result! 125 makeExpectedExposureResult("test-exposure-bbb"), 126 makeExpectedExposureResult("test-exposure-aaa"), 127 ], 128 }, 129 { 130 query: "aaa keyword", 131 expectedResults: [makeExpectedExposureResult("test-exposure-aaa")], 132 }, 133 { 134 query: "aaa bbb keyword", 135 expectedResults: [ 136 makeExpectedExposureResult("test-exposure-bbb"), 137 makeExpectedExposureResult("test-exposure-aaa"), 138 ], 139 }, 140 ]; 141 142 let tests = [ 143 { 144 homeRegion: "US", 145 locales: ["de", "fr", "ja"], 146 expectedQuickSuggestEnabled: false, 147 queries, 148 }, 149 { 150 homeRegion: "CA", 151 locales: ["en-US", "en-CA", "en-GB", "fr"], 152 expectedQuickSuggestEnabled: false, 153 queries, 154 }, 155 { 156 homeRegion: "XX", 157 locales: ["de", "en-US", "fr"], 158 expectedQuickSuggestEnabled: false, 159 queries, 160 }, 161 ]; 162 163 for (let test of tests) { 164 await doLocaleTest(test); 165 } 166 }); 167 168 async function doLocaleTest({ 169 homeRegion, 170 locales, 171 expectedQuickSuggestEnabled, 172 queries, 173 }) { 174 for (let locale of locales) { 175 info("Doing locale test: " + JSON.stringify({ homeRegion, locale })); 176 177 // Set the region and locale. 178 await QuickSuggestTestUtils.withRegionAndLocale({ 179 locale, 180 region: homeRegion, 181 callback: async () => { 182 // Sanity-check prefs. At this point, the value of `quickSuggestEnabled` 183 // will be the value of its fallback pref, `quicksuggest.enabled`. 184 assertSuggestPrefs(expectedQuickSuggestEnabled); 185 Assert.equal( 186 UrlbarPrefs.get("quickSuggestEnabled"), 187 expectedQuickSuggestEnabled, 188 "quickSuggestEnabled Nimbus variable should be correct after setting locale" 189 ); 190 191 // Install an experiment that enables Suggest and exposures. 192 let nimbusCleanup = await UrlbarTestUtils.initNimbusFeature({ 193 quickSuggestEnabled: true, 194 quickSuggestDynamicSuggestionTypes: 195 "test-exposure-aaa,test-exposure-bbb", 196 }); 197 await QuickSuggestTestUtils.forceSync(); 198 199 // All default- and user-branch Suggest prefs should remain the same. 200 assertSuggestPrefs(expectedQuickSuggestEnabled); 201 202 // But `quickSuggestEnabled` should be true, since we just installed 203 // an experiment with it set to true. 204 Assert.ok( 205 UrlbarPrefs.get("quickSuggestEnabled"), 206 "quickSuggestEnabled Nimbus variable should be enabled after installing experiment" 207 ); 208 209 // Do a search and check the results. 210 for (let { query, expectedResults } of queries) { 211 await check_results({ 212 context: createContext(query, { 213 providers: [UrlbarProviderQuickSuggest.name], 214 isPrivate: false, 215 }), 216 matches: expectedResults, 217 }); 218 } 219 220 await nimbusCleanup(); 221 await QuickSuggestTestUtils.forceSync(); 222 }, 223 }); 224 } 225 } 226 227 function assertSuggestPrefs(expectedEnabled) { 228 let prefs = [ 229 "browser.urlbar.quicksuggest.enabled", 230 "browser.urlbar.suggest.quicksuggest.all", 231 "browser.urlbar.suggest.quicksuggest.sponsored", 232 ]; 233 for (let p of prefs) { 234 Assert.equal( 235 Services.prefs.getDefaultBranch("").getBoolPref(p), 236 expectedEnabled, 237 "Default-branch value should be correct: " + p 238 ); 239 Assert.equal( 240 Services.prefs.getBranch("").getBoolPref(p), 241 expectedEnabled, 242 "User-branch value should be correct: " + p 243 ); 244 } 245 } 246 247 function makeExpectedExposureResult(rsSuggestionType) { 248 return { 249 type: UrlbarUtils.RESULT_TYPE.DYNAMIC, 250 source: UrlbarUtils.RESULT_SOURCE.SEARCH, 251 heuristic: false, 252 exposureTelemetry: UrlbarUtils.EXPOSURE_TELEMETRY.HIDDEN, 253 payload: { 254 rsSuggestionType, 255 source: "rust", 256 dynamicType: "exposure", 257 provider: "Dynamic", 258 suggestionType: rsSuggestionType, 259 telemetryType: "exposure", 260 isSponsored: false, 261 }, 262 }; 263 }