From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../formautofill/test/unit/test_createRecords.js | 525 +++++++++++++++++++++ 1 file changed, 525 insertions(+) create mode 100644 browser/extensions/formautofill/test/unit/test_createRecords.js (limited to 'browser/extensions/formautofill/test/unit/test_createRecords.js') diff --git a/browser/extensions/formautofill/test/unit/test_createRecords.js b/browser/extensions/formautofill/test/unit/test_createRecords.js new file mode 100644 index 0000000000..3d028e808e --- /dev/null +++ b/browser/extensions/formautofill/test/unit/test_createRecords.js @@ -0,0 +1,525 @@ +/* + * Test for the normalization of records created by FormAutofillHandler. + */ + +"use strict"; + +var FormAutofillHandler; +add_task(async function seutp() { + ({ FormAutofillHandler } = ChromeUtils.importESModule( + "resource://gre/modules/shared/FormAutofillHandler.sys.mjs" + )); +}); + +const TESTCASES = [ + { + description: + "Don't contain a field whose length of value is greater than 200", + document: `
+ + + + + + +
`, + formValue: { + "given-name": "John", + organization: "*".repeat(200), + "address-level1": "*".repeat(201), + country: "US", + "cc-number": "1111222233334444", + "cc-name": "*".repeat(201), + }, + expectedRecord: { + address: [ + { + "given-name": "John", + organization: "*".repeat(200), + "address-level1": "", + country: "US", + }, + ], + creditCard: [ + { + "cc-number": "1111222233334444", + "cc-name": "", + }, + ], + }, + }, + { + description: "Don't create address record if filled data is less than 3", + document: `
+ + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + }, + expectedRecord: { + address: [], + creditCard: [], + }, + }, + { + description: `"country" using @autocomplete shouldn't be identified aggressively`, + document: `
+ + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + country: "United States", + }, + expectedRecord: { + // "United States" is not a valid country, only country-name. See isRecordCreatable. + address: [], + creditCard: [], + }, + }, + { + description: `"country" using heuristics should be identified aggressively`, + document: `
+ + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + country: "United States", + }, + expectedRecord: { + address: [ + { + "given-name": "John", + organization: "Mozilla", + country: "US", + }, + ], + creditCard: [], + }, + }, + { + description: `"tel" related fields should be concatenated`, + document: `
+ + + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + "tel-country-code": "+1", + "tel-national": "1234567890", + }, + expectedRecord: { + address: [ + { + "given-name": "John", + organization: "Mozilla", + tel: "+11234567890", + }, + ], + creditCard: [], + }, + }, + { + description: `"tel" should be removed if it's too short`, + document: `
+ + + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + country: "US", + tel: "1234", + }, + expectedRecord: { + address: [ + { + "given-name": "John", + organization: "Mozilla", + country: "US", + tel: "", + }, + ], + creditCard: [], + }, + }, + { + description: `"tel" should be removed if it's too long`, + document: `
+ + + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + country: "US", + tel: "1234567890123456", + }, + expectedRecord: { + address: [ + { + "given-name": "John", + organization: "Mozilla", + country: "US", + tel: "", + }, + ], + creditCard: [], + }, + }, + { + description: `"tel" should be removed if it contains invalid characters`, + document: `
+ + + + +
`, + formValue: { + "given-name": "John", + organization: "Mozilla", + country: "US", + tel: "12345###!!!", + }, + expectedRecord: { + address: [ + { + "given-name": "John", + organization: "Mozilla", + country: "US", + tel: "", + }, + ], + creditCard: [], + }, + }, + { + description: "All name related fields should be counted as 1 field only.", + document: `
+ + + +
`, + formValue: { + "given-name": "John", + "family-name": "Doe", + organization: "Mozilla", + }, + expectedRecord: { + address: [], + creditCard: [], + }, + }, + { + description: + "All telephone related fields should be counted as 1 field only.", + document: `
+ + + + +
`, + formValue: { + "tel-country-code": "+1", + "tel-area-code": "123", + "tel-local": "4567890", + organization: "Mozilla", + }, + expectedRecord: { + address: [], + creditCard: [], + }, + }, + { + description: + "A credit card form with the value of cc-number, cc-exp, cc-name and cc-type.", + document: `
+ + + + +
`, + formValue: { + "cc-number": "5105105105105100", + "cc-name": "Foo Bar", + "cc-exp": "2022-06", + "cc-type": "Visa", + }, + expectedRecord: { + address: [], + creditCard: [ + { + "cc-number": "5105105105105100", + "cc-name": "Foo Bar", + "cc-exp": "2022-06", + "cc-type": "Visa", + "cc-exp-month": "6", + "cc-exp-year": "2022", + }, + ], + }, + }, + { + description: "A credit card form with cc-number value only.", + document: `
+ +
`, + formValue: { + "cc-number": "4111111111111111", + }, + expectedRecord: { + address: [], + creditCard: [ + { + "cc-number": "4111111111111111", + }, + ], + }, + }, + { + description: "A credit card form must have cc-number value.", + document: `
+ + + +
`, + formValue: { + "cc-number": "", + "cc-name": "Foo Bar", + "cc-exp": "2022-06", + }, + expectedRecord: { + address: [], + creditCard: [], + }, + }, + { + description: "A credit card form must have cc-number field.", + document: `
+ + +
`, + formValue: { + "cc-name": "Foo Bar", + "cc-exp": "2022-06", + }, + expectedRecord: { + address: [], + creditCard: [], + }, + }, + { + description: "A form with multiple sections", + document: `
+ + + + + + + + + + + + + + + + + + + +
`, + formValue: { + "given-name": "Bar", + organization: "Foo", + country: "US", + + "given-name-shipping": "John", + "family-name-shipping": "Doe", + "organization-shipping": "Mozilla", + "country-shipping": "US", + + "given-name-billing": "Foo", + "organization-billing": "Bar", + "country-billing": "US", + + "cc-number-section-one": "4111111111111111", + "cc-name-section-one": "John", + + "cc-number-section-two": "5105105105105100", + "cc-name-section-two": "Foo Bar", + "cc-exp-section-two": "2026-26", + }, + expectedRecord: { + address: [ + { + "given-name": "Bar", + organization: "Foo", + country: "US", + }, + { + "given-name": "John", + "family-name": "Doe", + organization: "Mozilla", + country: "US", + }, + { + "given-name": "Foo", + organization: "Bar", + country: "US", + }, + ], + creditCard: [ + { + "cc-number": "4111111111111111", + "cc-name": "John", + }, + { + "cc-number": "5105105105105100", + "cc-name": "Foo Bar", + "cc-exp": "2026-26", + }, + ], + }, + }, + { + description: "A credit card form with a cc-type select.", + document: `
+ + + +
`, + formValue: { + "cc-number": "5105105105105100", + }, + expectedRecord: { + address: [], + creditCard: [ + { + "cc-number": "5105105105105100", + "cc-type": "visa", + }, + ], + }, + }, + { + description: "A credit card form with a cc-type select from label.", + document: `
+ + + +
`, + formValue: { + "cc-number": "5105105105105100", + "cc-type": "A", + }, + expectedRecord: { + address: [], + creditCard: [ + { + "cc-number": "5105105105105100", + "cc-type": "amex", + }, + ], + }, + }, + { + description: + "A credit card form with separate expiry fields should have normalized expiry data.", + document: `
+ + + +
`, + formValue: { + "cc-number": "5105105105105100", + "cc-exp-month": "05", + "cc-exp-year": "26", + }, + expectedRecord: { + address: [], + creditCard: [ + { + "cc-number": "5105105105105100", + "cc-exp-month": "5", + "cc-exp-year": "2026", + }, + ], + }, + }, + { + description: + "A credit card form with combined expiry fields should have normalized expiry data.", + document: `
+ + +
`, + formValue: { + "cc-number": "5105105105105100", + "cc-exp": "07/27", + }, + expectedRecord: { + address: [], + creditCard: [ + { + "cc-number": "5105105105105100", + "cc-exp": "07/27", + "cc-exp-month": "7", + "cc-exp-year": "2027", + }, + ], + }, + }, +]; + +for (let testcase of TESTCASES) { + add_task(async function () { + info("Starting testcase: " + testcase.description); + + let doc = MockDocument.createTestDocument( + "http://localhost:8080/test/", + testcase.document + ); + let form = doc.querySelector("form"); + let formLike = FormLikeFactory.createFromForm(form); + let handler = new FormAutofillHandler(formLike); + + handler.collectFormFields(); + + for (let id in testcase.formValue) { + doc.getElementById(id).value = testcase.formValue[id]; + } + + let record = handler.createRecords(); + + let expectedRecord = testcase.expectedRecord; + for (let type in record) { + Assert.deepEqual( + record[type].map(secRecord => secRecord.record), + expectedRecord[type] + ); + } + }); +} -- cgit v1.2.3