summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/mochitest/formautofill_common.js
blob: d65f0beb4921b2cc08b518e6285f5aafc2f8d6a1 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* import-globals-from ../../../../../testing/mochitest/tests/SimpleTest/SimpleTest.js */
/* import-globals-from ../../../../../testing/mochitest/tests/SimpleTest/EventUtils.js */
/* import-globals-from ../../../../../toolkit/components/satchel/test/satchel_common.js */
/* eslint-disable no-unused-vars */

"use strict";

let formFillChromeScript;
let defaultTextColor;
let defaultDisabledTextColor;
let expectingPopup = null;

const { FormAutofillUtils } = SpecialPowers.ChromeUtils.importESModule(
  "resource://gre/modules/shared/FormAutofillUtils.sys.mjs"
);

async function sleep(ms = 500, reason = "Intentionally wait for UI ready") {
  SimpleTest.requestFlakyTimeout(reason);
  await new Promise(resolve => setTimeout(resolve, ms));
}

async function focusAndWaitForFieldsIdentified(
  input,
  mustBeIdentified = false
) {
  info("expecting the target input being focused and indentified");
  if (typeof input === "string") {
    input = document.querySelector(input);
  }
  const rootElement = input.form || input.ownerDocument.documentElement;
  const previouslyFocused = input != document.activeElement;

  input.focus();

  if (mustBeIdentified) {
    rootElement.removeAttribute("test-formautofill-identified");
  }
  if (rootElement.hasAttribute("test-formautofill-identified")) {
    return;
  }
  if (!previouslyFocused) {
    await new Promise(resolve => {
      formFillChromeScript.addMessageListener(
        "FormAutofillTest:FieldsIdentified",
        function onIdentified() {
          formFillChromeScript.removeMessageListener(
            "FormAutofillTest:FieldsIdentified",
            onIdentified
          );
          resolve();
        }
      );
    });
  }
  // In order to ensure that "markAsAutofillField" is fully executed, a short period
  // of timeout is still required.
  await sleep(300, "Guarantee asynchronous identifyAutofillFields is invoked");
  rootElement.setAttribute("test-formautofill-identified", "true");
}

async function setInput(selector, value, userInput = false) {
  const input = document.querySelector("input" + selector);
  if (userInput) {
    SpecialPowers.wrap(input).setUserInput(value);
  } else {
    input.value = value;
  }
  await focusAndWaitForFieldsIdentified(input);

  return input;
}

function clickOnElement(selector) {
  let element = document.querySelector(selector);

  if (!element) {
    throw new Error("Can not find the element");
  }

  SimpleTest.executeSoon(() => element.click());
}

// The equivalent helper function to getAdaptedProfiles in FormAutofillHandler.jsm that
// transforms the given profile to expected filled profile.
function _getAdaptedProfile(profile) {
  const adaptedProfile = Object.assign({}, profile);

  if (profile["street-address"]) {
    adaptedProfile["street-address"] = FormAutofillUtils.toOneLineAddress(
      profile["street-address"]
    );
  }

  return adaptedProfile;
}

async function checkFieldHighlighted(elem, expectedValue) {
  let isHighlightApplied;
  await SimpleTest.promiseWaitForCondition(function checkHighlight() {
    isHighlightApplied = elem.matches(":autofill");
    return isHighlightApplied === expectedValue;
  }, `Checking #${elem.id} highlight style`);

  is(isHighlightApplied, expectedValue, `Checking #${elem.id} highlight style`);
}

async function checkFieldPreview(elem, expectedValue) {
  is(
    SpecialPowers.wrap(elem).previewValue,
    expectedValue,
    `Checking #${elem.id} previewValue`
  );
  let isTextColorApplied;
  await SimpleTest.promiseWaitForCondition(function checkPreview() {
    const computedStyle = window.getComputedStyle(elem);
    const actualColor = computedStyle.getPropertyValue("color");
    if (elem.disabled) {
      isTextColorApplied = actualColor !== defaultDisabledTextColor;
    } else {
      isTextColorApplied = actualColor !== defaultTextColor;
    }
    return isTextColorApplied === !!expectedValue;
  }, `Checking #${elem.id} preview style`);

  is(isTextColorApplied, !!expectedValue, `Checking #${elem.id} preview style`);
}

async function checkFormFieldsStyle(profile, isPreviewing = true) {
  const elems = document.querySelectorAll("input, select");

  for (const elem of elems) {
    let fillableValue;
    let previewValue;
    let isElementEligible =
      FormAutofillUtils.isCreditCardOrAddressFieldType(elem) &&
      FormAutofillUtils.isFieldAutofillable(elem);
    if (!isElementEligible) {
      fillableValue = "";
      previewValue = "";
    } else {
      fillableValue = profile && profile[elem.id];
      previewValue = (isPreviewing && fillableValue) || "";
    }
    await checkFieldHighlighted(elem, !!fillableValue);
    await checkFieldPreview(elem, previewValue);
  }
}

