1 /*
2  * Copyright (C) 2014 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.suggestions;
18 
19 import static junit.framework.TestCase.assertEquals;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import com.android.inputmethod.latin.SuggestedWords;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @SmallTest
33 @RunWith(AndroidJUnit4.class)
34 public class SuggestionStripLayoutHelperTests {
confirmShowTypedWord(final String message, final int inputType)35     private static void confirmShowTypedWord(final String message, final int inputType) {
36         assertFalse(message, SuggestionStripLayoutHelper.shouldOmitTypedWord(
37                 inputType,
38                 false /* gestureFloatingPreviewTextEnabled */,
39                 false /* shouldShowUiToAcceptTypedWord */));
40         assertFalse(message, SuggestionStripLayoutHelper.shouldOmitTypedWord(
41                 inputType,
42                 true /* gestureFloatingPreviewTextEnabled */,
43                 false /* shouldShowUiToAcceptTypedWord */));
44         assertFalse(message, SuggestionStripLayoutHelper.shouldOmitTypedWord(
45                 inputType,
46                 false /* gestureFloatingPreviewTextEnabled */,
47                 true /* shouldShowUiToAcceptTypedWord */));
48         assertFalse(message, SuggestionStripLayoutHelper.shouldOmitTypedWord(
49                 inputType,
50                 true /* gestureFloatingPreviewTextEnabled */,
51                 true /* shouldShowUiToAcceptTypedWord */));
52     }
53 
54     @Test
testShouldShowTypedWord()55     public void testShouldShowTypedWord() {
56         confirmShowTypedWord("no input style",
57                 SuggestedWords.INPUT_STYLE_NONE);
58         confirmShowTypedWord("application specifed",
59                 SuggestedWords.INPUT_STYLE_APPLICATION_SPECIFIED);
60         confirmShowTypedWord("recorrection",
61                 SuggestedWords.INPUT_STYLE_RECORRECTION);
62     }
63 
64     @Test
testShouldOmitTypedWordWhileTyping()65     public void testShouldOmitTypedWordWhileTyping() {
66         assertFalse("typing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
67                 SuggestedWords.INPUT_STYLE_TYPING,
68                 false /* gestureFloatingPreviewTextEnabled */,
69                 false /* shouldShowUiToAcceptTypedWord */));
70         assertFalse("typing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
71                 SuggestedWords.INPUT_STYLE_TYPING,
72                 true /* gestureFloatingPreviewTextEnabled */,
73                 false /* shouldShowUiToAcceptTypedWord */));
74         assertTrue("typing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
75                 SuggestedWords.INPUT_STYLE_TYPING,
76                 false /* gestureFloatingPreviewTextEnabled */,
77                 true /* shouldShowUiToAcceptTypedWord */));
78         assertTrue("typing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
79                 SuggestedWords.INPUT_STYLE_TYPING,
80                 true /* gestureFloatingPreviewTextEnabled */,
81                 true /* shouldShowUiToAcceptTypedWord */));
82     }
83 
84     @Test
testShouldOmitTypedWordWhileGesturing()85     public void testShouldOmitTypedWordWhileGesturing() {
86         assertFalse("gesturing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
87                 SuggestedWords.INPUT_STYLE_UPDATE_BATCH,
88                 false /* gestureFloatingPreviewTextEnabled */,
89                 false /* shouldShowUiToAcceptTypedWord */));
90         assertFalse("gesturing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
91                 SuggestedWords.INPUT_STYLE_UPDATE_BATCH,
92                 true /* gestureFloatingPreviewTextEnabled */,
93                 false /* shouldShowUiToAcceptTypedWord */));
94         assertFalse("gesturing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
95                 SuggestedWords.INPUT_STYLE_UPDATE_BATCH,
96                 false /* gestureFloatingPreviewTextEnabled */,
97                 true /* shouldShowUiToAcceptTypedWord */));
98         assertTrue("gesturing", SuggestionStripLayoutHelper.shouldOmitTypedWord(
99                 SuggestedWords.INPUT_STYLE_UPDATE_BATCH,
100                 true /* gestureFloatingPreviewTextEnabled */,
101                 true /* shouldShowUiToAcceptTypedWord */));
102     }
103 
104     @Test
testShouldOmitTypedWordWhenGestured()105     public void testShouldOmitTypedWordWhenGestured() {
106         assertFalse("gestured", SuggestionStripLayoutHelper.shouldOmitTypedWord(
107                 SuggestedWords.INPUT_STYLE_TAIL_BATCH,
108                 false /* gestureFloatingPreviewTextEnabled */,
109                 false /* shouldShowUiToAcceptTypedWord */));
110         assertFalse("gestured", SuggestionStripLayoutHelper.shouldOmitTypedWord(
111                 SuggestedWords.INPUT_STYLE_TAIL_BATCH,
112                 true /* gestureFloatingPreviewTextEnabled */,
113                 false /* shouldShowUiToAcceptTypedWord */));
114         assertTrue("gestured", SuggestionStripLayoutHelper.shouldOmitTypedWord(
115                 SuggestedWords.INPUT_STYLE_TAIL_BATCH,
116                 false /* gestureFloatingPreviewTextEnabled */,
117                 true /* shouldShowUiToAcceptTypedWord */));
118         assertTrue("gestured", SuggestionStripLayoutHelper.shouldOmitTypedWord(
119                 SuggestedWords.INPUT_STYLE_TAIL_BATCH,
120                 true /* gestureFloatingPreviewTextEnabled */,
121                 true /* shouldShowUiToAcceptTypedWord */));
122     }
123 
124     // Note that this unit test assumes that the number of suggested words in the suggestion strip
125     // is 3.
126     private static final int POSITION_OMIT = -1;
127     private static final int POSITION_LEFT = 0;
128     private static final int POSITION_CENTER = 1;
129     private static final int POSITION_RIGHT = 2;
130 
131     @Test
testGetPositionInSuggestionStrip()132     public void testGetPositionInSuggestionStrip() {
133         assertEquals("1st word without auto correction", POSITION_CENTER,
134                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
135                         SuggestedWords.INDEX_OF_TYPED_WORD /* indexInSuggestedWords */,
136                         false /* willAutoCorrect */,
137                         false /* omitTypedWord */,
138                         POSITION_CENTER /* centerPositionInStrip */,
139                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
140         assertEquals("2nd word without auto correction", POSITION_LEFT,
141                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
142                         SuggestedWords.INDEX_OF_AUTO_CORRECTION /* indexInSuggestedWords */,
143                         false /* willAutoCorrect */,
144                         false /* omitTypedWord */,
145                         POSITION_CENTER /* centerPositionInStrip */,
146                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
147         assertEquals("3rd word without auto correction", POSITION_RIGHT,
148                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
149                         2 /* indexInSuggestedWords */,
150                         false /* willAutoCorrect */,
151                         false /* omitTypedWord */,
152                         POSITION_CENTER /* centerPositionInStrip */,
153                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
154 
155         assertEquals("typed word with auto correction", POSITION_LEFT,
156                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
157                         SuggestedWords.INDEX_OF_TYPED_WORD /* indexInSuggestedWords */,
158                         true /* willAutoCorrect */,
159                         false /* omitTypedWord */,
160                         POSITION_CENTER /* centerPositionInStrip */,
161                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
162         assertEquals("2nd word with auto correction", POSITION_CENTER,
163                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
164                         SuggestedWords.INDEX_OF_AUTO_CORRECTION /* indexInSuggestedWords */,
165                         true /* willAutoCorrect */,
166                         false /* omitTypedWord */,
167                         POSITION_CENTER /* centerPositionInStrip */,
168                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
169         assertEquals("3rd word with auto correction", POSITION_RIGHT,
170                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
171                         2 /* indexInSuggestedWords */,
172                         true /* willAutoCorrect */,
173                         false /* omitTypedWord */,
174                         POSITION_CENTER /* centerPositionInStrip */,
175                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
176 
177         assertEquals("1st word without auto correction", POSITION_OMIT,
178                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
179                         SuggestedWords.INDEX_OF_TYPED_WORD /* indexInSuggestedWords */,
180                         false /* willAutoCorrect */,
181                         true /* omitTypedWord */,
182                         POSITION_CENTER /* centerPositionInStrip */,
183                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
184         assertEquals("2nd word without auto correction", POSITION_CENTER,
185                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
186                         SuggestedWords.INDEX_OF_AUTO_CORRECTION /* indexInSuggestedWords */,
187                         false /* willAutoCorrect */,
188                         true /* omitTypedWord */,
189                         POSITION_CENTER /* centerPositionInStrip */,
190                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
191         assertEquals("3rd word without auto correction", POSITION_LEFT,
192                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
193                         2 /* indexInSuggestedWords */,
194                         false /* willAutoCorrect */,
195                         true /* omitTypedWord */,
196                         POSITION_CENTER /* centerPositionInStrip */,
197                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
198         assertEquals("4th word without auto correction", POSITION_RIGHT,
199                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
200                         3 /* indexInSuggestedWords */,
201                         false /* willAutoCorrect */,
202                         true /* omitTypedWord */,
203                         POSITION_CENTER /* centerPositionInStrip */,
204                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
205 
206         assertEquals("typed word with auto correction", POSITION_OMIT,
207                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
208                         SuggestedWords.INDEX_OF_TYPED_WORD /* indexInSuggestedWords */,
209                         true /* willAutoCorrect */,
210                         true /* omitTypedWord */,
211                         POSITION_CENTER /* centerPositionInStrip */,
212                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
213         assertEquals("2nd word with auto correction", POSITION_CENTER,
214                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
215                         SuggestedWords.INDEX_OF_AUTO_CORRECTION /* indexInSuggestedWords */,
216                         true /* willAutoCorrect */,
217                         true /* omitTypedWord */,
218                         POSITION_CENTER /* centerPositionInStrip */,
219                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
220         assertEquals("3rd word with auto correction", POSITION_LEFT,
221                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
222                         2 /* indexInSuggestedWords */,
223                         true /* willAutoCorrect */,
224                         true /* omitTypedWord */,
225                         POSITION_CENTER /* centerPositionInStrip */,
226                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
227         assertEquals("4th word with auto correction", POSITION_RIGHT,
228                 SuggestionStripLayoutHelper.getPositionInSuggestionStrip(
229                         3 /* indexInSuggestedWords */,
230                         true /* willAutoCorrect */,
231                         true /* omitTypedWord */,
232                         POSITION_CENTER /* centerPositionInStrip */,
233                         POSITION_LEFT /* typedWordPositionWhenAutoCorrect */));
234     }
235 }
236