test_tab_matches.js (10660B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- 2 * vim:set ts=2 sw=2 sts=2 et: 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 testEngine_setup(); 8 9 add_task(async function test_tab_matches() { 10 Services.prefs.setBoolPref("browser.urlbar.autoFill", false); 11 Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false); 12 registerCleanupFunction(() => { 13 Services.prefs.clearUserPref("browser.urlbar.suggest.searches"); 14 Services.prefs.clearUserPref("browser.urlbar.autoFill"); 15 }); 16 17 let uri1 = Services.io.newURI("http://abc.com/"); 18 let uri2 = Services.io.newURI("http://xyz.net/"); 19 let uri3 = Services.io.newURI("about:mozilla"); 20 let uri4 = Services.io.newURI("data:text/html,test"); 21 let uri5 = Services.io.newURI("http://foobar.org"); 22 await PlacesTestUtils.addVisits([ 23 { 24 uri: uri5, 25 title: "foobar.org - much better than ABC, definitely better than XYZ", 26 }, 27 { uri: uri2, title: "xyz.net - we're better than ABC" }, 28 { uri: uri1, title: "ABC rocks" }, 29 ]); 30 await addOpenPages(uri1, 1); 31 // Pages that cannot be registered in history. 32 await addOpenPages(uri3, 1); 33 await addOpenPages(uri4, 1); 34 35 info("basic tab match"); 36 let context = createContext("abc.com", { isPrivate: false }); 37 await check_results({ 38 context, 39 matches: [ 40 makeVisitResult(context, { 41 source: UrlbarUtils.RESULT_SOURCE.HISTORY, 42 uri: "http://abc.com/", 43 title: "ABC rocks", 44 heuristic: true, 45 }), 46 makeTabSwitchResult(context, { 47 uri: "http://abc.com/", 48 title: "ABC rocks", 49 }), 50 ], 51 }); 52 53 info("three results, one tab match"); 54 context = createContext("abc", { isPrivate: false }); 55 await check_results({ 56 context, 57 matches: [ 58 makeSearchResult(context, { 59 engineName: SUGGESTIONS_ENGINE_NAME, 60 heuristic: true, 61 }), 62 makeTabSwitchResult(context, { 63 uri: "http://abc.com/", 64 title: "ABC rocks", 65 }), 66 makeVisitResult(context, { 67 uri: uri2.spec, 68 title: "xyz.net - we're better than ABC", 69 }), 70 makeVisitResult(context, { 71 uri: uri5.spec, 72 title: "foobar.org - much better than ABC, definitely better than XYZ", 73 }), 74 ], 75 }); 76 77 info("three results, both normal results are tab matches"); 78 await addOpenPages(uri2, 1); 79 context = createContext("abc", { isPrivate: false }); 80 await check_results({ 81 context, 82 matches: [ 83 makeSearchResult(context, { 84 engineName: SUGGESTIONS_ENGINE_NAME, 85 heuristic: true, 86 }), 87 makeTabSwitchResult(context, { 88 uri: "http://abc.com/", 89 title: "ABC rocks", 90 }), 91 makeTabSwitchResult(context, { 92 uri: "http://xyz.net/", 93 title: "xyz.net - we're better than ABC", 94 }), 95 makeVisitResult(context, { 96 uri: uri5.spec, 97 title: "foobar.org - much better than ABC, definitely better than XYZ", 98 }), 99 ], 100 }); 101 102 // This covers the following 3 tests. Container tests are in a dedicated 103 // test file anyway, so these are left to cover the disabled pref case. 104 UrlbarPrefs.set("switchTabs.searchAllContainers", false); 105 106 info("a container tab is not visible in 'switch to tab'"); 107 await addOpenPages(uri5, 1, /* userContextId: */ 3); 108 context = createContext("abc", { isPrivate: false }); 109 await check_results({ 110 context, 111 matches: [ 112 makeSearchResult(context, { 113 engineName: SUGGESTIONS_ENGINE_NAME, 114 heuristic: true, 115 }), 116 makeTabSwitchResult(context, { 117 uri: "http://abc.com/", 118 title: "ABC rocks", 119 }), 120 makeTabSwitchResult(context, { 121 uri: "http://xyz.net/", 122 title: "xyz.net - we're better than ABC", 123 }), 124 makeVisitResult(context, { 125 uri: uri5.spec, 126 title: "foobar.org - much better than ABC, definitely better than XYZ", 127 }), 128 ], 129 }); 130 131 info( 132 "a container tab should not see 'switch to tab' for other container tabs" 133 ); 134 context = createContext("abc", { isPrivate: false, userContextId: 3 }); 135 await check_results({ 136 context, 137 matches: [ 138 makeSearchResult(context, { 139 engineName: SUGGESTIONS_ENGINE_NAME, 140 heuristic: true, 141 }), 142 makeVisitResult(context, { 143 uri: uri1.spec, 144 title: "ABC rocks", 145 }), 146 makeVisitResult(context, { 147 uri: uri2.spec, 148 title: "xyz.net - we're better than ABC", 149 }), 150 makeTabSwitchResult(context, { 151 uri: "http://foobar.org/", 152 title: "foobar.org - much better than ABC, definitely better than XYZ", 153 userContextId: 3, 154 }), 155 ], 156 }); 157 158 info("a different container tab should not see any 'switch to tab'"); 159 context = createContext("abc", { isPrivate: false, userContextId: 2 }); 160 await check_results({ 161 context, 162 matches: [ 163 makeSearchResult(context, { 164 engineName: SUGGESTIONS_ENGINE_NAME, 165 heuristic: true, 166 }), 167 makeVisitResult(context, { uri: uri1.spec, title: "ABC rocks" }), 168 makeVisitResult(context, { 169 uri: uri2.spec, 170 title: "xyz.net - we're better than ABC", 171 }), 172 makeVisitResult(context, { 173 uri: uri5.spec, 174 title: "foobar.org - much better than ABC, definitely better than XYZ", 175 }), 176 ], 177 }); 178 179 UrlbarPrefs.clear("switchTabs.searchAllContainers"); 180 if (UrlbarPrefs.get("switchTabs.searchAllContainers")) { 181 // This would confuse the next tests, so remove it, containers are tested 182 // in a separate test file. 183 await removeOpenPages(uri5, 1, /* userContextId: */ 3); 184 } 185 186 info( 187 "three results, both normal results are tab matches, one has multiple tabs" 188 ); 189 await addOpenPages(uri2, 5); 190 context = createContext("abc", { isPrivate: false }); 191 await check_results({ 192 context, 193 matches: [ 194 makeSearchResult(context, { 195 engineName: SUGGESTIONS_ENGINE_NAME, 196 heuristic: true, 197 }), 198 makeTabSwitchResult(context, { 199 uri: "http://abc.com/", 200 title: "ABC rocks", 201 }), 202 makeTabSwitchResult(context, { 203 uri: "http://xyz.net/", 204 title: "xyz.net - we're better than ABC", 205 }), 206 makeVisitResult(context, { 207 uri: uri5.spec, 208 title: "foobar.org - much better than ABC, definitely better than XYZ", 209 }), 210 ], 211 }); 212 213 info("three results, no tab matches"); 214 await removeOpenPages(uri1, 1); 215 await removeOpenPages(uri2, 6); 216 context = createContext("abc", { isPrivate: false }); 217 await check_results({ 218 context, 219 matches: [ 220 makeSearchResult(context, { 221 engineName: SUGGESTIONS_ENGINE_NAME, 222 heuristic: true, 223 }), 224 makeVisitResult(context, { 225 uri: uri1.spec, 226 title: "ABC rocks", 227 }), 228 makeVisitResult(context, { 229 uri: uri2.spec, 230 title: "xyz.net - we're better than ABC", 231 }), 232 makeVisitResult(context, { 233 uri: uri5.spec, 234 title: "foobar.org - much better than ABC, definitely better than XYZ", 235 }), 236 ], 237 }); 238 239 info("tab match search with restriction character"); 240 await addOpenPages(uri1, 1); 241 context = createContext(UrlbarTokenizer.RESTRICT.OPENPAGE + " abc", { 242 isPrivate: false, 243 }); 244 await check_results({ 245 context, 246 matches: [ 247 makeSearchResult(context, { 248 query: "abc", 249 alias: UrlbarTokenizer.RESTRICT.OPENPAGE, 250 source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, 251 heuristic: true, 252 }), 253 makeTabSwitchResult(context, { 254 uri: "http://abc.com/", 255 title: "ABC rocks", 256 }), 257 ], 258 }); 259 260 info("tab match with not-addable pages"); 261 context = createContext("mozilla", { isPrivate: false }); 262 await check_results({ 263 context, 264 matches: [ 265 makeSearchResult(context, { 266 engineName: SUGGESTIONS_ENGINE_NAME, 267 heuristic: true, 268 }), 269 makeTabSwitchResult(context, { 270 uri: "about:mozilla", 271 title: "about:mozilla", 272 }), 273 ], 274 }); 275 276 info("tab match with not-addable pages, no boundary search"); 277 context = createContext("ut:mo", { isPrivate: false }); 278 await check_results({ 279 context, 280 matches: [ 281 makeSearchResult(context, { 282 engineName: SUGGESTIONS_ENGINE_NAME, 283 heuristic: true, 284 }), 285 makeTabSwitchResult(context, { 286 uri: "about:mozilla", 287 title: "about:mozilla", 288 }), 289 ], 290 }); 291 292 info("tab match with not-addable pages and restriction character"); 293 context = createContext(UrlbarTokenizer.RESTRICT.OPENPAGE + " mozilla", { 294 isPrivate: false, 295 }); 296 await check_results({ 297 context, 298 matches: [ 299 makeSearchResult(context, { 300 query: "mozilla", 301 alias: UrlbarTokenizer.RESTRICT.OPENPAGE, 302 source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, 303 heuristic: true, 304 }), 305 makeTabSwitchResult(context, { 306 uri: "about:mozilla", 307 title: "about:mozilla", 308 }), 309 ], 310 }); 311 312 info("tab match with not-addable pages and only restriction character"); 313 context = createContext(UrlbarTokenizer.RESTRICT.OPENPAGE, { 314 isPrivate: false, 315 }); 316 await check_results({ 317 context, 318 matches: [ 319 makeSearchResult(context, { 320 engineName: SUGGESTIONS_ENGINE_NAME, 321 heuristic: true, 322 }), 323 makeTabSwitchResult(context, { 324 uri: "http://abc.com/", 325 title: "ABC rocks", 326 }), 327 makeTabSwitchResult(context, { 328 uri: "data:text/html,test", 329 title: "data:text/html,test", 330 iconUri: UrlbarUtils.ICON.DEFAULT, 331 }), 332 makeTabSwitchResult(context, { 333 uri: "about:mozilla", 334 title: "about:mozilla", 335 }), 336 ], 337 }); 338 339 info("tab match should not return tags as part of the title"); 340 // Bookmark one of the pages, and add tags to it, to check they don't appear 341 // in the title. 342 let bm = await PlacesUtils.bookmarks.insert({ 343 url: uri1, 344 parentGuid: PlacesUtils.bookmarks.toolbarGuid, 345 }); 346 PlacesUtils.tagging.tagURI(uri1, ["test-tag"]); 347 context = createContext("abc.com", { isPrivate: false }); 348 await check_results({ 349 context, 350 matches: [ 351 makeVisitResult(context, { 352 source: UrlbarUtils.RESULT_SOURCE.HISTORY, 353 uri: "http://abc.com/", 354 title: "ABC rocks", 355 heuristic: true, 356 }), 357 makeTabSwitchResult(context, { 358 uri: "http://abc.com/", 359 title: "ABC rocks", 360 }), 361 ], 362 }); 363 await PlacesUtils.bookmarks.remove(bm); 364 365 await cleanupPlaces(); 366 });