summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/mochitest/creditCard/test_clear_form.html
blob: 3d8049f05370e106ad01923fa9cd1b0398b0ec5e (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test form autofill - clear form button</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="../formautofill_common.js"></script>
  <script type="text/javascript" src="../../../../../../toolkit/components/satchel/test/satchel_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Form autofill test: clear form button

<script>
"use strict";

const MOCK_ADDR_STORAGE = [{
  organization: "Sesame Street",
  "street-address": "2 Harrison St\nline2\nline3",
  tel: "+13453453456",
}, {
  organization: "Mozilla",
  "street-address": "331 E. Evelyn Avenue",
}, {
  organization: "Tel org",
  tel: "+12223334444",
}];
const MOCK_CC_STORAGE = [{
  "cc-name": "John Doe",
  "cc-number": "4929001587121045",
  "cc-exp-month": 4,
  "cc-exp-year": 2017,
}, {
  "cc-name": "Timothy Berners-Lee",
  "cc-number": "5103059495477870",
  "cc-exp-month": 12,
  "cc-exp-year": 2022,
}];

const MOCK_CC_STORAGE_EXPECTED_FILL = [{
  "cc-name": "John Doe",
  "cc-number": "4929001587121045",
  "cc-exp-month": "04",
  "cc-exp-year": 2017,
}, {
  "cc-name": "Timothy Berners-Lee",
  "cc-number": "5103059495477870",
  "cc-exp-month": "12",
  "cc-exp-year": 2022,
}];

initPopupListener();

add_task(async function setup_storage() {
  await addAddress(MOCK_ADDR_STORAGE[0]);
  await addAddress(MOCK_ADDR_STORAGE[1]);
  await addAddress(MOCK_ADDR_STORAGE[2]);

  await addCreditCard(MOCK_CC_STORAGE[0]);
  await addCreditCard(MOCK_CC_STORAGE[1]);
});


async function checkIsFormCleared(patch = {}) {
  const form = document.getElementById("form1");

  for (const elem of form.elements) {
    const expectedValue = patch[elem.id] || "";
    checkFieldValue(elem, expectedValue);
    await checkFieldHighlighted(elem, false);
    await checkFieldPreview(elem, "");
  }
}

async function confirmClear(selector) {
  info("Await for clearing input");
  let promise = new Promise(resolve => {
    let beforeInputFired = false;
    let element = document.querySelector(selector);
    element.addEventListener("beforeinput", (event) => {
      beforeInputFired = true;
      ok(event instanceof InputEvent,
         '"beforeinput" event should be dispatched with InputEvent interface');
      is(event.cancelable, SpecialPowers.getBoolPref("dom.input_event.allow_to_cancel_set_user_input"),
         `"beforeinput" event should be cancelable unless it's disabled by the pref`);
      is(event.bubbles, true,
         '"beforeinput" event should always bubble');
      is(event.inputType, "insertReplacementText",
         'inputType value of "beforeinput" should be "insertReplacementText"');
      is(event.data, "",
         'data value of "beforeinput" should be empty string');
      is(event.dataTransfer, null,
         'dataTransfer value of "beforeinput" should be null');
      is(event.getTargetRanges().length, 0,
         'getTargetRanges() of "beforeinput" event should return empty array');
    }, {once: true});
    element.addEventListener("input", (event) => {
      ok(beforeInputFired, `"beforeinput" event should've been fired before "input" on <${element.tagName} type="${element.type}">`);
      ok(event instanceof InputEvent,
         '"input" event should be dispatched with InputEvent interface');
      is(event.cancelable, false,
         '"input" event should be never cancelable');
      is(event.bubbles, true,
         '"input" event should always bubble');
      is(event.inputType, "insertReplacementText",
         'inputType value of "input" should be "insertReplacementText"');
      is(event.data, "",
         'data value of "input" should be empty string');
      is(event.dataTransfer, null,
         'dataTransfer value of "input" should be null');
      is(event.getTargetRanges().length, 0,
         'getTargetRanges() of "input" should return empty array');
      resolve();
    }, {once: true})
  });
  synthesizeKey("KEY_Enter");
  await promise;
}

add_task(async function simple_clear() {
  await triggerPopupAndHoverItem("#organization", 0);
  await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);

  await triggerPopupAndHoverItem("#tel", 0);
  await confirmClear("#tel");
  await checkIsFormCleared();
});

add_task(async function clear_adapted_record() {
  await triggerPopupAndHoverItem("#street-address", 0);
  await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);

  await triggerPopupAndHoverItem("#street-address", 0);
  await confirmClear("#street-address");
  await checkIsFormCleared();
});

add_task(async function clear_modified_form() {
  await triggerPopupAndHoverItem("#organization", 0);
  await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);

  await setInput("#tel", "+1111111111", true);

  await triggerPopupAndHoverItem("#street-address", 0);
  await confirmClear("#street-address");
  await checkIsFormCleared({tel: "+1111111111"});
});

add_task(async function clear_distinct_section() {
  if (!(await canTestOSKeyStoreLogin())) {
    todo(false, "Cannot test OS key store login on official builds.");
    return;
  }

  document.getElementById("form1").reset();
  await triggerPopupAndHoverItem("#cc-name", 0);
  let osKeyStoreLoginShown = waitForOSKeyStoreLogin(true);
  await triggerAutofillAndCheckProfile(MOCK_CC_STORAGE_EXPECTED_FILL[0]);
  await osKeyStoreLoginShown;

  await triggerPopupAndHoverItem("#organization", 0);
  await triggerAutofillAndCheckProfile(MOCK_ADDR_STORAGE[0]);
  await triggerPopupAndHoverItem("#street-address", 0);
  await confirmClear("#street-address");

  for (const [id, val] of Object.entries(MOCK_CC_STORAGE_EXPECTED_FILL[0])) {
    const element = document.getElementById(id);
    if (!element) {
      return;
    }
    checkFieldValue(element, val);
    await checkFieldHighlighted(element, true);
  }

  await triggerPopupAndHoverItem("#cc-name", 0);
  await confirmClear("#cc-name");
  await checkIsFormCleared();
});

</script>

<p id="display"></p>

<div id="content">

  <form id="form1">
    <p>This is a basic form.</p>
    <p><label>organization: <input id="organization" autocomplete="organization"></label></p>
    <p><label>streetAddress: <input id="street-address" autocomplete="street-address"></label></p>
    <p><label>tel: <input id="tel" autocomplete="tel"></label></p>
    <p><label>country: <input id="country" autocomplete="country"></label></p>

    <p><label>Name: <input id="cc-name" autocomplete="cc-name"></label></p>
    <p><label>Card Number: <input id="cc-number" autocomplete="cc-number"></label></p>
    <p><label>Expiration month: <input id="cc-exp-month" autocomplete="cc-exp-month"></label></p>
    <p><label>Expiration year: <input id="cc-exp-year" autocomplete="cc-exp-year"></label></p>
    <p><label>CSC: <input id="cc-csc" autocomplete="cc-csc"></label></p>
  </form>

</div>

<pre id="test"></pre>
</body>
</html>