blob: 4ba28582d7ba5f835dc9a3e9f18b4d0c66a990fd [file] [log] [blame]
TYM Tsai2fc027d2018-12-04 19:28:19 +08001/*
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
17package com.android.systemui.doze;
18
19import static org.mockito.ArgumentMatchers.any;
Jerry Chang5d3eb4472018-12-21 11:49:06 +080020import static org.mockito.ArgumentMatchers.anyInt;
TYM Tsai2fc027d2018-12-04 19:28:19 +080021import static org.mockito.ArgumentMatchers.eq;
Jerry Chang5d3eb4472018-12-21 11:49:06 +080022import static org.mockito.Mockito.doReturn;
TYM Tsai2fc027d2018-12-04 19:28:19 +080023import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.spy;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import android.app.Instrumentation;
Issei Suzukica19e6e2019-02-26 12:39:11 +010030import android.hardware.display.AmbientDisplayConfiguration;
TYM Tsai2fc027d2018-12-04 19:28:19 +080031import android.os.Handler;
32import android.os.Looper;
TYM Tsai2fc027d2018-12-04 19:28:19 +080033import android.testing.AndroidTestingRunner;
34import android.testing.TestableLooper;
35import android.testing.TestableLooper.RunWithLooper;
36
Brett Chabot84151d92019-02-27 15:37:59 -080037import androidx.test.InstrumentationRegistry;
38import androidx.test.filters.SmallTest;
39
TYM Tsai2fc027d2018-12-04 19:28:19 +080040import com.android.systemui.SysuiTestCase;
41import com.android.systemui.dock.DockManager;
42import com.android.systemui.dock.DockManagerFake;
Jerry Chang5d3eb4472018-12-21 11:49:06 +080043import com.android.systemui.doze.DozeMachine.State;
TYM Tsai2fc027d2018-12-04 19:28:19 +080044
45import org.junit.Before;
46import org.junit.BeforeClass;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49
50@SmallTest
51@RunWith(AndroidTestingRunner.class)
52@RunWithLooper
53public class DozeDockHandlerTest extends SysuiTestCase {
54 private DozeDockHandler mDockHandler;
55 private DozeMachine mMachine;
56 private DozeHostFake mHost;
57 private AmbientDisplayConfiguration mConfig;
58 private Instrumentation mInstrumentation;
59 private DockManagerFake mDockManagerFake;
60
61 @BeforeClass
62 public static void setupSuite() {
63 // We can't use KeyguardUpdateMonitor from tests.
64 DozeLog.setRegisterKeyguardCallback(false);
65 }
66
67 @Before
68 public void setUp() throws Exception {
69 mInstrumentation = InstrumentationRegistry.getInstrumentation();
70 mMachine = mock(DozeMachine.class);
71 mHost = spy(new DozeHostFake());
72 mConfig = DozeConfigurationUtil.createMockConfig();
Jerry Chang5d3eb4472018-12-21 11:49:06 +080073 doReturn(false).when(mConfig).alwaysOnEnabled(anyInt());
TYM Tsai2fc027d2018-12-04 19:28:19 +080074
75 mDockManagerFake = spy(new DockManagerFake());
76 mContext.putComponent(DockManager.class, mDockManagerFake);
77
78 mDockHandler = new DozeDockHandler(mContext, mMachine, mHost, mConfig,
lpeter8a5f4702019-01-18 16:53:07 +080079 Handler.createAsync(Looper.myLooper()), mDockManagerFake);
TYM Tsai2fc027d2018-12-04 19:28:19 +080080 }
81
82 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +080083 public void testDockEventListener_registerAndUnregister() {
TYM Tsai2fc027d2018-12-04 19:28:19 +080084 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
85
86 verify(mDockManagerFake).addListener(any());
87
88 mDockHandler.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH);
89
90 verify(mDockManagerFake).removeListener(any());
91 }
92
93 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +080094 public void testOnEvent_dockedWhenDoze_requestPulse() {
TYM Tsai2fc027d2018-12-04 19:28:19 +080095 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
96 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
97
Jerry Chang5d3eb4472018-12-21 11:49:06 +080098 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
TYM Tsai2fc027d2018-12-04 19:28:19 +080099
100 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
101 }
102
103 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800104 public void testOnEvent_dockedWhenDozeAoD_requestPulse() {
TYM Tsai2fc027d2018-12-04 19:28:19 +0800105 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800106 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800107
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800108 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800109
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800110 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
TYM Tsai2fc027d2018-12-04 19:28:19 +0800111 }
112
113 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800114 public void testOnEvent_dockedHideWhenPulsing_requestPulseOut() {
TYM Tsai2fc027d2018-12-04 19:28:19 +0800115 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800116 when(mMachine.getState()).thenReturn(State.DOZE_PULSING);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800117 when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING);
118
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800119 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800120
121 verify(mHost).stopPulsing();
122 }
123
124 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800125 public void testOnEvent_undockedWhenPulsing_requestPulseOut() {
126 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
127 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_PULSING);
128 when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING);
129
130 mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
131
132 verify(mHost).stopPulsing();
133 }
134
135 @Test
136 public void testOnEvent_undockedWhenDoze_neverRequestPulseOut() {
TYM Tsai2fc027d2018-12-04 19:28:19 +0800137 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
TYM Tsai2d236902018-12-27 16:46:06 +0800138 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800139
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800140 mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800141
142 verify(mHost, never()).stopPulsing();
143 }
144
145 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800146 public void testOnEvent_undockedWhenDozeAndEnabledAoD_requestDozeAoD() {
147 doReturn(true).when(mConfig).alwaysOnEnabled(anyInt());
TYM Tsai2fc027d2018-12-04 19:28:19 +0800148 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800149 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
150
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800151 mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
152
153 verify(mMachine).requestState(eq(State.DOZE_AOD));
154 }
155
156 @Test
157 public void testTransitionToDoze_whenDocked_requestPulse() {
158 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
159 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
160 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
161 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
162 mDockHandler.transitionTo(State.INITIALIZED, DozeMachine.State.DOZE);
163
TYM Tsai2fc027d2018-12-04 19:28:19 +0800164 TestableLooper.get(this).processAllMessages();
165
166 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
167 }
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800168
169 @Test
170 public void testTransitionToDozeAoD_whenDocked_requestPulse() {
171 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
172 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
173 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
174 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD);
175 mDockHandler.transitionTo(State.INITIALIZED, DozeMachine.State.DOZE_AOD);
176
177 TestableLooper.get(this).processAllMessages();
178
179 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
180 }
181
182 @Test
183 public void testTransitionToDoze_whenDockedHide_neverRequestPulse() {
184 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
185 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
186 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
187 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
188
189 mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
190
191 verify(mMachine, never()).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
192 }
193
194 @Test
195 public void testTransitionToDozeAoD_whenDockedHide_requestDoze() {
196 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
197 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
198 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
199 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD);
200
201 mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, State.DOZE_AOD);
202
203 verify(mMachine).requestState(eq(State.DOZE));
204 }
TYM Tsai2fc027d2018-12-04 19:28:19 +0800205}