summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/browser/creditCard/browser_insecure_form.js
blob: 5de499b9420c48c7273f5a3a5714fc8c1c17b713 (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
"use strict";

// Remove the scheme from the URLs so we can switch between http: and https: later.
const TEST_URL_PATH_CC =
  "://example.org" +
  HTTP_TEST_PATH +
  "creditCard/autocomplete_creditcard_basic.html";
const TEST_URL_PATH =
  "://example.org" + HTTP_TEST_PATH + "autocomplete_basic.html";

add_task(async function setup_storage() {
  await setStorage(
    TEST_ADDRESS_1,
    TEST_ADDRESS_2,
    TEST_ADDRESS_3,
    TEST_CREDIT_CARD_1,
    TEST_CREDIT_CARD_2,
    TEST_CREDIT_CARD_3
  );
});

add_task(async function test_insecure_form() {
  async function runTest({
    urlPath,
    protocol,
    focusInput,
    expectedType,
    expectedResultLength,
  }) {
    await BrowserTestUtils.withNewTab(
      { gBrowser, url: protocol + urlPath },
      async function (browser) {
        await openPopupOn(browser, focusInput);

        const items = getDisplayedPopupItems(browser);
        is(
          items.length,
          expectedResultLength,
          `Should show correct amount of results in "${protocol}"`
        );
        const firstItem = items[0];
        is(
          firstItem.getAttribute("originaltype"),
          expectedType,
          `Item should attach with correct binding in "${protocol}"`
        );

        await closePopup(browser);
      }
    );
  }

  const testSets = [
    {
      urlPath: TEST_URL_PATH,
      protocol: "https",
      focusInput: "#organization",
      expectedType: "autofill-profile",
      expectedResultLength: 2,
    },
    {
      urlPath: TEST_URL_PATH,
      protocol: "http",
      focusInput: "#organization",
      expectedType: "autofill-profile",
      expectedResultLength: 2,
    },
    {
      urlPath: TEST_URL_PATH_CC,
      protocol: "https",
      focusInput: "#cc-name",
      expectedType: "autofill-profile",
      expectedResultLength: 3,
    },
    {
      urlPath: TEST_URL_PATH_CC,
      protocol: "http",
      focusInput: "#cc-name",
      expectedType: "autofill-insecureWarning", // insecure warning field
      expectedResultLength: 1,
    },
  ];

  for (const test of testSets) {
    await runTest(test);
  }
});

add_task(async function test_click_on_insecure_warning() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "http" + TEST_URL_PATH_CC },
    async function (browser) {
      await openPopupOn(browser, "#cc-name");
      const insecureItem = getDisplayedPopupItems(browser)[0];
      let popupClosePromise = BrowserTestUtils.waitForPopupEvent(
        browser.autoCompletePopup,
        "hidden"
      );
      await EventUtils.synthesizeMouseAtCenter(insecureItem, {});
      // Check input's value after popup closed to ensure the completion of autofilling.
      await popupClosePromise;

      const inputValue = await SpecialPowers.spawn(
        browser,
        [],
        async function () {
          return content.document.querySelector("#cc-name").value;
        }
      );
      is(inputValue, "");

      await closePopup(browser);
    }
  );
});

add_task(async function test_press_enter_on_insecure_warning() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "http" + TEST_URL_PATH_CC },
    async function (browser) {
      await openPopupOn(browser, "#cc-name");

      await BrowserTestUtils.synthesizeKey("VK_DOWN", {}, browser);

      let popupClosePromise = BrowserTestUtils.waitForPopupEvent(
        browser.autoCompletePopup,
        "hidden"
      );
      await BrowserTestUtils.synthesizeKey("VK_RETURN", {}, browser);
      // Check input's value after popup closed to ensure the completion of autofilling.
      await popupClosePromise;

      const inputValue = await SpecialPowers.spawn(
        browser,
        [],
        async function () {
          return content.document.querySelector("#cc-name").value;
        }
      );
      is(inputValue, "");

      await closePopup(browser);
    }
  );
});