Merge "Fix alpha keyboard shortcuts with ToolbarActionBar" into lmp-mr1-ub-dev
diff --git a/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java b/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java
index f379130..0736752 100644
--- a/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java
+++ b/v7/appcompat/src/android/support/v7/internal/app/ToolbarActionBar.java
@@ -34,6 +34,7 @@
 import android.support.v7.widget.Toolbar;
 import android.util.TypedValue;
 import android.view.ContextThemeWrapper;
+import android.view.KeyCharacterMap;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -465,6 +466,9 @@
     public boolean onKeyShortcut(int keyCode, KeyEvent ev) {
         Menu menu = getMenu();
         if (menu != null) {
+            final KeyCharacterMap kmap = KeyCharacterMap.load(
+                    ev != null ? ev.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD);
+            menu.setQwertyMode(kmap.getKeyboardType() != KeyCharacterMap.NUMERIC);
             menu.performShortcut(keyCode, ev, 0);
         }
         // This action bar always returns true for handling keyboard shortcuts.
diff --git a/v7/appcompat/tests/res/menu/sample_actions.xml b/v7/appcompat/tests/res/menu/sample_actions.xml
index 3824579..3be3702 100644
--- a/v7/appcompat/tests/res/menu/sample_actions.xml
+++ b/v7/appcompat/tests/res/menu/sample_actions.xml
@@ -21,8 +21,12 @@
           app:showAsAction="ifRoom|collapseActionView"
           app:actionViewClass="android.support.v7.widget.SearchView"/>
 
-    <item android:id="@+id/action_1"
-          android:title="Action 1"
-          app:showAsAction="ifRoom"/>
+    <item android:id="@+id/action_alpha_shortcut"
+          android:title="Alpha"
+          android:alphabeticShortcut="A"/>
+
+    <item android:id="@+id/action_numeric_shortcut"
+          android:title="Numeric"
+          android:numericShortcut="1"/>
 
 </menu>
\ No newline at end of file
diff --git a/v7/appcompat/tests/src/android/support/v7/app/BaseKeyboardShortcutsTestCase.java b/v7/appcompat/tests/src/android/support/v7/app/BaseKeyboardShortcutsTestCase.java
new file mode 100644
index 0000000..bfe10c6
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/app/BaseKeyboardShortcutsTestCase.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2015 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 android.support.v7.app;
+
+import org.junit.Test;
+
+import android.os.SystemClock;
+import android.support.v7.appcompat.test.R;
+import android.view.KeyCharacterMap;
+import android.view.KeyEvent;
+import android.view.MenuItem;
+
+public abstract class BaseKeyboardShortcutsTestCase<A extends BaseTestActivity>
+        extends BaseInstrumentationTestCase<A> {
+
+    protected BaseKeyboardShortcutsTestCase(Class<A> activityClass) {
+        super(activityClass);
+    }
+
+    @Test
+    public void testAlphabeticCtrlShortcut() {
+        testKeyboardShortcut(KeyEvent.KEYCODE_A,
+                KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON,
+                R.id.action_alpha_shortcut);
+    }
+
+    private void testKeyboardShortcut(final int keyCode, final int meta, final int expectedId) {
+        final long downTime = SystemClock.uptimeMillis();
+        final KeyEvent downEvent = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
+                keyCode, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0);
+        getInstrumentation().sendKeySync(downEvent);
+        getInstrumentation().waitForIdleSync();
+
+        final KeyEvent upEvent = new KeyEvent(downTime, downTime + 500, KeyEvent.ACTION_UP,
+                keyCode, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0);
+        getInstrumentation().sendKeySync(upEvent);
+        getInstrumentation().waitForIdleSync();
+
+        MenuItem selectedItem = getActivity().getOptionsItemSelected();
+        assertNotNull("Options item selected", selectedItem);
+        assertEquals("Correct options item selected", selectedItem.getItemId(), expectedId);
+    }
+
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/app/BaseTestActivity.java b/v7/appcompat/tests/src/android/support/v7/app/BaseTestActivity.java
index 6987f6c..f336e91 100644
--- a/v7/appcompat/tests/src/android/support/v7/app/BaseTestActivity.java
+++ b/v7/appcompat/tests/src/android/support/v7/app/BaseTestActivity.java
@@ -21,6 +21,7 @@
 import android.support.v7.appcompat.test.R;
 import android.view.KeyEvent;
 import android.view.Menu;
+import android.view.MenuItem;
 import android.view.WindowManager;
 
 abstract class BaseTestActivity extends AppCompatActivity {
@@ -31,6 +32,8 @@
     private KeyEvent mOnKeyUpEvent;
     private KeyEvent mOnKeyShortcutEvent;
 
+    private MenuItem mOptionsItemSelected;
+
     private boolean mOnMenuOpenedCalled;
     private boolean mOnPanelClosedCalled;
 
@@ -52,6 +55,12 @@
     }
 
     @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        mOptionsItemSelected = item;
+        return super.onOptionsItemSelected(item);
+    }
+
+    @Override
     public boolean onMenuOpened(int featureId, Menu menu) {
         mOnMenuOpenedCalled = true;
         return super.onMenuOpened(featureId, menu);
@@ -120,6 +129,10 @@
         return MenuItemCompat.isActionViewExpanded(mMenu.findItem(R.id.action_search));
     }
 
+    public MenuItem getOptionsItemSelected() {
+        return mOptionsItemSelected;
+    }
+
     public void reset() {
         mOnKeyUpEvent = null;
         mOnKeyDownEvent = null;
diff --git a/v7/appcompat/tests/src/android/support/v7/app/KeyboardShortcutsTestCaseWithToolbar.java b/v7/appcompat/tests/src/android/support/v7/app/KeyboardShortcutsTestCaseWithToolbar.java
new file mode 100644
index 0000000..16c37a2
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/app/KeyboardShortcutsTestCaseWithToolbar.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2015 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 android.support.v7.app;
+
+public class KeyboardShortcutsTestCaseWithToolbar
+        extends BaseKeyboardShortcutsTestCase<ToolbarActionBarActivity> {
+    public KeyboardShortcutsTestCaseWithToolbar() {
+        super(ToolbarActionBarActivity.class);
+    }
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/app/KeyboardShortcutsTestCaseWithWindowDecor.java b/v7/appcompat/tests/src/android/support/v7/app/KeyboardShortcutsTestCaseWithWindowDecor.java
new file mode 100644
index 0000000..4289143
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/app/KeyboardShortcutsTestCaseWithWindowDecor.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2015 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 android.support.v7.app;
+
+public class KeyboardShortcutsTestCaseWithWindowDecor
+        extends BaseKeyboardShortcutsTestCase<WindowDecorActionBarActivity> {
+    public KeyboardShortcutsTestCaseWithWindowDecor() {
+        super(WindowDecorActionBarActivity.class);
+    }
+}