Merge "Reduce the number of arguments required to initialize dic_node."
diff --git a/java/proguard.flags b/java/proguard.flags
index d65924f..c08a968 100644
--- a/java/proguard.flags
+++ b/java/proguard.flags
@@ -1,11 +1,16 @@
 # Keep classes and methods that have the @UsedForTesting annotation
 -keep @com.android.inputmethod.annotations.UsedForTesting class *
 -keepclassmembers class * {
-@com.android.inputmethod.annotations.UsedForTesting *;
+    @com.android.inputmethod.annotations.UsedForTesting *;
 }
 
 # Keep classes and methods that have the @ExternallyReferenced annotation
 -keep @com.android.inputmethod.annotations.ExternallyReferenced class *
 -keepclassmembers class * {
-@com.android.inputmethod.annotations.ExternallyReferenced *;
+    @com.android.inputmethod.annotations.ExternallyReferenced *;
+}
+
+# Keep native methods
+-keepclassmembers class * {
+    native <methods>;
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index c867436..0560cf5 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -43,7 +43,6 @@
 import android.os.SystemClock;
 import android.preference.PreferenceManager;
 import android.text.InputType;
-import android.text.Spanned;
 import android.text.TextUtils;
 import android.text.style.SuggestionSpan;
 import android.util.Log;
@@ -778,6 +777,7 @@
         // span, so we should reset our state unconditionally, even if restarting is true.
         mEnteredText = null;
         resetComposingState(true /* alsoResetLastComposedWord */);
+        if (isDifferentTextField) mHandler.postResumeSuggestions();
         mDeleteCount = 0;
         mSpaceState = SPACE_STATE_NONE;
         mRecapitalizeStatus.deactivate();
@@ -2522,21 +2522,15 @@
         final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor();
         if (numberOfCharsInWordBeforeCursor > mLastSelectionStart) return;
         final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList();
-        final CharSequence word = range.mWord;
-        final String typedWord = word.toString();
-        if (word instanceof Spanned) {
-            final Spanned spanned = (Spanned)word;
-            int i = 0;
-            for (Object object : spanned.getSpans(0, spanned.length(),
-                    SuggestionSpan.class)) {
-                SuggestionSpan span = (SuggestionSpan)object;
-                for (String s : span.getSuggestions()) {
-                    ++i;
-                    if (!TextUtils.equals(s, typedWord)) {
-                        suggestions.add(new SuggestedWordInfo(s,
-                                SuggestionStripView.MAX_SUGGESTIONS - i,
-                                SuggestedWordInfo.KIND_RESUMED, Dictionary.TYPE_RESUMED));
-                    }
+        final String typedWord = range.mWord.toString();
+        int i = 0;
+        for (final SuggestionSpan span : range.getSuggestionSpansAtWord()) {
+            for (final String s : span.getSuggestions()) {
+                ++i;
+                if (!TextUtils.equals(s, typedWord)) {
+                    suggestions.add(new SuggestedWordInfo(s,
+                            SuggestionStripView.MAX_SUGGESTIONS - i,
+                            SuggestedWordInfo.KIND_RESUMED, Dictionary.TYPE_RESUMED));
                 }
             }
         }
diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java
index 5391b13..6b22cb1 100644
--- a/java/src/com/android/inputmethod/latin/RichInputConnection.java
+++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java
@@ -17,7 +17,9 @@
 package com.android.inputmethod.latin;
 
 import android.inputmethodservice.InputMethodService;
+import android.text.Spanned;
 import android.text.TextUtils;
+import android.text.style.SuggestionSpan;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.inputmethod.CompletionInfo;
@@ -32,6 +34,7 @@
 import com.android.inputmethod.latin.utils.StringUtils;
 import com.android.inputmethod.research.ResearchLogger;
 
+import java.util.Arrays;
 import java.util.Locale;
 import java.util.regex.Pattern;
 
@@ -457,6 +460,66 @@
             return mWordAtCursorEndIndex - mCursorIndex;
         }
 
+        /**
+         * Gets the suggestion spans that are put squarely on the word, with the exact start
+         * and end of the span matching the boundaries of the word.
+         * @return the list of spans.
+         */
+        public SuggestionSpan[] getSuggestionSpansAtWord() {
+            if (!(mTextAtCursor instanceof Spanned && mWord instanceof Spanned)) {
+                return new SuggestionSpan[0];
+            }
+            final Spanned text = (Spanned)mTextAtCursor;
+            // Note: it's fine to pass indices negative or greater than the length of the string
+            // to the #getSpans() method. The reason we need to get from -1 to +1 is that, the
+            // spans were cut at the cursor position, and #getSpans(start, end) does not return
+            // spans that end at `start' or begin at `end'. Consider the following case:
+            //              this| is          (The | symbolizes the cursor position
+            //              ---- ---
+            // In this case, the cursor is in position 4, so the 0~7 span has been split into
+            // a 0~4 part and a 4~7 part.
+            // If we called #getSpans(0, 4) in this case, we would only get the part from 0 to 4
+            // of the span, and not the part from 4 to 7, so we would not realize the span actually
+            // extends from 0 to 7. But if we call #getSpans(-1, 5) we'll get both the 0~4 and
+            // the 4~7 spans and we can merge them accordingly.
+            // Any span starting more than 1 char away from the word boundaries in any direction
+            // does not touch the word, so we don't need to consider it. That's why requesting
+            // -1 ~ +1 is enough.
+            // Of course this is only relevant if the cursor is at one end of the word. If it's
+            // in the middle, the -1 and +1 are not necessary, but they are harmless.
+            final SuggestionSpan[] spans = text.getSpans(mWordAtCursorStartIndex - 1,
+                    mWordAtCursorEndIndex + 1, SuggestionSpan.class);
+            int readIndex = 0;
+            int writeIndex = 0;
+            for (; readIndex < spans.length; ++readIndex) {
+                final SuggestionSpan span = spans[readIndex];
+                // The span may be null, as we null them when we find duplicates. Cf a few lines
+                // down.
+                if (null == span) continue;
+                // Tentative span start and end. This may be modified later if we realize the
+                // same span is also applied to other parts of the string.
+                int spanStart = text.getSpanStart(span);
+                int spanEnd = text.getSpanEnd(span);
+                for (int i = readIndex + 1; i < spans.length; ++i) {
+                    if (span.equals(spans[i])) {
+                        // We found the same span somewhere else. Read the new extent of this
+                        // span, and adjust our values accordingly.
+                        spanStart = Math.min(spanStart, text.getSpanStart(spans[i]));
+                        spanEnd = Math.max(spanEnd, text.getSpanEnd(spans[i]));
+                        // ...and mark the span as processed.
+                        spans[i] = null;
+                    }
+                }
+                if (spanStart == mWordAtCursorStartIndex && spanEnd == mWordAtCursorEndIndex) {
+                    // If the span does not start and stop here, we ignore it. It probably extends
+                    // past the start or end of the word, as happens in missing space correction
+                    // or EasyEditSpans put by voice input.
+                    spans[writeIndex++] = spans[readIndex];
+                }
+            }
+            return writeIndex == readIndex ? spans : Arrays.copyOfRange(spans, 0, writeIndex);
+        }
+
         public Range(final CharSequence textAtCursor, final int wordAtCursorStartIndex,
                 final int wordAtCursorEndIndex, final int cursorIndex) {
             if (wordAtCursorStartIndex < 0 || cursorIndex < wordAtCursorStartIndex
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index 33b6a6f..a93bbeb 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -37,7 +37,17 @@
 
 class ProximityInfo;
 
-static void releaseDictBuf(const void *dictBuf, const size_t length, const int fd);
+// Helper method
+static void releaseDictBuf(const void *dictBuf, const size_t length, const int fd) {
+    int ret = munmap(const_cast<void *>(dictBuf), length);
+    if (ret != 0) {
+        AKLOGE("DICT: Failure in munmap. ret=%d errno=%d", ret, errno);
+    }
+    ret = close(fd);
+    if (ret != 0) {
+        AKLOGE("DICT: Failure in close. ret=%d errno=%d", ret, errno);
+    }
+}
 
 static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring sourceDir,
         jlong dictOffset, jlong dictSize, jboolean isUpdatable) {
@@ -77,8 +87,8 @@
         return 0;
     }
     Dictionary *dictionary = 0;
-    if (BinaryDictionaryFormat::UNKNOWN_VERSION
-            == BinaryDictionaryFormat::detectFormatVersion(static_cast<uint8_t *>(dictBuf),
+    if (BinaryDictionaryFormatUtils::UNKNOWN_VERSION
+            == BinaryDictionaryFormatUtils::detectFormatVersion(static_cast<uint8_t *>(dictBuf),
                     static_cast<int>(dictSize))) {
         AKLOGE("DICT: dictionary format is unknown, bad magic number");
         releaseDictBuf(static_cast<const char *>(dictBuf) - offset, adjDictSize, fd);
@@ -91,6 +101,19 @@
     return reinterpret_cast<jlong>(dictionary);
 }
 
+static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dict) {
+    Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
+    if (!dictionary) return;
+    const BinaryDictionaryInfo *const binaryDictionaryInfo = dictionary->getBinaryDictionaryInfo();
+    const int dictBufOffset = binaryDictionaryInfo->getDictBufOffset();
+    const void *dictBuf = binaryDictionaryInfo->getDictBuf();
+    if (!dictBuf) return;
+    releaseDictBuf(static_cast<const char *>(dictBuf) - dictBufOffset,
+            binaryDictionaryInfo->getDictSize() + dictBufOffset,
+            binaryDictionaryInfo->getMmapFd());
+    delete dictionary;
+}
+
 static int latinime_BinaryDictionary_getSuggestions(JNIEnv *env, jclass clazz, jlong dict,
         jlong proximityInfo, jlong dicTraverseSession, jintArray xCoordinatesArray,
         jintArray yCoordinatesArray, jintArray timesArray, jintArray pointerIdsArray,
@@ -222,30 +245,6 @@
             afterCodePoints, afterLength);
 }
 
-static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dict) {
-    Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
-    if (!dictionary) return;
-    const BinaryDictionaryInfo *const binaryDictionaryInfo = dictionary->getBinaryDictionaryInfo();
-    const int dictBufOffset = binaryDictionaryInfo->getDictBufOffset();
-    const void *dictBuf = binaryDictionaryInfo->getDictBuf();
-    if (!dictBuf) return;
-    releaseDictBuf(static_cast<const char *>(dictBuf) - dictBufOffset,
-            binaryDictionaryInfo->getDictSize() + dictBufOffset,
-            binaryDictionaryInfo->getMmapFd());
-    delete dictionary;
-}
-
-static void releaseDictBuf(const void *dictBuf, const size_t length, const int fd) {
-    int ret = munmap(const_cast<void *>(dictBuf), length);
-    if (ret != 0) {
-        AKLOGE("DICT: Failure in munmap. ret=%d errno=%d", ret, errno);
-    }
-    ret = close(fd);
-    if (ret != 0) {
-        AKLOGE("DICT: Failure in close. ret=%d errno=%d", ret, errno);
-    }
-}
-
 static void latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz, jlong dict,
         jintArray word, jint probability) {
     Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.cpp b/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.cpp
index 737df63..bbb4ca3 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.cpp
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.cpp
@@ -22,7 +22,7 @@
  * Dictionary size
  */
 // Any file smaller than this is not a dictionary.
-const int BinaryDictionaryFormat::DICTIONARY_MINIMUM_SIZE = 4;
+const int BinaryDictionaryFormatUtils::DICTIONARY_MINIMUM_SIZE = 4;
 
 /**
  * Format versions
@@ -30,17 +30,18 @@
 // Originally, format version 1 had a 16-bit magic number, then the version number `01'
 // then options that must be 0. Hence the first 32-bits of the format are always as follow
 // and it's okay to consider them a magic number as a whole.
-const uint32_t BinaryDictionaryFormat::FORMAT_VERSION_1_MAGIC_NUMBER = 0x78B10100;
+const uint32_t BinaryDictionaryFormatUtils::FORMAT_VERSION_1_MAGIC_NUMBER = 0x78B10100;
 
 // The versions of Latin IME that only handle format version 1 only test for the magic
 // number, so we had to change it so that version 2 files would be rejected by older
 // implementations. On this occasion, we made the magic number 32 bits long.
-const uint32_t BinaryDictionaryFormat::FORMAT_VERSION_2_MAGIC_NUMBER = 0x9BC13AFE;
+const uint32_t BinaryDictionaryFormatUtils::FORMAT_VERSION_2_MAGIC_NUMBER = 0x9BC13AFE;
 // Magic number (4 bytes), version (2 bytes), options (2 bytes), header size (4 bytes) = 12
-const int BinaryDictionaryFormat::FORMAT_VERSION_2_MINIMUM_SIZE = 12;
+const int BinaryDictionaryFormatUtils::FORMAT_VERSION_2_MINIMUM_SIZE = 12;
 
-/* static */ BinaryDictionaryFormat::FORMAT_VERSION BinaryDictionaryFormat::detectFormatVersion(
-        const uint8_t *const dict, const int dictSize) {
+/* static */ BinaryDictionaryFormatUtils::FORMAT_VERSION
+        BinaryDictionaryFormatUtils::detectFormatVersion(const uint8_t *const dict,
+                const int dictSize) {
     // The magic number is stored big-endian.
     // If the dictionary is less than 4 bytes, we can't even read the magic number, so we don't
     // understand this format.
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.h
index c0fd561..33618b9 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.h
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_format_utils.h
@@ -31,7 +31,7 @@
  * reading methods and utility methods for various purposes.
  * On the other hand, this file deals with only about dictionary format version.
  */
-class BinaryDictionaryFormat {
+class BinaryDictionaryFormatUtils {
  public:
     // TODO: Remove obsolete version logic
     enum FORMAT_VERSION {
@@ -43,7 +43,7 @@
     static FORMAT_VERSION detectFormatVersion(const uint8_t *const dict, const int dictSize);
 
  private:
-    DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryFormat);
+    DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryFormatUtils);
 
     static const int DICTIONARY_MINIMUM_SIZE;
     static const uint32_t FORMAT_VERSION_1_MAGIC_NUMBER;
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_header.cpp b/native/jni/src/suggest/core/dictionary/binary_dictionary_header.cpp
index 04bb81f..91c643a 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_header.cpp
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_header.cpp
@@ -29,12 +29,12 @@
 BinaryDictionaryHeader::BinaryDictionaryHeader(
         const BinaryDictionaryInfo *const binaryDictionaryInfo)
         : mBinaryDictionaryInfo(binaryDictionaryInfo),
-          mDictionaryFlags(BinaryDictionaryHeaderReader::getFlags(binaryDictionaryInfo)),
-          mSize(BinaryDictionaryHeaderReader::getHeaderSize(binaryDictionaryInfo)),
+          mDictionaryFlags(BinaryDictionaryHeaderReadingUtils::getFlags(binaryDictionaryInfo)),
+          mSize(BinaryDictionaryHeaderReadingUtils::getHeaderSize(binaryDictionaryInfo)),
           mMultiWordCostMultiplier(readMultiWordCostMultiplier()) {}
 
 float BinaryDictionaryHeader::readMultiWordCostMultiplier() const {
-    const int headerValue = BinaryDictionaryHeaderReader::readHeaderValueInt(
+    const int headerValue = BinaryDictionaryHeaderReadingUtils::readHeaderValueInt(
             mBinaryDictionaryInfo, MULTIPLE_WORDS_DEMOTION_RATE_KEY);
     if (headerValue == S_INT_MIN) {
         // not found
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_header.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_header.h
index 9db0003..6dba0b2 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_header.h
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_header.h
@@ -37,15 +37,16 @@
     }
 
     AK_FORCE_INLINE bool supportsDynamicUpdate() const {
-        return BinaryDictionaryHeaderReader::supportsDynamicUpdate(mDictionaryFlags);
+        return BinaryDictionaryHeaderReadingUtils::supportsDynamicUpdate(mDictionaryFlags);
     }
 
     AK_FORCE_INLINE bool requiresGermanUmlautProcessing() const {
-        return BinaryDictionaryHeaderReader::requiresGermanUmlautProcessing(mDictionaryFlags);
+        return BinaryDictionaryHeaderReadingUtils::requiresGermanUmlautProcessing(mDictionaryFlags);
     }
 
     AK_FORCE_INLINE bool requiresFrenchLigatureProcessing() const {
-        return BinaryDictionaryHeaderReader::requiresFrenchLigatureProcessing(mDictionaryFlags);
+        return BinaryDictionaryHeaderReadingUtils::requiresFrenchLigatureProcessing(
+                mDictionaryFlags);
     }
 
     AK_FORCE_INLINE float getMultiWordCostMultiplier() const {
@@ -60,7 +61,7 @@
     static const float MULTI_WORD_COST_MULTIPLIER_SCALE;
 
     const BinaryDictionaryInfo *const mBinaryDictionaryInfo;
-    const BinaryDictionaryHeaderReader::DictionaryFlags mDictionaryFlags;
+    const BinaryDictionaryHeaderReadingUtils::DictionaryFlags mDictionaryFlags;
     const int mSize;
     const float mMultiWordCostMultiplier;
 
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.cpp b/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.cpp
index c09a78f..2c95931 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.cpp
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.cpp
@@ -24,32 +24,33 @@
 
 namespace latinime {
 
-const int BinaryDictionaryHeaderReader::MAX_OPTION_KEY_LENGTH = 256;
+const int BinaryDictionaryHeaderReadingUtils::MAX_OPTION_KEY_LENGTH = 256;
 
-const int BinaryDictionaryHeaderReader::FORMAT_VERSION_1_HEADER_SIZE = 5;
+const int BinaryDictionaryHeaderReadingUtils::FORMAT_VERSION_1_HEADER_SIZE = 5;
 
-const int BinaryDictionaryHeaderReader::VERSION_2_MAGIC_NUMBER_SIZE = 4;
-const int BinaryDictionaryHeaderReader::VERSION_2_DICTIONARY_VERSION_SIZE = 2;
-const int BinaryDictionaryHeaderReader::VERSION_2_DICTIONARY_FLAG_SIZE = 2;
-const int BinaryDictionaryHeaderReader::VERSION_2_DICTIONARY_HEADER_SIZE_SIZE = 4;
+const int BinaryDictionaryHeaderReadingUtils::VERSION_2_MAGIC_NUMBER_SIZE = 4;
+const int BinaryDictionaryHeaderReadingUtils::VERSION_2_DICTIONARY_VERSION_SIZE = 2;
+const int BinaryDictionaryHeaderReadingUtils::VERSION_2_DICTIONARY_FLAG_SIZE = 2;
+const int BinaryDictionaryHeaderReadingUtils::VERSION_2_DICTIONARY_HEADER_SIZE_SIZE = 4;
 
-const BinaryDictionaryHeaderReader::DictionaryFlags BinaryDictionaryHeaderReader::NO_FLAGS = 0;
+const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
+        BinaryDictionaryHeaderReadingUtils::NO_FLAGS = 0;
 // Flags for special processing
 // Those *must* match the flags in makedict (BinaryDictInputOutput#*_PROCESSING_FLAG) or
 // something very bad (like, the apocalypse) will happen. Please update both at the same time.
-const BinaryDictionaryHeaderReader::DictionaryFlags
-        BinaryDictionaryHeaderReader::GERMAN_UMLAUT_PROCESSING_FLAG = 0x1;
-const BinaryDictionaryHeaderReader::DictionaryFlags
-        BinaryDictionaryHeaderReader::SUPPORTS_DYNAMIC_UPDATE_FLAG = 0x2;
-const BinaryDictionaryHeaderReader::DictionaryFlags
-        BinaryDictionaryHeaderReader::FRENCH_LIGATURE_PROCESSING_FLAG = 0x4;
+const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
+        BinaryDictionaryHeaderReadingUtils::GERMAN_UMLAUT_PROCESSING_FLAG = 0x1;
+const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
+        BinaryDictionaryHeaderReadingUtils::SUPPORTS_DYNAMIC_UPDATE_FLAG = 0x2;
+const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
+        BinaryDictionaryHeaderReadingUtils::FRENCH_LIGATURE_PROCESSING_FLAG = 0x4;
 
-/* static */ int BinaryDictionaryHeaderReader::getHeaderSize(
+/* static */ int BinaryDictionaryHeaderReadingUtils::getHeaderSize(
         const BinaryDictionaryInfo *const binaryDictionaryInfo) {
     switch (binaryDictionaryInfo->getFormat()) {
-        case BinaryDictionaryFormat::VERSION_1:
+        case BinaryDictionaryFormatUtils::VERSION_1:
             return FORMAT_VERSION_1_HEADER_SIZE;
-        case BinaryDictionaryFormat::VERSION_2:
+        case BinaryDictionaryFormatUtils::VERSION_2:
             // See the format of the header in the comment in
             // BinaryDictionaryFormatUtils::detectFormatVersion()
             return ByteArrayUtils::readUint32(binaryDictionaryInfo->getDictBuf(),
@@ -60,12 +61,13 @@
     }
 }
 
-/* static */ BinaryDictionaryHeaderReader::DictionaryFlags BinaryDictionaryHeaderReader::getFlags(
-        const BinaryDictionaryInfo *const binaryDictionaryInfo) {
+/* static */ BinaryDictionaryHeaderReadingUtils::DictionaryFlags
+        BinaryDictionaryHeaderReadingUtils::getFlags(
+                const BinaryDictionaryInfo *const binaryDictionaryInfo) {
     switch (binaryDictionaryInfo->getFormat()) {
-        case BinaryDictionaryFormat::VERSION_1:
+        case BinaryDictionaryFormatUtils::VERSION_1:
             return NO_FLAGS;
-        case BinaryDictionaryFormat::VERSION_2:
+        case BinaryDictionaryFormatUtils::VERSION_2:
             return ByteArrayUtils::readUint16(binaryDictionaryInfo->getDictBuf(),
                     VERSION_2_MAGIC_NUMBER_SIZE + VERSION_2_DICTIONARY_VERSION_SIZE);
         default:
@@ -74,7 +76,7 @@
 }
 
 // Returns if the key is found or not and reads the found value into outValue.
-/* static */ bool BinaryDictionaryHeaderReader::readHeaderValue(
+/* static */ bool BinaryDictionaryHeaderReadingUtils::readHeaderValue(
         const BinaryDictionaryInfo *const binaryDictionaryInfo,
         const char *const key, int *outValue, const int outValueSize) {
     if (outValueSize <= 0 || !hasHeaderAttributes(binaryDictionaryInfo->getFormat())) {
@@ -97,7 +99,7 @@
     return false;
 }
 
-/* static */ int BinaryDictionaryHeaderReader::readHeaderValueInt(
+/* static */ int BinaryDictionaryHeaderReadingUtils::readHeaderValueInt(
         const BinaryDictionaryInfo *const binaryDictionaryInfo, const char *const key) {
     const int bufferSize = LARGEST_INT_DIGIT_COUNT;
     int intBuffer[bufferSize];
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.h
index 6e9dca7..49ed2b9 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.h
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_header_reading_utils.h
@@ -26,7 +26,7 @@
 
 class BinaryDictionaryInfo;
 
-class BinaryDictionaryHeaderReader {
+class BinaryDictionaryHeaderReadingUtils {
  public:
     typedef uint16_t DictionaryFlags;
 
@@ -49,10 +49,10 @@
     }
 
     static AK_FORCE_INLINE bool hasHeaderAttributes(
-            const BinaryDictionaryFormat::FORMAT_VERSION format) {
+            const BinaryDictionaryFormatUtils::FORMAT_VERSION format) {
         // Only format 2 and above have header attributes as {key,value} string pairs.
         switch (format) {
-        case BinaryDictionaryFormat::VERSION_2:
+        case BinaryDictionaryFormatUtils::VERSION_2:
             return  true;
             break;
         default:
@@ -61,9 +61,9 @@
     }
 
     static AK_FORCE_INLINE int getHeaderOptionsPosition(
-            const BinaryDictionaryFormat::FORMAT_VERSION format) {
+            const BinaryDictionaryFormatUtils::FORMAT_VERSION format) {
         switch (format) {
-        case BinaryDictionaryFormat::VERSION_2:
+        case BinaryDictionaryFormatUtils::VERSION_2:
             return VERSION_2_MAGIC_NUMBER_SIZE + VERSION_2_DICTIONARY_VERSION_SIZE
                     + VERSION_2_DICTIONARY_FLAG_SIZE + VERSION_2_DICTIONARY_HEADER_SIZE_SIZE;
             break;
@@ -80,7 +80,7 @@
             const BinaryDictionaryInfo *const binaryDictionaryInfo, const char *const key);
 
  private:
-    DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryHeaderReader);
+    DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryHeaderReadingUtils);
 
     static const int FORMAT_VERSION_1_HEADER_SIZE;
 
diff --git a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h
index e0b5835..c921236 100644
--- a/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h
+++ b/native/jni/src/suggest/core/dictionary/binary_dictionary_info.h
@@ -33,7 +33,8 @@
             const int dictBufOffset, const bool isUpdatable)
             : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd),
               mDictBufOffset(dictBufOffset), mIsUpdatable(isUpdatable),
-              mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, mDictSize)),
+              mDictionaryFormat(BinaryDictionaryFormatUtils::detectFormatVersion(
+                      mDictBuf, mDictSize)),
               mDictionaryHeader(this), mDictRoot(mDictBuf + mDictionaryHeader.getSize()) {}
 
     AK_FORCE_INLINE const uint8_t *getDictBuf() const {
@@ -56,7 +57,7 @@
         return mDictRoot;
     }
 
-    AK_FORCE_INLINE BinaryDictionaryFormat::FORMAT_VERSION getFormat() const {
+    AK_FORCE_INLINE BinaryDictionaryFormatUtils::FORMAT_VERSION getFormat() const {
         return mDictionaryFormat;
     }
 
@@ -82,7 +83,7 @@
     const int mMmapFd;
     const int mDictBufOffset;
     const bool mIsUpdatable;
-    const BinaryDictionaryFormat::FORMAT_VERSION mDictionaryFormat;
+    const BinaryDictionaryFormatUtils::FORMAT_VERSION mDictionaryFormat;
     const BinaryDictionaryHeader mDictionaryHeader;
     const uint8_t *const mDictRoot;
 };
diff --git a/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
new file mode 100644
index 0000000..0e077bb
--- /dev/null
+++ b/tests/src/com/android/inputmethod/latin/RichInputConnectionAndTextRangeTests.java
@@ -0,0 +1,312 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import android.inputmethodservice.InputMethodService;
+import android.os.Parcel;
+import android.test.AndroidTestCase;
+import android.test.MoreAsserts;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.text.SpannableString;
+import android.text.Spanned;
+import android.text.TextUtils;
+import android.text.style.SuggestionSpan;
+import android.view.inputmethod.ExtractedText;
+import android.view.inputmethod.ExtractedTextRequest;
+import android.view.inputmethod.InputConnection;
+import android.view.inputmethod.InputConnectionWrapper;
+
+import com.android.inputmethod.latin.RichInputConnection.Range;
+
+import java.util.Locale;
+
+@SmallTest
+public class RichInputConnectionAndTextRangeTests extends AndroidTestCase {
+
+    // The following is meant to be a reasonable default for
+    // the "word_separators" resource.
+    private static final String sSeparators = ".,:;!?-";
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    private class MockConnection extends InputConnectionWrapper {
+        final CharSequence mTextBefore;
+        final CharSequence mTextAfter;
+        final ExtractedText mExtractedText;
+
+        public MockConnection(final CharSequence text, final int cursorPosition) {
+            super(null, false);
+            // Interaction of spans with Parcels is completely non-trivial, but in the actual case
+            // the CharSequences do go through Parcels because they go through IPC. There
+            // are some significant differences between the behavior of Spanned objects that
+            // have and that have not gone through parceling, so it's much easier to simulate
+            // the environment with Parcels than try to emulate things by hand.
+            final Parcel p = Parcel.obtain();
+            TextUtils.writeToParcel(text.subSequence(0, cursorPosition), p, 0 /* flags */);
+            TextUtils.writeToParcel(text.subSequence(cursorPosition, text.length()), p,
+                    0 /* flags */);
+            final byte[] marshalled = p.marshall();
+            p.unmarshall(marshalled, 0, marshalled.length);
+            p.setDataPosition(0);
+            mTextBefore = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
+            mTextAfter = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
+            mExtractedText = null;
+            p.recycle();
+        }
+
+        public MockConnection(String textBefore, String textAfter, ExtractedText extractedText) {
+            super(null, false);
+            mTextBefore = textBefore;
+            mTextAfter = textAfter;
+            mExtractedText = extractedText;
+        }
+
+        /* (non-Javadoc)
+         * @see android.view.inputmethod.InputConnectionWrapper#getTextBeforeCursor(int, int)
+         */
+        @Override
+        public CharSequence getTextBeforeCursor(int n, int flags) {
+            return mTextBefore;
+        }
+
+        /* (non-Javadoc)
+         * @see android.view.inputmethod.InputConnectionWrapper#getTextAfterCursor(int, int)
+         */
+        @Override
+        public CharSequence getTextAfterCursor(int n, int flags) {
+            return mTextAfter;
+        }
+
+        /* (non-Javadoc)
+         * @see android.view.inputmethod.InputConnectionWrapper#getExtractedText(
+         *         ExtractedTextRequest, int)
+         */
+        @Override
+        public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
+            return mExtractedText;
+        }
+
+        @Override
+        public boolean beginBatchEdit() {
+            return true;
+        }
+
+        @Override
+        public boolean endBatchEdit() {
+            return true;
+        }
+
+        @Override
+        public boolean finishComposingText() {
+            return true;
+        }
+    }
+
+    private class MockInputMethodService extends InputMethodService {
+        InputConnection mInputConnection;
+        public void setInputConnection(final InputConnection inputConnection) {
+            mInputConnection = inputConnection;
+        }
+        @Override
+        public InputConnection getCurrentInputConnection() {
+            return mInputConnection;
+        }
+    }
+
+    /************************** Tests ************************/
+
+    /**
+     * Test for getting previous word (for bigram suggestions)
+     */
+    public void testGetPreviousWord() {
+        // If one of the following cases breaks, the bigram suggestions won't work.
+        assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 2), "abc");
+        assertNull(RichInputConnection.getNthPreviousWord("abc", sSeparators, 2));
+        assertNull(RichInputConnection.getNthPreviousWord("abc. def", sSeparators, 2));
+
+        // The following tests reflect the current behavior of the function
+        // RichInputConnection#getNthPreviousWord.
+        // TODO: However at this time, the code does never go
+        // into such a path, so it should be safe to change the behavior of
+        // this function if needed - especially since it does not seem very
+        // logical. These tests are just there to catch any unintentional
+        // changes in the behavior of the RichInputConnection#getPreviousWord method.
+        assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 2), "abc");
+        assertEquals(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 2), "abc");
+        assertEquals(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 2), "def");
+        assertNull(RichInputConnection.getNthPreviousWord("abc ", sSeparators, 2));
+
+        assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 1), "def");
+        assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 1), "def");
+        assertNull(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 1));
+        assertNull(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 1));
+    }
+
+    /**
+     * Test logic in getting the word range at the cursor.
+     */
+    public void testGetWordRangeAtCursor() {
+        ExtractedText et = new ExtractedText();
+        final MockInputMethodService mockInputMethodService = new MockInputMethodService();
+        final RichInputConnection ic = new RichInputConnection(mockInputMethodService);
+        mockInputMethodService.setInputConnection(new MockConnection("word wo", "rd", et));
+        et.startOffset = 0;
+        et.selectionStart = 7;
+        Range r;
+
+        ic.beginBatchEdit();
+        // basic case
+        r = ic.getWordRangeAtCursor(" ", 0);
+        assertTrue(TextUtils.equals("word", r.mWord));
+
+        // more than one word
+        r = ic.getWordRangeAtCursor(" ", 1);
+        assertTrue(TextUtils.equals("word word", r.mWord));
+        ic.endBatchEdit();
+
+        // tab character instead of space
+        mockInputMethodService.setInputConnection(new MockConnection("one\tword\two", "rd", et));
+        ic.beginBatchEdit();
+        r = ic.getWordRangeAtCursor("\t", 1);
+        ic.endBatchEdit();
+        assertTrue(TextUtils.equals("word\tword", r.mWord));
+
+        // only one word doesn't go too far
+        mockInputMethodService.setInputConnection(new MockConnection("one\tword\two", "rd", et));
+        ic.beginBatchEdit();
+        r = ic.getWordRangeAtCursor("\t", 1);
+        ic.endBatchEdit();
+        assertTrue(TextUtils.equals("word\tword", r.mWord));
+
+        // tab or space
+        mockInputMethodService.setInputConnection(new MockConnection("one word\two", "rd", et));
+        ic.beginBatchEdit();
+        r = ic.getWordRangeAtCursor(" \t", 1);
+        ic.endBatchEdit();
+        assertTrue(TextUtils.equals("word\tword", r.mWord));
+
+        // tab or space multiword
+        mockInputMethodService.setInputConnection(new MockConnection("one word\two", "rd", et));
+        ic.beginBatchEdit();
+        r = ic.getWordRangeAtCursor(" \t", 2);
+        ic.endBatchEdit();
+        assertTrue(TextUtils.equals("one word\tword", r.mWord));
+
+        // splitting on supplementary character
+        final String supplementaryChar = "\uD840\uDC8A";
+        mockInputMethodService.setInputConnection(
+                new MockConnection("one word" + supplementaryChar + "wo", "rd", et));
+        ic.beginBatchEdit();
+        r = ic.getWordRangeAtCursor(supplementaryChar, 0);
+        ic.endBatchEdit();
+        assertTrue(TextUtils.equals("word", r.mWord));
+    }
+
+    /**
+     * Test logic in getting the word range at the cursor.
+     */
+    public void testGetSuggestionSpansAtWord() {
+        helpTestGetSuggestionSpansAtWord(10);
+        helpTestGetSuggestionSpansAtWord(12);
+        helpTestGetSuggestionSpansAtWord(15);
+        helpTestGetSuggestionSpansAtWord(16);
+    }
+
+    private void helpTestGetSuggestionSpansAtWord(final int cursorPos) {
+        final MockInputMethodService mockInputMethodService = new MockInputMethodService();
+        final RichInputConnection ic = new RichInputConnection(mockInputMethodService);
+
+        final String[] SUGGESTIONS1 = { "swing", "strong" };
+        final String[] SUGGESTIONS2 = { "storing", "strung" };
+
+        // Test the usual case.
+        SpannableString text = new SpannableString("This is a string for test");
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
+                10 /* start */, 16 /* end */, 0 /* flags */);
+        mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
+        Range r;
+        SuggestionSpan[] suggestions;
+
+        r = ic.getWordRangeAtCursor(" ", 0);
+        suggestions = r.getSuggestionSpansAtWord();
+        assertEquals(suggestions.length, 1);
+        MoreAsserts.assertEquals(suggestions[0].getSuggestions(), SUGGESTIONS1);
+
+        // Test the case with 2 suggestion spans in the same place.
+        text = new SpannableString("This is a string for test");
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
+                10 /* start */, 16 /* end */, 0 /* flags */);
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS2, 0 /* flags */),
+                10 /* start */, 16 /* end */, 0 /* flags */);
+        mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
+        r = ic.getWordRangeAtCursor(" ", 0);
+        suggestions = r.getSuggestionSpansAtWord();
+        assertEquals(suggestions.length, 2);
+        MoreAsserts.assertEquals(suggestions[0].getSuggestions(), SUGGESTIONS1);
+        MoreAsserts.assertEquals(suggestions[1].getSuggestions(), SUGGESTIONS2);
+
+        // Test a case with overlapping spans, 2nd extending past the start of the word
+        text = new SpannableString("This is a string for test");
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
+                10 /* start */, 16 /* end */, 0 /* flags */);
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS2, 0 /* flags */),
+                5 /* start */, 16 /* end */, 0 /* flags */);
+        mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
+        r = ic.getWordRangeAtCursor(" ", 0);
+        suggestions = r.getSuggestionSpansAtWord();
+        assertEquals(suggestions.length, 1);
+        MoreAsserts.assertEquals(suggestions[0].getSuggestions(), SUGGESTIONS1);
+
+        // Test a case with overlapping spans, 2nd extending past the end of the word
+        text = new SpannableString("This is a string for test");
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
+                10 /* start */, 16 /* end */, 0 /* flags */);
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS2, 0 /* flags */),
+                10 /* start */, 20 /* end */, 0 /* flags */);
+        mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
+        r = ic.getWordRangeAtCursor(" ", 0);
+        suggestions = r.getSuggestionSpansAtWord();
+        assertEquals(suggestions.length, 1);
+        MoreAsserts.assertEquals(suggestions[0].getSuggestions(), SUGGESTIONS1);
+
+        // Test a case with overlapping spans, 2nd extending past both ends of the word
+        text = new SpannableString("This is a string for test");
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
+                10 /* start */, 16 /* end */, 0 /* flags */);
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS2, 0 /* flags */),
+                5 /* start */, 20 /* end */, 0 /* flags */);
+        mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
+        r = ic.getWordRangeAtCursor(" ", 0);
+        suggestions = r.getSuggestionSpansAtWord();
+        assertEquals(suggestions.length, 1);
+        MoreAsserts.assertEquals(suggestions[0].getSuggestions(), SUGGESTIONS1);
+
+        // Test a case with overlapping spans, none right on the word
+        text = new SpannableString("This is a string for test");
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS1, 0 /* flags */),
+                5 /* start */, 16 /* end */, 0 /* flags */);
+        text.setSpan(new SuggestionSpan(Locale.ENGLISH, SUGGESTIONS2, 0 /* flags */),
+                5 /* start */, 20 /* end */, 0 /* flags */);
+        mockInputMethodService.setInputConnection(new MockConnection(text, cursorPos));
+        r = ic.getWordRangeAtCursor(" ", 0);
+        suggestions = r.getSuggestionSpansAtWord();
+        assertEquals(suggestions.length, 0);
+    }
+}
diff --git a/tests/src/com/android/inputmethod/latin/RichInputConnectionTests.java b/tests/src/com/android/inputmethod/latin/RichInputConnectionTests.java
deleted file mode 100644
index aacd60f..0000000
--- a/tests/src/com/android/inputmethod/latin/RichInputConnectionTests.java
+++ /dev/null
@@ -1,194 +0,0 @@
-/*
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.inputmethod.latin;
-
-import android.inputmethodservice.InputMethodService;
-import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
-import android.text.TextUtils;
-import android.view.inputmethod.ExtractedText;
-import android.view.inputmethod.ExtractedTextRequest;
-import android.view.inputmethod.InputConnection;
-import android.view.inputmethod.InputConnectionWrapper;
-
-import com.android.inputmethod.latin.RichInputConnection.Range;
-
-@SmallTest
-public class RichInputConnectionTests extends AndroidTestCase {
-
-    // The following is meant to be a reasonable default for
-    // the "word_separators" resource.
-    private static final String sSeparators = ".,:;!?-";
-
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-    }
-
-    private class MockConnection extends InputConnectionWrapper {
-        final String mTextBefore;
-        final String mTextAfter;
-        final ExtractedText mExtractedText;
-
-        public MockConnection(String textBefore, String textAfter, ExtractedText extractedText) {
-            super(null, false);
-            mTextBefore = textBefore;
-            mTextAfter = textAfter;
-            mExtractedText = extractedText;
-        }
-
-        /* (non-Javadoc)
-         * @see android.view.inputmethod.InputConnectionWrapper#getTextBeforeCursor(int, int)
-         */
-        @Override
-        public CharSequence getTextBeforeCursor(int n, int flags) {
-            return mTextBefore;
-        }
-
-        /* (non-Javadoc)
-         * @see android.view.inputmethod.InputConnectionWrapper#getTextAfterCursor(int, int)
-         */
-        @Override
-        public CharSequence getTextAfterCursor(int n, int flags) {
-            return mTextAfter;
-        }
-
-        /* (non-Javadoc)
-         * @see android.view.inputmethod.InputConnectionWrapper#getExtractedText(
-         *         ExtractedTextRequest, int)
-         */
-        @Override
-        public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) {
-            return mExtractedText;
-        }
-
-        @Override
-        public boolean beginBatchEdit() {
-            return true;
-        }
-
-        @Override
-        public boolean endBatchEdit() {
-            return true;
-        }
-
-        @Override
-        public boolean finishComposingText() {
-            return true;
-        }
-    }
-
-    private class MockInputMethodService extends InputMethodService {
-        InputConnection mInputConnection;
-        public void setInputConnection(final InputConnection inputConnection) {
-            mInputConnection = inputConnection;
-        }
-        @Override
-        public InputConnection getCurrentInputConnection() {
-            return mInputConnection;
-        }
-    }
-
-    /************************** Tests ************************/
-
-    /**
-     * Test for getting previous word (for bigram suggestions)
-     */
-    public void testGetPreviousWord() {
-        // If one of the following cases breaks, the bigram suggestions won't work.
-        assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 2), "abc");
-        assertNull(RichInputConnection.getNthPreviousWord("abc", sSeparators, 2));
-        assertNull(RichInputConnection.getNthPreviousWord("abc. def", sSeparators, 2));
-
-        // The following tests reflect the current behavior of the function
-        // RichInputConnection#getNthPreviousWord.
-        // TODO: However at this time, the code does never go
-        // into such a path, so it should be safe to change the behavior of
-        // this function if needed - especially since it does not seem very
-        // logical. These tests are just there to catch any unintentional
-        // changes in the behavior of the RichInputConnection#getPreviousWord method.
-        assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 2), "abc");
-        assertEquals(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 2), "abc");
-        assertEquals(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 2), "def");
-        assertNull(RichInputConnection.getNthPreviousWord("abc ", sSeparators, 2));
-
-        assertEquals(RichInputConnection.getNthPreviousWord("abc def", sSeparators, 1), "def");
-        assertEquals(RichInputConnection.getNthPreviousWord("abc def ", sSeparators, 1), "def");
-        assertNull(RichInputConnection.getNthPreviousWord("abc def.", sSeparators, 1));
-        assertNull(RichInputConnection.getNthPreviousWord("abc def .", sSeparators, 1));
-    }
-
-    /**
-     * Test logic in getting the word range at the cursor.
-     */
-    public void testGetWordRangeAtCursor() {
-        ExtractedText et = new ExtractedText();
-        final MockInputMethodService mockInputMethodService = new MockInputMethodService();
-        final RichInputConnection ic = new RichInputConnection(mockInputMethodService);
-        mockInputMethodService.setInputConnection(new MockConnection("word wo", "rd", et));
-        et.startOffset = 0;
-        et.selectionStart = 7;
-        Range r;
-
-        ic.beginBatchEdit();
-        // basic case
-        r = ic.getWordRangeAtCursor(" ", 0);
-        assertTrue(TextUtils.equals("word", r.mWord));
-
-        // more than one word
-        r = ic.getWordRangeAtCursor(" ", 1);
-        assertTrue(TextUtils.equals("word word", r.mWord));
-        ic.endBatchEdit();
-
-        // tab character instead of space
-        mockInputMethodService.setInputConnection(new MockConnection("one\tword\two", "rd", et));
-        ic.beginBatchEdit();
-        r = ic.getWordRangeAtCursor("\t", 1);
-        ic.endBatchEdit();
-        assertTrue(TextUtils.equals("word\tword", r.mWord));
-
-        // only one word doesn't go too far
-        mockInputMethodService.setInputConnection(new MockConnection("one\tword\two", "rd", et));
-        ic.beginBatchEdit();
-        r = ic.getWordRangeAtCursor("\t", 1);
-        ic.endBatchEdit();
-        assertTrue(TextUtils.equals("word\tword", r.mWord));
-
-        // tab or space
-        mockInputMethodService.setInputConnection(new MockConnection("one word\two", "rd", et));
-        ic.beginBatchEdit();
-        r = ic.getWordRangeAtCursor(" \t", 1);
-        ic.endBatchEdit();
-        assertTrue(TextUtils.equals("word\tword", r.mWord));
-
-        // tab or space multiword
-        mockInputMethodService.setInputConnection(new MockConnection("one word\two", "rd", et));
-        ic.beginBatchEdit();
-        r = ic.getWordRangeAtCursor(" \t", 2);
-        ic.endBatchEdit();
-        assertTrue(TextUtils.equals("one word\tword", r.mWord));
-
-        // splitting on supplementary character
-        final String supplementaryChar = "\uD840\uDC8A";
-        mockInputMethodService.setInputConnection(
-                new MockConnection("one word" + supplementaryChar + "wo", "rd", et));
-        ic.beginBatchEdit();
-        r = ic.getWordRangeAtCursor(supplementaryChar, 0);
-        ic.endBatchEdit();
-        assertTrue(TextUtils.equals("word", r.mWord));
-    }
-}