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