Home | History | Annotate | Download | only in os
      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 android.os;
     18 
     19 import android.test.InstrumentationTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 import android.view.inputmethod.InputMethodSubtype;
     22 import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
     23 
     24 import java.util.Objects;
     25 
     26 public 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         verifyLocale("tl");
     60         verifyLocale("tl_PH");
     61         verifyLocale("fil");
     62         verifyLocale("fil_PH");
     63     }
     64 
     65     @SmallTest
     66     public void testDeprecatedLocaleString() throws Exception {
     67         // Make sure "iw" is not automatically replaced with "he".
     68         final InputMethodSubtype subtypeIw = createDummySubtype("iw");
     69         final InputMethodSubtype subtypeHe = createDummySubtype("he");
     70         assertEquals("iw", subtypeIw.getLocale());
     71         assertEquals("he", subtypeHe.getLocale());
     72         assertFalse(Objects.equals(subtypeIw, subtypeHe));
     73         assertFalse(Objects.equals(subtypeIw.hashCode(), subtypeHe.hashCode()));
     74 
     75         final InputMethodSubtype clonedSubtypeIw = cloneViaParcel(subtypeIw);
     76         final InputMethodSubtype clonedSubtypeHe = cloneViaParcel(subtypeHe);
     77         assertEquals(subtypeIw, clonedSubtypeIw);
     78         assertEquals(subtypeHe, clonedSubtypeHe);
     79         assertEquals("iw", clonedSubtypeIw.getLocale());
     80         assertEquals("he", clonedSubtypeHe.getLocale());
     81     }
     82 
     83     private static final InputMethodSubtype cloneViaParcel(final InputMethodSubtype original) {
     84         Parcel parcel = null;
     85         try {
     86             parcel = Parcel.obtain();
     87             original.writeToParcel(parcel, 0);
     88             parcel.setDataPosition(0);
     89             return InputMethodSubtype.CREATOR.createFromParcel(parcel);
     90         } finally {
     91             if (parcel != null) {
     92                 parcel.recycle();
     93             }
     94         }
     95     }
     96 
     97     private static final InputMethodSubtype createDummySubtype(final String locale) {
     98         final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder();
     99         return builder.setSubtypeNameResId(0)
    100                 .setSubtypeIconResId(0)
    101                 .setSubtypeLocale(locale)
    102                 .setIsAsciiCapable(true)
    103                 .build();
    104     }
    105 }
    106