test_quicksuggest_market.js (6315B)
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 Market suggestions. 6 7 "use strict"; 8 9 const TEST_MERINO_SINGLE = [ 10 { 11 provider: "polygon", 12 score: 0, 13 custom_details: { 14 polygon: { 15 values: [ 16 { 17 image_url: "https://example.com/aapl.svg", 18 query: "AAPL stock", 19 name: "Apple Inc", 20 ticker: "AAPL", 21 todays_change_perc: "-0.54", 22 last_price: "$181.98 USD", 23 }, 24 ], 25 }, 26 }, 27 }, 28 ]; 29 30 const TEST_MERINO_EMPTY_POLYGON_VALUES = [ 31 { 32 provider: "polygon", 33 score: 0, 34 custom_details: { 35 polygon: { 36 values: [], 37 }, 38 }, 39 }, 40 ]; 41 42 add_setup(async function init() { 43 await Services.search.init(); 44 45 // Disable search suggestions so we don't hit the network. 46 Services.prefs.setBoolPref("browser.search.suggest.enabled", false); 47 48 await QuickSuggestTestUtils.ensureQuickSuggestInit({ 49 merinoSuggestions: TEST_MERINO_SINGLE, 50 prefs: [ 51 ["market.featureGate", true], 52 ["suggest.market", true], 53 ["suggest.quicksuggest.all", true], 54 ], 55 }); 56 }); 57 58 add_task(async function telemetryType() { 59 Assert.equal( 60 QuickSuggest.getFeature("MarketSuggestions").getSuggestionTelemetryType({}), 61 "market", 62 "Telemetry type should be 'market'" 63 ); 64 }); 65 66 add_task(async function disabledPrefs() { 67 let prefs = [ 68 "quicksuggest.enabled", 69 "suggest.market", 70 "suggest.quicksuggest.all", 71 ]; 72 73 for (let pref of prefs) { 74 info("Testing pref: " + pref); 75 76 // First make sure the suggestion is added. 77 await check_results({ 78 context: createContext("test", { 79 providers: [UrlbarProviderQuickSuggest.name], 80 isPrivate: false, 81 }), 82 matches: [marketResult()], 83 }); 84 85 // Now disable them. 86 UrlbarPrefs.set(pref, false); 87 await check_results({ 88 context: createContext("test", { 89 providers: [UrlbarProviderQuickSuggest.name], 90 isPrivate: false, 91 }), 92 matches: [], 93 }); 94 95 // Revert. 96 UrlbarPrefs.set(pref, true); 97 await QuickSuggestTestUtils.forceSync(); 98 } 99 }); 100 101 // Tests the "Not interested" command: all Market suggestions should be disabled 102 // and not added anymore. 103 add_task(async function notInterested() { 104 await doDismissAllTest({ 105 result: marketResult(), 106 command: "not_interested", 107 feature: QuickSuggest.getFeature("MarketSuggestions"), 108 pref: "suggest.market", 109 queries: [{ query: "stock" }], 110 }); 111 }); 112 113 // Tests the "show less frequently" behavior. 114 add_task(async function showLessFrequently() { 115 UrlbarPrefs.clear("market.showLessFrequentlyCount"); 116 UrlbarPrefs.clear("market.minKeywordLength"); 117 118 let cleanUpNimbus = await UrlbarTestUtils.initNimbusFeature({ 119 realtimeMinKeywordLength: 0, 120 realtimeShowLessFrequentlyCap: 3, 121 }); 122 123 let result = marketResult(); 124 125 const testData = [ 126 { 127 input: "st", 128 before: { 129 canShowLessFrequently: true, 130 showLessFrequentlyCount: 0, 131 minKeywordLength: 0, 132 }, 133 after: { 134 canShowLessFrequently: true, 135 showLessFrequentlyCount: 1, 136 minKeywordLength: 3, 137 }, 138 }, 139 { 140 input: "stoc", 141 before: { 142 canShowLessFrequently: true, 143 showLessFrequentlyCount: 1, 144 minKeywordLength: 3, 145 }, 146 after: { 147 canShowLessFrequently: true, 148 showLessFrequentlyCount: 2, 149 minKeywordLength: 5, 150 }, 151 }, 152 { 153 input: "stock", 154 before: { 155 canShowLessFrequently: true, 156 showLessFrequentlyCount: 2, 157 minKeywordLength: 5, 158 }, 159 after: { 160 canShowLessFrequently: false, 161 showLessFrequentlyCount: 3, 162 minKeywordLength: 6, 163 }, 164 }, 165 ]; 166 167 for (let { input, before, after } of testData) { 168 let feature = QuickSuggest.getFeature("MarketSuggestions"); 169 170 await check_results({ 171 context: createContext(input, { 172 providers: [UrlbarProviderQuickSuggest.name], 173 isPrivate: false, 174 }), 175 matches: [result], 176 }); 177 178 Assert.equal( 179 UrlbarPrefs.get("market.minKeywordLength"), 180 before.minKeywordLength 181 ); 182 Assert.equal(feature.canShowLessFrequently, before.canShowLessFrequently); 183 Assert.equal( 184 feature.showLessFrequentlyCount, 185 before.showLessFrequentlyCount 186 ); 187 188 triggerCommand({ 189 result, 190 feature, 191 command: "show_less_frequently", 192 searchString: input, 193 }); 194 195 Assert.equal( 196 UrlbarPrefs.get("market.minKeywordLength"), 197 after.minKeywordLength 198 ); 199 Assert.equal(feature.canShowLessFrequently, after.canShowLessFrequently); 200 Assert.equal( 201 feature.showLessFrequentlyCount, 202 after.showLessFrequentlyCount 203 ); 204 205 await check_results({ 206 context: createContext(input, { 207 providers: [UrlbarProviderQuickSuggest.name], 208 isPrivate: false, 209 }), 210 matches: [], 211 }); 212 } 213 214 await cleanUpNimbus(); 215 UrlbarPrefs.clear("market.showLessFrequentlyCount"); 216 UrlbarPrefs.clear("market.minKeywordLength"); 217 }); 218 219 // Tests in case of that the polygon values is empty. 220 add_task(async function empty_polygon_values() { 221 MerinoTestUtils.server.response.body.suggestions = 222 TEST_MERINO_EMPTY_POLYGON_VALUES; 223 await check_results({ 224 context: createContext("stock", { 225 providers: [UrlbarProviderQuickSuggest.name], 226 isPrivate: false, 227 }), 228 matches: [], 229 }); 230 }); 231 232 function marketResult() { 233 return { 234 type: UrlbarUtils.RESULT_TYPE.DYNAMIC, 235 source: UrlbarUtils.RESULT_SOURCE.SEARCH, 236 isBestMatch: true, 237 hideRowLabel: true, 238 rowIndex: -1, 239 heuristic: false, 240 exposureTelemetry: 0, 241 payload: { 242 source: "merino", 243 provider: "polygon", 244 telemetryType: "market", 245 isSponsored: false, 246 engine: Services.search.defaultEngine.name, 247 items: [ 248 { 249 image_url: "https://example.com/aapl.svg", 250 query: "AAPL stock", 251 name: "Apple Inc", 252 ticker: "AAPL", 253 todays_change_perc: "-0.54", 254 last_price: "$181.98 USD", 255 }, 256 ], 257 dynamicType: "realtime-market", 258 }, 259 }; 260 }