Loading java/src/com/android/inputmethod/latin/SuggestedWords.java +15 −0 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,7 @@ package com.android.inputmethod.latin; import android.text.TextUtils; import android.text.TextUtils; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CompletionInfo; import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.StringUtils; Loading Loading @@ -420,4 +421,18 @@ public class SuggestedWords { mWillAutoCorrect, mIsObsoleteSuggestions, mIsPrediction, mWillAutoCorrect, mIsObsoleteSuggestions, mIsPrediction, INPUT_STYLE_TAIL_BATCH); INPUT_STYLE_TAIL_BATCH); } } /** * @return the {@link SuggestedWordInfo} which corresponds to the word that is originally * typed by the user. Otherwise returns {@code null}. Note that gesture input is not * considered to be a typed word. */ @UsedForTesting public SuggestedWordInfo getTypedWordInfoOrNull() { if (this == EMPTY) { return null; } final SuggestedWordInfo info = getInfo(SuggestedWords.INDEX_OF_TYPED_WORD); return (info.getKind() == SuggestedWordInfo.KIND_TYPED) ? info : null; } } } tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java +67 −21 Original line number Original line Diff line number Diff line Loading @@ -23,24 +23,49 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; import java.util.ArrayList; import java.util.Locale; import java.util.Locale; import java.util.Random; @SmallTest @SmallTest public class SuggestedWordsTests extends AndroidTestCase { public class SuggestedWordsTests extends AndroidTestCase { /** * Helper method to create a dummy {@link SuggestedWordInfo} with specifying * {@link SuggestedWordInfo#KIND_TYPED}. * * @param word the word to be used to create {@link SuggestedWordInfo}. * @return a new instance of {@link SuggestedWordInfo}. */ private static SuggestedWordInfo createTypedWordInfo(final String word) { // Use 100 as the frequency because the numerical value does not matter as // long as it's > 1 and < INT_MAX. return new SuggestedWordInfo(word, 100 /* score */, SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 1 /* autoCommitFirstWordConfidence */); } /** * Helper method to create a dummy {@link SuggestedWordInfo} with specifying * {@link SuggestedWordInfo#KIND_CORRECTION}. * * @param word the word to be used to create {@link SuggestedWordInfo}. * @return a new instance of {@link SuggestedWordInfo}. */ private static SuggestedWordInfo createCorrectionWordInfo(final String word) { return new SuggestedWordInfo(word, 1 /* score */, SuggestedWordInfo.KIND_CORRECTION, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); } public void testGetSuggestedWordsExcludingTypedWord() { public void testGetSuggestedWordsExcludingTypedWord() { final String TYPED_WORD = "typed"; final String TYPED_WORD = "typed"; final int TYPED_WORD_FREQ = 5; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); list.add(new SuggestedWordInfo(TYPED_WORD, TYPED_WORD_FREQ, list.add(createTypedWordInfo(TYPED_WORD)); SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { list.add(new SuggestedWordInfo("" + i, 1, SuggestedWordInfo.KIND_CORRECTION, list.add(createCorrectionWordInfo(Integer.toString(i))); null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); } } final SuggestedWords words = new SuggestedWords( final SuggestedWords words = new SuggestedWords( Loading @@ -65,20 +90,10 @@ public class SuggestedWordsTests extends AndroidTestCase { assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(SuggestedWordInfo.KIND_CORRECTION)); assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(SuggestedWordInfo.KIND_CORRECTION)); } } // Helper for testGetTransformedWordInfo private SuggestedWordInfo createWordInfo(final String s) { // Use 100 as the frequency because the numerical value does not matter as // long as it's > 1 and < INT_MAX. return new SuggestedWordInfo(s, 100, SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, new Random().nextInt(1000000) /* autoCommitFirstWordConfidence */); } // Helper for testGetTransformedWordInfo // Helper for testGetTransformedWordInfo private SuggestedWordInfo transformWordInfo(final String info, private SuggestedWordInfo transformWordInfo(final String info, final int trailingSingleQuotesCount) { final int trailingSingleQuotesCount) { final SuggestedWordInfo suggestedWordInfo = createWordInfo(info); final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info); final SuggestedWordInfo returnedWordInfo = final SuggestedWordInfo returnedWordInfo = Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */, Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */, Loading @@ -102,4 +117,35 @@ public class SuggestedWordsTests extends AndroidTestCase { result = transformWordInfo("didn't", 3); result = transformWordInfo("didn't", 3); assertEquals(result.mWord, "didn't''"); assertEquals(result.mWord, "didn't''"); } } public void testGetTypedWordInfoOrNull() { final String TYPED_WORD = "typed"; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); list.add(createTypedWordInfo(TYPED_WORD)); for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { list.add(createCorrectionWordInfo(Integer.toString(i))); } // Make sure getTypedWordInfoOrNull() returns non-null object. final SuggestedWords wordsWithTypedWord = new SuggestedWords( list, null /* rawSuggestions */, false /* typedWordValid */, false /* willAutoCorrect */, false /* isObsoleteSuggestions */, false /* isPrediction*/, SuggestedWords.INPUT_STYLE_NONE); final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull(); assertNotNull(typedWord); assertEquals(TYPED_WORD, typedWord.mWord); // Make sure getTypedWordInfoOrNull() returns null. final SuggestedWords wordsWithoutTypedWord = wordsWithTypedWord.getSuggestedWordsExcludingTypedWord( SuggestedWords.INPUT_STYLE_NONE); assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull()); // Make sure getTypedWordInfoOrNull() returns null. assertNull(SuggestedWords.EMPTY.getTypedWordInfoOrNull()); } } } Loading
java/src/com/android/inputmethod/latin/SuggestedWords.java +15 −0 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,7 @@ package com.android.inputmethod.latin; import android.text.TextUtils; import android.text.TextUtils; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CompletionInfo; import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.define.DebugFlags; import com.android.inputmethod.latin.utils.StringUtils; import com.android.inputmethod.latin.utils.StringUtils; Loading Loading @@ -420,4 +421,18 @@ public class SuggestedWords { mWillAutoCorrect, mIsObsoleteSuggestions, mIsPrediction, mWillAutoCorrect, mIsObsoleteSuggestions, mIsPrediction, INPUT_STYLE_TAIL_BATCH); INPUT_STYLE_TAIL_BATCH); } } /** * @return the {@link SuggestedWordInfo} which corresponds to the word that is originally * typed by the user. Otherwise returns {@code null}. Note that gesture input is not * considered to be a typed word. */ @UsedForTesting public SuggestedWordInfo getTypedWordInfoOrNull() { if (this == EMPTY) { return null; } final SuggestedWordInfo info = getInfo(SuggestedWords.INDEX_OF_TYPED_WORD); return (info.getKind() == SuggestedWordInfo.KIND_TYPED) ? info : null; } } }
tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java +67 −21 Original line number Original line Diff line number Diff line Loading @@ -23,24 +23,49 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; import java.util.ArrayList; import java.util.Locale; import java.util.Locale; import java.util.Random; @SmallTest @SmallTest public class SuggestedWordsTests extends AndroidTestCase { public class SuggestedWordsTests extends AndroidTestCase { /** * Helper method to create a dummy {@link SuggestedWordInfo} with specifying * {@link SuggestedWordInfo#KIND_TYPED}. * * @param word the word to be used to create {@link SuggestedWordInfo}. * @return a new instance of {@link SuggestedWordInfo}. */ private static SuggestedWordInfo createTypedWordInfo(final String word) { // Use 100 as the frequency because the numerical value does not matter as // long as it's > 1 and < INT_MAX. return new SuggestedWordInfo(word, 100 /* score */, SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 1 /* autoCommitFirstWordConfidence */); } /** * Helper method to create a dummy {@link SuggestedWordInfo} with specifying * {@link SuggestedWordInfo#KIND_CORRECTION}. * * @param word the word to be used to create {@link SuggestedWordInfo}. * @return a new instance of {@link SuggestedWordInfo}. */ private static SuggestedWordInfo createCorrectionWordInfo(final String word) { return new SuggestedWordInfo(word, 1 /* score */, SuggestedWordInfo.KIND_CORRECTION, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); } public void testGetSuggestedWordsExcludingTypedWord() { public void testGetSuggestedWordsExcludingTypedWord() { final String TYPED_WORD = "typed"; final String TYPED_WORD = "typed"; final int TYPED_WORD_FREQ = 5; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); list.add(new SuggestedWordInfo(TYPED_WORD, TYPED_WORD_FREQ, list.add(createTypedWordInfo(TYPED_WORD)); SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { list.add(new SuggestedWordInfo("" + i, 1, SuggestedWordInfo.KIND_CORRECTION, list.add(createCorrectionWordInfo(Integer.toString(i))); null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); } } final SuggestedWords words = new SuggestedWords( final SuggestedWords words = new SuggestedWords( Loading @@ -65,20 +90,10 @@ public class SuggestedWordsTests extends AndroidTestCase { assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(SuggestedWordInfo.KIND_CORRECTION)); assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(SuggestedWordInfo.KIND_CORRECTION)); } } // Helper for testGetTransformedWordInfo private SuggestedWordInfo createWordInfo(final String s) { // Use 100 as the frequency because the numerical value does not matter as // long as it's > 1 and < INT_MAX. return new SuggestedWordInfo(s, 100, SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, new Random().nextInt(1000000) /* autoCommitFirstWordConfidence */); } // Helper for testGetTransformedWordInfo // Helper for testGetTransformedWordInfo private SuggestedWordInfo transformWordInfo(final String info, private SuggestedWordInfo transformWordInfo(final String info, final int trailingSingleQuotesCount) { final int trailingSingleQuotesCount) { final SuggestedWordInfo suggestedWordInfo = createWordInfo(info); final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info); final SuggestedWordInfo returnedWordInfo = final SuggestedWordInfo returnedWordInfo = Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */, Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */, Loading @@ -102,4 +117,35 @@ public class SuggestedWordsTests extends AndroidTestCase { result = transformWordInfo("didn't", 3); result = transformWordInfo("didn't", 3); assertEquals(result.mWord, "didn't''"); assertEquals(result.mWord, "didn't''"); } } public void testGetTypedWordInfoOrNull() { final String TYPED_WORD = "typed"; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); list.add(createTypedWordInfo(TYPED_WORD)); for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { list.add(createCorrectionWordInfo(Integer.toString(i))); } // Make sure getTypedWordInfoOrNull() returns non-null object. final SuggestedWords wordsWithTypedWord = new SuggestedWords( list, null /* rawSuggestions */, false /* typedWordValid */, false /* willAutoCorrect */, false /* isObsoleteSuggestions */, false /* isPrediction*/, SuggestedWords.INPUT_STYLE_NONE); final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull(); assertNotNull(typedWord); assertEquals(TYPED_WORD, typedWord.mWord); // Make sure getTypedWordInfoOrNull() returns null. final SuggestedWords wordsWithoutTypedWord = wordsWithTypedWord.getSuggestedWordsExcludingTypedWord( SuggestedWords.INPUT_STYLE_NONE); assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull()); // Make sure getTypedWordInfoOrNull() returns null. assertNull(SuggestedWords.EMPTY.getTypedWordInfoOrNull()); } } }