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

"use strict";

// Bug 1762063 - we need to fix this pattern of having to wrap destructuring calls in parentheses.
// We can't do a standard destructuring call because FormAutofillUtils is already declared as a var in head.js
({ FormAutofillUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/shared/FormAutofillUtils.sys.mjs"
));
const { FIELD_STATES } = FormAutofillUtils;
const PREVIEW = FIELD_STATES.PREVIEW;
const NORMAL = FIELD_STATES.NORMAL;

const { OSKeyStore } = ChromeUtils.importESModule(
  "resource://gre/modules/OSKeyStore.sys.mjs"
);

const TESTCASES = [
  {
    description: "Preview best case address form",
    document: `<form>
    <input id="given-name" autocomplete="given-name">
    <input id="family-name" autocomplete="family-name">
    <input id="street-addr" autocomplete="street-address">
    <input id="city" autocomplete="address-level2">
   </form>`,
    focusedInputId: "given-name",
    profileData: {
      "given-name": "John",
      "family-name": "Doe",
      "street-address": "100 Main Street",
      "address-level2": "Hamilton",
    },
    expectedResultState: {
      "given-name": PREVIEW,
      "family-name": PREVIEW,
      "street-address": PREVIEW,
      "address-level2": PREVIEW,
    },
  },
  {
    description: "Preview form with a readonly input and non-readonly inputs",
    document: `<form>
    <input id="given-name" autocomplete="given-name">
    <input id="family-name" autocomplete="family-name">
    <input id="street-addr" autocomplete="street-address">
    <input id="city" autocomplete="address-level2" readonly value="TEST CITY">
   </form>`,
    focusedInputId: "given-name",
    profileData: {
      "given-name": "John",
      "family-name": "Doe",
      "street-address": "100 Main Street",
      city: "Hamilton",
    },
    expectedResultState: {
      "given-name": PREVIEW,
      "family-name": PREVIEW,
      "street-address": PREVIEW,
      "address-level2": undefined,
    },
  },
  {
    description: "Preview form with a disabled input and non-disabled inputs",
    document: `<form>
    <input id="given-name" autocomplete="given-name">
    <input id="family-name" autocomplete="family-name">
    <input id="street-addr" autocomplete="street-address">
    <input id="country" autocomplete="country" disabled value="US">
   </form>`,
    focusedInputId: "given-name",
    profileData: {
      "given-name": "John",
      "family-name": "Doe",
      "street-address": "100 Main Street",
      country: "CA",
    },
    expectedResultState: {
      "given-name": PREVIEW,
      "family-name": PREVIEW,
      "street-address": PREVIEW,
      country: undefined,
    },
  },
  {
    description:
      "Preview form with autocomplete select elements and matching option values",
    document: `<form>
               <input id="given-name" autocomplete="shipping given-name">
               <select id="country" autocomplete="shipping country">
                 <option value=""></option>
                 <option value="US">United States</option>
               </select>
               <select id="state" autocomplete="shipping address-level1">
                 <option value=""></option>
                 <option value="CA">California</option>
                 <option value="WA">Washington</option>
               </select>
               </form>`,
    focusedInputId: "given-name",
    profileData: {
      country: "US",
      "address-level1": "CA",
    },
    expectedResultState: {
      "given-name": NORMAL,
      country: PREVIEW,
      "address-level1": PREVIEW,
    },
  },
  {
    description: "Preview best case credit card form",
    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">
              <input id="cc-csc" autocomplete="cc-csc">
              </form>
              `,
    focusedInputId: "cc-number",
    profileData: {
      guid: "123",
      "cc-number": "4111111111111111",
      "cc-name": "test name",
      "cc-exp-month": 6,
      "cc-exp-year": 25,
    },
    expectedResultState: {
      "cc-number": PREVIEW,
      "cc-name": PREVIEW,
      "cc-exp-month": PREVIEW,
      "cc-exp-year": PREVIEW,
      "cc-csc": NORMAL,
    },
  },
];

function run_tests(testcases) {
  for (let testcase of testcases) {
    add_task(async function () {
      info("Starting testcase: " + testcase.description);
      let doc = MockDocument.createTestDocument(
        "http://localhost:8080/test/",
        testcase.document
      );
      let form = doc.querySelector("form");
      let formLike = FormLikeFactory.createFromForm(form);
      let handler = new FormAutofillHandler(formLike);

      // Replace the internal decrypt method with OSKeyStore API,
      // but don't pass the reauth parameter to avoid triggering
      // reauth login dialog in these tests.
      let decryptHelper = async (cipherText, reauth) => {
        return OSKeyStore.decrypt(cipherText, false);
      };
      handler.collectFormFields();

      let focusedInput = doc.getElementById(testcase.focusedInputId);
      try {
        handler.focusedInput = focusedInput;
      } catch (e) {
        if (e.message.includes("WeakMap key must be an object")) {
          throw new Error(
            `Couldn't find the focusedInputId in the current form! Make sure focusedInputId exists in your test form! testcase description:${testcase.description}`
          );
        } else {
          throw e;
        }
      }

      for (let section of handler.sections) {
        section._decrypt = decryptHelper;
      }

      let [adaptedProfile] = handler.activeSection.getAdaptedProfiles([
        testcase.profileData,
      ]);

      await handler.activeSection.previewFormFields(adaptedProfile);

      for (let field of handler.fieldDetails) {
        let actual = handler.getFilledStateByElement(
          field.elementWeakRef.get()
        );
        let expected = testcase.expectedResultState[field.fieldName];
        info(`Checking ${field.fieldName} state`);
        Assert.equal(
          actual,
          expected,
          "Check if preview state is set correctly"
        );
      }
    });
  }
}

run_tests(TESTCASES);