DistracterFilterTest.java revision e05eb2182602dd62e2bfa5b78ab6df7f331cff24
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
17package com.android.inputmethod.latin;
18
19import java.util.ArrayList;
20import java.util.Locale;
21
22import android.content.Context;
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.LargeTest;
25import android.view.inputmethod.InputMethodSubtype;
26
27import com.android.inputmethod.latin.utils.DistracterFilterCheckingExactMatchesAndSuggestions;
28
29/**
30 * Unit test for DistracterFilter
31 */
32@LargeTest
33public class DistracterFilterTest extends AndroidTestCase {
34    private DistracterFilterCheckingExactMatchesAndSuggestions mDistracterFilter;
35
36    @Override
37    protected void setUp() throws Exception {
38        super.setUp();
39        final Context context = getContext();
40        mDistracterFilter = new DistracterFilterCheckingExactMatchesAndSuggestions(context);
41        RichInputMethodManager.init(context);
42        final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
43        final ArrayList<InputMethodSubtype> subtypes = new ArrayList<>();
44        subtypes.add(richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
45                Locale.US.toString(), "qwerty"));
46        subtypes.add(richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
47                Locale.FRENCH.toString(), "azerty"));
48        subtypes.add(richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
49                Locale.GERMAN.toString(), "qwertz"));
50        mDistracterFilter.updateEnabledSubtypes(subtypes);
51    }
52
53    public void testIsDistractorToWordsInDictionaries() {
54        final PrevWordsInfo EMPTY_PREV_WORDS_INFO = PrevWordsInfo.EMPTY_PREV_WORDS_INFO;
55
56        final Locale localeEnUs = new Locale("en", "US");
57        String typedWord;
58
59        typedWord = "Bill";
60        // For this test case, we consider "Bill" is a distracter to "bill".
61        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
62                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
63
64        typedWord = "nOt";
65        // For this test case, we consider "nOt" is a distracter to "not".
66        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
67                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
68
69        typedWord = "youre";
70        // For this test case, we consider "youre" is a distracter to "you're".
71        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
72                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
73
74        typedWord = "Banana";
75        // For this test case, we consider "Banana" is a distracter to "banana".
76        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
77                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
78
79        typedWord = "orange";
80        // For this test case, we consider "orange" is not a distracter to any word in dictionaries.
81        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
82                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
83
84        typedWord = "Orange";
85        // For this test case, we consider "Orange" is a distracter to "orange".
86        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
87                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
88
89        typedWord = "café";
90        // For this test case, we consider "café" is a distracter to "cafe".
91        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
92                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
93
94        typedWord = "cafe";
95        // For this test case, we consider "cafe" is not a distracter to any word in dictionaries.
96        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
97                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
98
99        typedWord = "I'll";
100        // For this test case, we consider "I'll" is not a distracter to any word in dictionaries.
101        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
102                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
103
104        typedWord = "ill";
105        // For this test case, we consider "ill" is a distracter to "I'll"
106        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
107                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
108
109        typedWord = "asdfd";
110        // For this test case, we consider "asdfd" is not a distracter to any word in dictionaries.
111        assertFalse(
112                mDistracterFilter.isDistracterToWordsInDictionaries(
113                        EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
114
115        typedWord = "thank";
116        // For this test case, we consider "thank" is not a distracter to any other word
117        // in dictionaries.
118        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
119                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
120
121        typedWord = "thabk";
122        // For this test case, we consider "thabk" is a distracter to "thank"
123        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
124                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
125
126        typedWord = "thanks";
127        // For this test case, we consider "thanks" is not a distracter to any other word
128        // in dictionaries.
129        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
130                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
131
132        typedWord = "thabks";
133        // For this test case, we consider "thabks" is a distracter to "thanks"
134        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
135                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
136
137        typedWord = "think";
138        // For this test case, we consider "think" is not a distracter to any other word
139        // in dictionaries.
140        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
141                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
142
143        typedWord = "thibk";
144        // For this test case, we consider "thibk" is a distracter to "think"
145        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
146                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
147
148        typedWord = "tgis";
149        // For this test case, we consider "tgis" is a distracter to "this"
150        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
151                EMPTY_PREV_WORDS_INFO, typedWord, localeEnUs));
152
153        final Locale localeDeDe = new Locale("de");
154
155        typedWord = "fUEr";
156        // For this test case, we consider "fUEr" is a distracter to "für".
157        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
158                EMPTY_PREV_WORDS_INFO, typedWord, localeDeDe));
159
160        typedWord = "fuer";
161        // For this test case, we consider "fuer" is a distracter to "für".
162        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
163                EMPTY_PREV_WORDS_INFO, typedWord, localeDeDe));
164
165        typedWord = "fur";
166        // For this test case, we consider "fur" is a distracter to "für".
167        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
168                EMPTY_PREV_WORDS_INFO, typedWord, localeDeDe));
169
170        final Locale localeFrFr = new Locale("fr");
171
172        typedWord = "a";
173        // For this test case, we consider "a" is a distracter to "à".
174        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
175                EMPTY_PREV_WORDS_INFO, typedWord, localeFrFr));
176
177        typedWord = "à";
178        // For this test case, we consider "à" is not a distracter to any word in dictionaries.
179        assertFalse(mDistracterFilter.isDistracterToWordsInDictionaries(
180                EMPTY_PREV_WORDS_INFO, typedWord, localeFrFr));
181
182        typedWord = "etre";
183        // For this test case, we consider "etre" is a distracter to "être".
184        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
185                EMPTY_PREV_WORDS_INFO, typedWord, localeFrFr));
186
187        typedWord = "États-unis";
188        // For this test case, we consider "États-unis" is a distracter to "États-Unis".
189        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
190                EMPTY_PREV_WORDS_INFO, typedWord, localeFrFr));
191
192        typedWord = "ÉtatsUnis";
193        // For this test case, we consider "ÉtatsUnis" is a distracter to "États-Unis".
194        assertTrue(mDistracterFilter.isDistracterToWordsInDictionaries(
195                EMPTY_PREV_WORDS_INFO, typedWord, localeFrFr));
196    }
197}
198