Merge "Fix oscillations on ICS"
diff --git a/common/src/com/android/inputmethod/latin/common/StringUtils.java b/common/src/com/android/inputmethod/latin/common/StringUtils.java
index be72603..572f0cd 100644
--- a/common/src/com/android/inputmethod/latin/common/StringUtils.java
+++ b/common/src/com/android/inputmethod/latin/common/StringUtils.java
@@ -201,22 +201,22 @@
     public static String capitalizeFirstCodePoint(@Nonnull final String s,
             @Nonnull final Locale locale) {
         if (s.length() <= 1) {
-            return s.toUpperCase(locale);
+            return s.toUpperCase(getLocaleUsedForToTitleCase(locale));
         }
         // Please refer to the comment below in
         // {@link #capitalizeFirstAndDowncaseRest(String,Locale)} as this has the same shortcomings
         final int cutoff = s.offsetByCodePoints(0, 1);
-        return s.substring(0, cutoff).toUpperCase(locale) + s.substring(cutoff);
+        return s.substring(0, cutoff).toUpperCase(getLocaleUsedForToTitleCase(locale))
+                + s.substring(cutoff);
     }
 
     @Nonnull
     public static String capitalizeFirstAndDowncaseRest(@Nonnull final String s,
             @Nonnull final Locale locale) {
         if (s.length() <= 1) {
-            return s.toUpperCase(locale);
+            return s.toUpperCase(getLocaleUsedForToTitleCase(locale));
         }
         // TODO: fix the bugs below
-        // - This does not work for Greek, because it returns upper case instead of title case.
         // - It does not work for Serbian, because it fails to account for the "lj" character,
         // which should be "Lj" in title case and "LJ" in upper case.
         // - It does not work for Dutch, because it fails to account for the "ij" digraph when it's
@@ -224,7 +224,8 @@
         // be capitalized as "IJ" as if they were a single letter in most words (not all). If the
         // unicode char for the ligature is used however, it works.
         final int cutoff = s.offsetByCodePoints(0, 1);
-        return s.substring(0, cutoff).toUpperCase(locale) + s.substring(cutoff).toLowerCase(locale);
+        return s.substring(0, cutoff).toUpperCase(getLocaleUsedForToTitleCase(locale))
+                + s.substring(cutoff).toLowerCase(locale);
     }
 
     @Nonnull
@@ -584,25 +585,35 @@
         return bytes;
     }
 
