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.compat;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 
22 import android.annotation.TargetApi;
23 import android.content.Context;
24 import android.os.Build;
25 import android.text.Spanned;
26 import android.text.TextUtils;
27 import android.text.style.SuggestionSpan;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.filters.SmallTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import com.android.inputmethod.latin.SuggestedWords;
34 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Locale;
42 
43 import javax.annotation.Nullable;
44 
45 @SmallTest
46 @RunWith(AndroidJUnit4.class)
47 public class SuggestionSpanUtilsTest {
48 
getContext()49     private Context getContext() {
50         return InstrumentationRegistry.getTargetContext();
51     }
52 
53     /**
54      * Helper method to create a placeholder {@link SuggestedWordInfo}.
55      *
56      * @param kindAndFlags the kind and flags to be used to create {@link SuggestedWordInfo}.
57      * @param word the word to be used to create {@link SuggestedWordInfo}.
58      * @return a new instance of {@link SuggestedWordInfo}.
59      */
createWordInfo(final String word, final int kindAndFlags)60     private static SuggestedWordInfo createWordInfo(final String word, final int kindAndFlags) {
61         return new SuggestedWordInfo(word, "" /* prevWordsContext */, 1 /* score */, kindAndFlags,
62                 null /* sourceDict */,
63                 SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
64                 SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */);
65     }
66 
assertNotSuggestionSpan(final String expectedText, final CharSequence actualText)67     private static void assertNotSuggestionSpan(final String expectedText,
68             final CharSequence actualText) {
69         assertTrue(TextUtils.equals(expectedText, actualText));
70         if (!(actualText instanceof Spanned)) {
71             return;
72         }
73         final Spanned spanned = (Spanned)actualText;
74         final SuggestionSpan[] suggestionSpans = spanned.getSpans(0, spanned.length(),
75                 SuggestionSpan.class);
76         assertEquals(0, suggestionSpans.length);
77     }
78 
assertSuggestionSpan(final String expectedText, final int reuiredSuggestionSpanFlags, final int requiredSpanFlags, final String[] expectedSuggestions, @Nullable final Locale expectedLocale, final CharSequence actualText)79     private static void assertSuggestionSpan(final String expectedText,
80             final int reuiredSuggestionSpanFlags, final int requiredSpanFlags,
81             final String[] expectedSuggestions, @Nullable final Locale expectedLocale,
82             final CharSequence actualText) {
83         assertTrue(TextUtils.equals(expectedText, actualText));
84         assertTrue(actualText instanceof Spanned);
85         final Spanned spanned = (Spanned)actualText;
86         final SuggestionSpan[] suggestionSpans = spanned.getSpans(0, spanned.length(),
87                 SuggestionSpan.class);
88         assertEquals(1, suggestionSpans.length);
89         final SuggestionSpan suggestionSpan = suggestionSpans[0];
90         if (reuiredSuggestionSpanFlags != 0) {
91             assertTrue((suggestionSpan.getFlags() & reuiredSuggestionSpanFlags) != 0);
92         }
93         if (requiredSpanFlags != 0) {
94             assertTrue((spanned.getSpanFlags(suggestionSpan) & requiredSpanFlags) != 0);
95         }
96         if (expectedSuggestions != null) {
97             final String[] actualSuggestions = suggestionSpan.getSuggestions();
98             assertEquals(expectedSuggestions.length, actualSuggestions.length);
99             for (int i = 0; i < expectedSuggestions.length; ++i) {
100                 assertEquals(expectedSuggestions[i], actualSuggestions[i]);
101             }
102         }
103         // CAVEAT: SuggestionSpan#getLocale() returns String rather than Locale object.
104         assertEquals(expectedLocale.toString(), suggestionSpan.getLocale());
105     }
106 
107     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
108     @Test
testGetTextWithAutoCorrectionIndicatorUnderline()109     public void testGetTextWithAutoCorrectionIndicatorUnderline() {
110         final String ORIGINAL_TEXT = "Hey!";
111         final Locale NONNULL_LOCALE = new Locale("en", "GB");
112         final CharSequence text = SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
113                 getContext(), ORIGINAL_TEXT, NONNULL_LOCALE);
114         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
115             assertNotSuggestionSpan(ORIGINAL_TEXT, text);
116             return;
117         }
118         assertSuggestionSpan(ORIGINAL_TEXT,
119                 SuggestionSpan.FLAG_AUTO_CORRECTION /* reuiredSuggestionSpanFlags */,
120                 Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */,
121                 new String[]{}, NONNULL_LOCALE, text);
122     }
123 
124     @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
125     @Test
testGetTextWithAutoCorrectionIndicatorUnderlineRootLocale()126     public void testGetTextWithAutoCorrectionIndicatorUnderlineRootLocale() {
127         final String ORIGINAL_TEXT = "Hey!";
128         final CharSequence text = SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(
129                 getContext(), ORIGINAL_TEXT, Locale.ROOT);
130         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
131             assertNotSuggestionSpan(ORIGINAL_TEXT, text);
132             return;
133         }
134         assertSuggestionSpan(ORIGINAL_TEXT,
135                 SuggestionSpan.FLAG_AUTO_CORRECTION /* reuiredSuggestionSpanFlags */,
136                 Spanned.SPAN_COMPOSING | Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */,
137                 new String[]{}, Locale.ROOT, text);
138     }
139 
140     @Test
testGetTextWithSuggestionSpan()141     public void testGetTextWithSuggestionSpan() {
142         final SuggestedWordInfo prediction1 =
143                 createWordInfo("Quality", SuggestedWordInfo.KIND_PREDICTION);
144         final SuggestedWordInfo prediction2 =
145                 createWordInfo("Speed", SuggestedWordInfo.KIND_PREDICTION);
146         final SuggestedWordInfo prediction3 =
147                 createWordInfo("Price", SuggestedWordInfo.KIND_PREDICTION);
148 
149         final SuggestedWordInfo typed =
150                 createWordInfo("Hey", SuggestedWordInfo.KIND_TYPED);
151 
152         final SuggestedWordInfo[] corrections =
153                 new SuggestedWordInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE * 2];
154         for (int i = 0; i < corrections.length; ++i) {
155             corrections[i] = createWordInfo("correction" + i, SuggestedWordInfo.KIND_CORRECTION);
156         }
157 
158         final Locale NONNULL_LOCALE = new Locale("en", "GB");
159 
160         // SuggestionSpan will not be attached when {@link SuggestedWords#INPUT_STYLE_PREDICTION}
161         // is specified.
162         {
163             final SuggestedWords predictedWords = new SuggestedWords(
164                     new ArrayList<>(Arrays.asList(prediction1, prediction2, prediction3)),
165                     null /* rawSuggestions */,
166                     null /* typedWord */,
167                     false /* typedWordValid */,
168                     false /* willAutoCorrect */,
169                     false /* isObsoleteSuggestions */,
170                     SuggestedWords.INPUT_STYLE_PREDICTION,
171                     SuggestedWords.NOT_A_SEQUENCE_NUMBER);
172             final String PICKED_WORD = prediction2.mWord;
173             // Note that the framework uses the context locale as a fallback locale.
174             assertNotSuggestionSpan(
175                     PICKED_WORD,
176                     SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD,
177                             predictedWords, NONNULL_LOCALE));
178         }
179 
180         final ArrayList<SuggestedWordInfo> suggestedWordList = new ArrayList<>();
181         suggestedWordList.add(typed);
182         suggestedWordList.add(prediction1);
183         suggestedWordList.add(prediction2);
184         suggestedWordList.add(prediction3);
185         suggestedWordList.addAll(Arrays.asList(corrections));
186         final SuggestedWords typedAndCollectedWords = new SuggestedWords(
187                 suggestedWordList,
188                 null /* rawSuggestions */,
189                 null /* typedWord */,
190                 false /* typedWordValid */,
191                 false /* willAutoCorrect */,
192                 false /* isObsoleteSuggestions */,
193                 SuggestedWords.INPUT_STYLE_TYPING,
194                 SuggestedWords.NOT_A_SEQUENCE_NUMBER);
195 
196         for (final SuggestedWordInfo pickedWord : suggestedWordList) {
197             final String PICKED_WORD = pickedWord.mWord;
198 
199             final ArrayList<String> expectedSuggestions = new ArrayList<>();
200             for (SuggestedWordInfo suggestedWordInfo : suggestedWordList) {
201                 if (expectedSuggestions.size() >= SuggestionSpan.SUGGESTIONS_MAX_SIZE) {
202                     break;
203                 }
204                 if (suggestedWordInfo.isKindOf(SuggestedWordInfo.KIND_PREDICTION)) {
205                     // Currently predictions are not filled into SuggestionSpan.
206                     continue;
207                 }
208                 final String suggestedWord = suggestedWordInfo.mWord;
209                 if (TextUtils.equals(PICKED_WORD, suggestedWord)) {
210                     // Typed word itself is not added to SuggestionSpan.
211                     continue;
212                 }
213                 expectedSuggestions.add(suggestedWord);
214             }
215 
216             // non-null locale
217             assertSuggestionSpan(
218                     PICKED_WORD,
219                     0 /* reuiredSuggestionSpanFlags */,
220                     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */,
221                     expectedSuggestions.toArray(new String[expectedSuggestions.size()]),
222                     NONNULL_LOCALE,
223                     SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD,
224                             typedAndCollectedWords, NONNULL_LOCALE));
225 
226             // root locale
227             assertSuggestionSpan(
228                     PICKED_WORD,
229                     0 /* reuiredSuggestionSpanFlags */,
230                     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE /* requiredSpanFlags */,
231                     expectedSuggestions.toArray(new String[expectedSuggestions.size()]),
232                     Locale.ROOT,
233                     SuggestionSpanUtils.getTextWithSuggestionSpan(getContext(), PICKED_WORD,
234                             typedAndCollectedWords, Locale.ROOT));
235         }
236     }
237 
238     @Test
testFindFirstLocaleFromSuggestionSpans()239     public void testFindFirstLocaleFromSuggestionSpans() {
240         final String[] suggestions = new String[] {"Quality", "Speed", "Price"};
241         final SuggestionSpan nullLocaleSpan = new SuggestionSpan((Locale)null, suggestions, 0);
242         final SuggestionSpan emptyLocaleSpan = new SuggestionSpan(new Locale(""), suggestions, 0);
243         final SuggestionSpan enUsLocaleSpan = new SuggestionSpan(Locale.US, suggestions, 0);
244         final SuggestionSpan jaJpLocaleSpan = new SuggestionSpan(Locale.JAPAN, suggestions, 0);
245 
246         assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
247                 new SuggestionSpan[] {}));
248 
249         assertEquals(null, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
250                 new SuggestionSpan[] {emptyLocaleSpan}));
251 
252         assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
253                 new SuggestionSpan[] {enUsLocaleSpan}));
254 
255         assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
256                 new SuggestionSpan[] {nullLocaleSpan, enUsLocaleSpan}));
257 
258         assertEquals(Locale.US, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
259                 new SuggestionSpan[] {nullLocaleSpan, emptyLocaleSpan, enUsLocaleSpan}));
260 
261         assertEquals(Locale.JAPAN, SuggestionSpanUtils.findFirstLocaleFromSuggestionSpans(
262                 new SuggestionSpan[] {nullLocaleSpan, jaJpLocaleSpan, enUsLocaleSpan}));
263     }
264 }
265