SpellCheckerSubtypeTest.java revision 9534ae185809b69e153736287acbf9c8e52e7492
1/* 2 * Copyright (C) 2015 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 android.view.textservice; 18 19import android.os.Parcel; 20import android.test.InstrumentationTestCase; 21import android.test.suitebuilder.annotation.SmallTest; 22 23import java.util.Arrays; 24 25import static android.test.MoreAsserts.assertNotEqual; 26 27/** 28 * TODO: Most of part can be, and probably should be, moved to CTS. 29 */ 30public class SpellCheckerSubtypeTest extends InstrumentationTestCase { 31 private static final String SUBTYPE_SUBTYPE_LOCALE_STRING_A = "en_GB"; 32 private static final int SUBTYPE_NAME_RES_ID_A = 0x12345; 33 private static final String SUBTYPE_EXTRA_VALUE_A = "Key1=Value1,Key2=Value2"; 34 private static final String SUBTYPE_SUBTYPE_LOCALE_STRING_B = "en_IN"; 35 private static final int SUBTYPE_NAME_RES_ID_B = 0x54321; 36 private static final String SUBTYPE_EXTRA_VALUE_B = "Key3=Value3,Key4=Value4"; 37 38 private static int defaultHashCodeAlgorithm(String locale, String extraValue) { 39 return Arrays.hashCode(new Object[] {locale, extraValue}); 40 } 41 42 private static final SpellCheckerSubtype cloneViaParcel(final SpellCheckerSubtype original) { 43 Parcel parcel = null; 44 try { 45 parcel = Parcel.obtain(); 46 original.writeToParcel(parcel, 0); 47 parcel.setDataPosition(0); 48 return SpellCheckerSubtype.CREATOR.createFromParcel(parcel); 49 } finally { 50 if (parcel != null) { 51 parcel.recycle(); 52 } 53 } 54 } 55 56 @SmallTest 57 public void testSubtype() throws Exception { 58 final SpellCheckerSubtype subtype = new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, 59 SUBTYPE_SUBTYPE_LOCALE_STRING_A, SUBTYPE_EXTRA_VALUE_A); 60 61 assertEquals(SUBTYPE_NAME_RES_ID_A, subtype.getNameResId()); 62 assertEquals(SUBTYPE_SUBTYPE_LOCALE_STRING_A, subtype.getLocale()); 63 assertEquals("Value1", subtype.getExtraValueOf("Key1")); 64 assertEquals("Value2", subtype.getExtraValueOf("Key2")); 65 // Historically we have used SpellCheckerSubtype#hashCode() to track which subtype is 66 // enabled, and it is supposed to be stored in SecureSettings. Therefore we have to 67 // keep using the same algorithm for compatibility reasons. 68 assertEquals( 69 defaultHashCodeAlgorithm(SUBTYPE_SUBTYPE_LOCALE_STRING_A, SUBTYPE_EXTRA_VALUE_A), 70 subtype.hashCode()); 71 72 final SpellCheckerSubtype clonedSubtype = cloneViaParcel(subtype); 73 assertEquals(SUBTYPE_NAME_RES_ID_A, clonedSubtype.getNameResId()); 74 assertEquals(SUBTYPE_SUBTYPE_LOCALE_STRING_A, clonedSubtype.getLocale()); 75 assertEquals("Value1", clonedSubtype.getExtraValueOf("Key1")); 76 assertEquals("Value2", clonedSubtype.getExtraValueOf("Key2")); 77 assertEquals( 78 defaultHashCodeAlgorithm(SUBTYPE_SUBTYPE_LOCALE_STRING_A, SUBTYPE_EXTRA_VALUE_A), 79 clonedSubtype.hashCode()); 80 } 81 82 @SmallTest 83 public void testEquality() throws Exception { 84 assertEquals( 85 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, 86 SUBTYPE_EXTRA_VALUE_A), 87 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, 88 SUBTYPE_EXTRA_VALUE_A)); 89 assertNotEqual( 90 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, 91 SUBTYPE_EXTRA_VALUE_A), 92 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_B, SUBTYPE_SUBTYPE_LOCALE_STRING_B, 93 SUBTYPE_EXTRA_VALUE_A)); 94 assertNotEqual( 95 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, 96 SUBTYPE_EXTRA_VALUE_A), 97 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_B, 98 SUBTYPE_EXTRA_VALUE_A)); 99 assertNotEqual( 100 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, 101 SUBTYPE_EXTRA_VALUE_A), 102 new SpellCheckerSubtype(SUBTYPE_NAME_RES_ID_A, SUBTYPE_SUBTYPE_LOCALE_STRING_A, 103 SUBTYPE_EXTRA_VALUE_B)); 104 } 105} 106