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