Add en_US keyboard layout test

Bug: 13017434
Change-Id: I3fb343721221ecae210bffcb3e83ce4f2df0d0ff
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java b/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
new file mode 100644
index 0000000..427e7de
--- /dev/null
+++ b/tests/src/com/android/inputmethod/keyboard/layout/tests/LayoutTestsBase.java
@@ -0,0 +1,197 @@
+/*
+ * 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.layout.tests;
+
+import android.util.Log;
+import android.view.inputmethod.InputMethodSubtype;
+
+import com.android.inputmethod.keyboard.Key;
+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.layout.AlphabetShifted;
+import com.android.inputmethod.keyboard.layout.Symbols;
+import com.android.inputmethod.keyboard.layout.SymbolsShifted;
+import com.android.inputmethod.keyboard.layout.expected.ActualKeyboardBuilder;
+import com.android.inputmethod.keyboard.layout.expected.ExpectedKey;
+import com.android.inputmethod.keyboard.layout.expected.ExpectedKeyboardBuilder;
+import com.android.inputmethod.keyboard.layout.expected.LayoutBase;
+import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
+
+import java.util.Arrays;
+import java.util.Locale;
+
+/**
+ * Base class for keyboard layout unit test.
+ */
+abstract class LayoutTestsBase extends KeyboardLayoutSetTestsBase {
+    private InputMethodSubtype mSubtype;
+    private String mLogTag;
+    private KeyboardLayoutSet mKeyboardLayoutSet;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mSubtype = getSubtype(getTestLocale(), getTestKeyboardLayout());
+        mLogTag = SubtypeLocaleUtils.getSubtypeNameForLogging(mSubtype) + "/"
+                + (isPhone() ? "phone" : "tablet");
+        mKeyboardLayoutSet = createKeyboardLayoutSet(mSubtype, null /* editorInfo */);
+    }
+
+    // Those helper methods have a lower case name to be readable when defining expected keyboard
+    // layouts.
+
+    // Helper method to create {@link ExpectedKey} object that has the label.
+    static ExpectedKey key(final String label, final ExpectedKey ... moreKeys) {
+        return LayoutBase.key(label, moreKeys);
+    }
+
+    // Helper method to create {@link ExpectedKey} object that has the label and the output text.
+    static ExpectedKey key(final String label, final String outputText,
+            final ExpectedKey ... moreKeys) {
+        return LayoutBase.key(label, outputText, moreKeys);
+    }
+
+    // Helper method to create {@link ExpectedKey} object for "more key" that has the label.
+    static ExpectedKey moreKey(final String label) {
+        return LayoutBase.moreKey(label);
+    }
+
+    // Helper method to create {@link ExpectedKey} object for "more key" that has the label and the
+    // output text.
+    static ExpectedKey moreKey(final String label, final String outputText) {
+        return LayoutBase.moreKey(label, outputText);
+    }
+
+    // Locale for testing subtype.
+    abstract Locale getTestLocale();
+
+    // Keyboard layout name for testing subtype.
+    abstract String getTestKeyboardLayout();
+
+    // Alphabet keyboard for testing subtype.
+    abstract ExpectedKey[][] getAlphabet(final boolean isPhone);
+
+    // Alphabet automatic shifted keyboard for testing subtype.
+    ExpectedKey[][] getAlphabetAutomaticShifted(final boolean isPhone) {
+        return AlphabetShifted.getAlphabet(getAlphabet(isPhone), getTestLocale());
+    }
+
+    // Alphabet manual shifted  keyboard for testing subtype.
+    ExpectedKey[][] getAlphabetManualShifted(final boolean isPhone) {
+        return AlphabetShifted.getAlphabet(getAlphabet(isPhone), getTestLocale());
+    }
+
+    // Alphabet shift locked keyboard for testing subtype.
+    ExpectedKey[][] getAlphabetShiftLocked(final boolean isPhone) {
+        return AlphabetShifted.getAlphabet(getAlphabet(isPhone), getTestLocale());
+    }
+
+    // Alphabet shift lock shifted keyboard for testing subtype.
+    ExpectedKey[][] getAlphabetShiftLockShifted(final boolean isPhone) {
+        return AlphabetShifted.getAlphabet(getAlphabet(isPhone), getTestLocale());
+    }
+
+    // Symbols keyboard for testing subtype.
+    ExpectedKey[][] getSymbols(final boolean isPhone) {
+        return Symbols.getSymbols(isPhone);
+    }
+
+    // Symbols shifted keyboard for testing subtype.
+    ExpectedKey[][] getSymbolsShifted(final boolean isPhone) {
+        return SymbolsShifted.getSymbolsShifted(isPhone);
+    }
+
+    // TODO: Add phone, phone symbols, number, number password layout tests.
+
+    public final void testAlphabet() {
+        final int elementId = KeyboardId.ELEMENT_ALPHABET;
+        doKeyboardTests(elementId, getAlphabet(isPhone()));
+    }
+
+    public final void testAlphabetAutomaticShifted() {
+        final int elementId = KeyboardId.ELEMENT_ALPHABET_AUTOMATIC_SHIFTED;
+        doKeyboardTests(elementId, getAlphabetAutomaticShifted(isPhone()));
+    }
+
+    public final void testAlphabetManualShifted() {
+        final int elementId = KeyboardId.ELEMENT_ALPHABET_MANUAL_SHIFTED;
+        doKeyboardTests(elementId, getAlphabetManualShifted(isPhone()));
+    }
+
+    public final void testAlphabetShiftLocked() {
+        final int elementId = KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCKED;
+        doKeyboardTests(elementId, getAlphabetShiftLocked(isPhone()));
+    }
+
+    public final void testAlphabetShiftLockShifted() {
+        final int elementId = KeyboardId.ELEMENT_ALPHABET_SHIFT_LOCK_SHIFTED;
+        doKeyboardTests(elementId, getAlphabetShiftLockShifted(isPhone()));
+    }
+
+    public final void testSymbols() {
+        final int elementId = KeyboardId.ELEMENT_SYMBOLS;
+        doKeyboardTests(elementId, getSymbols(isPhone()));
+    }
+
+    public final void testSymbolsShifted() {
+        final int elementId = KeyboardId.ELEMENT_SYMBOLS_SHIFTED;
+        doKeyboardTests(elementId, getSymbolsShifted(isPhone()));
+    }
+
+    // Comparing expected keyboard and actual keyboard.
+    private void doKeyboardTests(final int elementId, final ExpectedKey[][] expectedKeyboard) {
+        // Skip test if no keyboard is defined.
+        if (expectedKeyboard == null) {
+            return;
+        }
+        final String tag = mLogTag + "/" + KeyboardId.elementIdToName(elementId);
+        // Create actual keyboard object.
+        final Keyboard keyboard = mKeyboardLayoutSet.getKeyboard(elementId);
+        // Create actual keyboard to be compared with the expected keyboard.
+        final Key[][] actualKeyboard = ActualKeyboardBuilder.buildKeyboard(keyboard.getKeys());
+
+        // Dump human readable definition of expected/actual keyboards.
+        Log.d(tag, "expected=\n" + ExpectedKeyboardBuilder.toString(expectedKeyboard));
+        Log.d(tag, "actual  =\n" + ActualKeyboardBuilder.toString(actualKeyboard));
+        // Test both keyboards have the same number of rows.
+        assertEquals(tag + " labels"
+                + "\nexpected=" + Arrays.deepToString(expectedKeyboard)
+                + "\nactual  =" + ActualKeyboardBuilder.toString(actualKeyboard),
+                expectedKeyboard.length, actualKeyboard.length);
+        for (int r = 0; r < actualKeyboard.length; r++) {
+            final int row = r + 1;
+            // Test both keyboards' rows have the same number of columns.
+            assertEquals(tag + " labels row=" + row
+                    + "\nexpected=" + Arrays.toString(expectedKeyboard[r])
+                    + "\nactual  =" + ActualKeyboardBuilder.toString(actualKeyboard[r]),
+                    expectedKeyboard[r].length, actualKeyboard[r].length);
+            for (int c = 0; c < actualKeyboard[r].length; c++) {
+                final int column = c + 1;
+                final Key actualKey = actualKeyboard[r][c];
+                final ExpectedKey expectedKey = expectedKeyboard[r][c];
+                // Test both keyboards' keys have the same visual outlook and key output.
+                assertTrue(tag + " labels row,column=" + row + "," + column
+                        + "\nexpected=" + expectedKey
+                        + "\nactual  =" + ActualKeyboardBuilder.toString(actualKey),
+                        expectedKey.equalsTo(actualKey));
+            }
+        }
+    }
+}
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/tests/TestsEnglishUS.java b/tests/src/com/android/inputmethod/keyboard/layout/tests/TestsEnglishUS.java
new file mode 100644
index 0000000..0792a57
--- /dev/null
+++ b/tests/src/com/android/inputmethod/keyboard/layout/tests/TestsEnglishUS.java
@@ -0,0 +1,98 @@
+/*
+ * 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.layout.tests;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.inputmethod.keyboard.layout.Qwerty;
+import com.android.inputmethod.keyboard.layout.expected.ExpectedKey;
+import com.android.inputmethod.keyboard.layout.expected.ExpectedKeyboardBuilder;
+
+import java.util.Locale;
+
+/**
+ * en_US: English (United States)/qwerty
+ */
+@SmallTest
+public final class TestsEnglishUS extends LayoutTestsBase {
+    @Override
+    Locale getTestLocale() {
+        return new Locale("en", "US");
+    }
+
+    @Override
+    String getTestKeyboardLayout() {
+        return "qwerty";
+    }
+
+    @Override
+    ExpectedKey[][] getAlphabet(final boolean isPhone) {
+        final ExpectedKey[][] keyboard = Qwerty.getAlphabet(isPhone);
+        final ExpectedKeyboardBuilder builder = new ExpectedKeyboardBuilder(keyboard);
+        setAccentedLetters(builder);
+        return builder.build();
+    }
+
+    static ExpectedKeyboardBuilder setAccentedLetters(final ExpectedKeyboardBuilder builder) {
+        return builder
+                // U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE
+                // U+00E9: "é" LATIN SMALL LETTER E WITH ACUTE
+                // U+00EA: "ê" LATIN SMALL LETTER E WITH CIRCUMFLEX
+                // U+00EB: "ë" LATIN SMALL LETTER E WITH DIAERESIS
+                // U+0113: "ē" LATIN SMALL LETTER E WITH MACRON
+                .setMoreKeysOf("e", "3", "\u00E8", "\u00E9", "\u00EA", "\u00EB", "\u0113")
+                // U+00FB: "û" LATIN SMALL LETTER U WITH CIRCUMFLEX
+                // U+00FC: "ü" LATIN SMALL LETTER U WITH DIAERESIS
+                // U+00F9: "ù" LATIN SMALL LETTER U WITH GRAVE
+                // U+00FA: "ú" LATIN SMALL LETTER U WITH ACUTE
+                // U+016B: "ū" LATIN SMALL LETTER U WITH MACRON
+                .setMoreKeysOf("u", "7", "\u00FB", "\u00FC", "\u00F9", "\u00FA", "\u016B")
+                // U+00EE: "î" LATIN SMALL LETTER I WITH CIRCUMFLEX
+                // U+00EF: "ï" LATIN SMALL LETTER I WITH DIAERESIS
+                // U+00ED: "í" LATIN SMALL LETTER I WITH ACUTE
+                // U+012B: "ī" LATIN SMALL LETTER I WITH MACRON
+                // U+00EC: "ì" LATIN SMALL LETTER I WITH GRAVE
+                .setMoreKeysOf("i", "8", "\u00EE", "\u00EF", "\u00ED", "\u012B", "\u00EC")
+                // U+00F4: "ô" LATIN SMALL LETTER O WITH CIRCUMFLEX
+                // U+00F6: "ö" LATIN SMALL LETTER O WITH DIAERESIS
+                // U+00F2: "ò" LATIN SMALL LETTER O WITH GRAVE
+                // U+00F3: "ó" LATIN SMALL LETTER O WITH ACUTE
+                // U+0153: "œ" LATIN SMALL LIGATURE OE
+                // U+00F8: "ø" LATIN SMALL LETTER O WITH STROKE
+                // U+014D: "ō" LATIN SMALL LETTER O WITH MACRON
+                // U+00F5: "õ" LATIN SMALL LETTER O WITH TILDE
+                .setMoreKeysOf("o",
+                        "9", "\u00F4", "\u00F6", "\u00F2", "\u00F3", "\u0153", "\u00F8", "\u014D",
+                        "\u00F5")
+                // U+00E1: "á" LATIN SMALL LETTER A WITH ACUTE
+                // U+00E2: "â" LATIN SMALL LETTER A WITH CIRCUMFLEX
+                // U+00E4: "ä" LATIN SMALL LETTER A WITH DIAERESIS
+                // U+00E6: "æ" LATIN SMALL LETTER AE
+                // U+00E3: "ã" LATIN SMALL LETTER A WITH TILDE
+                // U+00E5: "å" LATIN SMALL LETTER A WITH RING ABOVE
+                // U+0101: "ā" LATIN SMALL LETTER A WITH MACRON
+                .setMoreKeysOf("a",
+                        "\u00E0", "\u00E1", "\u00E2", "\u00E4", "\u00E6", "\u00E3", "\u00E5",
+                        "\u0101")
+                // U+00DF: "ß" LATIN SMALL LETTER SHARP S
+                .setMoreKeysOf("s", "\u00DF")
+                // U+00E7: "ç" LATIN SMALL LETTER C WITH CEDILLA
+                .setMoreKeysOf("c", "\u00E7")
+                // U+00F1: "ñ" LATIN SMALL LETTER N WITH TILDE
+                .setMoreKeysOf("n", "\u00F1");
+    }
+}