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