summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_getInfo.js
blob: a9a2423d3e7b6f61a4620f2fda467cb481fcfee3 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
"use strict";

const { FieldScanner } = ChromeUtils.import(
  "resource://autofill/FormAutofillHeuristics.jsm"
);
var FormAutofillHeuristics, LabelUtils, FormAutofill;
add_task(async function() {
  ({ FormAutofillHeuristics } = ChromeUtils.import(
    "resource://autofill/FormAutofillHeuristics.jsm"
  ));
  ({ LabelUtils } = ChromeUtils.import(
    "resource://autofill/FormAutofillUtils.jsm"
  ));
  ({ FormAutofill } = ChromeUtils.import(
    "resource://autofill/FormAutofill.jsm"
  ));
});

const TESTCASES = [
  {
    description: "Input element in a label element",
    document: `<form>
                 <label> E-Mail
                   <input id="targetElement" type="text">
                 </label>
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description:
      "A label element is out of the form contains the related input",
    document: `<label for="targetElement"> E-Mail</label>
               <form>
                 <input id="targetElement" type="text">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "A label element contains span element",
    document: `<label for="targetElement">FOO<span>E-Mail</span>BAR</label>
               <form>
                 <input id="targetElement" type="text">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "The signature in 'name' attr of an input",
    document: `<input id="targetElement" name="email" type="text">`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "The signature in 'id' attr of an input",
    document: `<input id="targetElement_email" name="tel" type="text">`,
    elementId: "targetElement_email",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "Select element in a label element",
    document: `<form>
                 <label> State
                   <select id="targetElement"></select>
                 </label>
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "address-level1",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "A select element without a form wrapped",
    document: `<label for="targetElement">State</label>
               <select id="targetElement"></select>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "address-level1",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "address line input",
    document: `<label for="targetElement">street</label>
               <input id="targetElement" type="text">`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "street-address",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "CJK character - Traditional Chinese",
    document: `<label> 郵遞區號
                 <input id="targetElement" />
               </label>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "postal-code",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "CJK character - Japanese",
    document: `<label> 郵便番号
                 <input id="targetElement" />
               </label>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "postal-code",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "CJK character - Korean",
    document: `<label> 우편 번호
                 <input id="targetElement" />
               </label>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "postal-code",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "",
    document: `<input id="targetElement" name="fullname">`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "name",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: 'input element with "submit" type',
    document: `<input id="targetElement" type="submit" />`,
    elementId: "targetElement",
    expectedReturnValue: null,
  },
  {
    description: "The signature in 'name' attr of an email input",
    document: `<input id="targetElement" name="email" type="number">`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: 'input element with "email" type',
    document: `<input id="targetElement" type="email" />`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "email",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "Exclude United State string",
    document: `<label>United State
                 <input id="targetElement" />
               </label>`,
    elementId: "targetElement",
    expectedReturnValue: null,
  },
  {
    description: '"County" field with "United State" string',
    document: `<label>United State County
                 <input id="targetElement" />
               </label>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "address-level1",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: '"city" field with double "United State" string',
    document: `<label>United State united sTATE city
                 <input id="targetElement" />
               </label>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "address-level2",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: "Verify credit card number",
    document: `<form>
                 <label for="targetElement"> Card Number</label>
                 <input id="targetElement" type="text">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "cc-number",
      section: "",
      addressType: "",
      contactType: "",
      confidence: 1,
    },
  },
  {
    description: "Identify credit card type field",
    document: `<form>
                 <label for="targetElement">Card Type</label>
                 <input id="targetElement" type="text">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "cc-type",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: `Identify address field when contained in a form with autocomplete="off"`,
    document: `<form autocomplete="off">
                <input id="given-name">
               </form>`,
    elementId: "given-name",
    expectedReturnValue: {
      fieldName: "given-name",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: `Identify address field that has a placeholder but no label associated with it`,
    document: `<form>
                <input id="targetElement" placeholder="Name">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "name",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: `Identify address field that has a placeholder, no associated label, and its autocomplete attribute is "off"`,
    document: `<form>
                <input id="targetElement" placeholder="Address" autocomplete="off">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "street-address",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
  {
    description: `Identify address field that has a placeholder, no associated label, and the form's autocomplete attribute is "off"`,
    document: `<form autocomplete="off">
                <input id="targetElement" placeholder="Country">
               </form>`,
    elementId: "targetElement",
    expectedReturnValue: {
      fieldName: "country",
      section: "",
      addressType: "",
      contactType: "",
      confidence: null,
    },
  },
];

add_setup(async function() {
  Services.prefs.setStringPref(
    "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
    "1"
  );

  registerCleanupFunction(() => {
    Services.prefs.clearUserPref(
      "extensions.formautofill.creditCards.heuristics.fathom.testConfidence"
    );
  });
});

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

    let doc = MockDocument.createTestDocument(
      "http://localhost:8080/test/",
      testcase.document
    );

    let element = doc.getElementById(testcase.elementId);
    let value = FormAutofillHeuristics.getInfo(
      element,
      new FieldScanner([element], {})
    );

    Assert.deepEqual(value, testcase.expectedReturnValue);
    LabelUtils.clearLabelMap();
  });
});

