blob: 23ff3d4835994373828e7cc8fa3a95330925f8d1 [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;
33import android.support.test.InstrumentationRegistry;
34import android.support.test.filters.SmallTest;
35import android.testing.AndroidTestingRunner;
36import android.testing.TestableLooper;
37import android.testing.TestableLooper.RunWithLooper;
38
TYM Tsai2fc027d2018-12-04 19:28:19 +080039import com.android.systemui.SysuiTestCase;
40import com.android.systemui.dock.DockManager;
41import com.android.systemui.dock.DockManagerFake;
Jerry Chang5d3eb4472018-12-21 11:49:06 +080042import com.android.systemui.doze.DozeMachine.State;
TYM Tsai2fc027d2018-12-04 19:28:19 +080043
44import org.junit.Before;
45import org.junit.BeforeClass;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48
49@SmallTest
50@RunWith(AndroidTestingRunner.class)
51@RunWithLooper
52public class DozeDockHandlerTest extends SysuiTestCase {
53 private DozeDockHandler mDockHandler;
54 private DozeMachine mMachine;
55 private DozeHostFake mHost;
56 private AmbientDisplayConfiguration mConfig;
57 private Instrumentation mInstrumentation;
58 private DockManagerFake mDockManagerFake;
59
60 @BeforeClass
61 public static void setupSuite() {
62 // We can't use KeyguardUpdateMonitor from tests.
63 DozeLog.setRegisterKeyguardCallback(false);
64 }
65
66 @Before
67 public void setUp() throws Exception {
68 mInstrumentation = InstrumentationRegistry.getInstrumentation();
69 mMachine = mock(DozeMachine.class);
70 mHost = spy(new DozeHostFake());
71 mConfig = DozeConfigurationUtil.createMockConfig();
Jerry Chang5d3eb4472018-12-21 11:49:06 +080072 doReturn(false).when(mConfig).alwaysOnEnabled(anyInt());
TYM Tsai2fc027d2018-12-04 19:28:19 +080073
74 mDockManagerFake = spy(new DockManagerFake());
75 mContext.putComponent(DockManager.class, mDockManagerFake);
76
77 mDockHandler = new DozeDockHandler(mContext, mMachine, mHost, mConfig,
lpeter8a5f4702019-01-18 16:53:07 +080078 Handler.createAsync(Looper.myLooper()), mDockManagerFake);
TYM Tsai2fc027d2018-12-04 19:28:19 +080079 }
80
81 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +080082 public void testDockEventListener_registerAndUnregister() {
TYM Tsai2fc027d2018-12-04 19:28:19 +080083 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
84
85 verify(mDockManagerFake).addListener(any());
86
87 mDockHandler.transitionTo(DozeMachine.State.DOZE, DozeMachine.State.FINISH);
88
89 verify(mDockManagerFake).removeListener(any());
90 }
91
92 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +080093 public void testOnEvent_dockedWhenDoze_requestPulse() {
TYM Tsai2fc027d2018-12-04 19:28:19 +080094 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
95 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
96
Jerry Chang5d3eb4472018-12-21 11:49:06 +080097 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
TYM Tsai2fc027d2018-12-04 19:28:19 +080098
99 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
100 }
101
102 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800103 public void testOnEvent_dockedWhenDozeAoD_requestPulse() {
TYM Tsai2fc027d2018-12-04 19:28:19 +0800104 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800105 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800106
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800107 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800108
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800109 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
TYM Tsai2fc027d2018-12-04 19:28:19 +0800110 }
111
112 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800113 public void testOnEvent_dockedHideWhenPulsing_requestPulseOut() {
TYM Tsai2fc027d2018-12-04 19:28:19 +0800114 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800115 when(mMachine.getState()).thenReturn(State.DOZE_PULSING);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800116 when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING);
117
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800118 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800119
120 verify(mHost).stopPulsing();
121 }
122
123 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800124 public void testOnEvent_undockedWhenPulsing_requestPulseOut() {
125 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
126 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_PULSING);
127 when(mMachine.getPulseReason()).thenReturn(DozeLog.PULSE_REASON_DOCKING);
128
129 mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
130
131 verify(mHost).stopPulsing();
132 }
133
134 @Test
135 public void testOnEvent_undockedWhenDoze_neverRequestPulseOut() {
TYM Tsai2fc027d2018-12-04 19:28:19 +0800136 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
TYM Tsai2d236902018-12-27 16:46:06 +0800137 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800138
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800139 mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800140
141 verify(mHost, never()).stopPulsing();
142 }
143
144 @Test
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800145 public void testOnEvent_undockedWhenDozeAndEnabledAoD_requestDozeAoD() {
146 doReturn(true).when(mConfig).alwaysOnEnabled(anyInt());
TYM Tsai2fc027d2018-12-04 19:28:19 +0800147 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
TYM Tsai2fc027d2018-12-04 19:28:19 +0800148 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
149
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800150 mDockManagerFake.setDockEvent(DockManager.STATE_NONE);
151
152 verify(mMachine).requestState(eq(State.DOZE_AOD));
153 }
154
155 @Test
156 public void testTransitionToDoze_whenDocked_requestPulse() {
157 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
158 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
159 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
160 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
161 mDockHandler.transitionTo(State.INITIALIZED, DozeMachine.State.DOZE);
162
TYM Tsai2fc027d2018-12-04 19:28:19 +0800163 TestableLooper.get(this).processAllMessages();
164
165 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
166 }
Jerry Chang5d3eb4472018-12-21 11:49:06 +0800167
168 @Test
169 public void testTransitionToDozeAoD_whenDocked_requestPulse() {
170 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
171 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
172 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED);
173 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD);
174 mDockHandler.transitionTo(State.INITIALIZED, DozeMachine.State.DOZE_AOD);
175
176 TestableLooper.get(this).processAllMessages();
177
178 verify(mMachine).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
179 }
180
181 @Test
182 public void testTransitionToDoze_whenDockedHide_neverRequestPulse() {
183 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
184 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
185 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
186 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE);
187
188 mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, DozeMachine.State.DOZE);
189
190 verify(mMachine, never()).requestPulse(eq(DozeLog.PULSE_REASON_DOCKING));
191 }
192
193 @Test
194 public void testTransitionToDozeAoD_whenDockedHide_requestDoze() {
195 mDockHandler.transitionTo(DozeMachine.State.UNINITIALIZED, DozeMachine.State.INITIALIZED);
196 when(mMachine.getState()).thenReturn(DozeMachine.State.INITIALIZED);
197 mDockManagerFake.setDockEvent(DockManager.STATE_DOCKED_HIDE);
198 when(mMachine.getState()).thenReturn(DozeMachine.State.DOZE_AOD);
199
200 mDockHandler.transitionTo(DozeMachine.State.INITIALIZED, State.DOZE_AOD);
201
202 verify(mMachine).requestState(eq(State.DOZE));
203 }
TYM Tsai2fc027d2018-12-04 19:28:19 +0800204}