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

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;
}

const ADDRESS_FIELD_VALUES = {
  "given-name": "John",
  organization: "Sesame Street",
  "street-address": "123 Sesame Street",
};

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

/**
 * Tests if the address is captured (address doorhanger is shown) after a
 * successful xhr or fetch request followed by a form removal and
 * that the stored address record has the right values.
 */
add_task(async function test_address_captured_after_form_removal() {
  const onStorageChanged = waitForStorageChangedEvents("add");
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      const onPopupShown = waitForPopupShown();

      info("Update identified address fields");
      // We don't submit the form
      await focusUpdateSubmitForm(
        browser,
        {
          focusSelector: "#given-name",
          newValues: {
            "#given-name": ADDRESS_FIELD_VALUES["given-name"],
            "#organization": ADDRESS_FIELD_VALUES.organization,
            "#street-address": ADDRESS_FIELD_VALUES["street-address"],
          },
        },
        false
      );

      info("Infer a successfull fetch request");
      await SpecialPowers.spawn(browser, [], async () => {
        await content.fetch(
          "https://example.org/browser/browser/extensions/formautofill/test/browser/empty.html"
        );
      });

      info("Infer form removal");
      await SpecialPowers.spawn(browser, [], async function () {
        let form = content.document.getElementById("form");
        form.parentNode.remove(form);
      });
      info("Wait for address doorhanger");
      await onPopupShown;

      info("Click Save in address doorhanger");
      await clickDoorhangerButton(MAIN_BUTTON, 0);
    }
  );

  info("Wait for the address to be added to the storage.");
  await onStorageChanged;

  info("Ensure that address record was captured and saved correctly.");
  await expectSavedAddresses([ADDRESS_FIELD_VALUES]);

  await removeAllRecords();
});

/**
 * Tests that the address is not captured without a prior fetch or xhr request event
 */
add_task(async function test_address_not_captured_without_prior_fetch() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_URL },
    async function (browser) {
      info("Update identified address fields");
      // We don't submit the form
      await focusUpdateSubmitForm(
        browser,
        {
          focusSelector: "#given-name",
          newValues: {
            "#given-name": ADDRESS_FIELD_VALUES["given-name"],
            "#organization": ADDRESS_FIELD_VALUES.organization,
            "#street-address": ADDRESS_FIELD_VALUES["street-address"],
          },
        },
        false
      );

      info("Infer form removal");
      await SpecialPowers.spawn(browser, [], async function () {
        let form = content.document.getElementById("form");
        form.parentNode.remove(form);
      });

      info("Ensure that address doorhanger is not shown");
      await ensureNoDoorhanger();
    }
  );
});