Tadashi G. Takaoka | 9f240ad | 2017-08-31 13:08:56 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settingslib.inputmethod; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.ApplicationInfo; |
| 21 | import android.content.pm.ResolveInfo; |
| 22 | import android.content.pm.ServiceInfo; |
| 23 | import android.support.test.InstrumentationRegistry; |
| 24 | import android.support.test.filters.SmallTest; |
| 25 | import android.support.test.runner.AndroidJUnit4; |
| 26 | import android.view.inputmethod.InputMethodInfo; |
| 27 | import android.view.inputmethod.InputMethodSubtype; |
| 28 | |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | |
| 32 | import java.text.Collator; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Collections; |
| 35 | import java.util.List; |
| 36 | import java.util.Locale; |
| 37 | |
| 38 | @SmallTest |
| 39 | @RunWith(AndroidJUnit4.class) |
| 40 | public class InputMethodPreferenceTest { |
| 41 | |
| 42 | private static final Collator COLLATOR = Collator.getInstance(Locale.US); |
| 43 | |
| 44 | @Test |
| 45 | public void testComparableOrdering() throws Exception { |
| 46 | final List<InputMethodPreference> itemsInAscendingOrder = Arrays.asList( |
| 47 | createPreference("", true, "no_title-system"), |
| 48 | createPreference("E", true, "E-system"), |
| 49 | createPreference("Z", true, "Z-system"), |
| 50 | createPreference("", false, "no_title-non_system"), |
| 51 | createPreference("E", false, "E-non_system"), |
| 52 | createPreference("Z", false, "Z-non_system") |
| 53 | ); |
| 54 | ComparableUtils.assertAscendingOrdering( |
| 55 | itemsInAscendingOrder, |
| 56 | (x, y) -> x.compareTo(y, COLLATOR), |
| 57 | x -> x.getInputMethodInfo().getServiceName()); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void testComparableEquality() { |
| 62 | final List<InputMethodPreference> itemsInSameOrder1 = Arrays.asList( |
| 63 | createPreference("", true, "no_title-system-1"), |
| 64 | createPreference("", true, "no_title-system-2") |
| 65 | ); |
| 66 | ComparableUtils.assertSameOrdering( |
| 67 | itemsInSameOrder1, |
| 68 | (x, y) -> x.compareTo(y, COLLATOR), |
| 69 | x -> x.getInputMethodInfo().getServiceName()); |
| 70 | |
| 71 | final List<InputMethodPreference> itemsInSameOrder2 = Arrays.asList( |
| 72 | createPreference("A", false, "A-non_system-1"), |
| 73 | createPreference("A", false, "A-non_system-2") |
| 74 | ); |
| 75 | ComparableUtils.assertSameOrdering( |
| 76 | itemsInSameOrder2, |
| 77 | (x, y) -> x.compareTo(y, COLLATOR), |
| 78 | x -> x.getInputMethodInfo().getServiceName()); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testComparableContracts() { |
| 83 | final List<InputMethodPreference> items = Arrays.asList( |
| 84 | // itemsInAscendingOrder. |
| 85 | createPreference("", true, "no_title-system"), |
| 86 | createPreference("E", true, "E-system"), |
| 87 | createPreference("Z", true, "Z-system"), |
| 88 | createPreference("", false, "no_title-non_system"), |
| 89 | createPreference("E", false, "E-non_system"), |
| 90 | createPreference("Z", false, "Z-non_system"), |
| 91 | // itemsInSameOrder1. |
| 92 | createPreference("", true, "no_title-system-1"), |
| 93 | createPreference("", true, "no_title-system-2"), |
| 94 | // itemsInSameOrder2. |
| 95 | createPreference("A", false, "A-non_system-1"), |
| 96 | createPreference("A", false, "A-non_system-2") |
| 97 | ); |
| 98 | |
| 99 | ComparableUtils.assertComparableContracts( |
| 100 | items, |
| 101 | (x, y) -> x.compareTo(y, COLLATOR), |
| 102 | x -> x.getInputMethodInfo().getServiceName()); |
| 103 | } |
| 104 | |
| 105 | private static InputMethodPreference createPreference( |
| 106 | final CharSequence title, |
| 107 | final boolean systemIme, |
| 108 | final String name) { |
| 109 | return new InputMethodPreference( |
| 110 | InstrumentationRegistry.getTargetContext(), |
| 111 | createInputMethodInfo(systemIme, name), |
| 112 | title, |
| 113 | true /* isAllowedByOrganization */, |
| 114 | p -> {} /* onSavePreferenceListener */); |
| 115 | } |
| 116 | |
| 117 | private static InputMethodInfo createInputMethodInfo( |
| 118 | final boolean systemIme, final String name) { |
| 119 | final Context targetContext = InstrumentationRegistry.getTargetContext(); |
| 120 | final Locale systemLocale = targetContext |
| 121 | .getResources() |
| 122 | .getConfiguration() |
| 123 | .getLocales() |
| 124 | .get(0); |
| 125 | final InputMethodSubtype systemLocaleSubtype = |
| 126 | new InputMethodSubtype.InputMethodSubtypeBuilder() |
| 127 | .setIsAsciiCapable(true) |
Yohei Yukawa | 4faac7d | 2018-04-27 14:00:06 -0700 | [diff] [blame] | 128 | .setSubtypeMode("keyboard") |
Tadashi G. Takaoka | 9f240ad | 2017-08-31 13:08:56 -0700 | [diff] [blame] | 129 | .setSubtypeLocale(systemLocale.getLanguage()) |
| 130 | .build(); |
| 131 | |
| 132 | final ResolveInfo resolveInfo = new ResolveInfo(); |
| 133 | resolveInfo.serviceInfo = new ServiceInfo(); |
| 134 | resolveInfo.serviceInfo.packageName = "com.android.ime"; |
| 135 | resolveInfo.serviceInfo.name = name; |
| 136 | resolveInfo.serviceInfo.applicationInfo = new ApplicationInfo(); |
| 137 | resolveInfo.serviceInfo.applicationInfo.enabled = true; |
| 138 | if (systemIme) { |
| 139 | resolveInfo.serviceInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 140 | } else { |
| 141 | resolveInfo.serviceInfo.applicationInfo.flags &= ~ApplicationInfo.FLAG_SYSTEM; |
| 142 | } |
| 143 | return new InputMethodInfo( |
| 144 | resolveInfo, |
| 145 | false /* isAuxIme */, |
| 146 | "SettingsActivity", |
| 147 | Collections.singletonList(systemLocaleSubtype), |
| 148 | 0 /* isDefaultResId */, |
| 149 | true /* forceDefault */); |
| 150 | } |
| 151 | } |