1 /* 2 * Copyright (C) 2015 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 androidx.appcompat.app; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.os.Build; 24 import android.os.SystemClock; 25 import android.support.test.InstrumentationRegistry; 26 import android.support.test.filters.SmallTest; 27 import android.support.test.rule.ActivityTestRule; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.view.KeyEvent; 30 import android.view.MenuItem; 31 import android.view.View; 32 import android.view.Window; 33 34 import androidx.appcompat.testutils.BaseTestActivity; 35 import androidx.appcompat.widget.Toolbar; 36 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 @RunWith(AndroidJUnit4.class) 42 public class KeyboardShortcutsTestCaseWithToolbar { 43 @Rule 44 public final ActivityTestRule<ToolbarAppCompatActivity> mActivityTestRule = 45 new ActivityTestRule<>(ToolbarAppCompatActivity.class); 46 47 @Test 48 @SmallTest 49 public void testAccessActionBar() throws Throwable { 50 // Since O, we rely on keyboard navigation clusters for jumping to actionbar 51 if (Build.VERSION.SDK_INT <= 25) { 52 return; 53 } 54 final BaseTestActivity activity = mActivityTestRule.getActivity(); 55 56 final View editText = activity.findViewById(androidx.appcompat.test.R.id.editText); 57 mActivityTestRule.runOnUiThread(new Runnable() { 58 @Override 59 public void run() { 60 editText.requestFocus(); 61 } 62 }); 63 64 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 65 sendMetaKey(KeyEvent.KEYCODE_TAB); 66 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 67 68 mActivityTestRule.runOnUiThread(new Runnable() { 69 @Override 70 public void run() { 71 assertFalse(editText.hasFocus()); 72 final View toolbar = activity.findViewById(androidx.appcompat.test.R.id.toolbar); 73 assertTrue(toolbar.hasFocus()); 74 } 75 }); 76 // We rely on keyboard navigation clusters for jumping out of actionbar since normal 77 // navigation won't leaves it. 78 sendMetaKey(KeyEvent.KEYCODE_TAB); 79 80 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 81 82 // Should jump to the first view again. 83 mActivityTestRule.runOnUiThread(new Runnable() { 84 @Override 85 public void run() { 86 assertTrue(editText.hasFocus()); 87 } 88 }); 89 } 90 91 @Test 92 @SmallTest 93 public void testKeyShortcuts() throws Throwable { 94 final ToolbarAppCompatActivity activity = mActivityTestRule.getActivity(); 95 96 final Toolbar toolbar = 97 activity.findViewById(androidx.appcompat.test.R.id.toolbar); 98 99 mActivityTestRule.runOnUiThread(new Runnable() { 100 @Override 101 public void run() { 102 toolbar.inflateMenu(androidx.appcompat.test.R.menu.sample_actions); 103 } 104 }); 105 106 final Boolean[] shareItemClicked = new Boolean[]{false}; 107 toolbar.getMenu().findItem(androidx.appcompat.test.R.id.action_alpha_shortcut) 108 .setOnMenuItemClickListener( 109 new MenuItem.OnMenuItemClickListener() { 110 @Override 111 public boolean onMenuItemClick(MenuItem item) { 112 return shareItemClicked[0] = true; 113 } 114 }); 115 116 final Window.Callback cb = activity.getWindow().getCallback(); 117 118 // Make sure valid menu shortcuts get handled by toolbar menu 119 long now = SystemClock.uptimeMillis(); 120 final KeyEvent handledShortcutKey = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, 121 KeyEvent.KEYCODE_A, 0, KeyEvent.META_CTRL_ON); 122 mActivityTestRule.runOnUiThread(new Runnable() { 123 @Override 124 public void run() { 125 assertTrue(cb.dispatchKeyShortcutEvent(handledShortcutKey)); 126 } 127 }); 128 assertTrue(shareItemClicked[0]); 129 130 final KeyEvent unhandledShortcutKey = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, 131 KeyEvent.KEYCODE_D, 0, KeyEvent.META_CTRL_ON); 132 133 // Make sure we aren't eating unused shortcuts. 134 mActivityTestRule.runOnUiThread(new Runnable() { 135 @Override 136 public void run() { 137 assertFalse(cb.dispatchKeyShortcutEvent(unhandledShortcutKey)); 138 } 139 }); 140 141 activity.resetCounters(); 142 143 // Make sure that unhandled shortcuts don't prepare menus (since toolbar is handling that). 144 InstrumentationRegistry.getInstrumentation().sendKeySync(unhandledShortcutKey); 145 assertEquals(1, activity.mKeyShortcutCount); 146 assertEquals(0, activity.mPrepareMenuCount); 147 assertEquals(0, activity.mCreateMenuCount); 148 } 149 150 private void sendMetaKey(int keyCode) throws Throwable { 151 long time = SystemClock.uptimeMillis(); 152 KeyEvent keyDown = new KeyEvent(time, time, KeyEvent.ACTION_DOWN, keyCode, 153 0, KeyEvent.META_META_ON); 154 InstrumentationRegistry.getInstrumentation().sendKeySync(keyDown); 155 time = SystemClock.uptimeMillis(); 156 KeyEvent keyUp = new KeyEvent(time, time, KeyEvent.ACTION_UP, keyCode, 157 0, KeyEvent.META_META_ON); 158 InstrumentationRegistry.getInstrumentation().sendKeySync(keyUp); 159 } 160 } 161