blob: f4cf314aa8fdcdc506fa7983e8890950b7baa2cf [file] [log] [blame]
Adrian Roosff2c4562016-11-03 12:13:36 -07001/*
2 * Copyright (C) 2016 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 com.android.systemui.doze.DozeMachine.State.DOZE;
20import static com.android.systemui.doze.DozeMachine.State.DOZE_AOD;
Jerry Changae4dc4b2019-10-16 18:45:03 +080021import static com.android.systemui.doze.DozeMachine.State.DOZE_AOD_DOCKED;
Adrian Roosff2c4562016-11-03 12:13:36 -070022import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSE_DONE;
23import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSING;
24import static com.android.systemui.doze.DozeMachine.State.DOZE_REQUEST_PULSE;
25import static com.android.systemui.doze.DozeMachine.State.FINISH;
26import static com.android.systemui.doze.DozeMachine.State.INITIALIZED;
27import static com.android.systemui.doze.DozeMachine.State.UNINITIALIZED;
28
29import static org.junit.Assert.assertEquals;
30import static org.junit.Assert.assertFalse;
31import static org.junit.Assert.assertTrue;
Adrian Roos0261fb22017-03-07 20:20:35 +000032import static org.mockito.ArgumentMatchers.any;
Jason Monka716bac2018-12-05 15:48:21 -050033import static org.mockito.ArgumentMatchers.anyInt;
Adrian Roos0261fb22017-03-07 20:20:35 +000034import static org.mockito.ArgumentMatchers.eq;
Adrian Roosff2c4562016-11-03 12:13:36 -070035import static org.mockito.Mockito.doAnswer;
36import static org.mockito.Mockito.mock;
37import static org.mockito.Mockito.never;
38import static org.mockito.Mockito.reset;
39import static org.mockito.Mockito.verify;
40import static org.mockito.Mockito.when;
41
Issei Suzukica19e6e2019-02-26 12:39:11 +010042import android.hardware.display.AmbientDisplayConfiguration;
Jason Monk340b0e52017-03-08 14:57:56 -050043import android.testing.AndroidTestingRunner;
Jason Monka716bac2018-12-05 15:48:21 -050044import android.testing.UiThreadTest;
Adrian Roosff2c4562016-11-03 12:13:36 -070045
Brett Chabot84151d92019-02-27 15:37:59 -080046import androidx.test.filters.SmallTest;
47
Jason Monkfba8faf2017-05-23 10:42:59 -040048import com.android.systemui.SysuiTestCase;
Jerry Changae4dc4b2019-10-16 18:45:03 +080049import com.android.systemui.dock.DockManager;
Lucas Dupin737b8512019-08-09 10:35:34 -070050import com.android.systemui.keyguard.WakefulnessLifecycle;
Lucas Dupinbd7366d2019-09-25 13:39:21 -070051import com.android.systemui.statusbar.policy.BatteryController;
Adrian Roosc1b50322017-02-27 21:07:58 +010052import com.android.systemui.util.wakelock.WakeLockFake;
Adrian Roosff2c4562016-11-03 12:13:36 -070053
54import org.junit.Before;
55import org.junit.Test;
56import org.junit.runner.RunWith;
Lucas Dupin737b8512019-08-09 10:35:34 -070057import org.mockito.Mock;
58import org.mockito.MockitoAnnotations;
Adrian Roosff2c4562016-11-03 12:13:36 -070059
60@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050061@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050062@UiThreadTest
Jason Monkfba8faf2017-05-23 10:42:59 -040063public class DozeMachineTest extends SysuiTestCase {
Adrian Roosff2c4562016-11-03 12:13:36 -070064
65 DozeMachine mMachine;
66
Lucas Dupin737b8512019-08-09 10:35:34 -070067 @Mock
68 private WakefulnessLifecycle mWakefulnessLifecycle;
Beverlycc4a62f2019-09-26 14:55:28 -040069 @Mock
70 private DozeLog mDozeLog;
Jerry Changae4dc4b2019-10-16 18:45:03 +080071 @Mock private DockManager mDockManager;
Adrian Roosff2c4562016-11-03 12:13:36 -070072 private DozeServiceFake mServiceFake;
73 private WakeLockFake mWakeLockFake;
Adrian Roos0261fb22017-03-07 20:20:35 +000074 private AmbientDisplayConfiguration mConfigMock;
Adrian Roosff2c4562016-11-03 12:13:36 -070075 private DozeMachine.Part mPartMock;
76
77 @Before
78 public void setUp() {
Lucas Dupin737b8512019-08-09 10:35:34 -070079 MockitoAnnotations.initMocks(this);
Adrian Roosff2c4562016-11-03 12:13:36 -070080 mServiceFake = new DozeServiceFake();
81 mWakeLockFake = new WakeLockFake();
Adrian Roos0261fb22017-03-07 20:20:35 +000082 mConfigMock = mock(AmbientDisplayConfiguration.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070083 mPartMock = mock(DozeMachine.Part.class);
Jerry Changae4dc4b2019-10-16 18:45:03 +080084 when(mDockManager.isDocked()).thenReturn(false);
85 when(mDockManager.isHidden()).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -070086
Beverlycc4a62f2019-09-26 14:55:28 -040087 mMachine = new DozeMachine(mServiceFake, mConfigMock, mWakeLockFake,
Jerry Changae4dc4b2019-10-16 18:45:03 +080088 mWakefulnessLifecycle, mock(BatteryController.class), mDozeLog, mDockManager);
Adrian Roosff2c4562016-11-03 12:13:36 -070089 mMachine.setParts(new DozeMachine.Part[]{mPartMock});
90 }
91
92 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070093 public void testInitialize_initializesParts() {
94 mMachine.requestState(INITIALIZED);
95
96 verify(mPartMock).transitionTo(UNINITIALIZED, INITIALIZED);
97 }
98
99 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700100 public void testInitialize_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000101 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700102
103 mMachine.requestState(INITIALIZED);
104
105 verify(mPartMock).transitionTo(INITIALIZED, DOZE);
106 assertEquals(DOZE, mMachine.getState());
107 }
108
109 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700110 public void testInitialize_goesToAod() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000111 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -0700112
113 mMachine.requestState(INITIALIZED);
114
115 verify(mPartMock).transitionTo(INITIALIZED, DOZE_AOD);
116 assertEquals(DOZE_AOD, mMachine.getState());
117 }
118
119 @Test
Jerry Changae4dc4b2019-10-16 18:45:03 +0800120 public void testInitialize_afterDocked_goesToDockedAod() {
121 when(mDockManager.isDocked()).thenReturn(true);
122
123 mMachine.requestState(INITIALIZED);
124
125 verify(mPartMock).transitionTo(INITIALIZED, DOZE_AOD_DOCKED);
126 assertEquals(DOZE_AOD_DOCKED, mMachine.getState());
127 }
128
129 @Test
130 public void testInitialize_afterDockPaused_goesToDoze() {
131 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
132 when(mDockManager.isDocked()).thenReturn(true);
133 when(mDockManager.isHidden()).thenReturn(true);
134
135 mMachine.requestState(INITIALIZED);
136
137 verify(mPartMock).transitionTo(INITIALIZED, DOZE);
138 assertEquals(DOZE, mMachine.getState());
139 }
140
141 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700142 public void testPulseDone_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000143 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700144 mMachine.requestState(INITIALIZED);
Ned Burns30d67702020-01-28 12:58:45 -0500145 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700146 mMachine.requestState(DOZE_PULSING);
147
148 mMachine.requestState(DOZE_PULSE_DONE);
149
150 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE);
151 assertEquals(DOZE, mMachine.getState());
152 }
153
154 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700155 public void testPulseDone_goesToAoD() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000156 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -0700157 mMachine.requestState(INITIALIZED);
Ned Burns30d67702020-01-28 12:58:45 -0500158 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700159 mMachine.requestState(DOZE_PULSING);
160
161 mMachine.requestState(DOZE_PULSE_DONE);
162
163 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE_AOD);
164 assertEquals(DOZE_AOD, mMachine.getState());
165 }
166
167 @Test
Jerry Changae4dc4b2019-10-16 18:45:03 +0800168 public void testPulseDone_afterDocked_goesToDockedAoD() {
169 when(mDockManager.isDocked()).thenReturn(true);
170 mMachine.requestState(INITIALIZED);
Ned Burns30d67702020-01-28 12:58:45 -0500171 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Jerry Changae4dc4b2019-10-16 18:45:03 +0800172 mMachine.requestState(DOZE_PULSING);
173
174 mMachine.requestState(DOZE_PULSE_DONE);
175
176 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE_AOD_DOCKED);
177 assertEquals(DOZE_AOD_DOCKED, mMachine.getState());
178 }
179
180 @Test
181 public void testPulseDone_afterDockPaused_goesToDoze() {
182 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
183 when(mDockManager.isDocked()).thenReturn(true);
184 when(mDockManager.isHidden()).thenReturn(true);
185 mMachine.requestState(INITIALIZED);
Ned Burns30d67702020-01-28 12:58:45 -0500186 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Jerry Changae4dc4b2019-10-16 18:45:03 +0800187 mMachine.requestState(DOZE_PULSING);
188
189 mMachine.requestState(DOZE_PULSE_DONE);
190
191 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE);
192 assertEquals(DOZE, mMachine.getState());
193 }
194
195 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700196 public void testFinished_staysFinished() {
197 mMachine.requestState(INITIALIZED);
198 mMachine.requestState(FINISH);
199 reset(mPartMock);
200
201 mMachine.requestState(DOZE);
202
203 verify(mPartMock, never()).transitionTo(any(), any());
204 assertEquals(FINISH, mMachine.getState());
205 }
206
207 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700208 public void testFinish_finishesService() {
209 mMachine.requestState(INITIALIZED);
210
211 mMachine.requestState(FINISH);
212
213 assertTrue(mServiceFake.finished);
214 }
215
216 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700217 public void testWakeLock_heldInTransition() {
218 doAnswer((inv) -> {
219 assertTrue(mWakeLockFake.isHeld());
220 return null;
221 }).when(mPartMock).transitionTo(any(), any());
222
223 mMachine.requestState(INITIALIZED);
224 }
225
226 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700227 public void testWakeLock_heldInPulseStates() {
228 mMachine.requestState(INITIALIZED);
229
Ned Burns30d67702020-01-28 12:58:45 -0500230 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700231 assertTrue(mWakeLockFake.isHeld());
232
233 mMachine.requestState(DOZE_PULSING);
234 assertTrue(mWakeLockFake.isHeld());
235 }
236
237 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700238 public void testWakeLock_notHeldInDozeStates() {
239 mMachine.requestState(INITIALIZED);
240
241 mMachine.requestState(DOZE);
242 assertFalse(mWakeLockFake.isHeld());
243
244 mMachine.requestState(DOZE_AOD);
245 assertFalse(mWakeLockFake.isHeld());
246 }
247
248 @Test
Adrian Roosae0c5e82016-11-16 19:56:19 -0800249 public void testWakeLock_releasedAfterPulse() {
250 mMachine.requestState(INITIALIZED);
251
252 mMachine.requestState(DOZE);
Ned Burns30d67702020-01-28 12:58:45 -0500253 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800254 mMachine.requestState(DOZE_PULSING);
255 mMachine.requestState(DOZE_PULSE_DONE);
256
257 assertFalse(mWakeLockFake.isHeld());
258 }
259
260 @Test
Adrian Roosb84dc182016-12-02 09:01:09 -0800261 public void testPulseDuringPulse_doesntCrash() {
262 mMachine.requestState(INITIALIZED);
263
264 mMachine.requestState(DOZE);
Ned Burns30d67702020-01-28 12:58:45 -0500265 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800266 mMachine.requestState(DOZE_PULSING);
Ned Burns30d67702020-01-28 12:58:45 -0500267 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800268 mMachine.requestState(DOZE_PULSE_DONE);
269 }
270
271 @Test
Adrian Rooscd139a62016-12-16 12:23:51 -0800272 public void testSuppressingPulse_doesntCrash() {
273 mMachine.requestState(INITIALIZED);
274
275 mMachine.requestState(DOZE);
Ned Burns30d67702020-01-28 12:58:45 -0500276 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Rooscd139a62016-12-16 12:23:51 -0800277 mMachine.requestState(DOZE_PULSE_DONE);
278 }
279
280 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700281 public void testTransitions_canRequestTransitions() {
282 mMachine.requestState(INITIALIZED);
283 mMachine.requestState(DOZE);
284 doAnswer(inv -> {
285 mMachine.requestState(DOZE_PULSING);
286 return null;
287 }).when(mPartMock).transitionTo(any(), eq(DOZE_REQUEST_PULSE));
288
Ned Burns30d67702020-01-28 12:58:45 -0500289 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700290
291 assertEquals(DOZE_PULSING, mMachine.getState());
292 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100293
294 @Test
Adrian Roosd7b9d102017-04-28 15:42:58 -0700295 public void testPulseReason_getMatchesRequest() {
296 mMachine.requestState(INITIALIZED);
297 mMachine.requestState(DOZE);
Ned Burns30d67702020-01-28 12:58:45 -0500298 mMachine.requestPulse(DozeLog.REASON_SENSOR_DOUBLE_TAP);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700299
Ned Burns30d67702020-01-28 12:58:45 -0500300 assertEquals(DozeLog.REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
Adrian Roosd7b9d102017-04-28 15:42:58 -0700301 }
302
303 @Test
304 public void testPulseReason_getFromTransition() {
305 mMachine.requestState(INITIALIZED);
306 mMachine.requestState(DOZE);
307 doAnswer(inv -> {
308 DozeMachine.State newState = inv.getArgument(1);
309 if (newState == DOZE_REQUEST_PULSE
310 || newState == DOZE_PULSING
311 || newState == DOZE_PULSE_DONE) {
Ned Burns30d67702020-01-28 12:58:45 -0500312 assertEquals(DozeLog.PULSE_REASON_NOTIFICATION, mMachine.getPulseReason());
Adrian Roosd7b9d102017-04-28 15:42:58 -0700313 } else {
314 assertTrue("unexpected state " + newState,
315 newState == DOZE || newState == DOZE_AOD);
316 }
317 return null;
318 }).when(mPartMock).transitionTo(any(), any());
319
Ned Burns30d67702020-01-28 12:58:45 -0500320 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700321 mMachine.requestState(DOZE_PULSING);
322 mMachine.requestState(DOZE_PULSE_DONE);
323 }
324
325 @Test
Adrian Roos4fb1f512017-02-14 14:01:32 +0100326 public void testWakeUp_wakesUp() {
327 mMachine.wakeUp();
328
329 assertTrue(mServiceFake.requestedWakeup);
330 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700331}