1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.inputmethod.latin; 18 19 import android.test.AndroidTestCase; 20 import android.test.suitebuilder.annotation.SmallTest; 21 22 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; 23 24 import java.util.ArrayList; 25 import java.util.Locale; 26 27 @SmallTest 28 public class SuggestedWordsTests extends AndroidTestCase { 29 30 /** 31 * Helper method to create a dummy {@link SuggestedWordInfo} with specifying 32 * {@link SuggestedWordInfo#KIND_TYPED}. 33 * 34 * @param word the word to be used to create {@link SuggestedWordInfo}. 35 * @return a new instance of {@link SuggestedWordInfo}. 36 */ createTypedWordInfo(final String word)37 private static SuggestedWordInfo createTypedWordInfo(final String word) { 38 // Use 100 as the frequency because the numerical value does not matter as 39 // long as it's > 1 and < INT_MAX. 40 return new SuggestedWordInfo(word, 100 /* score */, 41 SuggestedWordInfo.KIND_TYPED, 42 null /* sourceDict */, 43 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 44 1 /* autoCommitFirstWordConfidence */); 45 } 46 47 /** 48 * Helper method to create a dummy {@link SuggestedWordInfo} with specifying 49 * {@link SuggestedWordInfo#KIND_CORRECTION}. 50 * 51 * @param word the word to be used to create {@link SuggestedWordInfo}. 52 * @return a new instance of {@link SuggestedWordInfo}. 53 */ createCorrectionWordInfo(final String word)54 private static SuggestedWordInfo createCorrectionWordInfo(final String word) { 55 return new SuggestedWordInfo(word, 1 /* score */, 56 SuggestedWordInfo.KIND_CORRECTION, 57 null /* sourceDict */, 58 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, 59 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */); 60 } 61 testGetSuggestedWordsExcludingTypedWord()62 public void testGetSuggestedWordsExcludingTypedWord() { 63 final String TYPED_WORD = "typed"; 64 final int NUMBER_OF_ADDED_SUGGESTIONS = 5; 65 final int KIND_OF_SECOND_CORRECTION = SuggestedWordInfo.KIND_CORRECTION; 66 final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); 67 list.add(createTypedWordInfo(TYPED_WORD)); 68 for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { 69 list.add(createCorrectionWordInfo(Integer.toString(i))); 70 } 71 72 final SuggestedWords words = new SuggestedWords( 73 list, null /* rawSuggestions */, 74 false /* typedWordValid */, 75 false /* willAutoCorrect */, 76 false /* isObsoleteSuggestions */, 77 SuggestedWords.INPUT_STYLE_NONE); 78 assertEquals(NUMBER_OF_ADDED_SUGGESTIONS + 1, words.size()); 79 assertEquals("typed", words.getWord(0)); 80 assertTrue(words.getInfo(0).isKindOf(SuggestedWordInfo.KIND_TYPED)); 81 assertEquals("0", words.getWord(1)); 82 assertTrue(words.getInfo(1).isKindOf(KIND_OF_SECOND_CORRECTION)); 83 assertEquals("4", words.getWord(5)); 84 assertTrue(words.getInfo(5).isKindOf(KIND_OF_SECOND_CORRECTION)); 85 86 final SuggestedWords wordsWithoutTyped = 87 words.getSuggestedWordsExcludingTypedWordForRecorrection(); 88 // Make sure that the typed word has indeed been excluded, by testing the size of the 89 // suggested words, the string and the kind of the top suggestion, which should match 90 // the string and kind of what we inserted after the typed word. 91 assertEquals(words.size() - 1, wordsWithoutTyped.size()); 92 assertEquals("0", wordsWithoutTyped.getWord(0)); 93 assertTrue(wordsWithoutTyped.getInfo(0).isKindOf(KIND_OF_SECOND_CORRECTION)); 94 } 95 96 // Helper for testGetTransformedWordInfo transformWordInfo(final String info, final int trailingSingleQuotesCount)97 private SuggestedWordInfo transformWordInfo(final String info, 98 final int trailingSingleQuotesCount) { 99 final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info); 100 final SuggestedWordInfo returnedWordInfo = 101 Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo, 102 Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */, 103 trailingSingleQuotesCount); 104 assertEquals(suggestedWordInfo.mAutoCommitFirstWordConfidence, 105 returnedWordInfo.mAutoCommitFirstWordConfidence); 106 return returnedWordInfo; 107 } 108 testGetTransformedSuggestedWordInfo()109 public void testGetTransformedSuggestedWordInfo() { 110 SuggestedWordInfo result = transformWordInfo("word", 0); 111 assertEquals(result.mWord, "word"); 112 result = transformWordInfo("word", 1); 113 assertEquals(result.mWord, "word'"); 114 result = transformWordInfo("word", 3); 115 assertEquals(result.mWord, "word'''"); 116 result = transformWordInfo("didn't", 0); 117 assertEquals(result.mWord, "didn't"); 118 result = transformWordInfo("didn't", 1); 119 assertEquals(result.mWord, "didn't"); 120 result = transformWordInfo("didn't", 3); 121 assertEquals(result.mWord, "didn't''"); 122 } 123 testGetTypedWordInfoOrNull()124 public void testGetTypedWordInfoOrNull() { 125 final String TYPED_WORD = "typed"; 126 final int NUMBER_OF_ADDED_SUGGESTIONS = 5; 127 final ArrayList<SuggestedWordInfo> list = new ArrayList<>(); 128 list.add(createTypedWordInfo(TYPED_WORD)); 129 for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) { 130 list.add(createCorrectionWordInfo(Integer.toString(i))); 131 } 132 133 // Make sure getTypedWordInfoOrNull() returns non-null object. 134 final SuggestedWords wordsWithTypedWord = new SuggestedWords( 135 list, null /* rawSuggestions */, 136 false /* typedWordValid */, 137 false /* willAutoCorrect */, 138 false /* isObsoleteSuggestions */, 139 SuggestedWords.INPUT_STYLE_NONE); 140 final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull(); 141 assertNotNull(typedWord); 142 assertEquals(TYPED_WORD, typedWord.mWord); 143 144 // Make sure getTypedWordInfoOrNull() returns null. 145 final SuggestedWords wordsWithoutTypedWord = 146 wordsWithTypedWord.getSuggestedWordsExcludingTypedWordForRecorrection(); 147 assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull()); 148 149 // Make sure getTypedWordInfoOrNull() returns null. 150 assertNull(SuggestedWords.EMPTY.getTypedWordInfoOrNull()); 151 152 final SuggestedWords emptySuggestedWords = new SuggestedWords( 153 new ArrayList<SuggestedWordInfo>(), null /* rawSuggestions */, 154 false /* typedWordValid */, 155 false /* willAutoCorrect */, 156 false /* isObsoleteSuggestions */, 157 SuggestedWords.INPUT_STYLE_NONE); 158 assertNull(emptySuggestedWords.getTypedWordInfoOrNull()); 159 160 assertNull(SuggestedWords.EMPTY.getTypedWordInfoOrNull()); 161 } 162 } 163