-    @Nullable
-    public static String toUpperCaseOfStringForLocale(@Nullable final String text,
-            final boolean needsToUpperCase, @Nonnull final Locale locale) {
-        if (text == null || !needsToUpperCase) {
-            return text;
+    private static final String LANGUAGE_GREEK = "el";
+
+    @Nonnull
+    private static Locale getLocaleUsedForToTitleCase(@Nonnull final Locale locale) {
+        // In Greek locale {@link String#toUpperCase(Locale)} eliminates accents from its result.
+        // In order to get accented upper case letter, {@link Locale#ROOT} should be used.
+        if (LANGUAGE_GREEK.equals(locale.getLanguage())) {
+            return Locale.ROOT;
         }
-        return text.toUpperCase(locale);
+        return locale;
     }
 
-    public static int toUpperCaseOfCodeForLocale(final int code, final boolean needsToUpperCase,
+    @Nullable
+    public static String toTitleCaseOfKeyLabel(@Nullable final String label,
             @Nonnull final Locale locale) {
-        if (!Constants.isLetterCode(code) || !needsToUpperCase) {
+        if (label == null) {
+            return label;
+        }
+        return label.toUpperCase(getLocaleUsedForToTitleCase(locale));
+    }
+
+    public static int toTitleCaseOfKeyCode(final int code, @Nonnull final Locale locale) {
+        if (!Constants.isLetterCode(code)) {
             return code;
         }
-        final String text = newSingleCodePointString(code);
-        final String casedText = toUpperCaseOfStringForLocale(
-                text, needsToUpperCase, locale);
-        return codePointCount(casedText) == 1
-                ? casedText.codePointAt(0) : Constants.CODE_UNSPECIFIED;
+        final String label = newSingleCodePointString(code);
+        final String titleCaseLabel = toTitleCaseOfKeyLabel(label, locale);
+        return codePointCount(titleCaseLabel) == 1
+                ? titleCaseLabel.codePointAt(0) : Constants.CODE_UNSPECIFIED;
     }
 
     public static int getTrailingSingleQuotesCount(@Nonnull final CharSequence charSequence) {
diff --git a/java/res/xml/method.xml b/java/res/xml/method.xml
index 5f05e8b..e148622 100644
--- a/java/res/xml/method.xml
+++ b/java/res/xml/method.xml
@@ -28,7 +28,7 @@
     be_BY: Belarusian (Belarus)/east_slavic
     bg: Bulgarian/bulgarian
     bg: Bulgarian/bulgarian_bds
-    bn_BD: Bengali (Bangladesh)/bengali_akkhor # This is a preliminary keyboard layout.
+    bn_BD: Bengali (Bangladesh)/bengali_akkhor
     bn_IN: Bengali (India)/bengali
     ca: Catalan/spanish
     cs: Czech/qwertz
@@ -181,8 +181,6 @@
             android:imeSubtypeExtraValue="KeyboardLayoutSet=bulgarian_bds,EmojiCapable"
             android:isAsciiCapable="false"
     />
-    <!-- TODO: This Bengali (Bangladesh) keyboard is a preliminary layout.
-               This isn't based on the final specification. -->
     <subtype android:icon="@drawable/ic_ime_switcher_dark"
             android:label="@string/subtype_generic"
             android:subtypeId="0xa2144b0c"
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java
index 6b2094b..06e552e 100644
--- a/java/src/com/android/inputmethod/keyboard/Key.java
+++ b/java/src/com/android/inputmethod/keyboard/Key.java
@@ -341,17 +341,24 @@
             // code point nor as a surrogate pair.
             mLabel = new StringBuilder().appendCodePoint(code).toString();
         } else {
-            mLabel = StringUtils.toUpperCaseOfStringForLocale(
-                    KeySpecParser.getLabel(keySpec), needsToUpcase, localeForUpcasing);
+            final String label = KeySpecParser.getLabel(keySpec);
+            mLabel = needsToUpcase
+                    ? StringUtils.toTitleCaseOfKeyLabel(label, localeForUpcasing)
+                    : label;
         }
         if ((mLabelFlags & LABEL_FLAGS_DISABLE_HINT_LABEL) != 0) {
             mHintLabel = null;
         } else {
-            mHintLabel = StringUtils.toUpperCaseOfStringForLocale(style.getString(keyAttr,
-                    R.styleable.Keyboard_Key_keyHintLabel), needsToUpcase, localeForUpcasing);
+            final String hintLabel = style.getString(
+                    keyAttr, R.styleable.Keyboard_Key_keyHintLabel);
+            mHintLabel = needsToUpcase
+                    ? StringUtils.toTitleCaseOfKeyLabel(hintLabel, localeForUpcasing)
+                    : hintLabel;
         }
-        String outputText = StringUtils.toUpperCaseOfStringForLocale(
-                KeySpecParser.getOutputText(keySpec), needsToUpcase, localeForUpcasing);
+        String outputText = KeySpecParser.getOutputText(keySpec);
+        if (needsToUpcase) {
+            outputText = StringUtils.toTitleCaseOfKeyLabel(outputText, localeForUpcasing);
+        }
         // Choose the first letter of the label as primary code if not specified.
         if (code == CODE_UNSPECIFIED && TextUtils.isEmpty(outputText)
                 && !TextUtils.isEmpty(mLabel)) {
@@ -377,12 +384,14 @@
                 mCode = CODE_OUTPUT_TEXT;
             }
         } else {
-            mCode = StringUtils.toUpperCaseOfCodeForLocale(code, needsToUpcase, localeForUpcasing);
+            mCode = needsToUpcase ? StringUtils.toTitleCaseOfKeyCode(code, localeForUpcasing)
+                    : code;
         }
         final int altCodeInAttr = KeySpecParser.parseCode(
                 style.getString(keyAttr, R.styleable.Keyboard_Key_altCode), CODE_UNSPECIFIED);
-        final int altCode = StringUtils.toUpperCaseOfCodeForLocale(
-                altCodeInAttr, needsToUpcase, localeForUpcasing);
+        final int altCode = needsToUpcase
+                ? StringUtils.toTitleCaseOfKeyCode(altCodeInAttr, localeForUpcasing)
+                : altCodeInAttr;
         mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode,
                 disabledIconId, visualInsetsLeft, visualInsetsRight);
         mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
index cba7ff2..06b87bd 100644
--- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java
@@ -57,7 +57,6 @@
 import com.android.inputmethod.latin.SuggestedWords;
 import com.android.inputmethod.latin.common.Constants;
 import com.android.inputmethod.latin.common.CoordinateUtils;
-import com.android.inputmethod.latin.common.StringUtils;
 import com.android.inputmethod.latin.settings.DebugSettings;
 import com.android.inputmethod.latin.utils.TypefaceUtils;
 
@@ -874,8 +873,7 @@
             final Locale[] locales = subtype.getLocales();
             final String[] languages = new String[locales.length];
             for (int i = 0; i < locales.length; ++i) {
-                languages[i] = StringUtils.toUpperCaseOfStringForLocale(
-                        locales[i].getLanguage(), true /* needsToUpperCase */, Locale.ROOT);
+                languages[i] = locales[i].getLanguage().toUpperCase(Locale.ROOT);
             }
             return TextUtils.join(" / ", languages);
         }
diff --git a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java
index b1a3887..87c96cc 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/MoreKeySpec.java
@@ -55,10 +55,11 @@
         if (TextUtils.isEmpty(moreKeySpec)) {
             throw new KeySpecParser.KeySpecParserError("Empty more key spec");
         }
-        mLabel = StringUtils.toUpperCaseOfStringForLocale(
-                KeySpecParser.getLabel(moreKeySpec), needsToUpperCase, locale);
-        final int code = StringUtils.toUpperCaseOfCodeForLocale(
-                KeySpecParser.getCode(moreKeySpec), needsToUpperCase, locale);
+        final String label = KeySpecParser.getLabel(moreKeySpec);
+        mLabel = needsToUpperCase ? StringUtils.toTitleCaseOfKeyLabel(label, locale) : label;
+        final int codeInSpec = KeySpecParser.getCode(moreKeySpec);
+        final int code = needsToUpperCase ? StringUtils.toTitleCaseOfKeyCode(codeInSpec, locale)
+                : codeInSpec;
         if (code == Constants.CODE_UNSPECIFIED) {
             // Some letter, for example German Eszett (U+00DF: "ß"), has multiple characters
             // upper case representation ("SS").
@@ -66,8 +67,9 @@
             mOutputText = mLabel;
         } else {
             mCode = code;
-            mOutputText = StringUtils.toUpperCaseOfStringForLocale(
-                    KeySpecParser.getOutputText(moreKeySpec), needsToUpperCase, locale);
+            final String outputText = KeySpecParser.getOutputText(moreKeySpec);
+            mOutputText = needsToUpperCase
+                    ? StringUtils.toTitleCaseOfKeyLabel(outputText, locale) : outputText;
         }
         mIconId = KeySpecParser.getIconId(moreKeySpec);
     }
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 7a409c8..db67e34 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1189,9 +1189,13 @@
                 SuggestedWords.getFromApplicationSpecifiedCompletions(
                         applicationSpecifiedCompletions);
         final SuggestedWords suggestedWords = new SuggestedWords(applicationSuggestedWords,
-                null /* rawSuggestions */, false /* typedWordValid */, false /* willAutoCorrect */,
+                null /* rawSuggestions */,
+                null /* typedWord */,
+                false /* typedWordValid */,
+                false /* willAutoCorrect */,
                 false /* isObsoleteSuggestions */,
-                SuggestedWords.INPUT_STYLE_APPLICATION_SPECIFIED /* inputStyle */);
+                SuggestedWords.INPUT_STYLE_APPLICATION_SPECIFIED /* inputStyle */,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
         // When in fullscreen mode, show completions generated by the application forcibly
         setSuggestedWords(suggestedWords);
     }
