Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2017 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 package androidx.appcompat.app;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.os.Build;
     23 import android.os.SystemClock;
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.rule.ActivityTestRule;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.view.KeyEvent;
     28 
     29 import androidx.appcompat.test.R;
     30 
     31 import org.junit.Before;
     32 import org.junit.Rule;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 /**
     37  * Test shortcut trigger in case of MenuItems with non-default modifiers.
     38  */
     39 @SmallTest
     40 @RunWith(AndroidJUnit4.class)
     41 public class AppCompatMenuItemShortcutsTest {
     42 
     43     private AppCompatMenuItemShortcutsTestActivity mActivity;
     44 
     45     @Rule
     46     public ActivityTestRule<AppCompatMenuItemShortcutsTestActivity> mActivityTestRule =
     47             new ActivityTestRule<>(AppCompatMenuItemShortcutsTestActivity.class);
     48 
     49     @Before
     50     public void setup() {
     51         mActivity = mActivityTestRule.getActivity();
     52     }
     53 
     54     @Test
     55     public void testPerformShortcut() {
     56         // The support library is only needed for API 25 or lower.
     57         if (Build.VERSION.SDK_INT < 26) {
     58             final long downTime = SystemClock.uptimeMillis();
     59             int keyCodeToSend, metaState;
     60             KeyEvent keyEventToSend;
     61 
     62             // Test shortcut trigger in case of non-default single modifier
     63             keyCodeToSend = KeyEvent.KEYCODE_C;
     64             metaState = KeyEvent.META_SHIFT_ON;
     65             keyEventToSend = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
     66                     keyCodeToSend, 0, metaState);
     67             assertTrue(mActivity.onKeyDown(keyCodeToSend, keyEventToSend));
     68             assertEquals(mActivity.getMenuItemIdTracker(), R.id.single_modifier);
     69 
     70             // Test shortcut trigger in case of multiple non-default modifiers
     71             keyCodeToSend = KeyEvent.KEYCODE_D;
     72             metaState = KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON;
     73             keyEventToSend = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
     74                     keyCodeToSend, 0, metaState);
     75             assertTrue(mActivity.onKeyDown(keyCodeToSend, keyEventToSend));
     76             assertEquals(mActivity.getMenuItemIdTracker(), R.id.multiple_modifiers);
     77 
     78             // Test no shortcut trigger in case of incorrect modifier
     79             keyCodeToSend = KeyEvent.KEYCODE_E;
     80             metaState = KeyEvent.META_CTRL_ON;
     81             keyEventToSend = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
     82                     keyCodeToSend, 0, metaState);
     83             assertFalse(mActivity.onKeyDown(keyCodeToSend, keyEventToSend));
     84         }
     85     }
     86 }
     87