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