1 /*
2  * Copyright (C) 2023 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 
17 package com.android.server.policy;
18 
19 import static android.view.KeyEvent.KEYCODE_STEM_PRIMARY;
20 
21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
22 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
23 import static com.android.server.policy.PhoneWindowManager.SHORT_PRESS_PRIMARY_LAUNCH_ALL_APPS;
24 
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.ArgumentMatchers.eq;
27 
28 import android.content.Context;
29 import android.content.res.Resources;
30 
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 
35 /**
36  * Test class for stem key gesture.
37  *
38  * Build/Install/Run:
39  * atest WmTests:StemKeyGestureTests
40  */
41 public class StemKeyGestureTests extends ShortcutKeyTestBase {
42     @Mock private Resources mResources;
43 
44     /**
45      * Stem single key should not launch behavior during set up.
46      */
47     @Test
stemSingleKey_duringSetup_doNothing()48     public void stemSingleKey_duringSetup_doNothing() {
49         stemKeySetup(
50                 () -> overrideBehavior(
51                         com.android.internal.R.integer.config_shortPressOnStemPrimaryBehavior,
52                         SHORT_PRESS_PRIMARY_LAUNCH_ALL_APPS));
53         mPhoneWindowManager.setKeyguardServiceDelegateIsShowing(false);
54         mPhoneWindowManager.overrideIsUserSetupComplete(false);
55 
56         sendKey(KEYCODE_STEM_PRIMARY);
57 
58         mPhoneWindowManager.assertNotOpenAllAppView();
59     }
60 
61     /**
62      * Stem single key should launch all app after set up.
63      */
64     @Test
stemSingleKey_AfterSetup_openAllApp()65     public void stemSingleKey_AfterSetup_openAllApp() {
66         stemKeySetup(
67                 () -> overrideBehavior(
68                         com.android.internal.R.integer.config_shortPressOnStemPrimaryBehavior,
69                         SHORT_PRESS_PRIMARY_LAUNCH_ALL_APPS));
70         mPhoneWindowManager.setKeyguardServiceDelegateIsShowing(false);
71         mPhoneWindowManager.overrideIsUserSetupComplete(true);
72 
73         sendKey(KEYCODE_STEM_PRIMARY);
74 
75         mPhoneWindowManager.assertOpenAllAppView();
76     }
77 
stemKeySetup(Runnable behaviorOverrideRunnable)78     private void stemKeySetup(Runnable behaviorOverrideRunnable) {
79         super.tearDown();
80         setupResourcesMock();
81         behaviorOverrideRunnable.run();
82         super.setUp();
83     }
84 
setupResourcesMock()85     private void setupResourcesMock() {
86         Resources realResources = mContext.getResources();
87 
88         mResources = Mockito.mock(Resources.class);
89         doReturn(mResources).when(mContext).getResources();
90 
91         doAnswer(invocation -> realResources.getXml((Integer) invocation.getArguments()[0]))
92                 .when(mResources).getXml(anyInt());
93         doAnswer(invocation -> realResources.getString((Integer) invocation.getArguments()[0]))
94                 .when(mResources).getString(anyInt());
95         doAnswer(invocation -> realResources.getBoolean((Integer) invocation.getArguments()[0]))
96                 .when(mResources).getBoolean(anyInt());
97     }
98 
overrideBehavior(int resId, int expectedBehavior)99     private void overrideBehavior(int resId, int expectedBehavior) {
100         doReturn(expectedBehavior).when(mResources).getInteger(eq(resId));
101     }
102 }
103