function checkFieldValue(elem, expectedValue) {
  if (typeof elem === "string") {
    elem = document.querySelector(elem);
  }
  is(elem.value, String(expectedValue), "Checking " + elem.id + " field");
}

async function triggerAutofillAndCheckProfile(profile) {
  let adaptedProfile = _getAdaptedProfile(profile);
  const promises = [];
  for (const [fieldName, value] of Object.entries(adaptedProfile)) {
    info(`triggerAutofillAndCheckProfile: ${fieldName}`);
    const element = document.getElementById(fieldName);
    const expectingEvent =
      document.activeElement == element ? "input" : "change";
    const checkFieldAutofilled = Promise.all([
      new Promise(resolve => {
        let beforeInputFired = false;
        let hadEditor = SpecialPowers.wrap(element).hasEditor;
        element.addEventListener(
          "beforeinput",
          event => {
            beforeInputFired = true;
            is(
              event.inputType,
              "insertReplacementText",
              'inputType value should be "insertReplacementText"'
            );
            is(
              event.data,
              String(value),
              `data value of "beforeinput" should be "${value}"`
            );
            is(
              event.dataTransfer,
              null,
              'dataTransfer of "beforeinput" should be null'
            );
            is(
              event.getTargetRanges().length,
              0,
              'getTargetRanges() of "beforeinput" should return empty array'
            );
            is(
              event.cancelable,
              SpecialPowers.getBoolPref(
                "dom.input_event.allow_to_cancel_set_user_input"
              ),
              `"beforeinput" event should be cancelable on ${element.tagName} unless it's suppressed by the pref`
            );
            is(
              event.bubbles,
              true,
              `"beforeinput" event should always bubble on ${element.tagName}`
            );
            resolve();
          },
          { once: true }
        );
        element.addEventListener(
          "input",
          event => {
            if (element.tagName == "INPUT" && element.type == "text") {
              if (hadEditor) {
                ok(
                  beforeInputFired,
                  `"beforeinput" event should've been fired before "input" event on ${element.tagName}`
                );
              } else {
                ok(
                  beforeInputFired,
                  `"beforeinput" event should've been fired before "input" event on ${element.tagName}`
                );
              }
              ok(
                event instanceof InputEvent,
                `"input" event should be dispatched with InputEvent interface on ${element.tagName}`
              );
              is(
                event.inputType,
                "insertReplacementText",
                'inputType value should be "insertReplacementText"'
              );
              is(event.data, String(value), `data value should be "${value}"`);
              is(event.dataTransfer, null, "dataTransfer should be null");
              is(
                event.getTargetRanges().length,
                0,
                "getTargetRanges() should return empty array"
              );
            } else {
              ok(
                !beforeInputFired,
                `"beforeinput" event shouldn't be fired on ${element.tagName}`
              );
              ok(
                event instanceof Event && !(event instanceof UIEvent),
                `"input" event should be dispatched with Event interface on ${element.tagName}`
              );
            }
            is(
              event.cancelable,
              false,
              `"input" event should be never cancelable on ${element.tagName}`
            );
            is(
              event.bubbles,
              true,
              `"input" event should always bubble on ${element.tagName}`
            );
            resolve();
          },
          { once: true }
        );
      }),
      new Promise(resolve =>
        element.addEventListener(expectingEvent, resolve, { once: true })
      ),
    ]).then(() => checkFieldValue(element, value));

    promises.push(checkFieldAutofilled);
  }
  // Press Enter key and trigger form autofill.
  synthesizeKey("KEY_Enter");

  return Promise.all(promises);
}

async function onStorageChanged(type) {
  info(`expecting the storage changed: ${type}`);
  return new Promise(resolve => {
    formFillChromeScript.addMessageListener(
      "formautofill-storage-changed",
      function onChanged(data) {
        formFillChromeScript.removeMessageListener(
          "formautofill-storage-changed",
          onChanged
        );
        is(data.data, type, `Receive ${type} storage changed event`);
        resolve();
      }
    );
  });
}

function checkMenuEntries(expectedValues, isFormAutofillResult = true) {
  let actualValues = getMenuEntries();
  // Expect one more item would appear at the bottom as the footer if the result is from form autofill.
  let expectedLength = isFormAutofillResult
    ? expectedValues.length + 1
    : expectedValues.length;

  is(actualValues.length, expectedLength, " Checking length of expected menu");
  for (let i = 0; i < expectedValues.length; i++) {
    is(actualValues[i], expectedValues[i], " Checking menu entry #" + i);
  }
}

function invokeAsyncChromeTask(message, payload = {}) {
  info(`expecting the chrome task finished: ${message}`);
  return formFillChromeScript.sendQuery(message, payload);
}

async function addAddress(address) {
  await invokeAsyncChromeTask("FormAutofillTest:AddAddress", { address });
  await sleep();
}

