summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/creditCard/browser_creditCard_heuristics.js
blob: 380123923438258fb7a04c4a323cf1e45d905720 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TESTCASES = [
  {
    description: "@autocomplete - all fields in the same form",
    document: `<form>
                <input id="cc-number" autocomplete="cc-number">
                <input id="cc-name" autocomplete="cc-name">
                <input id="cc-exp" autocomplete="cc-exp">
               </form>`,
    idsToShowPopup: ["cc-number", "cc-name", "cc-exp"],
  },
  {
    description: "without @autocomplete - all fields in the same form",
    document: `<form>
                <input id="cc-number" placeholder="credit card number">
                <input id="cc-name" placeholder="credit card holder name">
                <input id="cc-exp" placeholder="expiration date">
               </form>`,
    idsToShowPopup: ["cc-number", "cc-name", "cc-exp"],
  },
  {
    description: "@autocomplete - each field in its own form",
    document: `<form><input id="cc-number" autocomplete="cc-number"></form>
               <form><input id="cc-name" autocomplete="cc-name"></form>
               <form><input id="cc-exp" autocomplete="cc-exp"></form>`,
    idsToShowPopup: ["cc-number", "cc-name", "cc-exp"],
  },
  {
    description:
      "without @autocomplete - each field in its own form (high-confidence cc-number & cc-name)",
    document: `<form><input id="cc-number" placeholder="credit card number"></form>
               <form><input id="cc-name" placeholder="credit card holder name"></form>
               <form><input id="cc-exp" placeholder="expiration date"></form>`,
    prefs: [
      [
        "extensions.formautofill.creditCards.heuristics.fathom.highConfidenceThreshold",
        "0.9",
      ],
      [
        "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
        "0.95",
      ],
    ],
    idsToShowPopup: ["cc-number", "cc-name"],
    idsWithNoPopup: ["cc-exp"],
  },
  {
    description:
      "without @autocomplete - each field in its own form (normal-confidence cc-number & cc-name)",
    document: `<form><input id="cc-number" placeholder="credit card number"></form>
               <form><input id="cc-name" placeholder="credit card holder name"></form>
               <form><input id="cc-exp" placeholder="expiration date"></form>`,
    prefs: [
      [
        "extensions.formautofill.creditCards.heuristics.fathom.highConfidenceThreshold",
        "0.9",
      ],
      [
        "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
        "0.8",
      ],
    ],
    idsWithNoPopup: ["cc-number", "cc-name", "cc-exp"],
  },
  {
    description:
      "with @autocomplete - cc-number/cc-name and another <input> in a form",
    document: `<form>
                 <input id="cc-number" autocomplete="cc-number">
                 <input id="password" type="password">
               </form>
               <form>
                 <input id="cc-name" autocomplete="cc-name">
                 <input id="password" type="password">
               </form>`,
    idsToShowPopup: ["cc-number", "cc-name"],
  },
  {
    description:
      "without @autocomplete - high-confidence cc-number/cc-name and another <input> in a form",
    document: `<form>
                 <input id="cc-number" placeholder="credit card number">
                 <input id="password" type="password">
               </form>
               <form>
                 <input id="cc-name" placeholder="credit card holder name">
                 <input id="password" type="password">
               </form>`,
    idsWithNoPopup: ["cc-number", "cc-name"],
  },
  {
    description:
      "without @autocomplete - high-confidence cc-number/cc-name and another hidden <input> in a form",
    document: `<form>
                 <input id="cc-number" placeholder="credit card number">
                 <input id="token" type="hidden">
               </form>
               <form>
                 <input id="cc-name" placeholder="credit card holder name">
                 <input id="token" type="hidden">
               </form>`,
    prefs: [
      [
        "extensions.formautofill.creditCards.heuristics.fathom.highConfidenceThreshold",
        "0.9",
      ],
      [
        "extensions.formautofill.creditCards.heuristics.fathom.testConfidence",
        "0.95",
      ],
    ],
    idsToShowPopup: ["cc-number", "cc-name"],
  },
];

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.formautofill.creditCards.supported", "on"],
      ["extensions.formautofill.creditCards.enabled", true],
    ],
  });

  await setStorage(TEST_CREDIT_CARD_1);
});

add_task(async function test_heuristics() {
  for (const TEST of TESTCASES) {
    info(`Test ${TEST.description}`);
    if (TEST.prefs) {
      await SpecialPowers.pushPrefEnv({ set: TEST.prefs });
    }

    await BrowserTestUtils.withNewTab(EMPTY_URL, async function (browser) {
      await SpecialPowers.spawn(browser, [TEST.document], doc => {
        // eslint-disable-next-line no-unsanitized/property
        content.document.body.innerHTML = doc;
      });

      await SimpleTest.promiseFocus(browser);

      let ids = TEST.idsToShowPopup ?? [];
      for (const id of ids) {
        await runAndWaitForAutocompletePopupOpen(browser, async () => {
          await focusAndWaitForFieldsIdentified(browser, `#${id}`);
        });
        ok(true, `popup is opened on <input id=${id}>`);
      }

      ids = TEST.idsWithNoPopup ?? [];
      for (const id of ids) {
        await focusAndWaitForFieldsIdentified(browser, `#${id}`);
        await ensureNoAutocompletePopup(browser);
      }
    });

    if (TEST.prefs) {
      await SpecialPowers.popPrefEnv();
    }
  }
});