summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/address/browser_address_doorhanger_ui.js
blob: 9451054de9ef30224bf31a16084f37718b49aeea (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
"use strict";

const { FormAutofill } = ChromeUtils.importESModule(
  "resource://autofill/FormAutofill.sys.mjs"
);

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

function verifyDoorhangerContent(saved, removed = {}) {
  const rows = [
    ...getNotification().querySelectorAll(`.address-save-update-row-container`),
  ];

  let texts = rows.reduce((acc, cur) => acc + cur.textContent, "");
  for (const text of Object.values(saved)) {
    ok(texts.includes(text), `Show ${text} in the doorhanger`);
    texts = texts.replace(text, "");
  }
  for (const text of Object.values(removed)) {
    ok(texts.includes(text), `Show ${text} in the doorhanger (removed)`);
    texts = texts.replace(text, "");
  }
  is(texts.trim(), "", `Doorhanger shows all the submitted data`);
}

function checkVisibility(element) {
  return element.checkVisibility({
    checkOpacity: true,
    checkVisibilityCSS: true,
  });
}

function recordToFormSelector(record) {
  let obj = {};
  for (const [key, value] of Object.entries(record)) {
    obj[`#${key}`] = value;
  }
  return obj;
}

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

// Save address doorhanger should show description when users has no saved address
add_task(async function test_save_doorhanger_show_description() {
  await expectSavedAddresses(0);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      await showAddressDoorhanger(browser);

      const header = AutofillDoorhanger.header(getNotification());
      is(checkVisibility(header), true, "Should always show header");

      const description = AutofillDoorhanger.description(getNotification());
      is(
        checkVisibility(description),
        true,
        "Should show description when this is the first address saved"
      );
    }
  );
});

// Save address doorhanger should not show description when users has at least one saved address
add_task(async function test_save_doorhanger_hide_description() {
  await setStorage(TEST_ADDRESS_1);
  await expectSavedAddresses(1);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      await showAddressDoorhanger(browser);

      const header = AutofillDoorhanger.header(getNotification());
      is(checkVisibility(header), true, "Should always show header");

      const description = AutofillDoorhanger.description(getNotification());
      is(
        checkVisibility(description),
        false,
        "Should not show description when there is at least one saved address"
      );
    }
  );

  await removeAllRecords();
});

// Test open edit address popup and then click "learn more" button
add_task(async function test_click_learn_more_button_in_edit_doorhanger() {
  await expectSavedAddresses(0);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      await showAddressDoorhanger(browser);

      let tabOpenPromise = BrowserTestUtils.waitForNewTab(gBrowser, url =>
        url.endsWith(AddressSaveDoorhanger.learnMoreURL)
      );
      await clickAddressDoorhangerButton(
        ADDRESS_MENU_BUTTON,
        ADDRESS_MENU_LEARN_MORE
      );
      const tab = await tabOpenPromise;
      gBrowser.removeTab(tab);
    }
  );
});

add_task(async function test_click_address_setting_button_in_edit_doorhanger() {
  await expectSavedAddresses(0);

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      await showAddressDoorhanger(browser);

      let tabOpenPromise = BrowserTestUtils.waitForNewTab(
        gBrowser,
        `about:preferences#${AddressSaveDoorhanger.preferenceURL}`
      );
      await clickAddressDoorhangerButton(
        ADDRESS_MENU_BUTTON,
        ADDRESS_MENU_PREFENCE
      );
      const tab = await tabOpenPromise;
      gBrowser.removeTab(tab);
    }
  );
});

add_task(async function test_address_display_in_save_doorhanger() {
  await expectSavedAddresses(0);

  const TESTS = [
    {
      description: "Test submit a form without email and tel fields",
      form: {
        "#given-name": "John",
        "#family-name": "Doe",
        "#organization": "Mozilla",
        "#street-address": "123 Sesame Street",
      },
      expectedSectionCount: 1,
    },
    {
      description: "Test submit a form with email field",
      form: {
        "#given-name": "John",
        "#family-name": "Doe",
        "#organization": "Mozilla",
        "#street-address": "123 Sesame Street",
        "#email": "test@mozilla.org",
      },
      expectedSectionCount: 2,
    },
    {
      description: "Test submit a form with tel field",
      form: {
        "#given-name": "John",
        "#family-name": "Doe",
        "#organization": "Mozilla",
        "#street-address": "123 Sesame Street",
        "#tel": "+13453453456",
      },
      expectedSectionCount: 2,
    },
  ];

  for (const TEST of TESTS) {
    await BrowserTestUtils.withNewTab(
      { gBrowser, url: ADDRESS_FORM_URL },
      async function (browser) {
        info(TEST.description);
        await showAddressDoorhanger(browser, TEST.form);

        is(
          getNotification().querySelectorAll(
            `.address-save-update-row-container`
          ).length,
          TEST.expectedSectionCount,
          `Should have ${TEST.expectedSectionCount} address section`
        );

        // When the form has no country field, doorhanger shows the default region
        verifyDoorhangerContent({
          ...TEST.form,
          country: FormAutofill.DEFAULT_REGION,
        });

        await clickAddressDoorhangerButton(SECONDARY_BUTTON);
      }
    );
  }

  await removeAllRecords();
});

add_task(async function test_show_added_text_in_update_doorhanger() {
  await setStorage(TEST_ADDRESS_2);
  await expectSavedAddresses(1);

  const form = {
    ...TEST_ADDRESS_2,

    email: "test@mozilla.org", // Add email field
    "given-name": TEST_ADDRESS_2["given-name"] + " Doe", // Append
    "street-address": TEST_ADDRESS_2["street-address"] + " 4F", // Append
  };

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      await showAddressDoorhanger(browser, recordToFormSelector(form));

      // When the form has no country field, doorhanger shows the default region
      verifyDoorhangerContent({
        ...form,
        country: FormAutofill.DEFAULT_REGION,
      });

      await clickAddressDoorhangerButton(SECONDARY_BUTTON);
    }
  );

  await removeAllRecords();
});

add_task(async function test_show_removed_text_in_update_doorhanger() {
  const SAVED_ADDRESS = {
    ...TEST_ADDRESS_2,
    organization: "Mozilla",
  };
  await setStorage(SAVED_ADDRESS);
  await expectSavedAddresses(1);

  // We will ask whether users would like to update "Mozilla" to "mozilla"
  const form = {
    ...SAVED_ADDRESS,

    organization: SAVED_ADDRESS.organization.toLowerCase(),
  };

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      await showAddressDoorhanger(browser, recordToFormSelector(form));

      // When the form has no country field, doorhanger shows the default region
      verifyDoorhangerContent(
        { ...form, country: FormAutofill.DEFAULT_REGION },
        { organization: SAVED_ADDRESS.organization }
      );

      await clickAddressDoorhangerButton(SECONDARY_BUTTON);
    }
  );

  await removeAllRecords();
});