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 */
16package androidx.appcompat.app;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import android.os.Build;
23import android.os.SystemClock;
24import android.support.test.filters.SmallTest;
25import android.support.test.rule.ActivityTestRule;
26import android.support.test.runner.AndroidJUnit4;
27import android.view.KeyEvent;
28
29import androidx.appcompat.test.R;
30
31import org.junit.Before;
32import org.junit.Rule;
33import org.junit.Test;
34import 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)
41public 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