InputMethodInfoTest.java revision b360b995c91c2abdac7426ba37b154d15b235113
1/*
2 * Copyright (C) 2017 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 static org.hamcrest.CoreMatchers.equalTo;
20import static org.hamcrest.CoreMatchers.is;
21import static org.junit.Assert.assertThat;
22
23import android.annotation.XmlRes;
24import android.content.Context;
25import android.content.pm.ResolveInfo;
26import android.content.pm.ServiceInfo;
27import android.os.Bundle;
28import android.os.Parcel;
29import android.support.test.InstrumentationRegistry;
30import android.support.test.runner.AndroidJUnit4;
31
32import com.android.frameworks.coretests.R;
33
34import org.junit.Test;
35import org.junit.runner.RunWith;
36
37@RunWith(AndroidJUnit4.class)
38public class InputMethodInfoTest {
39
40    @Test
41    public void testEqualsAndHashCode() throws Exception {
42        final InputMethodInfo imi = buildInputMethodForTest(R.xml.ime_meta);
43        final InputMethodInfo clone = cloneViaParcel(imi);
44
45        assertThat(clone.equals(imi), is(true));
46        assertThat(clone.hashCode(), equalTo(imi.hashCode()));
47    }
48
49    @Test
50    public void testBooleanAttributes_DefaultValues() throws Exception {
51        final InputMethodInfo imi = buildInputMethodForTest(R.xml.ime_meta);
52
53        assertThat(imi.supportsSwitchingToNextInputMethod(), is(false));
54
55        final InputMethodInfo clone = cloneViaParcel(imi);
56
57        assertThat(clone.supportsSwitchingToNextInputMethod(), is(false));
58    }
59
60    @Test
61    public void testSupportsSwitchingToNextInputMethod() throws Exception {
62        final InputMethodInfo imi = buildInputMethodForTest(R.xml.ime_meta_sw_next);
63
64        assertThat(imi.supportsSwitchingToNextInputMethod(), is(true));
65
66        final InputMethodInfo clone = cloneViaParcel(imi);
67
68        assertThat(clone.supportsSwitchingToNextInputMethod(), is(true));
69    }
70
71    @Test
72    public void testIsVrOnly() throws Exception {
73        final InputMethodInfo imi = buildInputMethodForTest(R.xml.ime_meta_vr_only);
74
75        assertThat(imi.isVrOnly(), is(true));
76
77        final InputMethodInfo clone = cloneViaParcel(imi);
78
79        assertThat(clone.isVrOnly(), is(true));
80    }
81
82    private InputMethodInfo buildInputMethodForTest(final @XmlRes int metaDataRes)
83            throws Exception {
84        final Context context = InstrumentationRegistry.getContext();
85        final ServiceInfo serviceInfo = new ServiceInfo();
86        serviceInfo.applicationInfo = context.getApplicationInfo();
87        serviceInfo.packageName = context.getPackageName();
88        serviceInfo.name = "DummyImeForTest";
89        serviceInfo.metaData = new Bundle();
90        serviceInfo.metaData.putInt(InputMethod.SERVICE_META_DATA, metaDataRes);
91        final ResolveInfo resolveInfo = new ResolveInfo();
92        resolveInfo.serviceInfo = serviceInfo;
93        return new InputMethodInfo(context, resolveInfo, null /* additionalSubtypesMap */);
94    }
95
96    private InputMethodInfo cloneViaParcel(final InputMethodInfo original) {
97        Parcel parcel = null;
98        try {
99            parcel = Parcel.obtain();
100            original.writeToParcel(parcel, 0);
101            parcel.setDataPosition(0);
102            return InputMethodInfo.CREATOR.createFromParcel(parcel);
103        } finally {
104            if (parcel != null) {
105                parcel.recycle();
106            }
107        }
108    }
109}
110