am cc2751ba: Make commitCurrentAutoCorrection asynchronous.

* commit 'cc2751ba03fad6af5da0a7b5d421963e040d690f':
  Make commitCurrentAutoCorrection asynchronous.
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index a8a29a1..3d29c5a 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -2532,11 +2532,8 @@
                 false /* isPrediction */);
     }
 
-    private void showSuggestionStrip(final SuggestedWords suggestedWords) {
-        if (suggestedWords.isEmpty()) {
-            clearSuggestionStrip();
-            return;
-        }
+    private void setAutoCorrection(final SuggestedWords suggestedWords) {
+        if (suggestedWords.isEmpty()) return;
         final String autoCorrection;
         if (suggestedWords.mWillAutoCorrect) {
             autoCorrection = suggestedWords.getWord(SuggestedWords.INDEX_OF_AUTO_CORRECTION);
@@ -2544,17 +2541,21 @@
             autoCorrection = suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD);
         }
         mWordComposer.setAutoCorrection(autoCorrection);
+    }
+
+    private void showSuggestionStrip(final SuggestedWords suggestedWords) {
+        if (suggestedWords.isEmpty()) {
+            clearSuggestionStrip();
+            return;
+        }
+        setAutoCorrection(suggestedWords);
         final boolean isAutoCorrection = suggestedWords.willAutoCorrect();
         setSuggestedWords(suggestedWords, isAutoCorrection);
         setAutoCorrectionIndicator(isAutoCorrection);
         setSuggestionStripShown(isSuggestionsStripVisible());
     }
 
-    private void commitCurrentAutoCorrection(final String separator, final Runnable callback) {
-        // Complete any pending suggestions query first
-        if (mHandler.hasPendingUpdateSuggestions()) {
-            updateSuggestionStrip();
-        }
+    private void completeCommitCurrentAutoCorrection(final String separator) {
         final String typedAutoCorrection = mWordComposer.getAutoCorrectionOrNull();
         final String typedWord = mWordComposer.getTypedWord();
         final String autoCorrection = (typedAutoCorrection != null)
@@ -2588,9 +2589,22 @@
                         typedWord, autoCorrection));
             }
         }
-        if (callback != null) {
-            callback.run();
-        }
+    }
+
+    private void commitCurrentAutoCorrection(final String separator, final Runnable callback) {
+        getSuggestedWordsOrOlderSuggestionsAsync(Suggest.SESSION_TYPING,
+                new OnGetSuggestedWordsCallback() {
+                    @Override
+                    public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
+                        if (suggestedWords != null) {
+                            setAutoCorrection(suggestedWords);
+                        }
+                        completeCommitCurrentAutoCorrection(separator);
+                        if (callback != null) {
+                            callback.run();
+                        }
+                    }
+                });
     }
 
     // Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener}
diff --git a/tests/src/com/android/inputmethod/latin/InputTestsBase.java b/tests/src/com/android/inputmethod/latin/InputTestsBase.java
index 0a1c4e9..da1fb6f 100644
--- a/tests/src/com/android/inputmethod/latin/InputTestsBase.java
+++ b/tests/src/com/android/inputmethod/latin/InputTestsBase.java
@@ -44,10 +44,12 @@
 
     private static final String PREF_DEBUG_MODE = "debug_mode";
 
-    // The message that sets the underline is posted with a 100 ms delay
+    // The message that sets the underline is posted with a 200 ms delay
     protected static final int DELAY_TO_WAIT_FOR_UNDERLINE = 200;
-    // The message that sets predictions is posted with a 100 ms delay
+    // The message that sets predictions is posted with a 200 ms delay
     protected static final int DELAY_TO_WAIT_FOR_PREDICTIONS = 200;
+    // The message that sets auto-corrections is posted within a 100 ms delay.
+    protected static final int DELAY_TO_WAIT_FOR_AUTOCORRECTION = 100;
 
     protected LatinIME mLatinIME;
     protected Keyboard mKeyboard;
@@ -221,6 +223,7 @@
     protected void type(final String stringToType) {
         for (int i = 0; i < stringToType.length(); i = stringToType.offsetByCodePoints(i, 1)) {
             type(stringToType.codePointAt(i));
+            sleep(DELAY_TO_WAIT_FOR_AUTOCORRECTION);
         }
     }