summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/address/browser_address_doorhanger_state.js
blob: f3b04d7f9cbb5f0b9af45cb9310a8566bdf1cdb0 (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
"use strict";
requestLongerTimeout(3);

async function expectSavedAddresses(expectedAddresses) {
  const addresses = await getAddresses();
  is(
    addresses.length,
    expectedAddresses.length,
    `${addresses.length} address in the storage`
  );

  for (let i = 0; i < expectedAddresses.length; i++) {
    for (const [key, value] of Object.entries(expectedAddresses[i])) {
      is(addresses[i][key] ?? "", value, `field ${key} should be equal`);
    }
  }
  return addresses;
}

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

add_task(async function test_save_doorhanger_state_invalid() {
  const DEFAULT = {
    "given-name": "John",
    "family-name": "Doe",
    organization: "Mozilla",
    "street-address": "123 Sesame Street",
    country: "US",
  };

  const TEST_CASES = [
    {
      filled: { "address-level1": "floridaa" }, // typo
      expected: { "address-level1": "" },
    },
    {
      filled: { "address-level1": "AB" }, // non-exist region code
      expected: { "address-level1": "" },
    },
  ];

  for (const TEST of TEST_CASES) {
    await expectSavedAddresses([]);

    await BrowserTestUtils.withNewTab(
      { gBrowser, url: ADDRESS_FORM_URL },
      async function (browser) {
        let onPopupShown = waitForPopupShown();

        await focusUpdateSubmitForm(browser, {
          focusSelector: "#given-name",
          newValues: {
            "#given-name": DEFAULT["given-name"],
            "#family-name": DEFAULT["family-name"],
            "#organization": DEFAULT.organization,
            "#street-address": DEFAULT["street-address"],
            "#address-level1": TEST.filled["address-level1"],
          },
        });

        await onPopupShown;
        await clickDoorhangerButton(MAIN_BUTTON, 0);
      }
    );

    await expectSavedAddresses([Object.assign(DEFAULT, TEST.expected)]);
    await removeAllRecords();
  }
});

add_task(async function test_save_doorhanger_state_valid() {
  const DEFAULT = {
    "given-name": "John",
    "family-name": "Doe",
    organization: "Mozilla",
    "street-address": "123 Sesame Street",
    country: "US",
  };

  const TEST_CASES = [
    {
      filled: { "address-level1": "ca" },
      expected: { "address-level1": "ca" },
    },
    {
      filled: { "address-level1": "CA" },
      expected: { "address-level1": "CA" },
    },
    {
      filled: { "address-level1": "california" },
      expected: { "address-level1": "california" },
    },
    {
      filled: { "address-level1": "California" },
      expected: { "address-level1": "California" },
    },
  ];

  for (const TEST of TEST_CASES) {
    await expectSavedAddresses([]);

    const onChanged = waitForStorageChangedEvents("add");
    await BrowserTestUtils.withNewTab(
      { gBrowser, url: ADDRESS_FORM_URL },
      async function (browser) {
        let onPopupShown = waitForPopupShown();

        await focusUpdateSubmitForm(browser, {
          focusSelector: "#given-name",
          newValues: {
            "#given-name": DEFAULT["given-name"],
            "#family-name": DEFAULT["family-name"],
            "#organization": DEFAULT.organization,
            "#street-address": DEFAULT["street-address"],
            "#address-level1": TEST.filled["address-level1"],
          },
        });

        await onPopupShown;
        await clickDoorhangerButton(MAIN_BUTTON, 0);
      }
    );
    await onChanged;

    await expectSavedAddresses([Object.assign(DEFAULT, TEST.expected)]);
    await removeAllRecords();
  }
});

add_task(async function test_save_doorhanger_state_is_select() {
  const DEFAULT_TEST_DOC = `
    <form id="form">
      <input id="name" autocomplete="name">
      <input id="street-address" autocomplete="street-address">
      <select id="address-level1" autocomplete="address-level1">
        <option value=""></option>
        <option value="AL">Alabama</option>
        <option value="AK">Alaska</option>
        <option value="AP">Armed Forces Pacific</option>

        <option value="ca">california</option>
        <option value="AR">US-Arkansas</option>
        <option value="US-CA">California</option>
        <option value="CA">California</option>
        <option value="US-AZ">US_Arizona</option>
        <option value="Ariz">Arizonac</option>
      </select>
      <input id="city" autocomplete="address-level2">
      <input id="country" autocomplete="country">
      <input id="submit" type="submit">
    </form>`;

  const TEST_CASES = [
    {
      description: "Save state with regular select option",
      filled: { "address-level1": "CA" },
      expected: { "address-level1": "CA" },
    },
    {
      description: "Save state with lowercase value",
      filled: { "address-level1": "ca" },
      expected: { "address-level1": "CA" },
    },
    {
      description: "Save state with a country code prefixed to the label",
      filled: { "address-level1": "AR" },
      expected: { "address-level1": "AR" },
    },
    {
      description: "Save state with a country code prefixed to the value",
      filled: { "address-level1": "US-CA" },
      expected: { "address-level1": "CA" },
    },
    {
      description:
        "Save state with a country code prefixed to the value and label",
      filled: { "address-level1": "US-AZ" },
      expected: { "address-level1": "AZ" },
    },
    {
      description: "Should not save when failed to abbreviate the value",
      filled: { "address-level1": "Ariz" },
      expected: { "address-level1": "" },
    },
    {
      description: "Should not save select with multiple selections",
      filled: { "address-level1": ["AL", "AK", "AP"] },
      expected: { "address-level1": "" },
    },
  ];

  for (const TEST of TEST_CASES) {
    const onChanged = waitForStorageChangedEvents("add");
    await BrowserTestUtils.withNewTab(EMPTY_URL, async function (browser) {
      info(`Test ${TEST.description}`);

      await SpecialPowers.spawn(browser, [DEFAULT_TEST_DOC], doc => {
        content.document.body.innerHTML = doc;
      });

      await SimpleTest.promiseFocus(browser);

      const onPopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#name",
        newValues: {
          "#name": "John Doe",
          "#street-address": "Main Street",
          "#country": "US",
          "#address-level1": TEST.filled["address-level1"],
        },
      });

      await onPopupShown;
      await clickDoorhangerButton(MAIN_BUTTON, 0);
    });
    await onChanged;

    await expectSavedAddresses([TEST.expected]);
    await removeAllRecords();
  }
});