test_providerKeywords.js (11627B)
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 /** 6 * Test for bug 392143 that puts keyword results into the autocomplete. Makes 7 * sure that multiple parameter queries get spaces converted to +, + converted 8 * to %2B, non-ascii become escaped, and pages in history that match the 9 * keyword uses the page's title. 10 * 11 * Also test for bug 249468 by making sure multiple keyword bookmarks with the 12 * same keyword appear in the list. 13 */ 14 15 testEngine_setup(); 16 17 add_task(async function test_keyword_search() { 18 let uri1 = "http://abc/?search=%s"; 19 let uri2 = "http://abc/?search=ThisPageIsInHistory"; 20 let uri3 = "http://abc/?search=%s&raw=%S"; 21 let uri4 = "http://abc/?search=%s&raw=%S&mozcharset=ISO-8859-1"; 22 let uri5 = "http://def/?search=%s"; 23 let uri6 = "http://ghi/?search=%s&raw=%S"; 24 let uri7 = "http://somedomain.example/key2"; 25 await PlacesTestUtils.addVisits([ 26 { uri: uri1 }, 27 { uri: uri2 }, 28 { uri: uri3 }, 29 { uri: uri6 }, 30 { uri: uri7 }, 31 ]); 32 await PlacesTestUtils.addBookmarkWithDetails({ 33 uri: uri1, 34 title: "Keyword", 35 keyword: "key", 36 }); 37 await PlacesTestUtils.addBookmarkWithDetails({ 38 uri: uri1, 39 title: "Post", 40 keyword: "post", 41 postData: "post_search=%s", 42 }); 43 await PlacesTestUtils.addBookmarkWithDetails({ 44 uri: uri3, 45 title: "Encoded", 46 keyword: "encoded", 47 }); 48 await PlacesTestUtils.addBookmarkWithDetails({ 49 uri: uri4, 50 title: "Charset", 51 keyword: "charset", 52 }); 53 await PlacesTestUtils.addBookmarkWithDetails({ 54 uri: uri2, 55 title: "Noparam", 56 keyword: "noparam", 57 }); 58 await PlacesTestUtils.addBookmarkWithDetails({ 59 uri: uri2, 60 title: "Noparam-Post", 61 keyword: "post_noparam", 62 postData: "noparam=1", 63 }); 64 await PlacesTestUtils.addBookmarkWithDetails({ 65 uri: uri5, 66 title: "Keyword", 67 keyword: "key2", 68 }); 69 await PlacesTestUtils.addBookmarkWithDetails({ 70 uri: uri6, 71 title: "Charset-history", 72 keyword: "charset_history", 73 }); 74 75 await PlacesUtils.history.update({ 76 url: uri6, 77 annotations: new Map([[PlacesUtils.CHARSET_ANNO, "ISO-8859-1"]]), 78 }); 79 80 info("Plain keyword query"); 81 let context = createContext("key term", { isPrivate: false }); 82 await check_results({ 83 context, 84 matches: [ 85 makeKeywordSearchResult(context, { 86 uri: "http://abc/?search=term", 87 keyword: "key", 88 title: "abc: term", 89 iconUri: "page-icon:http://abc/?search=%s", 90 heuristic: true, 91 }), 92 ], 93 }); 94 95 info("Plain keyword UC"); 96 context = createContext("key TERM", { isPrivate: false }); 97 await check_results({ 98 context, 99 matches: [ 100 makeKeywordSearchResult(context, { 101 uri: "http://abc/?search=TERM", 102 keyword: "key", 103 title: "abc: TERM", 104 iconUri: "page-icon:http://abc/?search=%s", 105 heuristic: true, 106 }), 107 ], 108 }); 109 110 info("Multi-word keyword query"); 111 context = createContext("key multi word", { isPrivate: false }); 112 await check_results({ 113 context, 114 matches: [ 115 makeKeywordSearchResult(context, { 116 uri: "http://abc/?search=multi%20word", 117 keyword: "key", 118 title: "abc: multi word", 119 iconUri: "page-icon:http://abc/?search=%s", 120 heuristic: true, 121 }), 122 ], 123 }); 124 125 info("Keyword query with +"); 126 context = createContext("key blocking+", { isPrivate: false }); 127 await check_results({ 128 context, 129 matches: [ 130 makeKeywordSearchResult(context, { 131 uri: "http://abc/?search=blocking%2B", 132 keyword: "key", 133 title: "abc: blocking+", 134 iconUri: "page-icon:http://abc/?search=%s", 135 heuristic: true, 136 }), 137 ], 138 }); 139 140 info("Keyword query with *"); 141 // We need a space before the asterisk to ensure it's considered a restriction 142 // token otherwise it will be a regular string character. 143 context = createContext("key blocking *", { isPrivate: false }); 144 await check_results({ 145 context, 146 matches: [ 147 makeKeywordSearchResult(context, { 148 uri: "http://abc/?search=blocking%20*", 149 keyword: "key", 150 title: "abc: blocking *", 151 iconUri: "page-icon:http://abc/?search=%s", 152 heuristic: true, 153 }), 154 ], 155 }); 156 157 info("Keyword query with?"); 158 context = createContext("key blocking?", { isPrivate: false }); 159 await check_results({ 160 context, 161 matches: [ 162 makeKeywordSearchResult(context, { 163 uri: "http://abc/?search=blocking%3F", 164 keyword: "key", 165 title: "abc: blocking?", 166 iconUri: "page-icon:http://abc/?search=%s", 167 heuristic: true, 168 }), 169 ], 170 }); 171 172 info("Keyword query with ?"); 173 context = createContext("key blocking ?", { isPrivate: false }); 174 await check_results({ 175 context, 176 matches: [ 177 makeKeywordSearchResult(context, { 178 uri: "http://abc/?search=blocking%20%3F", 179 keyword: "key", 180 title: "abc: blocking ?", 181 iconUri: "page-icon:http://abc/?search=%s", 182 heuristic: true, 183 }), 184 ], 185 }); 186 187 info("Unescaped term in query"); 188 // ... but note that we call encodeURIComponent() on the query string when we 189 // build the URL, so the expected result will have the ユニコード substring 190 // encoded in the URL. 191 context = createContext("key ユニコード", { isPrivate: false }); 192 await check_results({ 193 context, 194 matches: [ 195 makeKeywordSearchResult(context, { 196 uri: "http://abc/?search=" + encodeURIComponent("ユニコード"), 197 keyword: "key", 198 title: "abc: ユニコード", 199 iconUri: "page-icon:http://abc/?search=%s", 200 heuristic: true, 201 }), 202 ], 203 }); 204 205 info("Keyword that happens to match a page"); 206 context = createContext("key ThisPageIsInHistory", { isPrivate: false }); 207 await check_results({ 208 context, 209 matches: [ 210 makeKeywordSearchResult(context, { 211 uri: "http://abc/?search=ThisPageIsInHistory", 212 keyword: "key", 213 title: "abc: ThisPageIsInHistory", 214 iconUri: "page-icon:http://abc/?search=%s", 215 heuristic: true, 216 }), 217 ], 218 }); 219 220 info("Keyword with partial page match"); 221 context = createContext("key ThisPage", { isPrivate: false }); 222 await check_results({ 223 context, 224 matches: [ 225 makeKeywordSearchResult(context, { 226 uri: "http://abc/?search=ThisPage", 227 keyword: "key", 228 title: "abc: ThisPage", 229 iconUri: "page-icon:http://abc/?search=%s", 230 heuristic: true, 231 }), 232 // Only the most recent bookmark for the URL: 233 makeBookmarkResult(context, { 234 uri: "http://abc/?search=ThisPageIsInHistory", 235 title: "Noparam-Post", 236 }), 237 ], 238 }); 239 240 // For the keyword with no query terms (with or without space after), the 241 // domain is different from the other tests because otherwise all the other 242 // test bookmarks and history entries would be matches. 243 info("Keyword without query (without space)"); 244 context = createContext("key2", { isPrivate: false }); 245 await check_results({ 246 context, 247 matches: [ 248 makeKeywordSearchResult(context, { 249 uri: "http://def/?search=", 250 title: "http://def/?search=", 251 keyword: "key2", 252 iconUri: "page-icon:http://def/?search=%s", 253 heuristic: true, 254 }), 255 makeBookmarkResult(context, { 256 uri: uri5, 257 title: "Keyword", 258 }), 259 ], 260 }); 261 262 info("Keyword without query (with space)"); 263 context = createContext("key2 ", { isPrivate: false }); 264 await check_results({ 265 context, 266 matches: [ 267 makeKeywordSearchResult(context, { 268 uri: "http://def/?search=", 269 title: "http://def/?search=", 270 keyword: "key2", 271 iconUri: "page-icon:http://def/?search=%s", 272 heuristic: true, 273 }), 274 makeBookmarkResult(context, { 275 uri: uri5, 276 title: "Keyword", 277 }), 278 ], 279 }); 280 281 info("POST Keyword"); 282 context = createContext("post foo", { isPrivate: false }); 283 await check_results({ 284 context, 285 matches: [ 286 makeKeywordSearchResult(context, { 287 uri: "http://abc/?search=foo", 288 keyword: "post", 289 title: "abc: foo", 290 postData: "post_search=foo", 291 iconUri: "page-icon:http://abc/?search=%s", 292 heuristic: true, 293 }), 294 ], 295 }); 296 297 info("escaping with default UTF-8 charset"); 298 context = createContext("encoded foé", { isPrivate: false }); 299 await check_results({ 300 context, 301 matches: [ 302 makeKeywordSearchResult(context, { 303 uri: "http://abc/?search=fo%C3%A9&raw=foé", 304 keyword: "encoded", 305 title: "abc: foé", 306 iconUri: "page-icon:http://abc/?search=%s&raw=%S", 307 heuristic: true, 308 }), 309 ], 310 }); 311 312 info("escaping with forced ISO-8859-1 charset"); 313 context = createContext("charset foé", { isPrivate: false }); 314 await check_results({ 315 context, 316 matches: [ 317 makeKeywordSearchResult(context, { 318 uri: "http://abc/?search=fo%E9&raw=foé", 319 keyword: "charset", 320 title: "abc: foé", 321 iconUri: "page-icon:http://abc/?search=%s&raw=%S&mozcharset=ISO-8859-1", 322 heuristic: true, 323 }), 324 ], 325 }); 326 327 info("escaping with ISO-8859-1 charset annotated in history"); 328 context = createContext("charset_history foé", { isPrivate: false }); 329 await check_results({ 330 context, 331 matches: [ 332 makeKeywordSearchResult(context, { 333 uri: "http://ghi/?search=fo%E9&raw=foé", 334 keyword: "charset_history", 335 title: "ghi: foé", 336 iconUri: "page-icon:http://ghi/?search=%s&raw=%S", 337 heuristic: true, 338 }), 339 ], 340 }); 341 342 info("Bug 359809: escaping +, / and @ with default UTF-8 charset"); 343 context = createContext("encoded +/@", { isPrivate: false }); 344 await check_results({ 345 context, 346 matches: [ 347 makeKeywordSearchResult(context, { 348 uri: "http://abc/?search=%2B%2F%40&raw=+/@", 349 keyword: "encoded", 350 title: "abc: +/@", 351 iconUri: "page-icon:http://abc/?search=%s&raw=%S", 352 heuristic: true, 353 }), 354 ], 355 }); 356 357 info("Bug 359809: escaping +, / and @ with forced ISO-8859-1 charset"); 358 context = createContext("charset +/@", { isPrivate: false }); 359 await check_results({ 360 context, 361 matches: [ 362 makeKeywordSearchResult(context, { 363 uri: "http://abc/?search=%2B%2F%40&raw=+/@", 364 keyword: "charset", 365 title: "abc: +/@", 366 iconUri: "page-icon:http://abc/?search=%s&raw=%S&mozcharset=ISO-8859-1", 367 heuristic: true, 368 }), 369 ], 370 }); 371 372 info("Bug 1228111 - Keyword with a space in front"); 373 context = createContext(" key test", { isPrivate: false }); 374 await check_results({ 375 context, 376 matches: [ 377 makeKeywordSearchResult(context, { 378 uri: "http://abc/?search=test", 379 keyword: "key", 380 title: "abc: test", 381 iconUri: "page-icon:http://abc/?search=%s", 382 heuristic: true, 383 }), 384 ], 385 }); 386 387 info("Bug 1481319 - Keyword with a prefix in front"); 388 context = createContext("http://key2", { isPrivate: false }); 389 await check_results({ 390 context, 391 matches: [ 392 makeVisitResult(context, { 393 source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, 394 uri: "http://key2/", 395 title: "http://key2/", 396 heuristic: true, 397 providerName: "UrlbarProviderHeuristicFallback", 398 }), 399 makeVisitResult(context, { 400 uri: uri7, 401 title: "test visit for http://somedomain.example/key2", 402 }), 403 ], 404 }); 405 406 await cleanupPlaces(); 407 });