test_profileAutocompleteResult.js (14925B)
1 "use strict"; 2 3 var AddressResult, CreditCardResult; 4 add_setup(async () => { 5 ({ AddressResult, CreditCardResult } = ChromeUtils.importESModule( 6 "resource://autofill/ProfileAutoCompleteResult.sys.mjs" 7 )); 8 }); 9 10 let matchingProfiles = [ 11 { 12 guid: "test-guid-1", 13 "given-name": "Timothy", 14 "family-name": "Berners-Lee", 15 name: "Timothy Berners-Lee", 16 organization: "Sesame Street", 17 "street-address": "123 Sesame Street.", 18 "address-line1": "123 Sesame Street.", 19 tel: "1-345-345-3456.", 20 }, 21 { 22 guid: "test-guid-2", 23 "given-name": "John", 24 "family-name": "Doe", 25 name: "John Doe", 26 organization: "Mozilla", 27 "street-address": "331 E. Evelyn Avenue", 28 "address-line1": "331 E. Evelyn Avenue", 29 tel: "1-650-903-0800", 30 }, 31 { 32 guid: "test-guid-3", 33 organization: "", 34 "street-address": "321, No Name St. 2nd line 3rd line", 35 "-moz-street-address-one-line": "321, No Name St. 2nd line 3rd line", 36 "address-line1": "321, No Name St.", 37 "address-line2": "2nd line", 38 "address-line3": "3rd line", 39 tel: "1-000-000-0000", 40 }, 41 ]; 42 43 let allFieldNames = [ 44 "given-name", 45 "family-name", 46 "street-address", 47 "address-line1", 48 "address-line2", 49 "address-line3", 50 "organization", 51 "tel", 52 ]; 53 54 function makeAddressComment({ primary, secondary, status, profile }) { 55 return JSON.stringify({ 56 primary, 57 secondary, 58 status, 59 ariaLabel: primary + " " + secondary + " " + status, 60 fillMessageName: "FormAutofill:FillForm", 61 fillMessageData: { profile }, 62 }); 63 } 64 65 function makeCreditCardComment({ 66 primary, 67 secondary, 68 ariaLabel, 69 image, 70 profile, 71 }) { 72 return JSON.stringify({ 73 primary, 74 secondary, 75 ariaLabel, 76 image, 77 fillMessageName: "FormAutofill:FillForm", 78 fillMessageData: { profile }, 79 }); 80 } 81 82 let addressTestCases = [ 83 { 84 description: "Focus on an `organization` field", 85 options: {}, 86 matchingProfiles, 87 filledCategories: [ 88 ["address", "name", "tel"], 89 ["address", "name", "tel"], 90 ], 91 allFieldNames, 92 searchString: "", 93 fieldDetail: { fieldName: "organization" }, 94 expected: { 95 searchResult: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 96 defaultIndex: 0, 97 items: [ 98 { 99 value: "", 100 style: "autofill", 101 label: "Sesame Street", 102 comment: makeAddressComment({ 103 primary: "Sesame Street", 104 secondary: "123 Sesame Street.", 105 status: "Also autofills address, name, phone", 106 profile: matchingProfiles[0], 107 }), 108 image: "", 109 }, 110 { 111 value: "", 112 style: "autofill", 113 label: "Mozilla", 114 comment: makeAddressComment({ 115 primary: "Mozilla", 116 secondary: "331 E. Evelyn Avenue", 117 status: "Also autofills address, name, phone", 118 profile: matchingProfiles[1], 119 }), 120 image: "", 121 }, 122 ], 123 }, 124 }, 125 { 126 description: "Focus on an `tel` field", 127 options: {}, 128 matchingProfiles, 129 filledCategories: [ 130 ["address", "name", "tel", "organization"], 131 ["address", "name", "tel", "organization"], 132 ["address", "tel"], 133 ], 134 allFieldNames, 135 searchString: "", 136 fieldDetail: { fieldName: "tel" }, 137 expected: { 138 searchResult: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 139 defaultIndex: 0, 140 items: [ 141 { 142 value: "", 143 style: "autofill", 144 label: "1-345-345-3456.", 145 comment: makeAddressComment({ 146 primary: "1-345-345-3456.", 147 secondary: "123 Sesame Street.", 148 status: "Also autofills address, name, organization", 149 profile: matchingProfiles[0], 150 }), 151 image: "", 152 }, 153 { 154 value: "", 155 style: "autofill", 156 label: "1-650-903-0800", 157 comment: makeAddressComment({ 158 primary: "1-650-903-0800", 159 secondary: "331 E. Evelyn Avenue", 160 status: "Also autofills address, name, organization", 161 profile: matchingProfiles[1], 162 }), 163 image: "", 164 }, 165 { 166 value: "", 167 style: "autofill", 168 label: "1-000-000-0000", 169 comment: makeAddressComment({ 170 primary: "1-000-000-0000", 171 secondary: "321, No Name St. 2nd line 3rd line", 172 status: "Also autofills address", 173 profile: matchingProfiles[2], 174 }), 175 image: "", 176 }, 177 ], 178 }, 179 }, 180 { 181 description: "Focus on an `street-address` field", 182 options: {}, 183 matchingProfiles, 184 filledCategories: [ 185 ["address", "name", "tel", "organization"], 186 ["address", "name", "tel", "organization"], 187 ["address", "tel"], 188 ], 189 allFieldNames, 190 searchString: "", 191 fieldDetail: { fieldName: "street-address" }, 192 expected: { 193 searchResult: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 194 defaultIndex: 0, 195 items: [ 196 { 197 value: "", 198 style: "autofill", 199 label: "123 Sesame Street.", 200 comment: makeAddressComment({ 201 primary: "123 Sesame Street.", 202 secondary: "Timothy Berners-Lee", 203 status: "Also autofills name, organization, phone", 204 profile: matchingProfiles[0], 205 }), 206 image: "", 207 }, 208 { 209 value: "", 210 style: "autofill", 211 label: "331 E. Evelyn Avenue", 212 comment: makeAddressComment({ 213 primary: "331 E. Evelyn Avenue", 214 secondary: "John Doe", 215 status: "Also autofills name, organization, phone", 216 profile: matchingProfiles[1], 217 }), 218 image: "", 219 }, 220 { 221 value: "", 222 style: "autofill", 223 label: "321, No Name St. 2nd line 3rd line", 224 comment: makeAddressComment({ 225 primary: "321, No Name St. 2nd line 3rd line", 226 secondary: "1-000-000-0000", 227 status: "Also autofills phone", 228 profile: matchingProfiles[2], 229 }), 230 image: "", 231 }, 232 ], 233 }, 234 }, 235 { 236 description: "Focus on an `address-line1` field", 237 options: {}, 238 matchingProfiles, 239 filledCategories: [ 240 ["address", "name", "tel", "organization"], 241 ["address", "name", "tel", "organization"], 242 ["address", "tel"], 243 ], 244 allFieldNames, 245 searchString: "", 246 fieldDetail: { fieldName: "address-line1" }, 247 expected: { 248 searchResult: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 249 defaultIndex: 0, 250 items: [ 251 { 252 value: "", 253 style: "autofill", 254 label: "123 Sesame Street.", 255 comment: makeAddressComment({ 256 primary: "123 Sesame Street.", 257 secondary: "Timothy Berners-Lee", 258 status: "Also autofills name, organization, phone", 259 profile: matchingProfiles[0], 260 }), 261 image: "", 262 }, 263 { 264 value: "", 265 style: "autofill", 266 label: "331 E. Evelyn Avenue", 267 comment: makeAddressComment({ 268 primary: "331 E. Evelyn Avenue", 269 secondary: "John Doe", 270 status: "Also autofills name, organization, phone", 271 profile: matchingProfiles[1], 272 }), 273 image: "", 274 }, 275 { 276 value: "", 277 style: "autofill", 278 label: "321, No Name St.", 279 comment: makeAddressComment({ 280 primary: "321, No Name St.", 281 secondary: "1-000-000-0000", 282 status: "Also autofills phone", 283 profile: matchingProfiles[2], 284 }), 285 image: "", 286 }, 287 ], 288 }, 289 }, 290 { 291 description: "No matching profiles", 292 options: {}, 293 matchingProfiles: [], 294 allFieldNames, 295 searchString: "", 296 fieldDetail: { fieldName: "" }, 297 expected: { 298 searchResult: Ci.nsIAutoCompleteResult.RESULT_NOMATCH, 299 defaultIndex: 0, 300 items: [], 301 }, 302 }, 303 { 304 description: "Search with failure", 305 options: { resultCode: Ci.nsIAutoCompleteResult.RESULT_FAILURE }, 306 matchingProfiles: [], 307 allFieldNames, 308 searchString: "", 309 fieldDetail: { fieldName: "" }, 310 expected: { 311 searchResult: Ci.nsIAutoCompleteResult.RESULT_FAILURE, 312 defaultIndex: 0, 313 items: [], 314 }, 315 }, 316 ]; 317 318 matchingProfiles = [ 319 { 320 guid: "test-guid-1", 321 "cc-name": "Timothy Berners-Lee", 322 "cc-number": "************6785", 323 "cc-exp-month": 12, 324 "cc-exp-year": 2014, 325 "cc-type": "visa", 326 }, 327 { 328 guid: "test-guid-2", 329 "cc-name": "John Doe", 330 "cc-number": "************1234", 331 "cc-exp-month": 4, 332 "cc-exp-year": 2014, 333 "cc-type": "amex", 334 }, 335 { 336 guid: "test-guid-3", 337 "cc-number": "************5678", 338 "cc-exp-month": 8, 339 "cc-exp-year": 2018, 340 }, 341 ]; 342 343 allFieldNames = ["cc-name", "cc-number", "cc-exp-month", "cc-exp-year"]; 344 345 let creditCardTestCases = [ 346 { 347 description: "Focus on a `cc-name` field", 348 options: {}, 349 matchingProfiles, 350 allFieldNames, 351 searchString: "", 352 fieldDetail: { fieldName: "cc-name" }, 353 expected: { 354 searchResult: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 355 defaultIndex: 0, 356 items: [ 357 { 358 value: "", 359 style: "autofill", 360 label: "Timothy Berners-Lee", 361 comment: makeCreditCardComment({ 362 primary: "Timothy Berners-Lee", 363 secondary: "••••6785", 364 ariaLabel: "Visa Timothy Berners-Lee ****6785", 365 image: "chrome://formautofill/content/third-party/cc-logo-visa.svg", 366 profile: matchingProfiles[0], 367 }), 368 image: "chrome://formautofill/content/third-party/cc-logo-visa.svg", 369 }, 370 { 371 value: "", 372 style: "autofill", 373 label: "John Doe", 374 comment: makeCreditCardComment({ 375 primary: "John Doe", 376 secondary: "••••1234", 377 ariaLabel: "American Express John Doe ****1234", 378 image: "chrome://formautofill/content/third-party/cc-logo-amex.png", 379 profile: matchingProfiles[1], 380 }), 381 image: "chrome://formautofill/content/third-party/cc-logo-amex.png", 382 }, 383 ], 384 }, 385 }, 386 { 387 description: "Focus on a `cc-number` field", 388 options: {}, 389 matchingProfiles, 390 allFieldNames, 391 searchString: "", 392 fieldDetail: { fieldName: "cc-number" }, 393 expected: { 394 searchResult: Ci.nsIAutoCompleteResult.RESULT_SUCCESS, 395 defaultIndex: 0, 396 items: [ 397 { 398 value: "", 399 style: "autofill", 400 label: "••••6785", 401 comment: makeCreditCardComment({ 402 primary: "••••6785", 403 secondary: "Timothy Berners-Lee", 404 ariaLabel: "Visa 6785 Timothy Berners-Lee", 405 image: "chrome://formautofill/content/third-party/cc-logo-visa.svg", 406 profile: matchingProfiles[0], 407 }), 408 image: "chrome://formautofill/content/third-party/cc-logo-visa.svg", 409 }, 410 { 411 value: "", 412 style: "autofill", 413 label: "••••1234", 414 comment: makeCreditCardComment({ 415 primary: "••••1234", 416 secondary: "John Doe", 417 ariaLabel: "American Express 1234 John Doe", 418 image: "chrome://formautofill/content/third-party/cc-logo-amex.png", 419 profile: matchingProfiles[1], 420 }), 421 image: "chrome://formautofill/content/third-party/cc-logo-amex.png", 422 }, 423 { 424 value: "", 425 style: "autofill", 426 label: "••••5678", 427 comment: makeCreditCardComment({ 428 primary: "••••5678", 429 secondary: "", 430 ariaLabel: "5678", 431 image: "chrome://formautofill/content/icon-credit-card-generic.svg", 432 profile: matchingProfiles[2], 433 }), 434 image: "chrome://formautofill/content/icon-credit-card-generic.svg", 435 }, 436 ], 437 }, 438 }, 439 { 440 description: "No matching profiles", 441 options: {}, 442 matchingProfiles: [], 443 allFieldNames, 444 searchString: "", 445 fieldDetail: { fieldName: "" }, 446 expected: { 447 searchResult: Ci.nsIAutoCompleteResult.RESULT_NOMATCH, 448 defaultIndex: 0, 449 items: [], 450 }, 451 }, 452 { 453 description: "Search with failure", 454 options: { resultCode: Ci.nsIAutoCompleteResult.RESULT_FAILURE }, 455 matchingProfiles: [], 456 allFieldNames, 457 searchString: "", 458 fieldDetail: { fieldName: "" }, 459 expected: { 460 searchResult: Ci.nsIAutoCompleteResult.RESULT_FAILURE, 461 defaultIndex: 0, 462 items: [], 463 }, 464 }, 465 ]; 466 467 add_task(async function test_all_patterns() { 468 let testSets = [ 469 { 470 collectionConstructor: AddressResult, 471 testCases: addressTestCases, 472 }, 473 { 474 collectionConstructor: CreditCardResult, 475 testCases: creditCardTestCases, 476 }, 477 ]; 478 479 testSets.forEach(({ collectionConstructor, testCases }) => { 480 testCases.forEach(testCase => { 481 info("Starting testcase: " + testCase.description); 482 let actual = new collectionConstructor( 483 testCase.searchString, 484 testCase.fieldDetail, 485 testCase.allFieldNames, 486 testCase.matchingProfiles, 487 testCase.filledCategories, 488 testCase.options 489 ); 490 let expectedValue = testCase.expected; 491 let expectedItemLength = expectedValue.items.length; 492 // If the last item shows up as a footer, we expect one more item 493 // than expected. 494 if (actual.getStyleAt(actual.matchCount - 1) == "action") { 495 expectedItemLength++; 496 } 497 // Add one row for the status. 498 if ( 499 actual.matchCount > 2 && 500 actual.getStyleAt(actual.matchCount - 2) == "status" 501 ) { 502 expectedItemLength++; 503 } 504 505 equal(actual.searchResult, expectedValue.searchResult); 506 equal(actual.defaultIndex, expectedValue.defaultIndex); 507 equal(actual.matchCount, expectedItemLength); 508 expectedValue.items.forEach((item, index) => { 509 equal(actual.getValueAt(index), item.value); 510 Assert.deepEqual( 511 JSON.parse(actual.getCommentAt(index)), 512 JSON.parse(item.comment) 513 ); 514 equal(actual.getLabelAt(index), item.label); 515 equal(actual.getStyleAt(index), item.style); 516 equal(actual.getImageAt(index), item.image); 517 }); 518 519 if (expectedValue.items.length) { 520 Assert.throws( 521 () => actual.getValueAt(expectedItemLength), 522 /Index out of range\./ 523 ); 524 525 Assert.throws( 526 () => actual.getLabelAt(expectedItemLength), 527 /Index out of range\./ 528 ); 529 530 Assert.throws( 531 () => actual.getCommentAt(expectedItemLength), 532 /Index out of range\./ 533 ); 534 } 535 }); 536 }); 537 });