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.view.inputmethod; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.os.Parcel; 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.util.ArrayList; 30 31 @SmallTest 32 @RunWith(AndroidJUnit4.class) 33 public class InputMethodSubtypeArrayTest { 34 35 @Test 36 public void testInstantiate() throws Exception { 37 final ArrayList<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>(); 38 subtypes.add(createDummySubtype(0, "en_US")); 39 subtypes.add(createDummySubtype(1, "en_US")); 40 subtypes.add(createDummySubtype(2, "ja_JP")); 41 42 final InputMethodSubtypeArray array = new InputMethodSubtypeArray(subtypes); 43 assertEquals(subtypes.size(), array.getCount()); 44 assertEquals(subtypes.get(0), array.get(0)); 45 assertEquals(subtypes.get(1), array.get(1)); 46 assertEquals(subtypes.get(2), array.get(2)); 47 48 final InputMethodSubtypeArray clonedArray = cloneViaParcel(array); 49 assertEquals(subtypes.size(), clonedArray.getCount()); 50 assertEquals(subtypes.get(0), clonedArray.get(0)); 51 assertEquals(subtypes.get(1), clonedArray.get(1)); 52 assertEquals(subtypes.get(2), clonedArray.get(2)); 53 54 final InputMethodSubtypeArray clonedClonedArray = cloneViaParcel(clonedArray); 55 assertEquals(clonedArray.getCount(), clonedClonedArray.getCount()); 56 assertEquals(clonedArray.get(0), clonedClonedArray.get(0)); 57 assertEquals(clonedArray.get(1), clonedClonedArray.get(1)); 58 assertEquals(clonedArray.get(2), clonedClonedArray.get(2)); 59 } 60 61 InputMethodSubtypeArray cloneViaParcel(final InputMethodSubtypeArray original) { 62 Parcel parcel = null; 63 try { 64 parcel = Parcel.obtain(); 65 original.writeToParcel(parcel); 66 parcel.setDataPosition(0); 67 return new InputMethodSubtypeArray(parcel); 68 } finally { 69 if (parcel != null) { 70 parcel.recycle(); 71 } 72 } 73 } 74 75 private static InputMethodSubtype createDummySubtype(final int id, final String locale) { 76 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); 77 return builder.setSubtypeNameResId(0) 78 .setSubtypeIconResId(0) 79 .setSubtypeId(id) 80 .setSubtypeLocale(locale) 81 .setIsAsciiCapable(true) 82 .build(); 83 } 84 } 85