blob: d203465577dafe32d23f335c6840db12db82d56f [file] [log] [blame]
Yohei Yukawa638e4782014-07-11 19:41:07 +09001/*
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
Yohei Yukawa35f74372015-08-12 20:14:08 -070017package android.view.inputmethod;
Yohei Yukawa638e4782014-07-11 19:41:07 +090018
Yohei Yukawa35f74372015-08-12 20:14:08 -070019import android.os.Parcel;
Yohei Yukawa638e4782014-07-11 19:41:07 +090020import android.test.InstrumentationTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
Yohei Yukawa638e4782014-07-11 19:41:07 +090022import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
23
Tony Makafcd19c2017-08-07 15:15:57 +010024import java.util.Locale;
Yohei Yukawaa52aeda2014-07-11 20:42:10 +090025import java.util.Objects;
26
Yohei Yukawa638e4782014-07-11 19:41:07 +090027public class InputMethodSubtypeTest extends InstrumentationTestCase {
Yohei Yukawaa52aeda2014-07-11 20:42:10 +090028
29 public void verifyLocale(final String localeString) {
30 // InputMethodSubtype#getLocale() returns exactly the same string that is passed to the
31 // constructor.
32 assertEquals(localeString, createDummySubtype(localeString).getLocale());
33
34 // InputMethodSubtype#getLocale() should be preserved via marshaling.
35 assertEquals(createDummySubtype(localeString).getLocale(),
36 cloneViaParcel(createDummySubtype(localeString)).getLocale());
37
38 // InputMethodSubtype#getLocale() should be preserved via marshaling.
39 assertEquals(createDummySubtype(localeString).getLocale(),
40 cloneViaParcel(cloneViaParcel(createDummySubtype(localeString))).getLocale());
41
42 // Make sure InputMethodSubtype#hashCode() returns the same hash code.
43 assertEquals(createDummySubtype(localeString).hashCode(),
44 createDummySubtype(localeString).hashCode());
45 assertEquals(createDummySubtype(localeString).hashCode(),
46 cloneViaParcel(createDummySubtype(localeString)).hashCode());
47 assertEquals(createDummySubtype(localeString).hashCode(),
48 cloneViaParcel(cloneViaParcel(createDummySubtype(localeString))).hashCode());
Yohei Yukawa638e4782014-07-11 19:41:07 +090049 }
50
51 @SmallTest
Tony Makafcd19c2017-08-07 15:15:57 +010052 public void testLocaleObj_locale() {
53 final InputMethodSubtype usSubtype = createDummySubtype("en_US");
54 Locale localeObject = usSubtype.getLocaleObject();
55 assertEquals("en", localeObject.getLanguage());
56 assertEquals("US", localeObject.getCountry());
57
58 // The locale object should be cached.
59 assertTrue(localeObject == usSubtype.getLocaleObject());
60 }
61
62 @SmallTest
63 public void testLocaleObj_languageTag() {
64 final InputMethodSubtype usSubtype = createDummySubtypeUsingLanguageTag("en-US");
65 Locale localeObject = usSubtype.getLocaleObject();
66 assertNotNull(localeObject);
67 assertEquals("en", localeObject.getLanguage());
68 assertEquals("US", localeObject.getCountry());
69
70 // The locale object should be cached.
71 assertTrue(localeObject == usSubtype.getLocaleObject());
72 }
73
74 @SmallTest
75 public void testLocaleObj_emptyLocale() {
76 final InputMethodSubtype emptyLocaleSubtype = createDummySubtype("");
77 assertNull(emptyLocaleSubtype.getLocaleObject());
78 // It should continue returning null when called multiple times.
79 assertNull(emptyLocaleSubtype.getLocaleObject());
80 assertNull(emptyLocaleSubtype.getLocaleObject());
81 }
82
83 @SmallTest
Yohei Yukawaa52aeda2014-07-11 20:42:10 +090084 public void testLocaleString() throws Exception {
85 // The locale string in InputMethodSubtype has accepted an arbitrary text actually,
86 // regardless of the validity of the text as a locale string.
87 verifyLocale("en_US");
88 verifyLocale("apparently invalid locale string");
89 verifyLocale("zz");
90 verifyLocale("iw");
91 verifyLocale("he");
Yohei Yukawa92280cd2015-06-02 16:50:14 -070092 verifyLocale("tl");
93 verifyLocale("tl_PH");
94 verifyLocale("fil");
95 verifyLocale("fil_PH");
Yohei Yukawaa52aeda2014-07-11 20:42:10 +090096 }
97
98 @SmallTest
99 public void testDeprecatedLocaleString() throws Exception {
100 // Make sure "iw" is not automatically replaced with "he".
101 final InputMethodSubtype subtypeIw = createDummySubtype("iw");
102 final InputMethodSubtype subtypeHe = createDummySubtype("he");
103 assertEquals("iw", subtypeIw.getLocale());
104 assertEquals("he", subtypeHe.getLocale());
105 assertFalse(Objects.equals(subtypeIw, subtypeHe));
106 assertFalse(Objects.equals(subtypeIw.hashCode(), subtypeHe.hashCode()));
107
108 final InputMethodSubtype clonedSubtypeIw = cloneViaParcel(subtypeIw);
109 final InputMethodSubtype clonedSubtypeHe = cloneViaParcel(subtypeHe);
110 assertEquals(subtypeIw, clonedSubtypeIw);
111 assertEquals(subtypeHe, clonedSubtypeHe);
112 assertEquals("iw", clonedSubtypeIw.getLocale());
113 assertEquals("he", clonedSubtypeHe.getLocale());
Yohei Yukawa638e4782014-07-11 19:41:07 +0900114 }
115
116 private static final InputMethodSubtype cloneViaParcel(final InputMethodSubtype original) {
117 Parcel parcel = null;
118 try {
119 parcel = Parcel.obtain();
120 original.writeToParcel(parcel, 0);
121 parcel.setDataPosition(0);
122 return InputMethodSubtype.CREATOR.createFromParcel(parcel);
123 } finally {
124 if (parcel != null) {
125 parcel.recycle();
126 }
127 }
128 }
129
Tony Makafcd19c2017-08-07 15:15:57 +0100130 private static InputMethodSubtype createDummySubtype(final String locale) {
Yohei Yukawa638e4782014-07-11 19:41:07 +0900131 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder();
132 return builder.setSubtypeNameResId(0)
133 .setSubtypeIconResId(0)
134 .setSubtypeLocale(locale)
135 .setIsAsciiCapable(true)
136 .build();
137 }
Tony Makafcd19c2017-08-07 15:15:57 +0100138
139 private static InputMethodSubtype createDummySubtypeUsingLanguageTag(
140 final String languageTag) {
141 final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder();
142 return builder.setSubtypeNameResId(0)
143 .setSubtypeIconResId(0)
144 .setLanguageTag(languageTag)
145 .setIsAsciiCapable(true)
146 .build();
147 }
148}