From c5dc74067385f9ab6ec3086825d0b369c26a1ea3 Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Fri, 6 Jun 2014 12:11:35 +0900
Subject: [PATCH] Add action key test for Lxx theme

Change-Id: I849cec7deb5607e8ce8fb25c1f903f15328fb611
---
 .../inputmethod/keyboard/KeyboardTheme.java   | 45 +++++----
 ...KeyboardLayoutSetActionLabelKlpTests.java} | 38 ++++++--
 .../KeyboardLayoutSetActionLabelLxxTests.java | 97 +++++++++++++++++++
 .../KeyboardLayoutSetSubtypesCountTests.java  |  5 +
 .../keyboard/KeyboardLayoutSetTestsBase.java  | 11 +--
 .../layout/tests/LayoutTestsBase.java         |  6 ++
 6 files changed, 171 insertions(+), 31 deletions(-)
 rename tests/src/com/android/inputmethod/keyboard/{KeyboardLayoutSetActionLabelTests.java => KeyboardLayoutSetActionLabelKlpTests.java} (84%)
 create mode 100644 tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelLxxTests.java

diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java b/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
index 60d9a054d9..0ea9c742f7 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardTheme.java
@@ -21,46 +21,43 @@ import android.os.Build;
 import android.os.Build.VERSION_CODES;
 import android.util.Log;
 
+import com.android.inputmethod.annotations.UsedForTesting;
 import com.android.inputmethod.latin.R;
 
 import java.util.Arrays;
-import java.util.Comparator;
 
