/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.support.v7.app;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.os.SystemClock;
import android.support.test.filters.SmallTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.appcompat.test.R;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test shortcut trigger in case of MenuItems with non-default modifiers.
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class AppCompatMenuItemShortcutsTest {

    private AppCompatMenuItemShortcutsTestActivity mActivity;
    private MenuInflater mMenuInflater;
    private Menu mMenu;

    @Rule
    public ActivityTestRule<AppCompatMenuItemShortcutsTestActivity> mActivityTestRule =
            new ActivityTestRule<>(AppCompatMenuItemShortcutsTestActivity.class);

    @Before
    public void setup() {
        mActivity = mActivityTestRule.getActivity();
    }

    @Test
    public void testPerformShortcut() {
        final long downTime = SystemClock.uptimeMillis();
        int keyCodeToSend, metaState;
        KeyEvent keyEventToSend;

        // Test shortcut trigger in case of non-default single modifier
        keyCodeToSend = KeyEvent.KEYCODE_C;
        metaState = KeyEvent.META_SHIFT_ON;
        keyEventToSend = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
                keyCodeToSend, 0, metaState);
        assertTrue(mActivity.onKeyDown(keyCodeToSend, keyEventToSend));
        assertEquals(mActivity.getMenuItemIdTracker(), R.id.single_modifier);

        // Test shortcut trigger in case of multiple non-default modifiers
        keyCodeToSend = KeyEvent.KEYCODE_D;
        metaState = KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON;
        keyEventToSend = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
                keyCodeToSend, 0, metaState);
        assertTrue(mActivity.onKeyDown(keyCodeToSend, keyEventToSend));
        assertEquals(mActivity.getMenuItemIdTracker(), R.id.multiple_modifiers);

        // Test no shortcut trigger in case of incorrect modifier
        keyCodeToSend = KeyEvent.KEYCODE_E;
        metaState = KeyEvent.META_CTRL_ON;
        keyEventToSend = new KeyEvent(downTime, downTime, KeyEvent.ACTION_DOWN,
                keyCodeToSend, 0, metaState);
        assertFalse(mActivity.onKeyDown(keyCodeToSend, keyEventToSend));
    }
}

@@ -958,16 +958,58 @@
 JUnit4;%0A
+import android.support.v4.os.BuildCompat;%0A
 import a
@@ -1862,24 +1862,136 @@
 hortcut() %7B%0A
+        // The support library is only needed for API 25 or lower.%0A        if (!BuildCompat.isAtLeastO()) %7B%0A    
         fina
@@ -2044,16 +2044,20 @@
         
+    
 int keyC
@@ -2086,16 +2086,20 @@
         
+   
+ 
 KeyEvent
@@ -2116,16 +2116,20 @@
 oSend;%0A%0A
+    
         
@@ -2192,32 +2192,36 @@
 odifier%0A        
+    
 keyCodeToSend = 
@@ -2240,16 +2240,20 @@
 CODE_C;%0A
+    
         
@@ -2288,32 +2288,36 @@
 IFT_ON;%0A        
+    
 keyEventToSend =
@@ -2380,32 +2380,36 @@
                 
+   
+ 
 keyCodeToSend, 0
@@ -2414,32 +2414,36 @@
  0, metaState);%0A
+    
         assertTr
@@ -2498,32 +2498,36 @@
 Send));%0A        
+ 
+   
 assertEquals(mAc
@@ -2577,24 +2577,28 @@
 modifier);%0A%0A
+    
         // T
@@ -2660,32 +2660,36 @@
 difiers%0A        
+    
 keyCodeToSend = 
@@ -2708,32 +2708,36 @@
 CODE_D;%0A        
+  
+  
 metaState = KeyE
@@ -2771,32 +2771,36 @@
 .META_SHIFT_ON;%0A
+    
         keyEvent
@@ -2871,32 +2871,36 @@
                 
+ 
+   
 keyCodeToSend, 0
@@ -2913,32 +2913,36 @@
 State);%0A        
+   
+ 
 assertTrue(mActi
@@ -2981,32 +2981,36 @@
 yEventToSend));%0A
+    
         assertEq
@@ -3075,32 +3075,36 @@
 iers);%0A%0A        
+  
+  
 // Test no short
@@ -3137,32 +3137,36 @@
 orrect modifier%0A
+    
         keyCodeT
@@ -3193,32 +3193,36 @@
 CODE_E;%0A        
+    
 metaState = KeyE
@@ -3236,24 +3236,28 @@
 TA_CTRL_ON;%0A
+    
         keyE
@@ -3332,32 +3332,36 @@
                 
+    
 keyCodeToSend, 0
@@ -3378,24 +3378,28 @@
 e);%0A        
+ 
+   
 assertFalse(
@@ -3447,20 +3447,30 @@
 ntToSend));%0A
+        %7D%0A
     %7D%0A%7D%0A

Test keyboard shortcut in support only if API 25 or lower