diff --git a/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java b/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java
index a65304c..555bbc7 100644
--- a/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java
+++ b/java/src/com/android/inputmethod/latin/PunctuationSuggestions.java
@@ -33,10 +33,12 @@
     private PunctuationSuggestions(final ArrayList<SuggestedWordInfo> punctuationsList) {
         super(punctuationsList,
                 null /* rawSuggestions */,
+                null /* typedWord */,
                 false /* typedWordValid */,
                 false /* hasAutoCorrectionCandidate */,
                 false /* isObsoleteSuggestions */,
-                INPUT_STYLE_NONE /* inputStyle */);
+                INPUT_STYLE_NONE /* inputStyle */,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
     }
 
     /**
diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java
index bddeac4..02b2d56 100644
--- a/java/src/com/android/inputmethod/latin/SuggestedWords.java
+++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java
@@ -50,8 +50,9 @@
     private static final ArrayList<SuggestedWordInfo> EMPTY_WORD_INFO_LIST = new ArrayList<>(0);
     @Nonnull
     private static final SuggestedWords EMPTY = new SuggestedWords(
-            EMPTY_WORD_INFO_LIST, null /* rawSuggestions */, false /* typedWordValid */,
-            false /* willAutoCorrect */, false /* isObsoleteSuggestions */, INPUT_STYLE_NONE);
+            EMPTY_WORD_INFO_LIST, null /* rawSuggestions */, null /* typedWord */,
+            false /* typedWordValid */, false /* willAutoCorrect */,
+            false /* isObsoleteSuggestions */, INPUT_STYLE_NONE, NOT_A_SEQUENCE_NUMBER);
 
     public final String mTypedWord;
     public final boolean mTypedWordValid;
@@ -69,19 +70,6 @@
 
     public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList,
             final ArrayList<SuggestedWordInfo> rawSuggestions,
-            final boolean typedWordValid,
-            final boolean willAutoCorrect,
-            final boolean isObsoleteSuggestions,
-            final int inputStyle) {
-        this(suggestedWordInfoList, rawSuggestions,
-                (suggestedWordInfoList.isEmpty() || isPrediction(inputStyle)) ? null
-                        : suggestedWordInfoList.get(INDEX_OF_TYPED_WORD).mWord,
-                typedWordValid, willAutoCorrect,
-                isObsoleteSuggestions, inputStyle, NOT_A_SEQUENCE_NUMBER);
-    }
-
-    public SuggestedWords(final ArrayList<SuggestedWordInfo> suggestedWordInfoList,
-            final ArrayList<SuggestedWordInfo> rawSuggestions,
             final String typedWord,
             final boolean typedWordValid,
             final boolean willAutoCorrect,
@@ -423,8 +411,10 @@
                     info.mSourceDict, SuggestedWordInfo.NOT_AN_INDEX,
                     SuggestedWordInfo.NOT_A_CONFIDENCE));
         }
