summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_markAsAutofillField.js
blob: 72960fcaf2ff8ab82ec66f0b41b0839441a8ff62 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"use strict";

const TESTCASES = [
  {
    description: "Form containing 8 fields with autocomplete attribute.",
    document: `<form>
                 <input id="given-name" autocomplete="given-name">
                 <input id="additional-name" autocomplete="additional-name">
                 <input id="family-name" autocomplete="family-name">
                 <input id="street-addr" autocomplete="street-address">
                 <input id="city" autocomplete="address-level2">
                 <input id="country" autocomplete="country">
                 <input id="email" autocomplete="email">
                 <input id="tel" autocomplete="tel">
                 <input id="without-autocomplete-1">
                 <input id="without-autocomplete-2">
               </form>`,
    targetElementId: "given-name",
    expectedResult: [
      "given-name",
      "additional-name",
      "family-name",
      "street-addr",
      "city",
      "country",
      "email",
      "tel",
    ],
  },
  {
    description: "Form containing only 2 fields with autocomplete attribute.",
    document: `<form>
                 <input id="street-addr" autocomplete="street-address">
                 <input id="city" autocomplete="address-level2">
                 <input id="without-autocomplete-1">
                 <input id="without-autocomplete-2">
               </form>`,
    targetElementId: "street-addr",
    expectedResult: [],
  },
  {
    description: "Fields without form element.",
    document: `<input id="street-addr" autocomplete="street-address">
               <input id="city" autocomplete="address-level2">
               <input id="country" autocomplete="country">
               <input id="email" autocomplete="email">
               <input id="tel" autocomplete="tel">
               <input id="without-autocomplete-1">
               <input id="without-autocomplete-2">`,
    targetElementId: "street-addr",
    expectedResult: ["street-addr", "city", "country", "email", "tel"],
  },
  {
    description: "Form containing credit card autocomplete attributes.",
    document: `<form>
                <input id="cc-number" autocomplete="cc-number">
                <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>`,
    targetElementId: "cc-number",
    expectedResult: ["cc-number", "cc-name", "cc-exp-month", "cc-exp-year"],
  },
  {
    description:
      "Form containing multiple cc-number fields without autocomplete attributes.",
    document: `<form>
                <input id="cc-number1" maxlength="4">
                <input id="cc-number2" maxlength="4">
                <input id="cc-number3" maxlength="4">
                <input id="cc-number4" maxlength="4">
                <input id="cc-name">
                <input id="cc-exp-month">
                <input id="cc-exp-year">
               </form>`,
    targetElementId: "cc-number1",
    expectedResult: [
      "cc-number1",
      "cc-number2",
      "cc-number3",
      "cc-number4",
      "cc-name",
      "cc-exp-month",
      "cc-exp-year",
    ],
  },
  {
    description:
      "Invalid form containing three consecutive cc-number fields without autocomplete attributes.",
    document: `<form>
                <input id="cc-number1" maxlength="4">
                <input id="cc-number2" maxlength="4">
                <input id="cc-number3" maxlength="4">
               </form>`,
    targetElementId: "cc-number1",
    expectedResult: [],
    prefs: [
      [
        "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
        "1.0",
      ],
    ],
  },
  {
    description:
      "Invalid form containing five consecutive cc-number fields without autocomplete attributes.",
    document: `<form>
                <input id="cc-number1" maxlength="4">
                <input id="cc-number2" maxlength="4">
                <input id="cc-number3" maxlength="4">
                <input id="cc-number4" maxlength="4">
                <input id="cc-number5" maxlength="4">
               </form>`,
    targetElementId: "cc-number1",
    expectedResult: [],
    prefs: [
      [
        "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
        "1.0",
      ],
    ],
  },
  {
    description:
      "Valid form containing three consecutive cc-number fields without autocomplete attributes.",
    document: `<form>
                <input id="cc-number1" maxlength="4">
                <input id="cc-number2" maxlength="4">
                <input id="cc-number3" maxlength="4">
                <input id="cc-name">
                <input id="cc-exp-month">
                <input id="cc-exp-year">
               </form>`,
    targetElementId: "cc-number1",
    expectedResult: ["cc-number3", "cc-name", "cc-exp-month", "cc-exp-year"],
    prefs: [
      [
        "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
        "1.0",
      ],
    ],
  },
  {
    description:
      "Valid form containing five consecutive cc-number fields without autocomplete attributes.",
    document: `<form>
                <input id="cc-number1" maxlength="4">
                <input id="cc-number2" maxlength="4">
                <input id="cc-number3" maxlength="4">
                <input id="cc-number4" maxlength="4">
                <input id="cc-number5" maxlength="4">
                <input id="cc-name">
                <input id="cc-exp-month">
                <input id="cc-exp-year">
               </form>`,
    targetElementId: "cc-number1",
    expectedResult: ["cc-number5", "cc-name", "cc-exp-month", "cc-exp-year"],
  },
];

let markedFieldId = [];

var FormAutofillContent;
add_setup(async () => {
  ({ FormAutofillContent } = ChromeUtils.importESModule(
    "resource://autofill/FormAutofillContent.sys.mjs"
  ));

  FormAutofillContent._markAsAutofillField = function (field) {
    markedFieldId.push(field.id);
  };
});

TESTCASES.forEach(testcase => {
  add_task(async function () {
    info("Starting testcase: " + testcase.description);

    if (testcase.prefs) {
      testcase.prefs.forEach(pref => SetPref(pref[0], pref[1]));
    }

    markedFieldId = [];

    let doc = MockDocument.createTestDocument(
      "http://localhost:8080/test/",
      testcase.document
    );
    let element = doc.getElementById(testcase.targetElementId);
    FormAutofillContent.identifyAutofillFields(element);

    Assert.deepEqual(
      markedFieldId,
      testcase.expectedResult,
      "Check the fields were marked correctly."
    );

    if (testcase.prefs) {
      testcase.prefs.forEach(pref => Services.prefs.clearUserPref(pref[0]));
    }
  });
});