summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_getCategoriesFromFieldNames.js
blob: 66f4c18ea96259a7b124553b3b4ff0396587cb37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use strict";

var FormAutofillUtils;
add_task(async function () {
  ({ FormAutofillUtils } = ChromeUtils.importESModule(
    "resource://gre/modules/shared/FormAutofillUtils.sys.mjs"
  ));
});

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)) {
    info("Starting testcase: " + fieldName);
    let field = TEST_CASES[fieldName];
    Assert.equal(
      FormAutofillUtils.isAddressField(fieldName),
      field.isAddressField,
      "isAddressField"
    );
    Assert.equal(
      FormAutofillUtils.isCreditCardField(fieldName),
      field.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);
  }
});