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 android.os; 18 19import android.test.InstrumentationTestCase; 20import android.test.suitebuilder.annotation.SmallTest; 21import android.view.inputmethod.InputMethodSubtype; 22import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder; 23 24import java.util.Objects; 25 26public class InputMethodSubtypeTest extends InstrumentationTestCase { 27 28 public void verifyLocale(final String localeString) { 29 // InputMethodSubtype#getLocale() returns exactly the same string that is passed to the 30 // constructor. 31 assertEquals(localeString, createDummySubtype(localeString).getLocale()); 32 33 // InputMethodSubtype#getLocale() should be preserved via marshaling. 34 assertEquals(createDummySubtype(localeString).getLocale(), 35 cloneViaParcel(createDummySubtype(localeString)).getLocale()); 36 37 // InputMethodSubtype#getLocale() should be preserved via marshaling. 38 assertEquals(createDummySubtype(localeString).getLocale(), 39 cloneViaParcel(cloneViaParcel(createDummySubtype(localeString))).getLocale()); 40 41 // Make sure InputMethodSubtype#hashCode() returns the same hash code. 42 assertEquals(createDummySubtype(localeString).hashCode(), 43 createDummySubtype(localeString).hashCode()); 44 assertEquals(createDummySubtype(localeString).hashCode(), 45 cloneViaParcel(createDummySubtype(localeString)).hashCode()); 46 assertEquals(createDummySubtype(localeString).hashCode(), 47 cloneViaParcel(cloneViaParcel(createDummySubtype(localeString))).hashCode()); 48 } 49 50 @SmallTest 51 public void testLocaleString() throws Exception { 52 // The locale string in InputMethodSubtype has accepted an arbitrary text actually, 53 // regardless of the validity of the text as a locale string. 54 verifyLocale("en_US"); 55 verifyLocale("apparently invalid locale string"); 56 verifyLocale("zz"); 57 verifyLocale("iw"); 58 verifyLocale("he"); 59 } 60 61 @SmallTest 62 public void testDeprecatedLocaleString() throws Exception { 63 // Make sure "iw" is not automatically replaced with "he". 64 final InputMethodSubtype subtypeIw = createDummySubtype("iw"); 65 final InputMethodSubtype subtypeHe = createDummySubtype("he"); 66 assertEquals("iw", subtypeIw.getLocale()); 67 assertEquals("he", subtypeHe.getLocale()); 68 assertFalse(Objects.equals(subtypeIw, subtypeHe)); 69 assertFalse(Objects.equals(subtypeIw.hashCode(), subtypeHe.hashCode())); 70 71 final InputMethodSubtype clonedSubtypeIw = cloneViaParcel(subtypeIw); 72 final InputMethodSubtype clonedSubtypeHe = cloneViaParcel(subtypeHe); 73 assertEquals(subtypeIw, clonedSubtypeIw); 74 assertEquals(subtypeHe, clonedSubtypeHe); 75 assertEquals("iw", clonedSubtypeIw.getLocale()); 76 assertEquals("he", clonedSubtypeHe.getLocale()); 77 } 78 79 private static final InputMethodSubtype cloneViaParcel(final InputMethodSubtype original) { 80 Parcel parcel = null; 81 try { 82 parcel = Parcel.obtain(); 83 original.writeToParcel(parcel, 0); 84 parcel.setDataPosition(0); 85 return InputMethodSubtype.CREATOR.createFromParcel(parcel); 86 } finally { 87 if (parcel != null) { 88 parcel.recycle(); 89 } 90 } 91 } 92 93 private static final InputMethodSubtype createDummySubtype(final String locale) { 94 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); 95 return builder.setSubtypeNameResId(0) 96 .setSubtypeIconResId(0) 97 .setSubtypeLocale(locale) 98 .setIsAsciiCapable(true) 99 .build(); 100 } 101} 102