Commit 43095009 authored by Luke Chang's avatar Luke Chang
Browse files

Bug 1364818 - [Form Autofill] popup won't apply to an auto-focused input until...

Bug 1364818 - [Form Autofill] popup won't apply to an auto-focused input until it's refocused. r=MattN

MozReview-Commit-ID: H3CZEFzAJm6

--HG--
extra : rebase_source : 9b00296a179f1c4004e245357fbaad94b6c8c541
parent 6142d3e7
Loading
Loading
Loading
Loading
+11 −1
Original line number Original line Diff line number Diff line
@@ -41,10 +41,20 @@ var FormAutofillFrameScript = {
    switch (evt.type) {
    switch (evt.type) {
      case "focusin": {
      case "focusin": {
        let element = evt.target;
        let element = evt.target;
        let doc = element.ownerDocument;

        if (!FormAutofillUtils.isFieldEligibleForAutofill(element)) {
        if (!FormAutofillUtils.isFieldEligibleForAutofill(element)) {
          return;
          return;
        }
        }
        FormAutofillContent.identifyAutofillFields(element.ownerDocument);

        let doIdentifyAutofillFields =
          () => setTimeout(() => FormAutofillContent.identifyAutofillFields(doc));

        if (doc.readyState === "loading") {
          doc.addEventListener("DOMContentLoaded", doIdentifyAutofillFields, {once: true});
        } else {
          doIdentifyAutofillFields();
        }
        break;
        break;
      }
      }
    }
    }
+8 −0
Original line number Original line Diff line number Diff line
@@ -10,6 +10,14 @@ function setInput(selector, value) {
  let input = document.querySelector("input" + selector);
  let input = document.querySelector("input" + selector);
  input.value = value;
  input.value = value;
  input.focus();
  input.focus();

  // "identifyAutofillFields" is invoked asynchronously in "focusin" event. We
  // should make sure fields are ready for popup before doing tests.
  //
  // TODO: "setTimeout" is used here temporarily because there's no event to
  //       notify us of the state of "identifyAutofillFields" for now. We should
  //       figure out a better way after the heuristics land.
  return new Promise(resolve => setTimeout(resolve));
}
}


function checkMenuEntries(expectedValues) {
function checkMenuEntries(expectedValues) {
+0 −1
Original line number Original line Diff line number Diff line
@@ -43,7 +43,6 @@ var ParentUtils = {
  },
  },
};
};


ParentUtils.cleanUpAddress();
Services.obs.addObserver(ParentUtils, "formautofill-storage-changed");
Services.obs.addObserver(ParentUtils, "formautofill-storage-changed");


addMessageListener("FormAutofillTest:AddAddress", (msg) => {
addMessageListener("FormAutofillTest:AddAddress", (msg) => {
+1 −1
Original line number Original line Diff line number Diff line
@@ -5,5 +5,5 @@ support-files =
  formautofill_common.js
  formautofill_common.js
  formautofill_parent_utils.js
  formautofill_parent_utils.js


[test_autofocus_form.html]
[test_basic_autocomplete_form.html]
[test_basic_autocomplete_form.html]
+86 −0
Original line number Original line Diff line number Diff line
<!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: autocomplete on an autofocus form

<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",
}, {
  organization: "Mozilla",
  "street-address": "331 E. Evelyn Avenue",
  tel: "1-650-903-0800",
}];

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

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

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

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

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>
    <script>
      // Focuses the input before DOMContentLoaded
      document.getElementById("organization").focus();
    </script>
    <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>country: <input id="country" name="country" autocomplete="country" type="text"></label></p>
  </form>

</div>

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