-public final class KeyboardTheme {
+public final class KeyboardTheme implements Comparable<KeyboardTheme> {
     private static final String TAG = KeyboardTheme.class.getSimpleName();
 
     static final String KLP_KEYBOARD_THEME_KEY = "pref_keyboard_layout_20110916";
     static final String LXX_KEYBOARD_THEME_KEY = "pref_keyboard_theme_20140509";
 
-    static final int THEME_ID_ICS = 0;
-    static final int THEME_ID_KLP = 2;
-    static final int THEME_ID_LXX_DARK = 3;
-    static final int DEFAULT_THEME_ID = THEME_ID_KLP;
+    public static final int THEME_ID_ICS = 0;
+    public static final int THEME_ID_KLP = 2;
+    public static final int THEME_ID_LXX_DARK = 3;
+    public static final int DEFAULT_THEME_ID = THEME_ID_KLP;
 
     private static final KeyboardTheme[] KEYBOARD_THEMES = {
         new KeyboardTheme(THEME_ID_ICS, R.style.KeyboardTheme_ICS,
+                // This has never been selected because we support ICS or later.
                 VERSION_CODES.BASE),
         new KeyboardTheme(THEME_ID_KLP, R.style.KeyboardTheme_KLP,
+                // Default theme for ICS, JB, and KLP.
                 VERSION_CODES.ICE_CREAM_SANDWICH),
         new KeyboardTheme(THEME_ID_LXX_DARK, R.style.KeyboardTheme_LXX_Dark,
+                // Default theme for LXX.
                 // TODO: Update this constant once the *next* version becomes available.
                 VERSION_CODES.CUR_DEVELOPMENT),
     };
+
     static {
         // Sort {@link #KEYBOARD_THEME} by descending order of {@link #mMinApiVersion}.
-        Arrays.sort(KEYBOARD_THEMES, new Comparator<KeyboardTheme>() {
-            @Override
-            public int compare(final KeyboardTheme lhs, final KeyboardTheme rhs) {
-                if (lhs.mMinApiVersion > rhs.mMinApiVersion) return -1;
-                if (lhs.mMinApiVersion < rhs.mMinApiVersion) return 1;
-                return 0;
-            }
-        });
+        Arrays.sort(KEYBOARD_THEMES);
     }
 
     public final int mThemeId;
     public final int mStyleId;
-    final int mMinApiVersion;
+    private final int mMinApiVersion;
 
     // Note: The themeId should be aligned with "themeId" attribute of Keyboard style
     // in values/themes-<style>.xml.
@@ -70,6 +67,13 @@ public final class KeyboardTheme {
         mMinApiVersion = minApiVersion;
     }
 
+    @Override
+    public int compareTo(final KeyboardTheme rhs) {
+        if (mMinApiVersion > rhs.mMinApiVersion) return -1;
+        if (mMinApiVersion < rhs.mMinApiVersion) return 1;
+        return 0;
+    }
+
     @Override
     public boolean equals(final Object o) {
         if (o == this) return true;
@@ -81,7 +85,8 @@ public final class KeyboardTheme {
         return mThemeId;
     }
 
-    private static KeyboardTheme searchKeyboardThemeById(final int themeId) {
+    @UsedForTesting
+    static KeyboardTheme searchKeyboardThemeById(final int themeId) {
         // TODO: This search algorithm isn't optimal if there are many themes.
         for (final KeyboardTheme theme : KEYBOARD_THEMES) {
             if (theme.mThemeId == themeId) {
@@ -100,6 +105,7 @@ public final class KeyboardTheme {
         return sdkVersion;
     }
 
+    @UsedForTesting
     static KeyboardTheme getDefaultKeyboardTheme(final SharedPreferences prefs,
             final int sdkVersion) {
         final String klpThemeIdString = prefs.getString(KLP_KEYBOARD_THEME_KEY, null);
@@ -134,6 +140,7 @@ public final class KeyboardTheme {
         saveKeyboardThemeId(themeIdString, prefs, getSdkVersion());
     }
 
+    @UsedForTesting
     static String getPreferenceKey(final int sdkVersion) {
         if (sdkVersion <= VERSION_CODES.KITKAT) {
             return KLP_KEYBOARD_THEME_KEY;
@@ -141,8 +148,9 @@ public final class KeyboardTheme {
         return LXX_KEYBOARD_THEME_KEY;
     }
 
-    static void saveKeyboardThemeId(final String themeIdString, final SharedPreferences prefs,
-            final int sdkVersion) {
+    @UsedForTesting
+    static void saveKeyboardThemeId(final String themeIdString,
+            final SharedPreferences prefs, final int sdkVersion) {
         final String prefKey = getPreferenceKey(sdkVersion);
         prefs.edit().putString(prefKey, themeIdString).apply();
     }
@@ -151,6 +159,7 @@ public final class KeyboardTheme {
         return getKeyboardTheme(prefs, getSdkVersion());
     }
 
+    @UsedForTesting
     static KeyboardTheme getKeyboardTheme(final SharedPreferences prefs, final int sdkVersion) {
         final String lxxThemeIdString = prefs.getString(LXX_KEYBOARD_THEME_KEY, null);
         if (lxxThemeIdString == null) {
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelTests.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelKlpTests.java
similarity index 84%
rename from tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelTests.java
rename to tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelKlpTests.java
index 06139b808f..96f9255543 100644
--- a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelTests.java
+++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelKlpTests.java
@@ -29,9 +29,14 @@ import com.android.inputmethod.latin.utils.RunInLocale;
 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
 
 @MediumTest
-public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTestsBase {
+public class KeyboardLayoutSetActionLabelKlpTests extends KeyboardLayoutSetTestsBase {
+    @Override
+    protected int getKeyboardThemeForTests() {
+        return KeyboardTheme.THEME_ID_KLP;
+    }
+
     private static void doTestActionKey(final String tag, final KeyboardLayoutSet layoutSet,
-            final int elementId, final String label, final int iconId) {
+            final int elementId, final CharSequence label, final int iconId) {
         final Keyboard keyboard = layoutSet.getKeyboard(elementId);
         final Key enterKey = keyboard.getKey(Constants.CODE_ENTER);
         assertNotNull(tag + " enter key on " + keyboard.mId, enterKey);
@@ -39,7 +44,7 @@ public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTe
         assertEquals(tag + " enter icon " + enterKey, iconId, enterKey.getIconId());
     }
 
-    private void doTestActionLabel(final String tag, final InputMethodSubtype subtype,
+    protected void doTestActionLabel(final String tag, final InputMethodSubtype subtype,
             final int actionId, final int labelResId) {
         final EditorInfo editorInfo = new EditorInfo();
         editorInfo.imeOptions = actionId;
@@ -57,6 +62,11 @@ public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTe
         } else {
             label = job.runInLocale(res, SubtypeLocaleUtils.getSubtypeLocale(subtype));
         }
+        doTestActionLabel(tag, subtype, editorInfo, label);
+    }
+
+    protected void doTestActionLabel(final String tag, final InputMethodSubtype subtype,
+            final EditorInfo editorInfo, final CharSequence label) {
         // Test text layouts.
         editorInfo.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
         final KeyboardLayoutSet layoutSet = createKeyboardLayoutSet(subtype, editorInfo);
@@ -82,7 +92,7 @@ public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTe
                 label, KeyboardIconsSet.ICON_UNDEFINED);
     }
 
-    private void doTestActionKeyIcon(final String tag, final InputMethodSubtype subtype,
+    protected void doTestActionKeyIcon(final String tag, final InputMethodSubtype subtype,
             final int actionId, final String iconName) {
         final int iconId = KeyboardIconsSet.getIconId(iconName);
         final EditorInfo editorInfo = new EditorInfo();
@@ -111,14 +121,16 @@ public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTe
         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
             final String tag = "unspecifiled "
                     + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
-            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_UNSPECIFIED, "enter_key");
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_UNSPECIFIED,
+                    KeyboardIconsSet.NAME_ENTER_KEY);
         }
     }
 
     public void testActionNone() {
         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
             final String tag = "none " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
-            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_NONE, "enter_key");
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_NONE,
+                    KeyboardIconsSet.NAME_ENTER_KEY);
         }
     }
 
@@ -132,7 +144,8 @@ public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTe
     public void testActionSearch() {
         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
             final String tag = "search " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
-            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_SEARCH, "search_key");
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_SEARCH,
+                    KeyboardIconsSet.NAME_SEARCH_KEY);
         }
     }
 
@@ -164,4 +177,15 @@ public final class KeyboardLayoutSetActionLabelTests extends KeyboardLayoutSetTe
                     tag, subtype, EditorInfo.IME_ACTION_PREVIOUS, R.string.label_previous_key);
         }
     }
+
+    public void testActionCustom() {
+        for (final InputMethodSubtype subtype : getAllSubtypesList()) {
+            final String tag = "custom " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
+            final CharSequence customLabel = "customLabel";
+            final EditorInfo editorInfo = new EditorInfo();
+            editorInfo.imeOptions = EditorInfo.IME_ACTION_UNSPECIFIED;
+            editorInfo.actionLabel = customLabel;
+            doTestActionLabel(tag, subtype, editorInfo, customLabel);
+        }
+    }
 }
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelLxxTests.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelLxxTests.java
new file mode 100644
index 0000000000..7747ac5f9f
--- /dev/null
+++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetActionLabelLxxTests.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.inputmethod.keyboard;
+
+import android.test.suitebuilder.annotation.MediumTest;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodSubtype;
+
+import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
+import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
+
+@MediumTest
+public class KeyboardLayoutSetActionLabelLxxTests extends KeyboardLayoutSetActionLabelKlpTests {
+    @Override
+    protected int getKeyboardThemeForTests() {
+        return KeyboardTheme.THEME_ID_LXX_DARK;
+    }
+
+    @Override
+    public void testActionUnspecified() {
+        super.testActionUnspecified();
+    }
+
+    @Override
+    public void testActionNone() {
+        super.testActionNone();
+    }
+
+    @Override
+    public void testActionGo() {
+        for (final InputMethodSubtype subtype : getAllSubtypesList()) {
+            final String tag = "go " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_GO,
+                    KeyboardIconsSet.NAME_GO_KEY);
+        }
+    }
+
+    @Override
+    public void testActionSearch() {
+        super.testActionSearch();
+    }
+
+    @Override
+    public void testActionSend() {
+        for (final InputMethodSubtype subtype : getAllSubtypesList()) {
+            final String tag = "send " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_SEND,
+                    KeyboardIconsSet.NAME_SEND_KEY);
+        }
+    }
+
+    @Override
+    public void testActionNext() {
+        for (final InputMethodSubtype subtype : getAllSubtypesList()) {
+            final String tag = "next " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_NEXT,
+                    KeyboardIconsSet.NAME_NEXT_KEY);
+        }
+    }
+
+    @Override
+    public void testActionDone() {
+        for (final InputMethodSubtype subtype : getAllSubtypesList()) {
+            final String tag = "done " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_DONE,
+                    KeyboardIconsSet.NAME_DONE_KEY);
+        }
+    }
+
+    @Override
+    public void testActionPrevious() {
+        for (final InputMethodSubtype subtype : getAllSubtypesList()) {
+            final String tag = "previous " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
+            doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_PREVIOUS,
+                    KeyboardIconsSet.NAME_PREVIOUS_KEY);
+        }
+    }
+
+    @Override
+    public void testActionCustom() {
+        super.testActionCustom();
+    }
+}
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java
index 26402f8c76..bf19a8b460 100644
--- a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java
+++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java
@@ -29,6 +29,11 @@ public class KeyboardLayoutSetSubtypesCountTests extends KeyboardLayoutSetTestsB
     private static final int NUMBER_OF_ASCII_CAPABLE_SUBTYPES = 45;
     private static final int NUMBER_OF_PREDEFINED_ADDITIONAL_SUBTYPES = 2;
 
