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

const ADDRESS_VALUES = {
  "#given-name": "Test User",
  "#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.captureOnPageNavigation", true],
    ],
  });
});

/**
 * Tests if the address is captured (address doorhanger is shown)
 * after adding an entry to the browser's session history stack
 */
add_task(async function test_address_captured_after_changing_request_state() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_WITH_PAGE_NAVIGATION_BUTTONS },
    async function (browser) {
      const onPopupShown = waitForPopupShown();

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

      info("Change request state");
      await SpecialPowers.spawn(browser, [], () => {
        const historyPushStateButton =
          content.document.getElementById("historyPushState");
        historyPushStateButton.click();
      });

      info("Wait for address doorhanger");
      await onPopupShown;

      ok(true, "Address doorhanger is shown");
    }
  );
});

/**
 * Tests if the address is captured (address doorhanger is shown)
 * after navigating by opening another resource
 */
add_task(async function test_address_captured_after_navigation_same_window() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_WITH_PAGE_NAVIGATION_BUTTONS },
    async function (browser) {
      const onPopupShown = waitForPopupShown();

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

      info("Navigate with window.location");
      await SpecialPowers.spawn(browser, [], () => {
        const windowLocationButton =
          content.document.getElementById("windowLocation");
        windowLocationButton.click();
      });

      info("Wait for address doorhanger");
      await onPopupShown;

      ok(true, "Address doorhanger is shown");
    }
  );
});

/**
 * Test that a form submission is infered only once.
 */
add_task(async function test_form_submission_infered_only_once() {
  await setStorage(TEST_ADDRESS_1);

  let onUsed = waitForStorageChangedEvents("notifyUsed");
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: ADDRESS_FORM_WITH_PAGE_NAVIGATION_BUTTONS },
    async function (browser) {
      // Progress listener is added on address field identification
      await openPopupOn(browser, "form #given-name");

      info("Fill address input fields without changing the values");
      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);

      info("Submit form");
      await SpecialPowers.spawn(browser, [], async function () {
        // Progress listener is removed after form submission
        let form = content.document.getElementById("form");
        form.querySelector("input[type=submit]").click();
      });
    }
  );
  await onUsed;

  const addresses = await getAddresses();

  is(
    addresses[0].timesUsed,
    1,
    "timesUsed field set to 1, so form submission was only infered once"
  );
  await removeAllRecords();
});