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

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

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"],
    ],
  });
});

// Test different scenarios when we change something in the edit address dorhanger
add_task(async function test_save_edited_fields() {
  await expectSavedAddresses(0);

  const initRecord = {
    "given-name": "John",
    "family-name": "Doe",
    organization: "Mozilla",
    "street-address": "123 Sesame Street",
    tel: "+13453453456",
  };

  const TESTS = [
    {
      description: "adding the email field",
      editedFields: {
        email: "test@mozilla.org",
      },
    },
    {
      description: "changing the given-name field",
      editedFields: {
        name: "Jane",
      },
    },
    {
      description: "appending the street-address field",
      editedFields: {
        "street-address": initRecord["street-address"] + " 4F",
      },
    },
    {
      description: "removing some fields",
      editedFields: {
        name: "",
        tel: "",
      },
    },
    {
      description: "doing all kinds of stuff",
      editedFields: {
        organization: initRecord.organization.toLowerCase(),
        "address-level1": "California",
        tel: "",
        "street-address": initRecord["street-address"] + " Apt.6",
        name: "Jane Doe",
      },
    },
  ];

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

        const onSavePopupShown = waitForPopupShown();

        await focusUpdateSubmitForm(browser, {
          focusSelector: "#given-name",
          newValues: recordToFormSelector(initRecord),
        });

        await onSavePopupShown;

        const onEditPopupShown = waitForPopupShown();
        await clickAddressDoorhangerButton(EDIT_ADDRESS_BUTTON);
        await onEditPopupShown;
        fillEditDoorhanger(TEST.editedFields);

        await clickAddressDoorhangerButton(MAIN_BUTTON);
      }
    );

    const expectedRecord = normalizeAddressFields({
      ...initRecord,
      ...TEST.editedFields,
    });

    const addresses = await expectSavedAddresses(1);
    for (const [key, value] of Object.entries(expectedRecord)) {
      is(addresses[0][key] ?? "", value, `${key} field is saved`);
    }

    await removeAllRecords();
  }
});

// This test tests edit doorhanger "save" & "cancel" buttons work correctly
// when the edit doorhanger is triggered in an save doorhanger
add_task(async function test_edit_doorhanger_triggered_by_save_doorhanger() {
  for (const CLICKED_BUTTON of [MAIN_BUTTON, SECONDARY_BUTTON]) {
    await expectSavedAddresses(0);

    const initRecord = {
      "given-name": "John",
      "family-name": "Doe",
      organization: "Mozilla",
      "street-address": "123 Sesame Street",
      tel: "+13453453456",
    };

    const editRecord = {
      email: "test@mozilla.org",
    };

    await BrowserTestUtils.withNewTab(
      { gBrowser, url: ADDRESS_FORM_URL },
      async browser => {
        info(
          `Test clicking ${CLICKED_BUTTON == MAIN_BUTTON ? "save" : "cancel"}`
        );
        const onSavePopupShown = waitForPopupShown();
        await focusUpdateSubmitForm(browser, {
          focusSelector: "#given-name",
          newValues: recordToFormSelector(initRecord),
        });
        await onSavePopupShown;

        const onEditPopupShown = waitForPopupShown();
        await clickAddressDoorhangerButton(EDIT_ADDRESS_BUTTON);
        await onEditPopupShown;
        fillEditDoorhanger(editRecord);

        await clickAddressDoorhangerButton(CLICKED_BUTTON);
      }
    );

    await expectSavedAddresses(CLICKED_BUTTON == MAIN_BUTTON ? 1 : 0);
    await removeAllRecords();
  }
});

// This test tests edit doorhanger "save" & "cancel" buttons work correctly
// when the edit doorhanger is triggered in an update doorhnager
add_task(async function test_edit_doorhanger_triggered_by_update_doorhanger() {
  for (const CLICKED_BUTTON of [MAIN_BUTTON, SECONDARY_BUTTON]) {
    // TEST_ADDRESS_2 doesn't contain email field
    await setStorage(TEST_ADDRESS_2);
    await expectSavedAddresses(1);

    const initRecord = {
      "given-name": TEST_ADDRESS_2["given-name"],
      "street-address": TEST_ADDRESS_2["street-address"],
      country: TEST_ADDRESS_2.country,
      email: "test@mozilla.org",
    };

    const editRecord = {
      email: "test@mozilla.org",
    };

    await BrowserTestUtils.withNewTab(
      { gBrowser, url: ADDRESS_FORM_URL },
      async browser => {
        info(
          `Test clicking ${CLICKED_BUTTON == MAIN_BUTTON ? "save" : "cancel"}`
        );
        const onUpdatePopupShown = waitForPopupShown();
        await focusUpdateSubmitForm(browser, {
          focusSelector: "#given-name",
          newValues: recordToFormSelector(initRecord),
        });
        await onUpdatePopupShown;

        const onEditPopupShown = waitForPopupShown();
        await clickAddressDoorhangerButton(EDIT_ADDRESS_BUTTON);
        await onEditPopupShown;
        fillEditDoorhanger(editRecord);

        await clickAddressDoorhangerButton(CLICKED_BUTTON);
      }
    );

    const addresses = await expectSavedAddresses(1);
    const expectedRecord =
      CLICKED_BUTTON == MAIN_BUTTON
        ? { ...initRecord, ...editRecord }
        : TEST_ADDRESS_2;

    for (const [key, value] of Object.entries(expectedRecord)) {
      is(addresses[0][key] ?? "", value, `${key} field is saved`);
    }

    await removeAllRecords();
  }
});