+    @Override
+    protected int getKeyboardThemeForTests() {
+        return KeyboardTheme.THEME_ID_KLP;
+    }
+
     private static String toString(final ArrayList<InputMethodSubtype> subtypeList) {
         final StringBuilder sb = new StringBuilder();
         for (int index = 0; index < subtypeList.size(); index++) {
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java
index 1ccb58c0f8..15e2647c65 100644
--- a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java
+++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetTestsBase.java
@@ -18,9 +18,7 @@ package com.android.inputmethod.keyboard;
 
 import android.content.Context;
 import android.content.res.Resources;
-import android.preference.PreferenceManager;
 import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
 import android.view.ContextThemeWrapper;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputMethodInfo;
@@ -38,8 +36,7 @@ import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
 import java.util.ArrayList;
 import java.util.Locale;
 
-@SmallTest
-public class KeyboardLayoutSetTestsBase extends AndroidTestCase {
+public abstract class KeyboardLayoutSetTestsBase extends AndroidTestCase {
     // All input method subtypes of LatinIME.
     private final ArrayList<InputMethodSubtype> mAllSubtypesList = new ArrayList<>();
     private final ArrayList<InputMethodSubtype> mAsciiCapableSubtypesList = new ArrayList<>();
@@ -48,13 +45,15 @@ public class KeyboardLayoutSetTestsBase extends AndroidTestCase {
     private Context mThemeContext;
     private int mScreenMetrics;
 
+    protected abstract int getKeyboardThemeForTests();
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         mScreenMetrics = mContext.getResources().getInteger(R.integer.config_screen_metrics);
 
-        final KeyboardTheme keyboardTheme = KeyboardTheme.getKeyboardTheme(
-                PreferenceManager.getDefaultSharedPreferences(mContext));
+        final KeyboardTheme keyboardTheme = KeyboardTheme.searchKeyboardThemeById(
+                getKeyboardThemeForTests());
         mThemeContext = new ContextThemeWrapper(mContext, keyboardTheme.mStyleId);
         RichInputMethodManager.init(mThemeContext);
         final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java b/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
index d4e8fb647d..0d90055ec3 100644
--- a/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
+++ b/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
@@ -24,6 +24,7 @@ import com.android.inputmethod.keyboard.Keyboard;
 import com.android.inputmethod.keyboard.KeyboardId;
 import com.android.inputmethod.keyboard.KeyboardLayoutSet;
 import com.android.inputmethod.keyboard.KeyboardLayoutSetTestsBase;
+import com.android.inputmethod.keyboard.KeyboardTheme;
 import com.android.inputmethod.keyboard.layout.LayoutBase;
 import com.android.inputmethod.keyboard.layout.expected.AbstractLayoutBase;
 import com.android.inputmethod.keyboard.layout.expected.ActualKeyboardBuilder;
@@ -57,6 +58,11 @@ abstract class LayoutTestsBase extends KeyboardLayoutSetTestsBase {
                 true /* isLanguageSwitchKeyEnabled */);
     }
 
+    @Override
+    protected int getKeyboardThemeForTests() {
+        return KeyboardTheme.THEME_ID_KLP;
+    }
+
     // Those helper methods have a lower case name to be readable when defining expected keyboard
     // layouts.
 
-- 
GitLab