diff options
author | 2014-08-24 14:37:24 -0700 | |
---|---|---|
committer | 2014-08-24 16:32:17 -0700 | |
commit | cd6ef9a8b117b82623251510fac72ee108ec1a2a (patch) | |
tree | ce8db7f2eee374f02176ea084e78c50d980447e8 /tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java | |
parent | 2637be27c51be03e39b0db1c66312c4cc55bc7de (diff) | |
download | latinime-cd6ef9a8b117b82623251510fac72ee108ec1a2a.tar.gz latinime-cd6ef9a8b117b82623251510fac72ee108ec1a2a.tar.xz latinime-cd6ef9a8b117b82623251510fac72ee108ec1a2a.zip |
Indroduce SuggestedWords#getTypedWordInfoOrNull
This is a groundwork for subsequent CLs whith need to
do certain things for and only for the typed word.
Change-Id: I374b79cbc79470f57f523c996010301ea5701a61
Diffstat (limited to 'tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java')
-rw-r--r-- | tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java index a5f20b565..2785dec43 100644 --- a/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java +++ b/tests/src/com/android/inputmethod/latin/SuggestedWordsTests.java @@ -23,24 +23,49 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; import java.util.Locale; -import java.util.Random; @SmallTest 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() { final String TYPED_WORD = "typed"; - final int TYPED_WORD_FREQ = 5; final int NUMBER_OF_ADDED_SUGGESTIONS = 5; final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); - list.add(new SuggestedWordInfo(TYPED_WORD, TYPED_WORD_FREQ, - SuggestedWordInfo.KIND_TYPED, null /* sourceDict */, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); + list.add(createTypedWordInfo(TYPED_WORD)); for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { - list.add(new SuggestedWordInfo("" + i, 1, SuggestedWordInfo.KIND_CORRECTION, - null /* sourceDict */, - SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, - SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); + list.add(createCorrectionWordInfo(Integer.toString(i))); } final SuggestedWords words = new SuggestedWords( @@ -66,19 +91,9 @@ public class SuggestedWordsTests extends AndroidTestCase { } // 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 private SuggestedWordInfo transformWordInfo(final String info, final int trailingSingleQuotesCount) { - final SuggestedWordInfo suggestedWordInfo = createWordInfo(info); + final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info); final SuggestedWordInfo returnedWordInfo = Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */, @@ -102,4 +117,35 @@ public class SuggestedWordsTests extends AndroidTestCase { result = transformWordInfo("didn't", 3); 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()); + } } |