Bug 1422692 - Make search result always return success code for every filled fields to show clear form button popup. r=lchang draft
authorRay Lin <ralin@mozilla.com>
Tue, 05 Dec 2017 13:57:22 +0800
changeset 707343 a9cab1346d295fe2f1fc2c7c3b191c3e90e08951
parent 707249 f2cf6d1473808039be5ecd8727cc3791d5d7d2d4
child 742920 6e10ce5d698c07d66528e53189d637d3c50cba4d
push id92092
push userbmo:ralin@mozilla.com
push dateTue, 05 Dec 2017 06:01:48 +0000
reviewerslchang
bugs1422692
milestone59.0a1
Bug 1422692 - Make search result always return success code for every filled fields to show clear form button popup. r=lchang MozReview-Commit-ID: 6D4U2ZFf2QJ
browser/extensions/formautofill/FormAutofillContent.jsm
browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
browser/extensions/formautofill/test/mochitest/test_clear_form.html
--- a/browser/extensions/formautofill/FormAutofillContent.jsm
+++ b/browser/extensions/formautofill/FormAutofillContent.jsm
@@ -106,43 +106,51 @@ AutofillProfileAutoCompleteSearch.protot
     let isAddressField = FormAutofillUtils.isAddressField(info.fieldName);
     let isInputAutofilled = info.state == FIELD_STATES.AUTO_FILLED;
     let handler = FormAutofillContent.getFormHandler(focusedInput);
     let allFieldNames = handler.getAllFieldNames(focusedInput);
     let filledRecordGUID = handler.getFilledRecordGUID(focusedInput);
     let searchPermitted = isAddressField ?
                           FormAutofillUtils.isAutofillAddressesEnabled :
                           FormAutofillUtils.isAutofillCreditCardsEnabled;
+    let AutocompleteResult = isAddressField ? AddressResult : CreditCardResult;
 
     ProfileAutocomplete.lastProfileAutoCompleteFocusedInput = focusedInput;
     // Fallback to form-history if ...
     //   - specified autofill feature is pref off.
     //   - no profile can fill the currently-focused input.
     //   - the current form has already been populated.
     //   - (address only) less than 3 inputs are covered by all saved fields in the storage.
     if (!searchPermitted || !savedFieldNames.has(info.fieldName) ||
         (!isInputAutofilled && filledRecordGUID) || (isAddressField &&
         allFieldNames.filter(field => savedFieldNames.has(field)).length < FormAutofillUtils.AUTOFILL_FIELDS_THRESHOLD)) {
       if (focusedInput.autocomplete == "off") {
-        // Create a dummy AddressResult as an empty search result.
-        let result = new AddressResult("", "", [], [], {});
+        // Create a dummy result as an empty search result.
+        let result = new AutocompleteResult("", "", [], [], {});
         listener.onSearchResult(this, result);
         return;
       }
       let formHistory = Cc["@mozilla.org/autocomplete/search;1?name=form-history"]
                           .createInstance(Ci.nsIAutoCompleteSearch);
       formHistory.startSearch(searchString, searchParam, previousResult, {
         onSearchResult: (search, result) => {
           listener.onSearchResult(this, result);
           ProfileAutocomplete.lastProfileAutoCompleteResult = result;
         },
       });
       return;
     }
 
+    if (isInputAutofilled) {
+      let result = new AutocompleteResult(searchString, "", [], [], {isInputAutofilled});
+      listener.onSearchResult(this, result);
+      ProfileAutocomplete.lastProfileAutoCompleteResult = result;
+      return;
+    }
+
     let infoWithoutElement = Object.assign({}, info);
     delete infoWithoutElement.elementWeakRef;
 
     let data = {
       collectionName: isAddressField ? ADDRESSES_COLLECTION_NAME : CREDITCARDS_COLLECTION_NAME,
       info: infoWithoutElement,
       searchString,
     };
@@ -151,31 +159,23 @@ AutofillProfileAutoCompleteSearch.protot
       if (this.forceStop) {
         return;
       }
       // Sort addresses by timeLastUsed for showing the lastest used address at top.
       records.sort((a, b) => b.timeLastUsed - a.timeLastUsed);
 
       let adaptedRecords = handler.getAdaptedProfiles(records, focusedInput);
       let result = null;
-      if (isAddressField) {
-        result = new AddressResult(searchString,
-                                   info.fieldName,
-                                   allFieldNames,
-                                   adaptedRecords,
-                                   {isInputAutofilled});
-      } else {
-        let isSecure = InsecurePasswordUtils.isFormSecure(handler.form);
+      let isSecure = InsecurePasswordUtils.isFormSecure(handler.form);
 
-        result = new CreditCardResult(searchString,
+      result = new AutocompleteResult(searchString,
                                       info.fieldName,
                                       allFieldNames,
                                       adaptedRecords,
                                       {isSecure, isInputAutofilled});
-      }
       listener.onSearchResult(this, result);
       ProfileAutocomplete.lastProfileAutoCompleteResult = result;
     });
   },
 
   /**
    * Stops an asynchronous search that is in progress
    */
--- a/browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
+++ b/browser/extensions/formautofill/ProfileAutoCompleteResult.jsm
@@ -48,16 +48,21 @@ class ProfileAutoCompleteResult {
     this._allFieldNames = [...this._matchingProfiles.reduce((fieldSet, curProfile) => {
       for (let field of Object.keys(curProfile)) {
         fieldSet.add(field);
       }
 
       return fieldSet;
     }, new Set())].filter(field => allFieldNames.includes(field));
 
+    // Force return success code if the focused field is auto-filled in order
+    // to show clear form button popup.
+    if (isInputAutofilled) {
+      resultCode = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
+    }
     // The result code of this result object.
     if (resultCode) {
       this.searchResult = resultCode;
     } else if (matchingProfiles.length > 0) {
       this.searchResult = Ci.nsIAutoCompleteResult.RESULT_SUCCESS;
     } else {
       this.searchResult = Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
     }
--- a/browser/extensions/formautofill/test/mochitest/test_clear_form.html
+++ b/browser/extensions/formautofill/test/mochitest/test_clear_form.html
@@ -16,17 +16,17 @@ Form autofill test: clear form button
 /* 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";
 
 const MOCK_STORAGE = [{
   organization: "Sesame Street",
-  "street-address": "123 Sesame Street.",
+  "street-address": "2 Harrison St\nline2\nline3",
   tel: "+13453453456",
 }, {
   organization: "Mozilla",
   "street-address": "331 E. Evelyn Avenue",
 }, {
   organization: "Tel org",
   tel: "+12223334444",
 }];
@@ -54,16 +54,25 @@ add_task(async function simple_clear() {
   await triggerPopupAndHoverItem("#organization", 0);
   await triggerAutofillAndCheckProfile(MOCK_STORAGE[0]);
 
   await triggerPopupAndHoverItem("#tel", 0);
   doKey("return");
   checkIsFormCleared();
 });
 
+add_task(async function clear_adapted_record() {
+  await triggerPopupAndHoverItem("#street-address", 0);
+  await triggerAutofillAndCheckProfile(MOCK_STORAGE[0]);
+
+  await triggerPopupAndHoverItem("#street-address", 0);
+  doKey("return");
+  checkIsFormCleared();
+});
+
 add_task(async function clear_modified_form() {
   await triggerPopupAndHoverItem("#organization", 0);
   await triggerAutofillAndCheckProfile(MOCK_STORAGE[0]);
 
   await setInput("#tel", "+1111111111", true);
 
   await triggerPopupAndHoverItem("#street-address", 0);
   doKey("return");