Bug 1367297 - Implement getCategoriesFromFieldNames, isAddressField and isCreditCardField in FormAutofillUtils.; r?lchang,steveck,ralin
MozReview-Commit-ID: 1pu69QXE2Xn
--- a/browser/extensions/formautofill/FormAutofillUtils.jsm
+++ b/browser/extensions/formautofill/FormAutofillUtils.jsm
@@ -6,16 +6,57 @@
this.EXPORTED_SYMBOLS = ["FormAutofillUtils"];
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
this.FormAutofillUtils = {
+ _fieldNameInfo: {
+ "name": "name",
+ "given-name": "name",
+ "additional-name": "name",
+ "family-name": "name",
+ "organization": "organization",
+ "street-address": "address",
+ "address-line1": "address",
+ "address-line2": "address",
+ "address-line3": "address",
+ "address-level1": "address",
+ "address-level2": "address",
+ "postal-code": "address",
+ "country": "address",
+ "tel": "tel",
+ "email": "email",
+ "cc-name": "creditCard",
+ "cc-number": "creditCard",
+ "cc-exp-month": "creditCard",
+ "cc-exp-year": "creditCard",
+ },
+
+ isAddressField(fieldName) {
+ return !!this._fieldNameInfo[fieldName] && !this.isCreditCardField(fieldName);
+ },
+
+ isCreditCardField(fieldName) {
+ return this._fieldNameInfo[fieldName] == "creditCard";
+ },
+
+ getCategoriesFromFieldNames(fieldNames) {
+ let categories = new Set();
+ for (let fieldName of fieldNames) {
+ let info = this._fieldNameInfo[fieldName];
+ if (info) {
+ categories.add(info);
+ }
+ }
+ return categories;
+ },
+
defineLazyLogGetter(scope, logPrefix) {
XPCOMUtils.defineLazyGetter(scope, "log", () => {
let ConsoleAPI = Cu.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
return new ConsoleAPI({
maxLogLevelPref: "extensions.formautofill.loglevel",
prefix: logPrefix,
});
});
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/unit/test_getCategoriesFromFieldNames.js
@@ -0,0 +1,78 @@
+
+"use strict";
+
+Cu.import("resource://formautofill/FormAutofillUtils.jsm");
+
+add_task(async function test_isAddressField_isCreditCardField() {
+ const TEST_CASES = {
+ "given-name": {
+ isAddressField: true,
+ isCreditCardField: false,
+ },
+ "organization": {
+ isAddressField: true,
+ isCreditCardField: false,
+ },
+ "address-line2": {
+ isAddressField: true,
+ isCreditCardField: false,
+ },
+ "tel": {
+ isAddressField: true,
+ isCreditCardField: false,
+ },
+ "email": {
+ isAddressField: true,
+ isCreditCardField: false,
+ },
+ "cc-number": {
+ isAddressField: false,
+ isCreditCardField: true,
+ },
+ "UNKNOWN": {
+ isAddressField: false,
+ isCreditCardField: false,
+ },
+ "": {
+ isAddressField: false,
+ isCreditCardField: false,
+ },
+ };
+
+ for (let fieldName of Object.keys(TEST_CASES)) {
+ do_print("Starting testcase: " + fieldName);
+ let info = TEST_CASES[fieldName];
+ Assert.equal(FormAutofillUtils.isAddressField(fieldName),
+ info.isAddressField,
+ "isAddressField");
+ Assert.equal(FormAutofillUtils.isCreditCardField(fieldName),
+ info.isCreditCardField,
+ "isCreditCardField");
+ }
+});
+
+add_task(async function test_getCategoriesFromFieldNames() {
+ const TEST_CASES = [
+ {
+ fieldNames: ["given-name", "family-name", "name", "tel", "organization"],
+ set: ["name", "tel", "organization"],
+ },
+ {
+ fieldNames: ["address-line2", "family-name", "name", "tel", "organization", "email"],
+ set: ["address", "name", "tel", "organization", "email"],
+ },
+ {
+ fieldNames: ["address-line2", "family-name", "", "name", "tel", "UNKOWN"],
+ set: ["address", "name", "tel"],
+ },
+ {
+ fieldNames: ["tel", "family-name", "", "name", "tel", "UNKOWN"],
+ set: ["tel", "name"],
+ },
+ ];
+
+ for (let tc of TEST_CASES) {
+ let categories = FormAutofillUtils.getCategoriesFromFieldNames(tc.fieldNames);
+ Assert.deepEqual(Array.from(categories), tc.set);
+ }
+});
--- a/browser/extensions/formautofill/test/unit/xpcshell.ini
+++ b/browser/extensions/formautofill/test/unit/xpcshell.ini
@@ -17,16 +17,17 @@ support-files =
[heuristics/third_party/test_Staples.js]
[heuristics/third_party/test_Walmart.js]
[test_addressRecords.js]
[test_autofillFormFields.js]
[test_collectFormFields.js]
[test_creditCardRecords.js]
[test_enabledStatus.js]
[test_findLabelElements.js]
+[test_getCategoriesFromFieldNames.js]
[test_getFormInputDetails.js]
[test_isCJKName.js]
[test_markAsAutofillField.js]
[test_nameUtils.js]
[test_onFormSubmitted.js]
[test_profileAutocompleteResult.js]
[test_savedFieldNames.js]
[test_transformFields.js]