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 static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 
23 import androidx.test.filters.SmallTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.util.ArrayList;
32 import java.util.Locale;
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 public class SuggestedWordsTests {
37 
38     /**
39      * Helper method to create a placeholder {@link SuggestedWordInfo} with specifying
40      * {@link SuggestedWordInfo#KIND_TYPED}.
41      *
42      * @param word the word to be used to create {@link SuggestedWordInfo}.
43      * @return a new instance of {@link SuggestedWordInfo}.
44      */
createTypedWordInfo(final String word)45     private static SuggestedWordInfo createTypedWordInfo(final String word) {
46         // Use 100 as the frequency because the numerical value does not matter as
47         // long as it's > 1 and < INT_MAX.
48         return new SuggestedWordInfo(word, "" /* prevWordsContext */, 100 /* score */,
49                 SuggestedWordInfo.KIND_TYPED,
50                 null /* sourceDict */,
51                 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
52                 1 /* autoCommitFirstWordConfidence */);
53     }
54 
55     /**
56      * Helper method to create a placeholder {@link SuggestedWordInfo} with specifying
57      * {@link SuggestedWordInfo#KIND_CORRECTION}.
58      *
59      * @param word the word to be used to create {@link SuggestedWordInfo}.
60      * @return a new instance of {@link SuggestedWordInfo}.
61      */
createCorrectionWordInfo(final String word)62     private static SuggestedWordInfo createCorrectionWordInfo(final String word) {
63         return new SuggestedWordInfo(word, "" /* prevWordsContext */, 1 /* score */,
64                 SuggestedWordInfo.KIND_CORRECTION,
65                 null /* sourceDict */,
66                 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
67                 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */);
68     }
69 
createCorrectionWordInfos(final String... words)70     private static ArrayList<SuggestedWordInfo> createCorrectionWordInfos(final String... words) {
71         final ArrayList<SuggestedWordInfo> infos = new ArrayList<>();
72         for (final String word : words) {
73             infos.add(createCorrectionWordInfo(word));
74         }
75         return infos;
76     }
77 
78     // Helper for testGetTransformedWordInfo
transformWordInfo(final String info, final int trailingSingleQuotesCount)79     private static SuggestedWordInfo transformWordInfo(final String info,
80             final int trailingSingleQuotesCount) {
81         final SuggestedWordInfo suggestedWordInfo = createTypedWordInfo(info);
82         final SuggestedWordInfo returnedWordInfo =
83                 Suggest.getTransformedSuggestedWordInfo(suggestedWordInfo,
84                 Locale.ENGLISH, false /* isAllUpperCase */, false /* isFirstCharCapitalized */,
85                 trailingSingleQuotesCount);
86         assertEquals(suggestedWordInfo.mAutoCommitFirstWordConfidence,
87                 returnedWordInfo.mAutoCommitFirstWordConfidence);
88         return returnedWordInfo;
89     }
90 
91     @Test
testRemoveDupesNoDupes()92     public void testRemoveDupesNoDupes() {
93         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "c");
94         assertEquals(-1, SuggestedWordInfo.removeDups("b", infos));
95         assertEquals(2, infos.size());
96     }
97 
98     @Test
testRemoveDupesTypedWordNotDupe()99     public void testRemoveDupesTypedWordNotDupe() {
100         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "a", "c");
101         assertEquals(-1, SuggestedWordInfo.removeDups("b", infos));
102         assertEquals(2, infos.size());
103     }
104 
105     @Test
testRemoveDupesTypedWordOnlyDupe()106     public void testRemoveDupesTypedWordOnlyDupe() {
107         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "b", "c");
108         assertEquals(1, SuggestedWordInfo.removeDups("b", infos));
109         assertEquals(2, infos.size());
110     }
111 
112     @Test
testRemoveDupesTypedWordNotOnlyDupe()113     public void testRemoveDupesTypedWordNotOnlyDupe() {
114         final ArrayList<SuggestedWordInfo> infos = createCorrectionWordInfos("a", "b", "b", "c");
115         assertEquals(1, SuggestedWordInfo.removeDups("b", infos));
116         assertEquals(2, infos.size());
117     }
118 
119     @Test
testGetTransformedSuggestedWordInfo()120     public void testGetTransformedSuggestedWordInfo() {
121         SuggestedWordInfo result = transformWordInfo("word", 0);
122         assertEquals(result.mWord, "word");
123         result = transformWordInfo("word", 1);
124         assertEquals(result.mWord, "word'");
125         result = transformWordInfo("word", 3);
126         assertEquals(result.mWord, "word'''");
127         result = transformWordInfo("didn't", 0);
128         assertEquals(result.mWord, "didn't");
129         result = transformWordInfo("didn't", 1);
130         assertEquals(result.mWord, "didn't");
131         result = transformWordInfo("didn't", 3);
132         assertEquals(result.mWord, "didn't''");
133     }
134 
135     @Test
testGetTypedWordInfoOrNull()136     public void testGetTypedWordInfoOrNull() {
137         final String TYPED_WORD = "typed";
138         final SuggestedWordInfo TYPED_WORD_INFO = createTypedWordInfo(TYPED_WORD);
139         final int NUMBER_OF_ADDED_SUGGESTIONS = 5;
140         final ArrayList<SuggestedWordInfo> list = new ArrayList<>();
141         list.add(TYPED_WORD_INFO);
142         for (int i = 0; i < NUMBER_OF_ADDED_SUGGESTIONS; ++i) {
143             list.add(createCorrectionWordInfo(Integer.toString(i)));
144         }
145 
146         // Make sure getTypedWordInfoOrNull() returns non-null object.
147         final SuggestedWords wordsWithTypedWord = new SuggestedWords(
148                 list, null /* rawSuggestions */,
149                 TYPED_WORD_INFO,
150                 false /* typedWordValid */,
151                 false /* willAutoCorrect */,
152                 false /* isObsoleteSuggestions */,
153                 SuggestedWords.INPUT_STYLE_NONE,
154                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
155         final SuggestedWordInfo typedWord = wordsWithTypedWord.getTypedWordInfoOrNull();
156         assertNotNull(typedWord);
157         assertEquals(TYPED_WORD, typedWord.mWord);
158 
159         // Make sure getTypedWordInfoOrNull() returns null when no typed word.
160         list.remove(0);
161         final SuggestedWords wordsWithoutTypedWord = new SuggestedWords(
162                 list, null /* rawSuggestions */,
163                 null /* typedWord */,
164                 false /* typedWordValid */,
165                 false /* willAutoCorrect */,
166                 false /* isObsoleteSuggestions */,
167                 SuggestedWords.INPUT_STYLE_NONE,
168                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
169         assertNull(wordsWithoutTypedWord.getTypedWordInfoOrNull());
170 
171         // Make sure getTypedWordInfoOrNull() returns null.
172         assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
173 
174         final SuggestedWords emptySuggestedWords = new SuggestedWords(
175                 new ArrayList<SuggestedWordInfo>(), null /* rawSuggestions */,
176                 null /* typedWord */,
177                 false /* typedWordValid */,
178                 false /* willAutoCorrect */,
179                 false /* isObsoleteSuggestions */,
180                 SuggestedWords.INPUT_STYLE_NONE,
181                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
182         assertNull(emptySuggestedWords.getTypedWordInfoOrNull());
183 
184         assertNull(SuggestedWords.getEmptyInstance().getTypedWordInfoOrNull());
185     }
186 }
187