Home | History | Annotate | Download | only in app
      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.assertNotNull;
     21 
     22 import android.app.Instrumentation;
     23 import android.os.SystemClock;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.filters.SmallTest;
     26 import android.support.test.rule.ActivityTestRule;
     27 import android.support.test.runner.AndroidJUnit4;
     28 import android.view.KeyCharacterMap;
     29 import android.view.KeyEvent;
     30 import android.view.MenuItem;
     31 
     32 import androidx.appcompat.test.R;
     33 import androidx.appcompat.testutils.BaseTestActivity;
     34 
     35 import org.junit.Rule;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 @RunWith(AndroidJUnit4.class)
     40 public abstract class BaseKeyboardShortcutsTestCase<A extends BaseTestActivity> {
     41     @Rule
     42     public final ActivityTestRule<A> mActivityTestRule;
     43 
     44     protected BaseKeyboardShortcutsTestCase(Class<A> activityClass) {
     45         mActivityTestRule = new ActivityTestRule<>(activityClass);
     46     }
     47 
     48     @Test
     49     @SmallTest
     50     public void testAlphabeticCtrlShortcut() {
     51         testKeyboardShortcut(KeyEvent.KEYCODE_A,
     52                 KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON,
     53                 R.id.action_alpha_shortcut);
     54     }
     55 
     56     private void testKeyboardShortcut(final int keyCode, final int meta, final int expectedId) {
     57         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
     58         final long downTime = SystemClock.uptimeMillis();
     59         final KeyEvent downEvent = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
     60                 keyCode, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0);
     61         instrumentation.sendKeySync(downEvent);
     62         instrumentation.waitForIdleSync();
     63 
     64         final KeyEvent upEvent = new KeyEvent(downTime, downTime + 500, KeyEvent.ACTION_UP,
     65                 keyCode, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0);
     66         instrumentation.sendKeySync(upEvent);
     67         instrumentation.waitForIdleSync();
     68 
     69         MenuItem selectedItem = mActivityTestRule.getActivity().getOptionsItemSelected();
     70         assertNotNull("Options item selected", selectedItem);
     71         assertEquals("Correct options item selected", selectedItem.getItemId(), expectedId);
     72     }
     73 }
     74