summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/browser_update_doorhanger.js
blob: 73fd903de62ed30b9efcaecd1aaf4b24a11fcc48 (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
"use strict";

/**
 * Note that this testcase is built based on the assumption that subtests are
 * run in order. So if you're going to add a new subtest, please add it in the end.
 */

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

add_task(async function test_update_address() {
  await setStorage(TEST_ADDRESS_1);
  let addresses = await getAddresses();
  is(addresses.length, 1, "1 address in storage");

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: FORM_URL },
    async function (browser) {
      // Autofill address fields
      await openPopupOn(browser, "form #organization");
      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
      await waitForAutofill(browser, "#tel", addresses[0].tel);

      // Update address fields and submit
      let onPopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#organization",
        newValues: {
          "#organization": "Mozilla",
        },
      });

      await onPopupShown;

      // Choose to update address by doorhanger
      let onUpdated = waitForStorageChangedEvents("update");
      await clickDoorhangerButton(MAIN_BUTTON);
      await onUpdated;
    }
  );

  addresses = await getAddresses();
  is(addresses.length, 1, "Still 1 address in storage");
  is(addresses[0].organization, "Mozilla", "Verify the organization field");
});

add_task(async function test_create_new_address() {
  let addresses = await getAddresses();
  is(addresses.length, 1, "1 address in storage");

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: FORM_URL },
    async function (browser) {
      // Autofill address fields
      await openPopupOn(browser, "form #tel");
      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
      await waitForAutofill(browser, "#tel", addresses[0].tel);

      let onPopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#tel",
        newValues: {
          "#tel": "+1234567890",
        },
      });

      await onPopupShown;

      // Choose to add address by doorhanger
      let onAdded = waitForStorageChangedEvents("add");
      await clickDoorhangerButton(SECONDARY_BUTTON);
      await onAdded;
    }
  );

  addresses = await getAddresses();
  is(addresses.length, 2, "2 addresses in storage");
  is(addresses[1].tel, "+1234567890", "Verify the tel field");
});

add_task(async function test_create_new_address_merge() {
  let addresses = await getAddresses();
  is(addresses.length, 2, "2 addresses in storage");

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: FORM_URL },
    async function (browser) {
      await openPopupOn(browser, "form #tel");
      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
      await waitForAutofill(browser, "#tel", addresses[1].tel);

      // Choose the latest address and revert to the original phone number
      let onPopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        focusSelector: "#tel",
        newValues: {
          "#tel": "+16172535702",
        },
      });

      await onPopupShown;
      await clickDoorhangerButton(SECONDARY_BUTTON);
    }
  );

  addresses = await getAddresses();
  is(addresses.length, 2, "Still 2 addresses in storage");
});

add_task(async function test_submit_untouched_fields() {
  let addresses = await getAddresses();
  is(addresses.length, 2, "2 addresses in storage");

  await BrowserTestUtils.withNewTab(
    { gBrowser, url: FORM_URL },
    async function (browser) {
      await openPopupOn(browser, "form #organization");
      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
      await waitForAutofill(browser, "#tel", addresses[0].tel);

      let onPopupShown = waitForPopupShown();
      await SpecialPowers.spawn(browser, [], async function () {
        let form = content.document.getElementById("form");
        let tel = form.querySelector("#tel");
        tel.value = "12345"; // ".value" won't change the highlight status.
      });

      await focusUpdateSubmitForm(browser, {
        focusSelector: "#tel",
        newValues: {
          "#organization": "Organization",
        },
      });
      await onPopupShown;

      let onUpdated = waitForStorageChangedEvents("update");
      await clickDoorhangerButton(MAIN_BUTTON);
      await onUpdated;
    }
  );

  addresses = await getAddresses();
  is(addresses.length, 2, "Still 2 addresses in storage");
  is(addresses[0].organization, "Organization", "organization should change");
  is(addresses[0].tel, "+16172535702", "tel should remain unchanged");
});

add_task(async function test_submit_reduced_fields() {
  let addresses = await getAddresses();
  is(addresses.length, 2, "2 addresses in storage");

  let url = BASE_URL + "autocomplete_simple_basic.html";
  await BrowserTestUtils.withNewTab(
    { gBrowser, url },
    async function (browser) {
      await openPopupOn(browser, "form#simple input[name=tel]");
      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
      await waitForAutofill(browser, "form #simple_tel", "6172535702");

      let onPopupShown = waitForPopupShown();
      await focusUpdateSubmitForm(browser, {
        formSelector: "form#simple",
        focusSelector: "form #simple_tel",
        newValues: {
          "input[name=tel]": "123456789",
        },
      });

      await onPopupShown;

      let onUpdated = waitForStorageChangedEvents("update");
      await clickDoorhangerButton(MAIN_BUTTON);
      await onUpdated;
    }
  );

  addresses = await getAddresses();
  is(addresses.length, 2, "Still 2 addresses in storage");
  is(addresses[0].tel, "123456789", "tel should should be changed");
  is(addresses[0]["postal-code"], "02139", "postal code should be kept");
});