async function removeAddress(guid) {
  return invokeAsyncChromeTask("FormAutofillTest:RemoveAddress", { guid });
}

async function updateAddress(guid, address) {
  return invokeAsyncChromeTask("FormAutofillTest:UpdateAddress", {
    address,
    guid,
  });
}

async function checkAddresses(expectedAddresses) {
  return invokeAsyncChromeTask("FormAutofillTest:CheckAddresses", {
    expectedAddresses,
  });
}

async function cleanUpAddresses() {
  return invokeAsyncChromeTask("FormAutofillTest:CleanUpAddresses");
}

async function addCreditCard(creditcard) {
  await invokeAsyncChromeTask("FormAutofillTest:AddCreditCard", { creditcard });
  await sleep();
}

async function removeCreditCard(guid) {
  return invokeAsyncChromeTask("FormAutofillTest:RemoveCreditCard", { guid });
}

async function checkCreditCards(expectedCreditCards) {
  return invokeAsyncChromeTask("FormAutofillTest:CheckCreditCards", {
    expectedCreditCards,
  });
}

async function cleanUpCreditCards() {
  return invokeAsyncChromeTask("FormAutofillTest:CleanUpCreditCards");
}

async function cleanUpStorage() {
  await cleanUpAddresses();
  await cleanUpCreditCards();
}

async function canTestOSKeyStoreLogin() {
  let { canTest } = await invokeAsyncChromeTask(
    "FormAutofillTest:CanTestOSKeyStoreLogin"
  );
  return canTest;
}

async function waitForOSKeyStoreLogin(login = false) {
  await invokeAsyncChromeTask("FormAutofillTest:OSKeyStoreLogin", { login });
}

function patchRecordCCNumber(record) {
  const number = record["cc-number"];
  const ccNumberFmt = {
    affix: "****",
    label: number.substr(-4),
  };

  return Object.assign({}, record, { ccNumberFmt });
}

// Utils for registerPopupShownListener(in satchel_common.js) that handles dropdown popup
// Please call "initPopupListener()" in your test and "await expectPopup()"
// if you want to wait for dropdown menu displayed.
function expectPopup() {
  info("expecting a popup");
  return new Promise(resolve => {
    expectingPopup = resolve;
  });
}

function notExpectPopup(ms = 500) {
  info("not expecting a popup");
  return new Promise((resolve, reject) => {
    expectingPopup = reject.bind(this, "Unexpected Popup");
    // TODO: We don't have an event to notify no popup showing, so wait for 500
    // ms (in default) to predict any unexpected popup showing.
    setTimeout(resolve, ms);
  });
}

function popupShownListener() {
  info("popup shown for test ");
  if (expectingPopup) {
    expectingPopup();
    expectingPopup = null;
  }
}

function initPopupListener() {
  registerPopupShownListener(popupShownListener);
}

async function triggerPopupAndHoverItem(fieldSelector, selectIndex) {
  await focusAndWaitForFieldsIdentified(fieldSelector);
  synthesizeKey("KEY_ArrowDown");
  await expectPopup();
  for (let i = 0; i <= selectIndex; i++) {
    synthesizeKey("KEY_ArrowDown");
  }
  await notifySelectedIndex(selectIndex);
}

function formAutoFillCommonSetup() {
  // Remove the /creditCard path segement when referenced from the 'creditCard' subdirectory.
  let chromeURL = SimpleTest.getTestFileURL(
    "formautofill_parent_utils.js"
  ).replace(/\/creditCard/, "");
  formFillChromeScript = SpecialPowers.loadChromeScript(chromeURL);
  formFillChromeScript.addMessageListener("onpopupshown", ({ results }) => {
    gLastAutoCompleteResults = results;
    if (gPopupShownListener) {
      gPopupShownListener({ results });
    }
  });

  add_setup(async () => {
    info(`expecting the storage setup`);
    await formFillChromeScript.sendQuery("setup");
  });

  SimpleTest.registerCleanupFunction(async () => {
    info(`expecting the storage cleanup`);
    await formFillChromeScript.sendQuery("cleanup");

    formFillChromeScript.destroy();
    expectingPopup = null;
  });

  document.addEventListener(
    "DOMContentLoaded",
    function () {
      defaultTextColor = window
        .getComputedStyle(document.querySelector("input"))
        .getPropertyValue("color");

      // This is needed for test_formautofill_preview_highlight.html to work properly
      let disabledInput = document.querySelector(`input[disabled]`);
      if (disabledInput) {
        defaultDisabledTextColor = window
          .getComputedStyle(disabledInput)
          .getPropertyValue("color");
      }
    },
    { once: true }
  );
}

/*
 * Extremely over-simplified detection of card type from card number just for
 * our tests. This is needed to test the aria-label of credit card menu entries.
 */
function getCCTypeName(creditCard) {
  return creditCard["cc-number"][0] == "4" ? "Visa" : "MasterCard";
}

formAutoFillCommonSetup();