1 /*
2  * Copyright (C) 2018 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.systemui.doze;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.clearInvocations;
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.app.ActivityManager;
30 import android.hardware.display.AmbientDisplayConfiguration;
31 import android.testing.AndroidTestingRunner;
32 import android.testing.TestableLooper.RunWithLooper;
33 
34 import androidx.test.filters.SmallTest;
35 
36 import com.android.systemui.SysuiTestCase;
37 import com.android.systemui.dock.DockManager;
38 import com.android.systemui.dock.DockManagerFake;
39 import com.android.systemui.doze.DozeMachine.State;
40 import com.android.systemui.settings.UserTracker;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 
48 @SmallTest
49 @RunWith(AndroidTestingRunner.class)
50 @RunWithLooper
51 public class DozeDockHandlerTest extends SysuiTestCase {
52     @Mock private DozeMachine mMachine;
53     @Mock private UserTracker mUserTracker;
54     private AmbientDisplayConfiguration mConfig;
55     private DockManagerFake mDockManagerFake;
56     private DozeDockHandler mDockHandler;
57 
58     @Before
setUp()59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61         mConfig = DozeConfigurationUtil.createMockConfig();
62         mDockManagerFake = spy(new DockManagerFake());
63         mDockHandler = new DozeDockHandler(mConfig, mDockManagerFake, mUserTracker);
64         mDockHandler.setDozeMachine(mMachine);
65 
66         when(mUserTracker.getUserId()).thenReturn(ActivityManager.getCurrentUser());
67         when(mMachine.getState()).thenReturn(State.DOZE_AOD);
68         doReturn(true).when(mConfig).alwaysOnEnabled(anyInt());
69         mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
70     }
71 
72     @Test
transitionToInitialized_registersDockEventListener()73     public void transitionToInitialized_registersDockEventListener() {
74         verify(mDockManagerFake).addListener(any());
75     }
76 
77     @Test
transitionToFinish_unregistersDockEventListener()78     public void transitionToFinish_unregistersDockEventListener() {
79         mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.FINISH);
80 
81         verify(mDockManagerFake).removeListener(any());
82     }
83 
84     @Test
onEvent_docked_requestsDockedAodState()85     public void onEvent_docked_requestsDockedAodState() {
86         mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
87 
88         verify(mMachine).requestState(eq(State.DOZE_AOD_DOCKED));
89     }
90 
91     @Test
onEvent_noneWhileEnabledAod_ignoresIfAlreadyNone()92     public void onEvent_noneWhileEnabledAod_ignoresIfAlreadyNone() {
93         mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
94 
95         verify(mMachine, never()).requestState(eq(State.DOZE_AOD));
96     }
97 
98     @Test
onEvent_noneWhileEnabledAod_requestsAodState()99     public void onEvent_noneWhileEnabledAod_requestsAodState() {
100         mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
101         clearInvocations(mMachine);
102         mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
103 
104         verify(mMachine).requestState(eq(State.DOZE_AOD));
105     }
106 
107     @Test
onEvent_noneWhileDisabledAod_requestsDozeState()108     public void onEvent_noneWhileDisabledAod_requestsDozeState() {
109         mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
110         clearInvocations(mMachine);
111         doReturn(false).when(mConfig).alwaysOnEnabled(anyInt());
112 
113         mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
114 
115         verify(mMachine).requestState(eq(State.DOZE));
116     }
117 
118     @Test
onEvent_hide_requestsDozeState()119     public void onEvent_hide_requestsDozeState() {
120         mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
121 
122         verify(mMachine).requestState(eq(State.DOZE));
123     }
124 
125     @Test
onEvent_dockedWhilePulsing_wontRequestStateChange()126     public void onEvent_dockedWhilePulsing_wontRequestStateChange() {
127         when(mMachine.getState()).thenReturn(State.DOZE_PULSING);
128 
129         mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
130 
131         verify(mMachine, never()).requestState(any(State.class));
132     }
133 
134     @Test
onEvent_noneWhilePulsing_wontRequestStateChange()135     public void onEvent_noneWhilePulsing_wontRequestStateChange() {
136         when(mMachine.getState()).thenReturn(State.DOZE_PULSING);
137 
138         mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
139 
140         verify(mMachine, never()).requestState(any(State.class));
141     }
142 
143     @Test
onEvent_hideWhilePulsing_wontRequestStateChange()144     public void onEvent_hideWhilePulsing_wontRequestStateChange() {
145         when(mMachine.getState()).thenReturn(State.DOZE_PULSING);
146 
147         mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
148 
149         verify(mMachine, never()).requestState(any(State.class));
150     }
151 }
152