1 /*
2  * Copyright (C) 2022 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 com.android.server.policy;
17 
18 import static android.view.KeyEvent.KEYCODE_POWER;
19 import static android.view.KeyEvent.KEYCODE_VOLUME_DOWN;
20 import static android.view.KeyEvent.KEYCODE_VOLUME_UP;
21 
22 import static com.android.server.policy.PhoneWindowManager.POWER_VOLUME_UP_BEHAVIOR_GLOBAL_ACTIONS;
23 import static com.android.server.policy.PhoneWindowManager.POWER_VOLUME_UP_BEHAVIOR_MUTE;
24 
25 import android.view.ViewConfiguration;
26 
27 import androidx.test.filters.MediumTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 /**
34  * Test class for combination key shortcuts.
35  *
36  * Build/Install/Run:
37  *  atest WmTests:CombinationKeyTests
38  */
39 @MediumTest
40 @RunWith(AndroidJUnit4.class)
41 public class CombinationKeyTests extends ShortcutKeyTestBase {
42     private static final long A11Y_KEY_HOLD_MILLIS = 3500;
43 
44     /**
45      * Power-VolDown to take screenshot.
46      */
47     @Test
testPowerVolumeDown()48     public void testPowerVolumeDown() {
49         sendKeyCombination(new int[]{KEYCODE_POWER, KEYCODE_VOLUME_DOWN},
50                 ViewConfiguration.get(mContext).getScreenshotChordKeyTimeout());
51         mPhoneWindowManager.assertTakeScreenshotCalled();
52     }
53 
54     /**
55      * Power-VolUp to show global actions or mute audio. (Phone default behavior)
56      */
57     @Test
testPowerVolumeUp()58     public void testPowerVolumeUp() {
59         // Show global actions.
60         mPhoneWindowManager.overridePowerVolumeUp(POWER_VOLUME_UP_BEHAVIOR_GLOBAL_ACTIONS);
61         sendKeyCombination(new int[]{KEYCODE_POWER, KEYCODE_VOLUME_UP}, 0);
62         mPhoneWindowManager.assertShowGlobalActionsCalled();
63 
64         // Mute audio (hold over 100ms).
65         mPhoneWindowManager.overridePowerVolumeUp(POWER_VOLUME_UP_BEHAVIOR_MUTE);
66         sendKeyCombination(new int[]{KEYCODE_POWER, KEYCODE_VOLUME_UP}, 100);
67         mPhoneWindowManager.assertVolumeMute();
68     }
69 
70     /**
71      * VolDown-VolUp and hold 3 secs to enable accessibility service.
72      */
73     @Test
testVolumeDownVolumeUp()74     public void testVolumeDownVolumeUp() {
75         sendKeyCombination(new int[]{KEYCODE_VOLUME_DOWN, KEYCODE_VOLUME_UP}, A11Y_KEY_HOLD_MILLIS);
76         mPhoneWindowManager.assertAccessibilityKeychordCalled();
77     }
78 }
79