-        return new SuggestedWords(newSuggestions, null /* rawSuggestions */, mTypedWordValid,
-                mWillAutoCorrect, mIsObsoleteSuggestions, INPUT_STYLE_TAIL_BATCH);
+        return new SuggestedWords(newSuggestions, null /* rawSuggestions */,
+                newSuggestions.isEmpty() ? null : newSuggestions.get(0).mWord /* typedWord */,
+                mTypedWordValid, mWillAutoCorrect, mIsObsoleteSuggestions, INPUT_STYLE_TAIL_BATCH,
+                NOT_A_SEQUENCE_NUMBER);
     }
 
     /**
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index f74e9b5..bca2464 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -255,7 +255,7 @@
         handler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_TYPING);
         final String text = performSpecificTldProcessingOnTextInput(rawText);
         if (SpaceState.PHANTOM == mSpaceState) {
-            promotePhantomSpace(settingsValues);
+            insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
         }
         mConnection.commitText(text, 1);
         StatsUtils.onWordCommitUserTyped(mEnteredText, mWordComposer.isBatchMode());
@@ -322,7 +322,7 @@
             final int firstChar = Character.codePointAt(suggestion, 0);
             if (!settingsValues.isWordSeparator(firstChar)
                     || settingsValues.isUsuallyPrecededBySpace(firstChar)) {
-                promotePhantomSpace(settingsValues);
+                insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
             }
         }
 
@@ -584,7 +584,9 @@
                 if (candidate.mSourceDict.shouldAutoCommit(candidate)) {
                     final String[] commitParts = candidate.mWord.split(Constants.WORD_SEPARATOR, 2);
                     batchPointers.shift(candidate.mIndexOfTouchPointOfSecondWord);
-                    promotePhantomSpace(settingsValues);
+                    if (SpaceState.PHANTOM == mSpaceState) {
+                        insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
+                    }
                     mConnection.commitText(commitParts[0], 0);
                     StatsUtils.onWordCommitUserTyped(commitParts[0], mWordComposer.isBatchMode());
                     mSpaceState = SpaceState.PHANTOM;
@@ -861,7 +863,7 @@
                 // Sanity check
                 throw new RuntimeException("Should not be composing here");
             }
-            promotePhantomSpace(settingsValues);
+            insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
         }
 
         if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
@@ -972,7 +974,7 @@
         }
 
         if (needsPrecedingSpace) {
-            promotePhantomSpace(settingsValues);
+            insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
         }
 
         if (tryPerformDoubleSpacePeriod(event, inputTransaction)) {
@@ -1886,8 +1888,9 @@
         final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions =
                 SuggestedWords.getTypedWordAndPreviousSuggestions(typedWord, oldSuggestedWords);
         return new SuggestedWords(typedWordAndPreviousSuggestions, null /* rawSuggestions */,
-                false /* typedWordValid */, false /* hasAutoCorrectionCandidate */,
-                true /* isObsoleteSuggestions */, oldSuggestedWords.mInputStyle);
+                typedWord, false /* typedWordValid */, false /* hasAutoCorrectionCandidate */,
+                true /* isObsoleteSuggestions */, oldSuggestedWords.mInputStyle,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
     }
 
     /**
@@ -1964,14 +1967,14 @@
     }
 
     /**
-     * Promote a phantom space to an actual space.
+     * Insert an automatic space, if the options allow it.
      *
-     * This essentially inserts a space, and that's it. It just checks the options and the text
-     * before the cursor are appropriate before doing it.
+     * This checks the options and the text before the cursor are appropriate before inserting
+     * an automatic space.
      *
      * @param settingsValues the current values of the settings.
      */
