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