browser/extensions/formautofill/test/mochitest/test_basic_autocomplete_form.html
author Ray Lin <ralin@mozilla.com>
Thu, 01 Jun 2017 21:53:37 +0800
changeset 415309 c104436d1937cf3695b972238eab7b8ae3452df9
parent 415183 2ab8812c2505ec59896bbab9a020cfa3159ee4e3
child 415334 7a729935d60d6eb4d0f7b58ea6307634badd4c9e
permissions -rw-r--r--
Bug 1300995 - Part 1. Add a footer on formautofill popup to let users open a preferences privacy tab when click on it. r=MattN MozReview-Commit-ID: Izr6IbHlkLY

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test basic autofill</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="formautofill_common.js"></script>
  <script type="text/javascript" src="satchel_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
Form autofill test: simple form address autofill

<script>
/* import-globals-from ../../../../../testing/mochitest/tests/SimpleTest/SpawnTask.js */
/* import-globals-from ../../../../../toolkit/components/satchel/test/satchel_common.js */
/* import-globals-from formautofill_common.js */

"use strict";

let expectingPopup = null;
let MOCK_STORAGE = [{
  organization: "Sesame Street",
  "street-address": "123 Sesame Street.",
  tel: "1-345-345-3456",
  country: "US",
}, {
  organization: "Mozilla",
  "street-address": "331 E. Evelyn Avenue",
  tel: "1-650-903-0800",
  country: "US",
}];

function expectPopup() {
  info("expecting a popup");
  return new Promise(resolve => {
    expectingPopup = resolve;
  });
}

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

function checkElementFilled(element, expectedvalue) {
  return [
    new Promise(resolve => {
      element.addEventListener("input", function onInput() {
        ok(true, "Checking " + element.name + " field fires input event");
        resolve();
      }, {once: true});
    }),
    new Promise(resolve => {
      element.addEventListener("change", function onChange() {
        ok(true, "Checking " + element.name + " field fires change event");
        is(element.value, expectedvalue, "Checking " + element.name + " field");
        resolve();
      }, {once: true});
    }),
  ];
}

function checkAutoCompleteInputFilled(element, expectedvalue) {
  return new Promise(resolve => {
    element.addEventListener("DOMAutoComplete", function onChange() {
      is(element.value, expectedvalue, "Checking " + element.name + " field");
      resolve();
    }, {once: true});
  });
}

function checkFormFilled(address) {
  let promises = [];
  for (let prop in address) {
    let element = document.getElementById(prop);
    if (document.activeElement == element) {
      promises.push(checkAutoCompleteInputFilled(element, address[prop]));
    } else {
      promises.push(...checkElementFilled(element, address[prop]));
    }
  }
  doKey("return");
  return Promise.all(promises);
}

async function setupAddressStorage() {
  await addAddress(MOCK_STORAGE[0]);
  await addAddress(MOCK_STORAGE[1]);
}

async function setupFormHistory() {
  await updateFormHistory([
    {op: "add", fieldname: "tel", value: "1-234-567-890"},
    {op: "add", fieldname: "email", value: "foo@mozilla.com"},
  ]);
}

// Form with history only.
add_task(async function history_only_menu_checking() {
  await setupFormHistory();

  await setInput("#tel", "");
  doKey("down");
  await expectPopup();
  checkMenuEntries(["1-234-567-890"], false);
});

// Form with both history and address storage.
add_task(async function check_menu_when_both_existed() {
  await setupAddressStorage();

  await setInput("#organization", "");
  doKey("down");
  await expectPopup();
  checkMenuEntries(MOCK_STORAGE.map(address =>
    JSON.stringify({primary: address.organization, secondary: address["street-address"]})
  ));

  await setInput("#street-address", "");
  doKey("down");
  await expectPopup();
  checkMenuEntries(MOCK_STORAGE.map(address =>
    JSON.stringify({primary: address["street-address"], secondary: address.organization})
  ));

  await setInput("#tel", "");
  doKey("down");
  await expectPopup();
  checkMenuEntries(MOCK_STORAGE.map(address =>
    JSON.stringify({primary: address.tel, secondary: address["street-address"]})
  ));
});

// Display history search result if no matched data in addresses.
add_task(async function check_fallback_for_mismatched_field() {
  await setInput("#email", "");
  doKey("down");
  await expectPopup();
  checkMenuEntries(["foo@mozilla.com"], false);
});

// Autofill the address from dropdown menu.
add_task(async function check_fields_after_form_autofill() {
  await setInput("#organization", "Moz");
  doKey("down");
  await expectPopup();
  checkMenuEntries(MOCK_STORAGE.map(address =>
    JSON.stringify({primary: address.organization, secondary: address["street-address"]})
  ).slice(1));
  doKey("down");
  await checkFormFilled(MOCK_STORAGE[1]);
});

// Fallback to history search after autofill address.
add_task(async function check_fallback_after_form_autofill() {
  await setInput("#tel", "");
  doKey("down");
  await expectPopup();
  checkMenuEntries(["1-234-567-890"], false);
});

registerPopupShownListener(popupShownListener);

</script>

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

<div id="content">

  <form id="form1">
    <p>This is a basic form.</p>
    <p><label>organization: <input id="organization" name="organization" autocomplete="organization" type="text"></label></p>
    <p><label>streetAddress: <input id="street-address" name="street-address" autocomplete="street-address" type="text"></label></p>
    <p><label>tel: <input id="tel" name="tel" autocomplete="tel" type="text"></label></p>
    <p><label>email: <input id="email" name="email" autocomplete="email" type="text"></label></p>
    <p><label>country: <select id="country" name="country" autocomplete="country">
      <option/>
      <option value="US">United States</option>
    </label></p>
  </form>

</div>

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