-    private void promotePhantomSpace(final SettingsValues settingsValues) {
+    private void insertAutomaticSpaceIfOptionsAndTextAllow(final SettingsValues settingsValues) {
         if (settingsValues.shouldInsertSpacesAutomatically()
                 && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
                 && !mConnection.textBeforeCursorLooksLikeURL()) {
@@ -1994,7 +1997,7 @@
         }
         mConnection.beginBatchEdit();
         if (SpaceState.PHANTOM == mSpaceState) {
-            promotePhantomSpace(settingsValues);
+            insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
         }
         final SuggestedWordInfo autoCommitCandidate = mSuggestedWords.getAutoCommitCandidate();
         // Commit except the last word for phrase gesture if the top suggestion is eligible for auto
diff --git a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java
index 013f024..0e7f471 100644
--- a/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/SubtypeLocaleUtils.java
@@ -34,6 +34,7 @@
 import java.util.Locale;
 
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 /**
  * A helper class to deal with subtype locales.
@@ -273,7 +274,7 @@
     }
 
     @Nonnull
-    public static String getSubtypeNameForLogging(@Nonnull final InputMethodSubtype subtype) {
+    public static String getSubtypeNameForLogging(@Nullable final InputMethodSubtype subtype) {
         if (subtype == null) {
             return "<null subtype>";
         }
diff --git a/native/jni/src/utils/char_utils.cpp b/native/jni/src/utils/char_utils.cpp
index 3bb9055..a43e6dd 100644
--- a/native/jni/src/utils/char_utils.cpp
+++ b/native/jni/src/utils/char_utils.cpp
@@ -1117,7 +1117,9 @@
           // TODO: Check if it's really acceptable to consider ø a diacritical variant of o
     /* U+0100 */ 0x0041, 0x0061, 0x0041, 0x0061, 0x0041, 0x0061, 0x0043, 0x0063,
     /* U+0108 */ 0x0043, 0x0063, 0x0043, 0x0063, 0x0043, 0x0063, 0x0044, 0x0064,
-    /* U+0110 */ 0x0110, 0x0111, 0x0045, 0x0065, 0x0045, 0x0065, 0x0045, 0x0065,
+    /* U+0110 */ 0x0046, 0x0064, 0x0045, 0x0065, 0x0045, 0x0065, 0x0045, 0x0065,
+        // U+0110: Manually changed from 0110 to 0046
+        // U+0111: Manually changed from 0111 to 0064
     /* U+0118 */ 0x0045, 0x0065, 0x0045, 0x0065, 0x0047, 0x0067, 0x0047, 0x0067,
     /* U+0120 */ 0x0047, 0x0067, 0x0047, 0x0067, 0x0048, 0x0068, 0x0126, 0x0127,
     /* U+0128 */ 0x0049, 0x0069, 0x0049, 0x0069, 0x0049, 0x0069, 0x0049, 0x0069,
@@ -1135,6 +1137,9 @@
     /* U+0170 */ 0x0055, 0x0075, 0x0055, 0x0075, 0x0057, 0x0077, 0x0059, 0x0079,
     /* U+0178 */ 0x0059, 0x005A, 0x007A, 0x005A, 0x007A, 0x005A, 0x007A, 0x0073,
     /* U+0180 */ 0x0180, 0x0181, 0x0182, 0x0183, 0x0184, 0x0185, 0x0186, 0x0187,
+          // TODO: A lot of letters are their own base code points, but for
+          // some (e.g. U+0180) it doesn't seem right. Ideally each code point should
+          // be checked individually with all languages it's used in.
     /* U+0188 */ 0x0188, 0x0189, 0x018A, 0x018B, 0x018C, 0x018D, 0x018E, 0x018F,
     /* U+0190 */ 0x0190, 0x0191, 0x0192, 0x0193, 0x0194, 0x0195, 0x0196, 0x0197,
     /* U+0198 */ 0x0198, 0x0199, 0x019A, 0x019B, 0x019C, 0x019D, 0x019E, 0x019F,
diff --git a/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java b/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java
index 1c320db..7bbf965 100644
--- a/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java
+++ b/tests/src/com/android/inputmethod/compat/SuggestionSpanUtilsTest.java
@@ -102,11 +102,11 @@
     }
 
     public void testGetTextWithSuggestionSpan() {
-        final SuggestedWordInfo predicition1 =
+        final SuggestedWordInfo prediction1 =
                 createWordInfo("Quality", SuggestedWordInfo.KIND_PREDICTION);
-        final SuggestedWordInfo predicition2 =
+        final SuggestedWordInfo prediction2 =
                 createWordInfo("Speed", SuggestedWordInfo.KIND_PREDICTION);
-        final SuggestedWordInfo predicition3 =
+        final SuggestedWordInfo prediction3 =
                 createWordInfo("Price", SuggestedWordInfo.KIND_PREDICTION);
 
         final SuggestedWordInfo typed =
@@ -122,13 +122,15 @@
         // is specified.
         {
             final SuggestedWords predictedWords = new SuggestedWords(
-                    new ArrayList<>(Arrays.asList(predicition1, predicition2, predicition3)),
+                    new ArrayList<>(Arrays.asList(prediction1, prediction2, prediction3)),
                     null /* rawSuggestions */,
+                    null /* typedWord */,
                     false /* typedWordValid */,
                     false /* willAutoCorrect */,
                     false /* isObsoleteSuggestions */,
-                    SuggestedWords.INPUT_STYLE_PREDICTION);
-            final String PICKED_WORD = predicition2.mWord;
+                    SuggestedWords.INPUT_STYLE_PREDICTION,
+                    SuggestedWords.NOT_A_SEQUENCE_NUMBER);
+            final String PICKED_WORD = prediction2.mWord;
             assertNotSuggestionSpan(
                     PICKED_WORD,
                     SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD,
@@ -137,17 +139,19 @@
 
         final ArrayList<SuggestedWordInfo> suggestedWordList = new ArrayList<>();
         suggestedWordList.add(typed);
-        suggestedWordList.add(predicition1);
-        suggestedWordList.add(predicition2);
-        suggestedWordList.add(predicition3);
+        suggestedWordList.add(prediction1);
+        suggestedWordList.add(prediction2);
+        suggestedWordList.add(prediction3);
         suggestedWordList.addAll(Arrays.asList(corrections));
         final SuggestedWords typedAndCollectedWords = new SuggestedWords(
                 suggestedWordList,
                 null /* rawSuggestions */,
+                null /* typedWord */,
                 false /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isObsoleteSuggestions */,
-                SuggestedWords.INPUT_STYLE_TYPING);
+                SuggestedWords.INPUT_STYLE_TYPING,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
 
         for (final SuggestedWordInfo pickedWord : suggestedWordList) {
             final String PICKED_WORD = pickedWord.mWord;
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java b/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java
index 9bb5f18..e7b0f09 100644
--- a/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java
+++ b/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyOutput.java
@@ -63,7 +63,7 @@
                 final String codeString = StringUtils.newSingleCodePointString(mCode);
                 // A letter may have an upper case counterpart that consists of multiple code
                 // points, for instance the upper case of "ß" is "SS".
-                return newInstance(codeString.toUpperCase(locale));
+                return newInstance(StringUtils.toTitleCaseOfKeyLabel(codeString, locale));
             }
             // A special negative value has no upper case.
             return this;
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyVisual.java b/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyVisual.java
index 2f3a0c1..3f9f12a 100644
--- a/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyVisual.java
+++ b/tests/src/com/android/inputmethod/keyboard/layout/expected/ExpectedKeyVisual.java
@@ -19,6 +19,7 @@
 import com.android.inputmethod.keyboard.Key;
 import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
 import com.android.inputmethod.keyboard.internal.MoreKeySpec;
+import com.android.inputmethod.latin.common.StringUtils;
 
 import java.util.Locale;
 
@@ -134,7 +135,7 @@
 
         @Override
         ExpectedKeyVisual toUpperCase(final Locale locale) {
-            return new Label(mLabel.toUpperCase(locale));
+            return new Label(StringUtils.toTitleCaseOfKeyLabel(mLabel, locale));
         }
 
         @Override
diff --git a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java
index 90db75e..7a7edf0 100644
--- a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java
+++ b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java
@@ -99,10 +99,12 @@
         // Make sure getTypedWordInfoOrNull() returns non-null object.
         final SuggestedWords wordsWithTypedWord = new SuggestedWords(
                 list, null /* rawSuggestions */,
+                TYPED_WORD,
                 false /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isObsoleteSuggestions */,
-                SuggestedWords.INPUT_STYLE_NONE);
+                SuggestedWords.INPUT_STYLE_NONE,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
         final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull();
         assertNotNull(typedWord);
         assertEquals(TYPED_WORD, typedWord.mWord);
@@ -111,10 +113,12 @@
         list.remove(0);
         final SuggestedWords wordsWithoutTypedWord = new SuggestedWords(
                 list, null /* rawSuggestions */,
+                null /* typedWord */,
                 false /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isObsoleteSuggestions */,
-                SuggestedWords.INPUT_STYLE_NONE);
+                SuggestedWords.INPUT_STYLE_NONE,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
         assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull());
 
         // Make sure getTypedWordInfoOrNull() returns null.
@@ -122,10 +126,12 @@
 
         final SuggestedWords emptySuggestedWords = new SuggestedWords(
                 new ArrayList<SuggestedWordInfo>(), null /* rawSuggestions */,
+                null /* typedWord */,
                 false /* typedWordValid */,
                 false /* willAutoCorrect */,
                 false /* isObsoleteSuggestions */,
-                SuggestedWords.INPUT_STYLE_NONE);
+                SuggestedWords.INPUT_STYLE_NONE,
+                SuggestedWords.NOT_A_SEQUENCE_NUMBER);
         assertNull(emptySuggestedWords.getTypedWordInfoOrNull());
 
         assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
diff --git a/tests/src/com/android/inputmethod/latin/common/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/common/StringUtilsTests.java
new file mode 100644
index 0000000..15e310e
--- /dev/null
+++ b/tests/src/com/android/inputmethod/latin/common/StringUtilsTests.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright (C) 2014 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.common;
+
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.inputmethod.latin.common.StringUtils;
+
+import java.util.Locale;
+
+@SmallTest
+public class StringUtilsTests extends AndroidTestCase {
+    private static final Locale US = Locale.US;
+    private static final Locale GERMAN = Locale.GERMAN;
+    private static final Locale TURKEY = new Locale("tr", "TR");
+    private static final Locale GREECE = new Locale("el", "GR");
+
+    private static void assert_toTitleCaseOfKeyLabel(final Locale locale,
+            final String lowerCase, final String expected) {
+        assertEquals(lowerCase + " in " + locale, expected,
+                StringUtils.toTitleCaseOfKeyLabel(lowerCase, locale));
+    }
+
+    public void test_toTitleCaseOfKeyLabel() {
+        assert_toTitleCaseOfKeyLabel(US, null, null);
+        assert_toTitleCaseOfKeyLabel(US, "", "");
+        assert_toTitleCaseOfKeyLabel(US, "aeiou", "AEIOU");
+        // U+00E0: "à" LATIN SMALL LETTER A WITH GRAVE
+        // U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE
+        // U+00EE: "î" LATIN SMALL LETTER I WITH CIRCUMFLEX
+        // U+00F6: "ö" LATIN SMALL LETTER O WITH DIAERESIS
+        // U+016B: "ū" LATIN SMALL LETTER U WITH MACRON
+        // U+00F1: "ñ" LATIN SMALL LETTER N WITH TILDE
+        // U+00E7: "ç" LATIN SMALL LETTER C WITH CEDILLA
+        // U+00C0: "À" LATIN CAPITAL LETTER A WITH GRAVE
+        // U+00C8: "È" LATIN CAPITAL LETTER E WITH GRAVE
+        // U+00CE: "Î" LATIN CAPITAL LETTER I WITH CIRCUMFLEX
+        // U+00D6: "Ö" LATIN CAPITAL LETTER O WITH DIAERESIS
+        // U+016A: "Ū" LATIN CAPITAL LETTER U WITH MACRON
+        // U+00D1: "Ñ" LATIN CAPITAL LETTER N WITH TILDE
+        // U+00C7: "Ç" LATIN CAPITAL LETTER C WITH CEDILLA
+        assert_toTitleCaseOfKeyLabel(US,
+                "\u00E0\u00E8\u00EE\u00F6\u016B\u00F1\u00E7",
+                "\u00C0\u00C8\u00CE\u00D6\u016A\u00D1\u00C7");
+        // U+00DF: "ß" LATIN SMALL LETTER SHARP S
+        // U+015B: "ś" LATIN SMALL LETTER S WITH ACUTE
+        // U+0161: "š" LATIN SMALL LETTER S WITH CARON
+        // U+015A: "Ś" LATIN CAPITAL LETTER S WITH ACUTE
+        // U+0160: "Š" LATIN CAPITAL LETTER S WITH CARONZ
+        assert_toTitleCaseOfKeyLabel(GERMAN,
+                "\u00DF\u015B\u0161",
+                "SS\u015A\u0160");
+        // U+0259: "ə" LATIN SMALL LETTER SCHWA
+        // U+0069: "i" LATIN SMALL LETTER I
+        // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
+        // U+018F: "Ə" LATIN SMALL LETTER SCHWA
+        // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
+        // U+0049: "I" LATIN SMALL LETTER I
+        assert_toTitleCaseOfKeyLabel(TURKEY,
+                "\u0259\u0069\u0131",
+                "\u018F\u0130\u0049");
+        // U+03C3: "σ" GREEK SMALL LETTER SIGMA
+        // U+03C2: "ς" GREEK SMALL LETTER FINAL SIGMA
+        // U+03A3: "Σ" GREEK CAPITAL LETTER SIGMA
+        assert_toTitleCaseOfKeyLabel(GREECE,
+                "\u03C3\u03C2",
+                "\u03A3\u03A3");
+        // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
+        // U+03AD: "έ" GREEK SMALL LETTER EPSILON WITH TONOS
+        // U+03AE: "ή" GREEK SMALL LETTER ETA WITH TONOS
+        // U+03AF: "ί" GREEK SMALL LETTER IOTA WITH TONOS
+        // U+03CC: "ό" GREEK SMALL LETTER OMICRON WITH TONOS
+        // U+03CD: "ύ" GREEK SMALL LETTER UPSILON WITH TONOS
+        // U+03CE: "ώ" GREEK SMALL LETTER OMEGA WITH TONOS
+        // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
+        // U+0388: "Έ" GREEK CAPITAL LETTER EPSILON WITH TONOS
+        // U+0389: "Ή" GREEK CAPITAL LETTER ETA WITH TONOS
+        // U+038A: "Ί" GREEK CAPITAL LETTER IOTA WITH TONOS
+        // U+038C: "Ό" GREEK CAPITAL LETTER OMICRON WITH TONOS
+        // U+038E: "Ύ" GREEK CAPITAL LETTER UPSILON WITH TONOS
+        // U+038F: "Ώ" GREEK CAPITAL LETTER OMEGA WITH TONOS
+        assert_toTitleCaseOfKeyLabel(GREECE,
+                "\u03AC\u03AD\u03AE\u03AF\u03CC\u03CD\u03CE",
+                "\u0386\u0388\u0389\u038A\u038C\u038E\u038F");
+        // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
+        // U+03CB: "ϋ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA
+        // U+0390: "ΐ" GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS
+        // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
+        // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
+        // U+03AB: "Ϋ" GREEK CAPITAL LETTER UPSILON WITH DIALYTIKA
+        // U+0399: "Ι" GREEK CAPITAL LETTER IOTA
+        // U+03A5: "Υ" GREEK CAPITAL LETTER UPSILON
+        // U+0308: COMBINING DIAERESIS
+        // U+0301: COMBINING GRAVE ACCENT
+        assert_toTitleCaseOfKeyLabel(GREECE,
+                "\u03CA\u03CB\u0390\u03B0",
+                "\u03AA\u03AB\u0399\u0308\u0301\u03A5\u0308\u0301");
+    }
+
+    private static void assert_toTitleCaseOfKeyCode(final Locale locale, final int lowerCase,
+            final int expected) {
+        assertEquals(lowerCase + " in " + locale, expected,
+                StringUtils.toTitleCaseOfKeyCode(lowerCase, locale));
+    }
+
+    public void test_toTitleCaseOfKeyCode() {
+        assert_toTitleCaseOfKeyCode(US, Constants.CODE_ENTER, Constants.CODE_ENTER);
+        assert_toTitleCaseOfKeyCode(US, Constants.CODE_SPACE, Constants.CODE_SPACE);
+        assert_toTitleCaseOfKeyCode(US, Constants.CODE_COMMA, Constants.CODE_COMMA);
+        // U+0069: "i" LATIN SMALL LETTER I
+        // U+0131: "ı" LATIN SMALL LETTER DOTLESS I
+        // U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
+        // U+0049: "I" LATIN SMALL LETTER I
+        assert_toTitleCaseOfKeyCode(US, 0x0069, 0x0049); // i -> I
+        assert_toTitleCaseOfKeyCode(US, 0x0131, 0x0049); // ı -> I
+        assert_toTitleCaseOfKeyCode(TURKEY, 0x0069, 0x0130); // i -> İ
+        assert_toTitleCaseOfKeyCode(TURKEY, 0x0131, 0x0049); // ı -> I
+        // U+00DF: "ß" LATIN SMALL LETTER SHARP S
+        // The title case of "ß" is "SS".
+        assert_toTitleCaseOfKeyCode(US, 0x00DF, Constants.CODE_UNSPECIFIED);
+        // U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
+        // U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
+        assert_toTitleCaseOfKeyCode(GREECE, 0x03AC, 0x0386);
+        // U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
+        // U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
+        assert_toTitleCaseOfKeyCode(GREECE, 0x03CA, 0x03AA);
+        // U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
+        // The title case of "ΰ" is "\u03A5\u0308\u0301".
+        assert_toTitleCaseOfKeyCode(GREECE, 0x03B0, Constants.CODE_UNSPECIFIED);
+    }
+
+    private static void assert_capitalizeFirstCodePoint(final Locale locale, final String text,
+            final String expected) {
+        assertEquals(text + " in " + locale, expected,
+                StringUtils.capitalizeFirstCodePoint(text, locale));
+    }
+
+    public void test_capitalizeFirstCodePoint() {
+        assert_capitalizeFirstCodePoint(US, "", "");
+        assert_capitalizeFirstCodePoint(US, "a", "A");
+        assert_capitalizeFirstCodePoint(US, "à", "À");
+        assert_capitalizeFirstCodePoint(US, "ß", "SS");
+        assert_capitalizeFirstCodePoint(US, "text", "Text");
+        assert_capitalizeFirstCodePoint(US, "iGoogle", "IGoogle");
+        assert_capitalizeFirstCodePoint(TURKEY, "iyi", "İyi");
+        assert_capitalizeFirstCodePoint(TURKEY, "ısırdı", "Isırdı");
+        assert_capitalizeFirstCodePoint(GREECE, "ά", "Ά");
+        assert_capitalizeFirstCodePoint(GREECE, "άνεση", "Άνεση");
+    }
+
+    private static void assert_capitalizeFirstAndDowncaseRest(final Locale locale,
+            final String text, final String expected) {
+        assertEquals(text + " in " + locale, expected,
+                StringUtils.capitalizeFirstAndDowncaseRest(text, locale));
+    }
+
+    public void test_capitalizeFirstAndDowncaseRest() {
+        assert_capitalizeFirstAndDowncaseRest(US, "", "");
+        assert_capitalizeFirstAndDowncaseRest(US, "a", "A");
+        assert_capitalizeFirstAndDowncaseRest(US, "à", "À");
+        assert_capitalizeFirstAndDowncaseRest(US, "ß", "SS");
+        assert_capitalizeFirstAndDowncaseRest(US, "text", "Text");
+        assert_capitalizeFirstAndDowncaseRest(US, "iGoogle", "Igoogle");
+        assert_capitalizeFirstAndDowncaseRest(US, "invite", "Invite");
+        assert_capitalizeFirstAndDowncaseRest(US, "INVITE", "Invite");
+        assert_capitalizeFirstAndDowncaseRest(TURKEY, "iyi", "İyi");
+        assert_capitalizeFirstAndDowncaseRest(TURKEY, "İYİ", "İyi");
+        assert_capitalizeFirstAndDowncaseRest(TURKEY, "ısırdı", "Isırdı");
+        assert_capitalizeFirstAndDowncaseRest(TURKEY, "ISIRDI", "Isırdı");
+        assert_capitalizeFirstAndDowncaseRest(GREECE, "ά", "Ά");
+        assert_capitalizeFirstAndDowncaseRest(GREECE, "άνεση", "Άνεση");
+        assert_capitalizeFirstAndDowncaseRest(GREECE, "ΆΝΕΣΗ", "Άνεση");
+    }
+}