Bug 720598 - nsAutocompleteController HandleKeyNavigation() and EnterMatch() call GetDefaultCompleteValue() with a bogus aResultIndex.
authorMarco Bonardo <mbonardo@mozilla.com>
Thu, 26 Jan 2012 00:54:27 +0100
changeset 86636 1f2edf4aabf1cdd7d901d1a906d20a0594fe1f38
parent 86635 691af0af89da90c91ae9c007c4567ca3815b93ba
child 86637 c4a69ca8e7761666cb892d74aefd1efa6b5ee332
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs720598
milestone12.0a1
Bug 720598 - nsAutocompleteController HandleKeyNavigation() and EnterMatch() call GetDefaultCompleteValue() with a bogus aResultIndex. r=gavin
toolkit/components/autocomplete/nsAutoCompleteController.cpp
toolkit/components/autocomplete/tests/unit/test_completeDefaultIndex_casing.js
toolkit/components/autocomplete/tests/unit/xpcshell.ini
--- a/toolkit/components/autocomplete/nsAutoCompleteController.cpp
+++ b/toolkit/components/autocomplete/nsAutoCompleteController.cpp
@@ -519,17 +519,17 @@ nsAutoCompleteController::HandleKeyNavig
         // We usually try to preserve the casing of what user has typed, but
         // if he wants to autocomplete, we will replace the value with the
         // actual autocomplete result.
         // The user wants explicitely to use that result, so this ensures
         // association of the result with the autocompleted text.
         nsAutoString value;
         nsAutoString inputValue;
         input->GetTextValue(inputValue);
-        if (NS_SUCCEEDED(GetDefaultCompleteValue(selectedIndex, false, value)) &&
+        if (NS_SUCCEEDED(GetDefaultCompleteValue(-1, false, value)) &&
             value.Equals(inputValue, nsCaseInsensitiveStringComparator())) {
           input->SetTextValue(value);
           input->SelectTextRange(value.Length(), value.Length());
         }
       }
       // Close the pop-up even if nothing was selected
       ClearSearchTimer();
       ClosePopup();
@@ -1157,17 +1157,17 @@ nsAutoCompleteController::EnterMatch(boo
       // We usually try to preserve the casing of what user has typed, but
       // if he wants to autocomplete, we will replace the value with the
       // actual autocomplete result.
       // The user wants explicitely to use that result, so this ensures
       // association of the result with the autocompleted text.
       nsAutoString defaultIndexValue;
       nsAutoString inputValue;
       input->GetTextValue(inputValue);
-      if (NS_SUCCEEDED(GetDefaultCompleteValue(selectedIndex, false, defaultIndexValue)) &&
+      if (NS_SUCCEEDED(GetDefaultCompleteValue(-1, false, defaultIndexValue)) &&
           defaultIndexValue.Equals(inputValue, nsCaseInsensitiveStringComparator()))
         value = defaultIndexValue;
     }
 
     if (forceComplete && value.IsEmpty()) {
       // Since nothing was selected, and forceComplete is specified, that means
       // we have to find the first default match and enter it instead
       PRUint32 count = mResults.Count();
new file mode 100644
--- /dev/null
+++ b/toolkit/components/autocomplete/tests/unit/test_completeDefaultIndex_casing.js
@@ -0,0 +1,63 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function AutoCompleteResult(aValues) {
+  this._values = aValues;
+  this.defaultIndex = 0;
+}
+AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);
+
+function AutoCompleteInput(aSearches) {
+  this.searches = aSearches;
+  this.popup.selectedIndex = -1;
+  this.completeDefaultIndex = true;
+}
+AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype);
+
+function run_test() {
+  run_next_test();
+}
+
+add_test(function test_keyNavigation() {
+  doSearch("MOZ", "mozilla", function(aController) {
+    do_check_eq(aController.input.textValue, "MOZilla");
+    aController.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_RIGHT);
+    do_check_eq(aController.input.textValue, "mozilla");
+  });
+});
+
+add_test(function test_handleEnter() {
+  doSearch("MOZ", "mozilla", function(aController) {
+    do_check_eq(aController.input.textValue, "MOZilla");
+    aController.handleEnter(false);
+    do_check_eq(aController.input.textValue, "mozilla");
+  });
+});
+
+function doSearch(aSearchString, aResultValue, aOnCompleteCallback) {
+  let search = new AutoCompleteSearchBase("search",
+                                          new AutoCompleteResult([ "mozilla", "toolkit" ], 0));
+  registerAutoCompleteSearch(search);
+
+  let controller = Cc["@mozilla.org/autocomplete/controller;1"].
+                   getService(Ci.nsIAutoCompleteController);  
+  
+  // Make an AutoCompleteInput that uses our searches and confirms results.
+  let input = new AutoCompleteInput([ search.name ]);
+  input.textValue = aSearchString;
+
+  // Caret must be at the end for autofill to happen.
+  let strLen = aSearchString.length;
+  input.selectTextRange(strLen, strLen);
+  controller.input = input;
+  controller.startSearch(aSearchString);
+
+  input.onSearchComplete = function onSearchComplete() {
+    aOnCompleteCallback(controller);
+
+    // Clean up.
+    unregisterAutoCompleteSearch(search);
+    run_next_test();
+  };
+}
--- a/toolkit/components/autocomplete/tests/unit/xpcshell.ini
+++ b/toolkit/components/autocomplete/tests/unit/xpcshell.ini
@@ -5,11 +5,12 @@ tail =
 [test_330578.js]
 [test_378079.js]
 [test_393191.js]
 [test_440866.js]
 [test_463023.js]
 [test_660156.js]
 [test_autocomplete_multiple.js]
 [test_badDefaultIndex.js]
+[test_completeDefaultIndex_casing.js]
 [test_hiddenResult.js]
 [test_previousResult.js]
 [test_stopSearch.js]