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, "" /* prevWordsContext */, 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, "" /* prevWordsContext */, 1 /* score */,
56                 SuggestedWordInfo.KIND_CORRECTION,
57                 null /* sourceDict */,
58                 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
59                 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */);
60     }
61 
createCorrectionWordInfos(final String... words)62     private static ArrayList<SuggestedWordInfo> createCorrectionWordInfos(final String... words) {
63         final ArrayList<SuggestedWordInfo> infos = new ArrayList<>();
64         for (final String word : words) {
65             infos.add(createCorrectionWordInfo(word));
66         }
67         return infos;
68     }
69 
70     // Helper for testGetTransformedWordInfo
transformWordInfo(final String info, final int trailingSingleQuotesCount)71     private static SuggestedWordInfo transformWordInfo(final String info,
72             final int trailingSingleQuotesCount) {
73         final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info);
74         final SuggestedWordInfo returnedWordInfo =
75                 Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo,
76                 Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */,
77                 trailingSingleQuotesCount);
78         assertEquals(suggestedWordInfo.mAutoCommitFirstWordConfidence,
79                 returnedWordInfo.mAutoCommitFirstWordConfidence);
80         return returnedWordInfo;
81     }
82 
testRemoveDupesNoDupes()83     public void testRemoveDupesNoDupes() {
84         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "c");
85         assertEquals(-1, SuggestedWordInfo.removeDups("b", infos));
86         assertEquals(2, infos.size());
87     }
88 
testRemoveDupesTypedWordNotDupe()89     public void testRemoveDupesTypedWordNotDupe() {
90         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "a", "c");
91         assertEquals(-1, SuggestedWordInfo.removeDups("b", infos));
92         assertEquals(2, infos.size());
93     }
94 
testRemoveDupesTypedWordOnlyDupe()95     public void testRemoveDupesTypedWordOnlyDupe() {
96         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "b", "c");
97         assertEquals(1, SuggestedWordInfo.removeDups("b", infos));
98         assertEquals(2, infos.size());
99     }
100 
testRemoveDupesTypedWordNotOnlyDupe()101     public void testRemoveDupesTypedWordNotOnlyDupe() {
102         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "b", "b", "c");
103         assertEquals(1, SuggestedWordInfo.removeDups("b", infos));
104         assertEquals(2, infos.size());
105     }
106 
testGetTransformedSuggestedWordInfo()107     public void testGetTransformedSuggestedWordInfo() {
108         SuggestedWordInfo result = transformWordInfo("word", 0);
109         assertEquals(result.mWord, "word");
110         result = transformWordInfo("word", 1);
111         assertEquals(result.mWord, "word'");
112         result = transformWordInfo("word", 3);
113         assertEquals(result.mWord, "word'''");
114         result = transformWordInfo("didn't", 0);
115         assertEquals(result.mWord, "didn't");
116         result = transformWordInfo("didn't", 1);
117         assertEquals(result.mWord, "didn't");
118         result = transformWordInfo("didn't", 3);
119         assertEquals(result.mWord, "didn't''");
120     }
121 
testGetTypedWordInfoOrNull()122     public void testGetTypedWordInfoOrNull() {
123         final String TYPED_WORD = "typed";
124         final SuggestedWordInfo TYPED_WORD_INFO = createTypedWordInfo(TYPED_WORD);
125         final int NUMBER_OF_ADDED_SUGGESTIONS = 5;
126         final ArrayList<SuggestedWordInfo> list = new ArrayList<>();
127         list.add(TYPED_WORD_INFO);
128         for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) {
129             list.add(createCorrectionWordInfo(Integer.toString(i)));
130         }
131 
132         // Make sure getTypedWordInfoOrNull() returns non-null object.
133         final SuggestedWords wordsWithTypedWord = new SuggestedWords(
134                 list, null /* rawSuggestions */,
135                 TYPED_WORD_INFO,
136                 false /* typedWordValid */,
137                 false /* willAutoCorrect */,
138                 false /* isObsoleteSuggestions */,
139                 SuggestedWords.INPUT_STYLE_NONE,
140                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
141         final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull();
142         assertNotNull(typedWord);
143         assertEquals(TYPED_WORD, typedWord.mWord);
144 
145         // Make sure getTypedWordInfoOrNull() returns null when no typed word.
146         list.remove(0);
147         final SuggestedWords wordsWithoutTypedWord = new SuggestedWords(
148                 list, null /* rawSuggestions */,
149                 null /* typedWord */,
150                 false /* typedWordValid */,
151                 false /* willAutoCorrect */,
152                 false /* isObsoleteSuggestions */,
153                 SuggestedWords.INPUT_STYLE_NONE,
154                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
155         assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull());
156 
157         // Make sure getTypedWordInfoOrNull() returns null.
158         assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
159 
160         final SuggestedWords emptySuggestedWords = new SuggestedWords(
161                 new ArrayList<SuggestedWordInfo>(), null /* rawSuggestions */,
162                 null /* typedWord */,
163                 false /* typedWordValid */,
164                 false /* willAutoCorrect */,
165                 false /* isObsoleteSuggestions */,
166                 SuggestedWords.INPUT_STYLE_NONE,
167                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
168         assertNull(emptySuggestedWords.getTypedWordInfoOrNull());
169 
170         assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
171     }
172 }
173