1 /* 2 * Copyright (C) 2008 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 android.text.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertTrue; 23 24 import android.content.Context; 25 import android.content.res.Configuration; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.test.annotation.UiThreadTest; 28 import android.support.test.filters.SmallTest; 29 import android.support.test.runner.AndroidJUnit4; 30 import android.text.AutoText; 31 import android.view.View; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.Locale; 38 39 @SmallTest 40 @RunWith(AndroidJUnit4.class) 41 public class AutoTextTest { 42 private Context mContext; 43 44 @Before 45 public void setup() { 46 mContext = InstrumentationRegistry.getTargetContext(); 47 48 // set locale as English. 49 Locale.setDefault(Locale.ENGLISH); 50 Configuration config = mContext.getResources().getConfiguration(); 51 if (!config.locale.equals(Locale.getDefault())) { 52 config.locale = Locale.getDefault(); 53 mContext.getResources().updateConfiguration(config, null); 54 } 55 } 56 57 @UiThreadTest 58 @Test 59 public void testGet() { 60 // Define the necessary sources. 61 CharSequence src; 62 String actual; 63 // New a View instance. 64 View view = new View(mContext); 65 66 // Test a word key not in the autotext.xml. 67 src = "can"; 68 actual = AutoText.get(src, 0, src.length(), view); 69 assertNull(actual); 70 71 // get possible spelling correction in the scope of current 72 // local/language 73 src = "acn"; 74 actual = AutoText.get(src, 0, src.length(), view); 75 assertNotNull(actual); 76 assertEquals("can", actual); 77 78 /* 79 * get possible spelling correction in the scope of current 80 * local/language, with end bigger than end 81 */ 82 src = "acn"; 83 actual = AutoText.get(src, 0, src.length() + 1, view); 84 assertNull(actual); 85 86 /* 87 * get possible spelling correction in the scope of current 88 * local/language, with end smaller than end 89 */ 90 src = "acn"; 91 actual = AutoText.get(src, 0, src.length() - 1, view); 92 assertNull(actual); 93 94 // get possible spelling correction outside of the scope of current 95 // local/language 96 src = "acnh"; 97 actual = AutoText.get(src, 0, src.length() - 1, view); 98 assertNotNull(actual); 99 assertEquals("can", actual); 100 } 101 102 @UiThreadTest 103 @Test 104 public void testGetSize() { 105 View view = new View(mContext); 106 // Returns the size of the auto text dictionary. Just make sure it is bigger than 0. 107 assertTrue(AutoText.getSize(view) > 0); 108 } 109 } 110 111