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.utils;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import android.content.res.Resources;
24 
25 import androidx.test.InstrumentationRegistry;
26 import androidx.test.filters.SmallTest;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.android.inputmethod.latin.common.LocaleUtils;
30 import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.util.Locale;
36 
37 @SmallTest
38 @RunWith(AndroidJUnit4.class)
39 public class DictionaryInfoUtilsTests {
40     @Test
testLooksValidForDictionaryInsertion()41     public void testLooksValidForDictionaryInsertion() {
42         final RunInLocale<SpacingAndPunctuations> job = new RunInLocale<SpacingAndPunctuations>() {
43             @Override
44             protected SpacingAndPunctuations job(final Resources res) {
45                 return new SpacingAndPunctuations(res);
46             }
47         };
48         final Resources res = InstrumentationRegistry.getTargetContext().getResources();
49         final SpacingAndPunctuations sp = job.runInLocale(res, Locale.ENGLISH);
50         assertTrue(DictionaryInfoUtils.looksValidForDictionaryInsertion("aochaueo", sp));
51         assertFalse(DictionaryInfoUtils.looksValidForDictionaryInsertion("", sp));
52         assertTrue(DictionaryInfoUtils.looksValidForDictionaryInsertion("ao-ch'aueo", sp));
53         assertFalse(DictionaryInfoUtils.looksValidForDictionaryInsertion("2908743256", sp));
54         assertTrue(DictionaryInfoUtils.looksValidForDictionaryInsertion("31aochaueo", sp));
55         assertFalse(DictionaryInfoUtils.looksValidForDictionaryInsertion("akeo  raeoch oerch .",
56                 sp));
57         assertFalse(DictionaryInfoUtils.looksValidForDictionaryInsertion("!!!", sp));
58     }
59 
60     @Test
testGetMainDictId()61     public void testGetMainDictId() {
62         assertEquals("main:en",
63                 DictionaryInfoUtils.getMainDictId(LocaleUtils.constructLocaleFromString("en")));
64         assertEquals("main:en_us",
65                 DictionaryInfoUtils.getMainDictId(LocaleUtils.constructLocaleFromString("en_US")));
66         assertEquals("main:en_gb",
67                 DictionaryInfoUtils.getMainDictId(LocaleUtils.constructLocaleFromString("en_GB")));
68 
69         assertEquals("main:es",
70                 DictionaryInfoUtils.getMainDictId(LocaleUtils.constructLocaleFromString("es")));
71         assertEquals("main:es_us",
72                 DictionaryInfoUtils.getMainDictId(LocaleUtils.constructLocaleFromString("es_US")));
73 
74         assertEquals("main:en_us_posix", DictionaryInfoUtils.getMainDictId(
75                         LocaleUtils.constructLocaleFromString("en_US_POSIX")));
76     }
77 }
78