test_UrlbarUtils_getTokenMatches.js (7344B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /** 5 * Tests UrlbarUtils.getTokenMatches. 6 */ 7 8 "use strict"; 9 10 add_task(function testTyped() { 11 const tests = [ 12 { 13 tokens: ["mozilla", "is", "i"], 14 phrase: "mozilla is for the Open Web", 15 expected: [ 16 [0, 7], 17 [8, 2], 18 ], 19 }, 20 { 21 tokens: ["mozilla", "is", "i"], 22 phrase: "MOZILLA IS for the Open Web", 23 expected: [ 24 [0, 7], 25 [8, 2], 26 ], 27 }, 28 { 29 tokens: ["mozilla", "is", "i"], 30 phrase: "MoZiLlA Is for the Open Web", 31 expected: [ 32 [0, 7], 33 [8, 2], 34 ], 35 }, 36 { 37 tokens: ["MOZILLA", "IS", "I"], 38 phrase: "mozilla is for the Open Web", 39 expected: [ 40 [0, 7], 41 [8, 2], 42 ], 43 }, 44 { 45 tokens: ["MoZiLlA", "Is", "I"], 46 phrase: "mozilla is for the Open Web", 47 expected: [ 48 [0, 7], 49 [8, 2], 50 ], 51 }, 52 { 53 tokens: ["mo", "b"], 54 phrase: "mozilla is for the Open Web", 55 expected: [ 56 [0, 2], 57 [26, 1], 58 ], 59 }, 60 { 61 tokens: ["mo", "b"], 62 phrase: "MOZILLA is for the OPEN WEB", 63 expected: [ 64 [0, 2], 65 [26, 1], 66 ], 67 }, 68 { 69 tokens: ["MO", "B"], 70 phrase: "mozilla is for the Open Web", 71 expected: [ 72 [0, 2], 73 [26, 1], 74 ], 75 }, 76 { 77 tokens: ["mo", ""], 78 phrase: "mozilla is for the Open Web", 79 expected: [[0, 2]], 80 }, 81 { 82 tokens: ["mozilla"], 83 phrase: "mozilla", 84 expected: [[0, 7]], 85 }, 86 { 87 tokens: ["mozilla"], 88 phrase: "MOZILLA", 89 expected: [[0, 7]], 90 }, 91 { 92 tokens: ["mozilla"], 93 phrase: "MoZiLlA", 94 expected: [[0, 7]], 95 }, 96 { 97 tokens: ["mozilla"], 98 phrase: "mOzIlLa", 99 expected: [[0, 7]], 100 }, 101 { 102 tokens: ["MOZILLA"], 103 phrase: "mozilla", 104 expected: [[0, 7]], 105 }, 106 { 107 tokens: ["MoZiLlA"], 108 phrase: "mozilla", 109 expected: [[0, 7]], 110 }, 111 { 112 tokens: ["mOzIlLa"], 113 phrase: "mozilla", 114 expected: [[0, 7]], 115 }, 116 { 117 tokens: ["\u9996"], 118 phrase: "Test \u9996\u9875 Test", 119 expected: [[5, 1]], 120 }, 121 { 122 tokens: ["mo", "zilla"], 123 phrase: "mozilla", 124 expected: [[0, 7]], 125 }, 126 { 127 tokens: ["mo", "zilla"], 128 phrase: "MOZILLA", 129 expected: [[0, 7]], 130 }, 131 { 132 tokens: ["mo", "zilla"], 133 phrase: "MoZiLlA", 134 expected: [[0, 7]], 135 }, 136 { 137 tokens: ["mo", "zilla"], 138 phrase: "mOzIlLa", 139 expected: [[0, 7]], 140 }, 141 { 142 tokens: ["MO", "ZILLA"], 143 phrase: "mozilla", 144 expected: [[0, 7]], 145 }, 146 { 147 tokens: ["Mo", "Zilla"], 148 phrase: "mozilla", 149 expected: [[0, 7]], 150 }, 151 { 152 tokens: ["moz", "zilla"], 153 phrase: "mozilla", 154 expected: [[0, 7]], 155 }, 156 { 157 tokens: [""], // Should never happen in practice. 158 phrase: "mozilla", 159 expected: [], 160 }, 161 { 162 tokens: ["mo", "om"], 163 phrase: "mozilla mozzarella momo", 164 expected: [ 165 [0, 2], 166 [8, 2], 167 [19, 4], 168 ], 169 }, 170 { 171 tokens: ["mo", "om"], 172 phrase: "MOZILLA MOZZARELLA MOMO", 173 expected: [ 174 [0, 2], 175 [8, 2], 176 [19, 4], 177 ], 178 }, 179 { 180 tokens: ["MO", "OM"], 181 phrase: "mozilla mozzarella momo", 182 expected: [ 183 [0, 2], 184 [8, 2], 185 [19, 4], 186 ], 187 }, 188 { 189 tokens: ["resume"], 190 phrase: "résumé", 191 expected: [[0, 6]], 192 }, 193 { 194 // This test should succeed even in a Spanish locale where N and Ñ are 195 // considered distinct letters. 196 tokens: ["jalapeno"], 197 phrase: "jalapeño", 198 expected: [[0, 8]], 199 }, 200 ]; 201 for (let { tokens, phrase, expected } of tests) { 202 tokens = tokens.map(t => ({ 203 value: t, 204 lowerCaseValue: t.toLocaleLowerCase(), 205 })); 206 Assert.deepEqual( 207 UrlbarUtils.getTokenMatches(tokens, phrase, UrlbarUtils.HIGHLIGHT.TYPED), 208 expected, 209 `Match "${tokens.map(t => t.value).join(", ")}" on "${phrase}"` 210 ); 211 } 212 }); 213 214 /** 215 * Tests suggestion highlighting. Note that suggestions are only highlighted if 216 * the matching token is at the beginning of a word in the matched string. 217 */ 218 add_task(function testSuggestions() { 219 const tests = [ 220 { 221 tokens: ["mozilla", "is", "i"], 222 phrase: "mozilla is for the Open Web", 223 expected: [ 224 [7, 1], 225 [10, 17], 226 ], 227 }, 228 { 229 tokens: ["\u9996"], 230 phrase: "Test \u9996\u9875 Test", 231 expected: [ 232 [0, 5], 233 [6, 6], 234 ], 235 }, 236 { 237 tokens: ["mo", "zilla"], 238 phrase: "mOzIlLa", 239 expected: [[2, 5]], 240 }, 241 { 242 tokens: ["MO", "ZILLA"], 243 phrase: "mozilla", 244 expected: [[2, 5]], 245 }, 246 { 247 tokens: [""], // Should never happen in practice. 248 phrase: "mozilla", 249 expected: [[0, 7]], 250 }, 251 { 252 tokens: ["mo", "om", "la"], 253 phrase: "mozilla mozzarella momo", 254 expected: [ 255 [2, 6], 256 [10, 9], 257 [21, 2], 258 ], 259 }, 260 { 261 tokens: ["mo", "om", "la"], 262 phrase: "MOZILLA MOZZARELLA MOMO", 263 expected: [ 264 [2, 6], 265 [10, 9], 266 [21, 2], 267 ], 268 }, 269 { 270 tokens: ["MO", "OM", "LA"], 271 phrase: "mozilla mozzarella momo", 272 expected: [ 273 [2, 6], 274 [10, 9], 275 [21, 2], 276 ], 277 }, 278 ]; 279 for (let { tokens, phrase, expected } of tests) { 280 tokens = tokens.map(t => ({ 281 value: t, 282 lowerCaseValue: t.toLocaleLowerCase(), 283 })); 284 Assert.deepEqual( 285 UrlbarUtils.getTokenMatches( 286 tokens, 287 phrase, 288 UrlbarUtils.HIGHLIGHT.SUGGESTED 289 ), 290 expected, 291 `Match "${tokens.map(t => t.value).join(", ")}" on "${phrase}"` 292 ); 293 } 294 }); 295 296 /** 297 * Tests ALL highlighting. 298 */ 299 add_task(function testAll() { 300 const tests = [ 301 { 302 tokens: [], 303 phrase: "mozilla is for the Open Web", 304 expected: [[0, "mozilla is for the Open Web".length]], 305 }, 306 { 307 tokens: undefined, 308 phrase: "mozilla is for the Open Web", 309 expected: [[0, "mozilla is for the Open Web".length]], 310 }, 311 ]; 312 for (let { tokens, phrase, expected } of tests) { 313 Assert.deepEqual( 314 UrlbarUtils.getTokenMatches(tokens, phrase, UrlbarUtils.HIGHLIGHT.ALL), 315 expected, 316 `Match "${tokens?.join(", ")}" on "${phrase}"` 317 ); 318 } 319 }); 320 321 /** 322 * Tests no tokens parameter for TYPED or SUGGESTED type. 323 */ 324 add_task(function testNoTokensWithTypedOrSuggetion() { 325 const tests = [ 326 { 327 tokens: [], 328 phrase: "mozilla is for the Open Web", 329 expected: [], 330 }, 331 { 332 tokens: undefined, 333 phrase: "mozilla is for the Open Web", 334 expected: [], 335 }, 336 ]; 337 for (let { tokens, phrase, expected } of tests) { 338 Assert.deepEqual( 339 UrlbarUtils.getTokenMatches(tokens, phrase, UrlbarUtils.HIGHLIGHT.TYPED), 340 expected, 341 `Match "${tokens?.join(", ")}" on "${phrase} for TYPED"` 342 ); 343 Assert.deepEqual( 344 UrlbarUtils.getTokenMatches( 345 tokens, 346 phrase, 347 UrlbarUtils.HIGHLIGHT.SUGGESTED 348 ), 349 expected, 350 `Match "${tokens?.join(", ")}" on "${phrase} for SUGGESTED"` 351 ); 352 } 353 });