Yohei Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 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 | |
Yohei Yukawa | 35f7437 | 2015-08-12 20:14:08 -0700 | [diff] [blame] | 17 | package android.view.inputmethod; |
Yohei Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 18 | |
Yohei Yukawa | 35f7437 | 2015-08-12 20:14:08 -0700 | [diff] [blame] | 19 | import android.os.Parcel; |
Yohei Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 20 | import android.test.InstrumentationTestCase; |
| 21 | import android.test.suitebuilder.annotation.SmallTest; |
Yohei Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 22 | import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder; |
| 23 | |
Tony Mak | afcd19c | 2017-08-07 15:15:57 +0100 | [diff] [blame] | 24 | import java.util.Locale; |
Yohei Yukawa | a52aeda | 2014-07-11 20:42:10 +0900 | [diff] [blame] | 25 | import java.util.Objects; |
| 26 | |
Yohei Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 27 | public class InputMethodSubtypeTest extends InstrumentationTestCase { |
Yohei Yukawa | a52aeda | 2014-07-11 20:42:10 +0900 | [diff] [blame] | 28 | |
| 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 Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @SmallTest |
Tony Mak | afcd19c | 2017-08-07 15:15:57 +0100 | [diff] [blame] | 52 | 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 Yukawa | a52aeda | 2014-07-11 20:42:10 +0900 | [diff] [blame] | 84 | 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 Yukawa | 92280cd | 2015-06-02 16:50:14 -0700 | [diff] [blame] | 92 | verifyLocale("tl"); |
| 93 | verifyLocale("tl_PH"); |
| 94 | verifyLocale("fil"); |
| 95 | verifyLocale("fil_PH"); |
Yohei Yukawa | a52aeda | 2014-07-11 20:42:10 +0900 | [diff] [blame] | 96 | } |
| 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 Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 114 | } |
| 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 Mak | afcd19c | 2017-08-07 15:15:57 +0100 | [diff] [blame] | 130 | private static InputMethodSubtype createDummySubtype(final String locale) { |
Yohei Yukawa | 638e478 | 2014-07-11 19:41:07 +0900 | [diff] [blame] | 131 | final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder(); |
| 132 | return builder.setSubtypeNameResId(0) |
| 133 | .setSubtypeIconResId(0) |
| 134 | .setSubtypeLocale(locale) |
| 135 | .setIsAsciiCapable(true) |
| 136 | .build(); |
| 137 | } |
Tony Mak | afcd19c | 2017-08-07 15:15:57 +0100 | [diff] [blame] | 138 | |
| 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 | } |