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 
17 package com.android.inputmethod.keyboard.internal;
18 
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 
22 import android.content.Context;
23 import android.view.inputmethod.InputMethodInfo;
24 import android.view.inputmethod.InputMethodSubtype;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.filters.SmallTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.inputmethod.latin.RichInputMethodManager;
31 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Locale;
41 
42 @SmallTest
43 @RunWith(AndroidJUnit4.class)
44 public final class KeyboardTextsSetTests {
45     // All input method subtypes of LatinIME.
46     private List<InputMethodSubtype> mAllSubtypesList;
47 
getContext()48     private Context getContext() {
49         return InstrumentationRegistry.getTargetContext();
50     }
51 
52     @Before
setUp()53     public void setUp() throws Exception {
54         RichInputMethodManager.init(getContext());
55         final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
56 
57         final ArrayList<InputMethodSubtype> allSubtypesList = new ArrayList<>();
58         final InputMethodInfo imi = richImm.getInputMethodInfoOfThisIme();
59         final int subtypeCount = imi.getSubtypeCount();
60         for (int index = 0; index < subtypeCount; index++) {
61             final InputMethodSubtype subtype = imi.getSubtypeAt(index);
62             allSubtypesList.add(subtype);
63         }
64         mAllSubtypesList = Collections.unmodifiableList(allSubtypesList);
65     }
66 
67     // Test that the text {@link KeyboardTextsSet#SWITCH_TO_ALPHA_KEY_LABEL} exists for all
68     // subtypes. The text is needed to implement Emoji Keyboard, see
69     // {@link KeyboardSwitcher#setEmojiKeyboard()}.
70     @Test
testSwitchToAlphaKeyLabel()71     public void testSwitchToAlphaKeyLabel() {
72         final Context context = getContext();
73         final KeyboardTextsSet textsSet = new KeyboardTextsSet();
74         for (final InputMethodSubtype subtype : mAllSubtypesList) {
75             final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
76             textsSet.setLocale(locale, context);
77             final String switchToAlphaKeyLabel = textsSet.getText(
78                     KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL);
79             assertNotNull("Switch to alpha key label of " + locale, switchToAlphaKeyLabel);
80             assertFalse("Switch to alpha key label of " + locale, switchToAlphaKeyLabel.isEmpty());
81         }
82     }
83 
84     private static final String[] TEXT_NAMES_FROM_RESOURCE = {
85         // Labels for action.
86         "label_go_key",
87         "label_send_key",
88         "label_next_key",
89         "label_done_key",
90         "label_previous_key",
91         // Other labels.
92         "label_pause_key",
93         "label_wait_key",
94     };
95 
96     // Test that the text from resources are correctly loaded for all subtypes.
97     @Test
testTextFromResources()98     public void testTextFromResources() {
99         final Context context = getContext();
100         final KeyboardTextsSet textsSet = new KeyboardTextsSet();
101         for (final InputMethodSubtype subtype : mAllSubtypesList) {
102             final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
103             textsSet.setLocale(locale, context);
104             for (final String name : TEXT_NAMES_FROM_RESOURCE) {
105                 final String text = textsSet.getText(name);
106                 assertNotNull(name + " of " + locale, text);
107                 assertFalse(name + " of " + locale, text.isEmpty());
108             }
109         }
110     }
111 }
112