--- a/browser/extensions/formautofill/test/unit/test_collectFormFields.js
+++ b/browser/extensions/formautofill/test/unit/test_collectFormFields.js
@@ -17,20 +17,25 @@ const TESTCASES = [
{"section": "", "addressType": "", "contactType": "", "fieldName": "family-name"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "address-line1"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "address-level2"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "country"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "email"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
],
creditCardFieldDetails: [],
- isValidForm: {
- address: true,
- creditCard: false,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "address-line1"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "address-level2"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "country"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "email"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
+ ],
ids: ["given-name", "family-name", "street-addr", "city", "country", "email", "phone"],
},
{
description: "An address and credit card form with autocomplete properties and 1 token",
document: `<form>
<input id="given-name" autocomplete="given-name">
<input id="family-name" autocomplete="family-name">
<input id="street-addr" autocomplete="street-address">
@@ -53,20 +58,29 @@ const TESTCASES = [
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
],
creditCardFieldDetails: [
{"section": "", "addressType": "", "contactType": "", "fieldName": "cc-number"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "cc-name"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-month"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-year"},
],
- isValidForm: {
- address: true,
- creditCard: true,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "street-address"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "address-level2"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "country"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "email"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-number"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-name"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-month"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-year"},
+ ],
},
{
description: "An address form with autocomplete properties and 2 tokens",
document: `<form><input id="given-name" autocomplete="shipping given-name">
<input id="family-name" autocomplete="shipping family-name">
<input id="street-addr" autocomplete="shipping street-address">
<input id="city" autocomplete="shipping address-level2">
<input id="country" autocomplete="shipping country">
@@ -77,20 +91,25 @@ const TESTCASES = [
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level2"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "country"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
],
creditCardFieldDetails: [],
- isValidForm: {
- address: true,
- creditCard: false,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level2"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "country"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
+ ],
},
{
description: "Form with autocomplete properties and profile is partly matched",
document: `<form><input id="given-name" autocomplete="shipping given-name">
<input id="family-name" autocomplete="shipping family-name">
<input id="street-addr" autocomplete="shipping street-address">
<input autocomplete="shipping address-level2">
<select autocomplete="shipping country"></select>
@@ -101,20 +120,25 @@ const TESTCASES = [
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level2"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "country"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
],
creditCardFieldDetails: [],
- isValidForm: {
- address: true,
- creditCard: false,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level2"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "country"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
+ ],
},
{
description: "It's a valid address and credit card form.",
document: `<form>
<input id="given-name" autocomplete="shipping given-name">
<input id="family-name" autocomplete="shipping family-name">
<input id="street-addr" autocomplete="shipping street-address">
<input id="cc-number" autocomplete="shipping cc-number">
@@ -122,43 +146,35 @@ const TESTCASES = [
addressFieldDetails: [
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
],
creditCardFieldDetails: [
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "cc-number"},
],
- isValidForm: {
- address: true,
- creditCard: true,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "cc-number"},
+ ],
},
{
description: "It's an invalid address and credit form.",
document: `<form>
<input id="given-name" autocomplete="shipping given-name">
<input autocomplete="shipping address-level2">
<input id="cc-name" autocomplete="cc-name">
<input id="cc-exp-month" autocomplete="cc-exp-month">
<input id="cc-exp-year" autocomplete="cc-exp-year">
</form>`,
- addressFieldDetails: [
- {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
- {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level2"},
- ],
- creditCardFieldDetails: [
- {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-name"},
- {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-month"},
- {"section": "", "addressType": "", "contactType": "", "fieldName": "cc-exp-year"},
- ],
- isValidForm: {
- address: false,
- creditCard: false,
- },
+ addressFieldDetails: [],
+ creditCardFieldDetails: [],
+ validFieldDetails: [],
},
{
description: "Three sets of adjacent phone number fields",
document: `<form>
<input id="shippingAreaCode" autocomplete="shipping tel" maxlength="3">
<input id="shippingPrefix" autocomplete="shipping tel" maxlength="3">
<input id="shippingSuffix" autocomplete="shipping tel" maxlength="4">
<input id="shippingTelExt" autocomplete="shipping tel-extension">
@@ -181,20 +197,29 @@ const TESTCASES = [
{"section": "", "addressType": "billing", "contactType": "", "fieldName": "tel-local-prefix"},
{"section": "", "addressType": "billing", "contactType": "", "fieldName": "tel-local-suffix"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel-country-code"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel-area-code"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel-local-prefix"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel-local-suffix"},
],
creditCardFieldDetails: [],
- isValidForm: {
- address: true,
- creditCard: false,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-area-code"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-local-prefix"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-local-suffix"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-extension"},
+ {"section": "", "addressType": "billing", "contactType": "", "fieldName": "tel-area-code"},
+ {"section": "", "addressType": "billing", "contactType": "", "fieldName": "tel-local-prefix"},
+ {"section": "", "addressType": "billing", "contactType": "", "fieldName": "tel-local-suffix"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-country-code"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-area-code"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-local-prefix"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel-local-suffix"},
+ ],
ids: [
"shippingAreaCode", "shippingPrefix", "shippingSuffix", "shippingTelExt",
"billingAreaCode", "billingPrefix", "billingSuffix",
"otherCountryCode", "otherAreaCode", "otherPrefix", "otherSuffix",
],
},
{
description: "Dedup the same field names of the different telephone fields.",
@@ -211,20 +236,23 @@ const TESTCASES = [
addressFieldDetails: [
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
{"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
],
creditCardFieldDetails: [],
- isValidForm: {
- address: true,
- creditCard: false,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
+ {"section": "", "addressType": "", "contactType": "", "fieldName": "tel"},
+ ],
ids: ["i1", "i2", "i3", "i4", "homePhone"],
},
{
description: "The duplicated phones of a single one and a set with ac, prefix, suffix.",
document: `<form>
<input id="i1" autocomplete="shipping given-name">
<input id="i2" autocomplete="shipping family-name">
<input id="i3" autocomplete="shipping street-address">
@@ -244,63 +272,79 @@ const TESTCASES = [
// this case. We can see if there is any better solution later.
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-area-code"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-local-prefix"},
{"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-local-suffix"},
],
creditCardFieldDetails: [],
- isValidForm: {
- address: true,
- creditCard: false,
- },
+ validFieldDetails: [
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "given-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "family-name"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-area-code"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-local-prefix"},
+ {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel-local-suffix"},
+ ],
ids: ["i1", "i2", "i3", "i4", "singlePhone",
"shippingAreaCode", "shippingPrefix", "shippingSuffix"],
},
];
for (let tc of TESTCASES) {
(function() {
let testcase = tc;
add_task(async function() {
do_print("Starting testcase: " + testcase.description);
let doc = MockDocument.createTestDocument("http://localhost:8080/test/",
testcase.document);
let form = doc.querySelector("form");
let formLike = FormLikeFactory.createFromForm(form);
- Array.of(
- ...testcase.addressFieldDetails,
- ...testcase.creditCardFieldDetails
- ).forEach((detail, index) => {
- let elementRef;
- if (testcase.ids && testcase.ids[index]) {
- elementRef = doc.getElementById(testcase.ids[index]);
- } else {
- elementRef = doc.querySelector("*[autocomplete*='" + detail.fieldName + "']");
+ function setElementWeakRef(details) {
+ if (!details) {
+ return;
}
- detail.elementWeakRef = Cu.getWeakReference(elementRef);
- });
- let handler = new FormAutofillHandler(formLike);
- handler.collectFormFields();
+ details.forEach((detail, index) => {
+ let elementRef;
+ if (testcase.ids && testcase.ids[index]) {
+ elementRef = doc.getElementById(testcase.ids[index]);
+ } else {
+ elementRef = doc.querySelector("*[autocomplete*='" + detail.fieldName + "']");
+ }
+ detail.elementWeakRef = Cu.getWeakReference(elementRef);
+ });
+ }
function verifyDetails(handlerDetails, testCaseDetails) {
+ if (handlerDetails === null) {
+ Assert.equal(handlerDetails, testCaseDetails);
+ return;
+ }
Assert.equal(handlerDetails.length, testCaseDetails.length);
handlerDetails.forEach((detail, index) => {
Assert.equal(detail.fieldName, testCaseDetails[index].fieldName, "fieldName");
Assert.equal(detail.section, testCaseDetails[index].section, "section");
Assert.equal(detail.addressType, testCaseDetails[index].addressType, "addressType");
Assert.equal(detail.contactType, testCaseDetails[index].contactType, "contactType");
Assert.equal(detail.elementWeakRef.get(), testCaseDetails[index].elementWeakRef.get(), "DOM reference");
});
}
+ [
+ testcase.addressFieldDetails,
+ testcase.creditCardFieldDetails,
+ testcase.validFieldDetails,
+ ].forEach(details => setElementWeakRef(details));
- verifyDetails(handler.addressFieldDetails, testcase.addressFieldDetails);
- verifyDetails(handler.creditCardFieldDetails, testcase.creditCardFieldDetails);
+ let handler = new FormAutofillHandler(formLike);
+ let validFieldDetails = handler.collectFormFields();
- Assert.equal(handler.isValidAddressForm, testcase.isValidForm.address, "Valid Address Form Checking");
- Assert.equal(handler.isValidCreditCardForm, testcase.isValidForm.creditCard, "Valid Credit Card Form Checking");
+ verifyDetails(handler.address.fieldDetails, testcase.addressFieldDetails);
+ verifyDetails(handler.creditCard.fieldDetails, testcase.creditCardFieldDetails);
+ verifyDetails(validFieldDetails, testcase.validFieldDetails);
});
})();
}