add_task(async function test_regexp_list() {
  info("Verify the fieldName support for select element.");
  let SUPPORT_LIST = {
    email: null, // email
    "tel-extension": null, // tel-extension
    phone: null, // tel
    organization: null, // organization
    "street-address": null, // street-address
    address1: null, // address-line1
    address2: null, // address-line2
    address3: null, // address-line3
    city: "address-level2",
    region: "address-level1",
    "postal-code": null, // postal-code
    country: "country",
    fullname: null, // name
    fname: null, // given-name
    mname: null, // additional-name
    lname: null, // family-name
    cardholder: null, // cc-name
    "cc-number": null, // cc-number
    addmonth: "cc-exp-month",
    addyear: "cc-exp-year",
  };
  for (let label of Object.keys(SUPPORT_LIST)) {
    let testcase = {
      description: `A select element supports ${label} or not`,
      document: `<select id="${label}"></select>`,
      elementId: label,
      expectedReturnValue: SUPPORT_LIST[label]
        ? {
            fieldName: SUPPORT_LIST[label],
            section: "",
            addressType: "",
            contactType: "",
            confidence: null,
          }
        : null,
    };
    info(testcase.description);
    info(testcase.document);
    let doc = MockDocument.createTestDocument(
      "http://localhost:8080/test/",
      testcase.document
    );

    let element = doc.getElementById(testcase.elementId);
    let value = FormAutofillHeuristics.getInfo(
      element,
      new FieldScanner([element], {})
    );

    Assert.deepEqual(value, testcase.expectedReturnValue, label);
  }
  LabelUtils.clearLabelMap();
});

add_task(async function test_autofill_creditCards_autocomplete_off_pref() {
  let document = `<form autocomplete="off">
                    <label for="targetElement"> Card Number</label>
                    <input id="targetElement" type="text">
                  </form>`;
  let expected = null;
  info(`Set pref so that credit card autofill respects autocomplete="off"`);
  Services.prefs.setBoolPref(
    FormAutofill.AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF,
    false
  );
  let doc = MockDocument.createTestDocument(
    "http://localhost:8080/test/",
    document
  );
  let element = doc.getElementById("targetElement");
  let value = FormAutofillHeuristics.getInfo(
    element,
    new FieldScanner([element], {})
  );

  Assert.deepEqual(value, expected);
  document = `<form>
                <label for="targetElement"> Card Number</label>
                <input id="targetElement" type="text">
              </form>`;
  expected = {
    fieldName: "cc-number",
    section: "",
    addressType: "",
    contactType: "",
    confidence: 1,
  };
  info(
    `Set pref so that credit card autofill does not respect autocomplete="off"`
  );
  Services.prefs.setBoolPref(
    FormAutofill.AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF,
    true
  );
  doc = MockDocument.createTestDocument(
    "http://localhost:8080/test/",
    document
  );
  element = doc.getElementById("targetElement");
  value = FormAutofillHeuristics.getInfo(
    element,
    new FieldScanner([element], {})
  );

  Assert.deepEqual(value, expected);
  Services.prefs.clearUserPref(
    FormAutofill.AUTOFILL_CREDITCARDS_AUTOCOMPLETE_OFF_PREF
  );
});

add_task(async function test_autofill_addresses_autocomplete_off_pref() {
  let document = `<form autocomplete="off">
                    <input id="given-name">
                  </form>`;
  let expected = null;
  info(`Set pref so that address autofill respects autocomplete="off"`);
  Services.prefs.setBoolPref(
    FormAutofill.AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF,
    false
  );
  let doc = MockDocument.createTestDocument(
    "http://localhost:8080/test/",
    document
  );
  let element = doc.getElementById("given-name");
  let value = FormAutofillHeuristics.getInfo(
    element,
    new FieldScanner([element], {})
  );

  Assert.deepEqual(value, expected);
  document = `<form>
                <input id="given-name">
              </form>`;
  expected = {
    fieldName: "given-name",
    section: "",
    addressType: "",
    contactType: "",
    confidence: null,
  };
  info(`Set pref so that address autofill does not respect autocomplete="off"`);
  Services.prefs.setBoolPref(
    FormAutofill.AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF,
    true
  );
  doc = MockDocument.createTestDocument(
    "http://localhost:8080/test/",
    document
  );
  element = doc.getElementById("given-name");
  value = FormAutofillHeuristics.getInfo(
    element,
    new FieldScanner([element], {})
  );

  Assert.deepEqual(value, expected);
  Services.prefs.clearUserPref(
    FormAutofill.AUTOFILL_ADDRESSES_AUTOCOMPLETE_OFF_PREF
  );
});