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_UP;
20 
21 import static com.android.server.policy.PhoneWindowManager.LONG_PRESS_POWER_ASSISTANT;
22 import static com.android.server.policy.PhoneWindowManager.LONG_PRESS_POWER_GLOBAL_ACTIONS;
23 import static com.android.server.policy.PhoneWindowManager.SHORT_PRESS_POWER_DREAM_OR_SLEEP;
24 
25 import android.provider.Settings;
26 import android.view.Display;
27 
28 import org.junit.Test;
29 
30 /**
31  * Test class for power key gesture.
32  *
33  * Build/Install/Run:
34  *  atest WmTests:PowerKeyGestureTests
35  */
36 public class PowerKeyGestureTests extends ShortcutKeyTestBase {
37     /**
38      * Power single press to turn screen on/off.
39      */
40     @Test
testPowerSinglePress()41     public void testPowerSinglePress() {
42         sendKey(KEYCODE_POWER);
43         mPhoneWindowManager.assertPowerSleep();
44 
45         // turn screen on when begin from non-interactive.
46         mPhoneWindowManager.overrideDisplayState(Display.STATE_OFF);
47         sendKey(KEYCODE_POWER);
48         mPhoneWindowManager.assertPowerWakeUp();
49         mPhoneWindowManager.assertNoPowerSleep();
50     }
51 
52     /**
53      * Power single press to start dreaming when so configured.
54      */
55     @Test
testPowerSinglePressRequestsDream()56     public void testPowerSinglePressRequestsDream() {
57         mPhoneWindowManager.overrideShortPressOnPower(SHORT_PRESS_POWER_DREAM_OR_SLEEP);
58         mPhoneWindowManager.overrideCanStartDreaming(true);
59         sendKey(KEYCODE_POWER);
60         mPhoneWindowManager.assertDreamRequest();
61         mPhoneWindowManager.overrideIsDreaming(true);
62         mPhoneWindowManager.assertLockedAfterAppTransitionFinished();
63     }
64 
65     @Test
testAppTransitionFinishedCalledAfterDreamStoppedWillNotLockAgain()66     public void testAppTransitionFinishedCalledAfterDreamStoppedWillNotLockAgain() {
67         mPhoneWindowManager.overrideShortPressOnPower(SHORT_PRESS_POWER_DREAM_OR_SLEEP);
68         mPhoneWindowManager.overrideCanStartDreaming(true);
69         sendKey(KEYCODE_POWER);
70         mPhoneWindowManager.assertDreamRequest();
71         mPhoneWindowManager.overrideIsDreaming(false);
72         mPhoneWindowManager.assertDidNotLockAfterAppTransitionFinished();
73     }
74 
75     /**
76      * Power double-press to launch camera does not lock device when the single press behavior is to
77      * dream.
78      */
79     @Test
testPowerDoublePressWillNotLockDevice()80     public void testPowerDoublePressWillNotLockDevice() {
81         mPhoneWindowManager.overrideShortPressOnPower(SHORT_PRESS_POWER_DREAM_OR_SLEEP);
82         mPhoneWindowManager.overrideCanStartDreaming(false);
83         sendKey(KEYCODE_POWER);
84         sendKey(KEYCODE_POWER);
85         mPhoneWindowManager.assertCameraLaunch();
86         mPhoneWindowManager.assertDidNotLockAfterAppTransitionFinished();
87     }
88 
89     /**
90      * Power double press to trigger camera.
91      */
92     @Test
testPowerDoublePress()93     public void testPowerDoublePress() {
94         sendKey(KEYCODE_POWER);
95         sendKey(KEYCODE_POWER);
96         mPhoneWindowManager.assertCameraLaunch();
97     }
98 
99     /**
100      * Power long press to show assistant or global actions.
101      */
102     @Test
testPowerLongPress()103     public void testPowerLongPress() {
104         // Show assistant.
105         mPhoneWindowManager.overrideLongPressOnPower(LONG_PRESS_POWER_ASSISTANT);
106         sendKey(KEYCODE_POWER, true);
107         mPhoneWindowManager.assertAssistLaunch();
108 
109         // Show global actions.
110         mPhoneWindowManager.overrideLongPressOnPower(LONG_PRESS_POWER_GLOBAL_ACTIONS);
111         sendKey(KEYCODE_POWER, true);
112         mPhoneWindowManager.assertShowGlobalActionsCalled();
113     }
114 
115     /**
116      * Ignore power press if combination key already triggered.
117      */
118     @Test
testIgnoreSinglePressWhenCombinationKeyTriggered()119     public void testIgnoreSinglePressWhenCombinationKeyTriggered() {
120         sendKeyCombination(new int[]{KEYCODE_POWER, KEYCODE_VOLUME_UP}, 0);
121         mPhoneWindowManager.assertNoPowerSleep();
122     }
123 
124     /**
125      * When a phone call is active, and INCALL_POWER_BUTTON_BEHAVIOR_HANGUP is enabled, then the
126      * power button should only stop phone call. The screen should not be turned off (power sleep
127      * should not be activated).
128      */
129     @Test
testIgnoreSinglePressWhenEndCall()130     public void testIgnoreSinglePressWhenEndCall() {
131         mPhoneWindowManager.overrideIncallPowerBehavior(
132                 Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_HANGUP);
133         sendKey(KEYCODE_POWER);
134         mPhoneWindowManager.assertNoPowerSleep();
135     }
136 }
137