Bug 998893 - Update the autocomplete search string when pressing DOM_VK_DOWN to initiate a search. r=mak
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Wed, 06 May 2015 11:23:51 -0700
changeset 274019 651329fc467add421e4188851163b39ac820b6bc
parent 274018 3dae1f0d8b0bbcdb17b56650752b80b2f41e9aaf
child 274020 55d489bb8ad56d0ec55123f48e44682653ec8ee1
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs998893
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 998893 - Update the autocomplete search string when pressing DOM_VK_DOWN to initiate a search. r=mak Some script may have changed the value of the text field since our last keypress or after our focus handler and we don't want to search for a stale string.
toolkit/components/autocomplete/nsAutoCompleteController.cpp
toolkit/content/tests/chrome/chrome.ini
toolkit/content/tests/chrome/test_autocomplete_change_after_focus.html
--- a/toolkit/components/autocomplete/nsAutoCompleteController.cpp
+++ b/toolkit/components/autocomplete/nsAutoCompleteController.cpp
@@ -465,16 +465,23 @@ nsAutoCompleteController::HandleKeyNavig
 
           if (!mInput) {
             // StopSearch() can call PostSearchCleanup() which might result
             // in a blur event, which could null out mInput, so we need to check it
             // again.  See bug #395344 for more details
             return NS_OK;
           }
 
+          // Some script may have changed the value of the text field since our
+          // last keypress or after our focus handler and we don't want to search
+          // for a stale string.
+          nsAutoString value;
+          input->GetTextValue(value);
+          mSearchString = value;
+
           StartSearches();
         }
       }
     }
   } else if (   aKey == nsIDOMKeyEvent::DOM_VK_LEFT
              || aKey == nsIDOMKeyEvent::DOM_VK_RIGHT
 #ifndef XP_MACOSX
              || aKey == nsIDOMKeyEvent::DOM_VK_HOME
--- a/toolkit/content/tests/chrome/chrome.ini
+++ b/toolkit/content/tests/chrome/chrome.ini
@@ -49,16 +49,17 @@ support-files =
   rtltest/content/dirtest.xul
 
 [test_about_networking.html]
 [test_arrowpanel.xul]
 [test_autocomplete2.xul]
 [test_autocomplete3.xul]
 [test_autocomplete4.xul]
 [test_autocomplete5.xul]
+[test_autocomplete_change_after_focus.html]
 [test_autocomplete_delayOnPaste.xul]
 [test_autocomplete_with_composition_on_input.html]
 [test_autocomplete_with_composition_on_textbox.xul]
 [test_autocomplete_placehold_last_complete.xul]
 [test_browser_drop.xul]
 skip-if = buildapp == 'mulet'
 [test_bug253481.xul]
 [test_bug263683.xul]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/chrome/test_autocomplete_change_after_focus.html
@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=998893
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 998893 - Ensure that input.value changes affect autocomplete</title>
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
+  <script type="application/javascript">
+  /** Test for Bug 998893 **/
+  SimpleTest.waitForExplicitFinish();
+  SimpleTest.waitForFocus(setup);
+
+  function setup() {
+    SpecialPowers.formHistory.update([
+      { op : "bump", fieldname: "field1", value: "Default text option" },
+      { op : "bump", fieldname: "field1", value: "New value option" },
+    ], {
+      handleCompletion: function() {
+        runTest();
+      },
+    });
+  }
+
+  function handleEnter(evt) {
+    if (evt.keyCode != KeyEvent.DOM_VK_RETURN) {
+      return;
+    }
+    info("RETURN received for phase: " + evt.eventPhase);
+    is(evt.target.value, "New value option", "Check that the correct autocomplete entry was used");
+    SimpleTest.finish();
+  }
+
+  function popupShownListener(evt) {
+    info("popupshown");
+    sendKey("DOWN");
+    sendKey("RETURN"); // select the first entry in the popup
+    sendKey("RETURN"); // try to submit the form with the filled value
+  }
+
+  SpecialPowers.addAutoCompletePopupEventListener(window, "popupshown", popupShownListener);
+
+  function runTest() {
+    var field = document.getElementById("field1");
+    field.addEventListener("focus", function onFocus() {
+      info("field focused");
+      field.value = "New value";
+      sendKey("DOWN");
+    });
+
+    field.addEventListener("keypress", handleEnter, true);
+
+    field.focus();
+  }
+
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=998893">Mozilla Bug 998893</a>
+<p id="display"><input id="field1" value="Default text"></p>
+<div id="content" style="display: none"></div>
+<pre id="test">
+</pre>
+</body>
+</html>