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 17package android.support.v7.app; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertNotNull; 21 22import android.os.SystemClock; 23import android.support.test.filters.SmallTest; 24import android.support.v7.appcompat.test.R; 25import android.support.v7.testutils.BaseTestActivity; 26import android.view.KeyCharacterMap; 27import android.view.KeyEvent; 28import android.view.MenuItem; 29 30import org.junit.Test; 31 32public abstract class BaseKeyboardShortcutsTestCase<A extends BaseTestActivity> 33 extends BaseInstrumentationTestCase<A> { 34 35 protected BaseKeyboardShortcutsTestCase(Class<A> activityClass) { 36 super(activityClass); 37 } 38 39 @Test 40 @SmallTest 41 public void testAlphabeticCtrlShortcut() { 42 testKeyboardShortcut(KeyEvent.KEYCODE_A, 43 KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON, 44 R.id.action_alpha_shortcut); 45 } 46 47 private void testKeyboardShortcut(final int keyCode, final int meta, final int expectedId) { 48 final long downTime = SystemClock.uptimeMillis(); 49 final KeyEvent downEvent = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN, 50 keyCode, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0); 51 getInstrumentation().sendKeySync(downEvent); 52 getInstrumentation().waitForIdleSync(); 53 54 final KeyEvent upEvent = new KeyEvent(downTime, downTime + 500, KeyEvent.ACTION_UP, 55 keyCode, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0); 56 getInstrumentation().sendKeySync(upEvent); 57 getInstrumentation().waitForIdleSync(); 58 59 MenuItem selectedItem = getActivity().getOptionsItemSelected(); 60 assertNotNull("Options item selected", selectedItem); 61 assertEquals("Correct options item selected", selectedItem.getItemId(), expectedId); 62 } 63} 64