1 /*
2  * Copyright (C) 2019 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 com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
22 import static com.android.dx.mockito.inline.extended.ExtendedMockito.never;
23 import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset;
24 import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
25 import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
26 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
27 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
28 
29 import static org.mockito.ArgumentMatchers.anyBoolean;
30 import static org.mockito.ArgumentMatchers.anyInt;
31 import static org.mockito.ArgumentMatchers.anyString;
32 
33 import android.app.ActivityManager;
34 
35 import androidx.test.filters.SmallTest;
36 
37 import com.android.server.pm.UserManagerInternal;
38 import com.android.server.wm.ActivityTaskManagerInternal;
39 
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 
44 /**
45  * Test class for {@link PhoneWindowManager}.
46  *
47  * Build/Install/Run:
48  *  atest WmTests:PhoneWindowManagerTests
49  */
50 @SmallTest
51 public class PhoneWindowManagerTests {
52 
53     PhoneWindowManager mPhoneWindowManager;
54 
55     @Before
setUp()56     public void setUp() {
57         mPhoneWindowManager = spy(new PhoneWindowManager());
58         spyOn(ActivityManager.getService());
59     }
60 
61     @After
tearDown()62     public void tearDown() {
63         reset(ActivityManager.getService());
64     }
65 
66     @Test
testShouldNotStartDockOrHomeWhenSetup()67     public void testShouldNotStartDockOrHomeWhenSetup() throws Exception {
68         mockStartDockOrHome();
69         doReturn(false).when(mPhoneWindowManager).isUserSetupComplete();
70 
71         mPhoneWindowManager.startDockOrHome(
72                 0 /* displayId */, false /* fromHomeKey */, false /* awakenFromDreams */);
73 
74         verify(mPhoneWindowManager, never()).createHomeDockIntent();
75     }
76 
77     @Test
testShouldStartDockOrHomeAfterSetup()78     public void testShouldStartDockOrHomeAfterSetup() throws Exception {
79         mockStartDockOrHome();
80         doReturn(true).when(mPhoneWindowManager).isUserSetupComplete();
81 
82         mPhoneWindowManager.startDockOrHome(
83                 0 /* displayId */, false /* fromHomeKey */, false /* awakenFromDreams */);
84 
85         verify(mPhoneWindowManager).createHomeDockIntent();
86     }
87 
mockStartDockOrHome()88     private void mockStartDockOrHome() throws Exception {
89         doNothing().when(ActivityManager.getService()).stopAppSwitches();
90         ActivityTaskManagerInternal mMockActivityTaskManagerInternal =
91                 mock(ActivityTaskManagerInternal.class);
92         when(mMockActivityTaskManagerInternal.startHomeOnDisplay(
93                 anyInt(), anyString(), anyInt(), anyBoolean(), anyBoolean())).thenReturn(false);
94         mPhoneWindowManager.mActivityTaskManagerInternal = mMockActivityTaskManagerInternal;
95         mPhoneWindowManager.mUserManagerInternal = mock(UserManagerInternal.class);
96     }
97 }
98