From b2a8614398fbc401b1847eaaffe051aa3e2d01cb Mon Sep 17 00:00:00 2001 From: Luke Chang Date: Mon, 15 May 2017 13:26:25 +0800 Subject: [PATCH] Bug 1364818 - [Form Autofill] popup won't apply to an auto-focused input until it's refocused. r=MattN MozReview-Commit-ID: H3CZEFzAJm6 --- .../content/FormAutofillFrameScript.js | 12 ++- .../test/mochitest/formautofill_common.js | 8 ++ .../mochitest/formautofill_parent_utils.js | 1 - .../formautofill/test/mochitest/mochitest.ini | 2 +- .../test/mochitest/test_autofocus_form.html | 86 +++++++++++++++++++ .../test_basic_autocomplete_form.html | 14 +-- 6 files changed, 113 insertions(+), 10 deletions(-) create mode 100644 browser/extensions/formautofill/test/mochitest/test_autofocus_form.html diff --git a/browser/extensions/formautofill/content/FormAutofillFrameScript.js b/browser/extensions/formautofill/content/FormAutofillFrameScript.js index e49a0f333d63..6e01aaa5583d 100644 --- a/browser/extensions/formautofill/content/FormAutofillFrameScript.js +++ b/browser/extensions/formautofill/content/FormAutofillFrameScript.js @@ -41,10 +41,20 @@ var FormAutofillFrameScript = { switch (evt.type) { case "focusin": { let element = evt.target; + let doc = element.ownerDocument; + if (!FormAutofillUtils.isFieldEligibleForAutofill(element)) { return; } - FormAutofillContent.identifyAutofillFields(element.ownerDocument); + + let doIdentifyAutofillFields = + () => setTimeout(() => FormAutofillContent.identifyAutofillFields(doc)); + + if (doc.readyState === "loading") { + doc.addEventListener("DOMContentLoaded", doIdentifyAutofillFields, {once: true}); + } else { + doIdentifyAutofillFields(); + } break; } } diff --git a/browser/extensions/formautofill/test/mochitest/formautofill_common.js b/browser/extensions/formautofill/test/mochitest/formautofill_common.js index 45856217289e..6ae85ed02d30 100644 --- a/browser/extensions/formautofill/test/mochitest/formautofill_common.js +++ b/browser/extensions/formautofill/test/mochitest/formautofill_common.js @@ -10,6 +10,14 @@ function setInput(selector, value) { let input = document.querySelector("input" + selector); input.value = value; 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) { diff --git a/browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js b/browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js index 56da0f8cf491..7887d5a5d973 100644 --- a/browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js +++ b/browser/extensions/formautofill/test/mochitest/formautofill_parent_utils.js @@ -43,7 +43,6 @@ var ParentUtils = { }, }; -ParentUtils.cleanUpAddress(); Services.obs.addObserver(ParentUtils, "formautofill-storage-changed"); addMessageListener("FormAutofillTest:AddAddress", (msg) => { diff --git a/browser/extensions/formautofill/test/mochitest/mochitest.ini b/browser/extensions/formautofill/test/mochitest/mochitest.ini index 151ce246916e..15c7f760dcb1 100644 --- a/browser/extensions/formautofill/test/mochitest/mochitest.ini +++ b/browser/extensions/formautofill/test/mochitest/mochitest.ini @@ -5,5 +5,5 @@ support-files = formautofill_common.js formautofill_parent_utils.js +[test_autofocus_form.html] [test_basic_autocomplete_form.html] - diff --git a/browser/extensions/formautofill/test/mochitest/test_autofocus_form.html b/browser/extensions/formautofill/test/mochitest/test_autofocus_form.html new file mode 100644 index 000000000000..880fc4fb214e --- /dev/null +++ b/browser/extensions/formautofill/test/mochitest/test_autofocus_form.html @@ -0,0 +1,86 @@ + + + + + Test basic autofill + + + + + + + +Form autofill test: autocomplete on an autofocus form + + + +

+ +
+ +
+

This is a basic form.

+

+ +

+

+

+
+ +
+ +

+
+
diff --git a/browser/extensions/formautofill/test/mochitest/test_basic_autocomplete_form.html b/browser/extensions/formautofill/test/mochitest/test_basic_autocomplete_form.html
index ef1618d57bdb..bd923e4c7d8b 100644
--- a/browser/extensions/formautofill/test/mochitest/test_basic_autocomplete_form.html
+++ b/browser/extensions/formautofill/test/mochitest/test_basic_autocomplete_form.html
@@ -95,7 +95,7 @@ async function setupFormHistory() {
 add_task(async function history_only_menu_checking() {
   await setupFormHistory();
 
-  setInput("#tel", "");
+  await setInput("#tel", "");
   doKey("down");
   await expectPopup();
   checkMenuEntries(["1-234-567-890"]);
@@ -105,21 +105,21 @@ add_task(async function history_only_menu_checking() {
 add_task(async function check_menu_when_both_existed() {
   await setupAddressStorage();
 
-  setInput("#organization", "");
+  await setInput("#organization", "");
   doKey("down");
   await expectPopup();
   checkMenuEntries(MOCK_STORAGE.map(address =>
     JSON.stringify({primary: address.organization, secondary: address["street-address"]})
   ));
 
-  setInput("#street-address", "");
+  await setInput("#street-address", "");
   doKey("down");
   await expectPopup();
   checkMenuEntries(MOCK_STORAGE.map(address =>
     JSON.stringify({primary: address["street-address"], secondary: address.organization})
   ));
 
-  setInput("#tel", "");
+  await setInput("#tel", "");
   doKey("down");
   await expectPopup();
   checkMenuEntries(MOCK_STORAGE.map(address =>
@@ -129,7 +129,7 @@ add_task(async function check_menu_when_both_existed() {
 
 // Display history search result if no matched data in addresses.
 add_task(async function check_fallback_for_mismatched_field() {
-  setInput("#email", "");
+  await setInput("#email", "");
   doKey("down");
   await expectPopup();
   checkMenuEntries(["foo@mozilla.com"]);
@@ -137,7 +137,7 @@ add_task(async function check_fallback_for_mismatched_field() {
 
 // Autofill the address from dropdown menu.
 add_task(async function check_fields_after_form_autofill() {
-  setInput("#organization", "Moz");
+  await setInput("#organization", "Moz");
   doKey("down");
   await expectPopup();
   checkMenuEntries(MOCK_STORAGE.map(address =>
@@ -149,7 +149,7 @@ add_task(async function check_fields_after_form_autofill() {
 
 // Fallback to history search after autofill address.
 add_task(async function check_fallback_after_form_autofill() {
-  setInput("#tel", "");
+  await setInput("#tel", "");
   doKey("down");
   await expectPopup();
   checkMenuEntries(["1-234-567-890"]);