blob: 1e18e51bc079f7590b1fdf52bb285d7e54b10840 [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;
21import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSE_DONE;
22import static com.android.systemui.doze.DozeMachine.State.DOZE_PULSING;
23import static com.android.systemui.doze.DozeMachine.State.DOZE_REQUEST_PULSE;
24import static com.android.systemui.doze.DozeMachine.State.FINISH;
25import static com.android.systemui.doze.DozeMachine.State.INITIALIZED;
26import static com.android.systemui.doze.DozeMachine.State.UNINITIALIZED;
27
28import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertTrue;
Adrian Roos0261fb22017-03-07 20:20:35 +000031import static org.mockito.ArgumentMatchers.any;
Jason Monka716bac2018-12-05 15:48:21 -050032import static org.mockito.ArgumentMatchers.anyInt;
Adrian Roos0261fb22017-03-07 20:20:35 +000033import static org.mockito.ArgumentMatchers.eq;
Adrian Roosff2c4562016-11-03 12:13:36 -070034import static org.mockito.Mockito.doAnswer;
35import static org.mockito.Mockito.mock;
36import static org.mockito.Mockito.never;
37import static org.mockito.Mockito.reset;
38import static org.mockito.Mockito.verify;
39import static org.mockito.Mockito.when;
40
Issei Suzukica19e6e2019-02-26 12:39:11 +010041import android.hardware.display.AmbientDisplayConfiguration;
Jason Monk340b0e52017-03-08 14:57:56 -050042import android.testing.AndroidTestingRunner;
Jason Monka716bac2018-12-05 15:48:21 -050043import android.testing.UiThreadTest;
Adrian Roosff2c4562016-11-03 12:13:36 -070044
Brett Chabot84151d92019-02-27 15:37:59 -080045import androidx.test.filters.SmallTest;
46
Jason Monkfba8faf2017-05-23 10:42:59 -040047import com.android.systemui.SysuiTestCase;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070048import com.android.systemui.keyguard.WakefulnessLifecycle;
Adrian Roosc1b50322017-02-27 21:07:58 +010049import com.android.systemui.util.wakelock.WakeLockFake;
Adrian Roosff2c4562016-11-03 12:13:36 -070050
51import org.junit.Before;
52import org.junit.Test;
53import org.junit.runner.RunWith;
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070054import org.mockito.Mock;
55import org.mockito.MockitoAnnotations;
Adrian Roosff2c4562016-11-03 12:13:36 -070056
57@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050058@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050059@UiThreadTest
Jason Monkfba8faf2017-05-23 10:42:59 -040060public class DozeMachineTest extends SysuiTestCase {
Adrian Roosff2c4562016-11-03 12:13:36 -070061
62 DozeMachine mMachine;
63
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070064 @Mock
65 private WakefulnessLifecycle mWakefulnessLifecycle;
Adrian Roosff2c4562016-11-03 12:13:36 -070066 private DozeServiceFake mServiceFake;
67 private WakeLockFake mWakeLockFake;
Adrian Roos0261fb22017-03-07 20:20:35 +000068 private AmbientDisplayConfiguration mConfigMock;
Adrian Roosff2c4562016-11-03 12:13:36 -070069 private DozeMachine.Part mPartMock;
70
71 @Before
72 public void setUp() {
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070073 MockitoAnnotations.initMocks(this);
Adrian Roosff2c4562016-11-03 12:13:36 -070074 mServiceFake = new DozeServiceFake();
75 mWakeLockFake = new WakeLockFake();
Adrian Roos0261fb22017-03-07 20:20:35 +000076 mConfigMock = mock(AmbientDisplayConfiguration.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070077 mPartMock = mock(DozeMachine.Part.class);
78
Lucas Dupin7a0e5bc2019-08-09 10:35:34 -070079 mMachine = new DozeMachine(mServiceFake, mConfigMock, mWakeLockFake, mWakefulnessLifecycle);
Adrian Roosff2c4562016-11-03 12:13:36 -070080
81 mMachine.setParts(new DozeMachine.Part[]{mPartMock});
82 }
83
84 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070085 public void testInitialize_initializesParts() {
86 mMachine.requestState(INITIALIZED);
87
88 verify(mPartMock).transitionTo(UNINITIALIZED, INITIALIZED);
89 }
90
91 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070092 public void testInitialize_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +000093 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -070094
95 mMachine.requestState(INITIALIZED);
96
97 verify(mPartMock).transitionTo(INITIALIZED, DOZE);
98 assertEquals(DOZE, mMachine.getState());
99 }
100
101 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700102 public void testInitialize_goesToAod() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000103 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -0700104
105 mMachine.requestState(INITIALIZED);
106
107 verify(mPartMock).transitionTo(INITIALIZED, DOZE_AOD);
108 assertEquals(DOZE_AOD, mMachine.getState());
109 }
110
111 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700112 public void testPulseDone_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000113 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700114 mMachine.requestState(INITIALIZED);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700115 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700116 mMachine.requestState(DOZE_PULSING);
117
118 mMachine.requestState(DOZE_PULSE_DONE);
119
120 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE);
121 assertEquals(DOZE, mMachine.getState());
122 }
123
124 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700125 public void testPulseDone_goesToAoD() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000126 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -0700127 mMachine.requestState(INITIALIZED);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700128 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700129 mMachine.requestState(DOZE_PULSING);
130
131 mMachine.requestState(DOZE_PULSE_DONE);
132
133 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE_AOD);
134 assertEquals(DOZE_AOD, mMachine.getState());
135 }
136
137 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700138 public void testFinished_staysFinished() {
139 mMachine.requestState(INITIALIZED);
140 mMachine.requestState(FINISH);
141 reset(mPartMock);
142
143 mMachine.requestState(DOZE);
144
145 verify(mPartMock, never()).transitionTo(any(), any());
146 assertEquals(FINISH, mMachine.getState());
147 }
148
149 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700150 public void testFinish_finishesService() {
151 mMachine.requestState(INITIALIZED);
152
153 mMachine.requestState(FINISH);
154
155 assertTrue(mServiceFake.finished);
156 }
157
158 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700159 public void testWakeLock_heldInTransition() {
160 doAnswer((inv) -> {
161 assertTrue(mWakeLockFake.isHeld());
162 return null;
163 }).when(mPartMock).transitionTo(any(), any());
164
165 mMachine.requestState(INITIALIZED);
166 }
167
168 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700169 public void testWakeLock_heldInPulseStates() {
170 mMachine.requestState(INITIALIZED);
171
Adrian Roosd7b9d102017-04-28 15:42:58 -0700172 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700173 assertTrue(mWakeLockFake.isHeld());
174
175 mMachine.requestState(DOZE_PULSING);
176 assertTrue(mWakeLockFake.isHeld());
177 }
178
179 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700180 public void testWakeLock_notHeldInDozeStates() {
181 mMachine.requestState(INITIALIZED);
182
183 mMachine.requestState(DOZE);
184 assertFalse(mWakeLockFake.isHeld());
185
186 mMachine.requestState(DOZE_AOD);
187 assertFalse(mWakeLockFake.isHeld());
188 }
189
190 @Test
Adrian Roosae0c5e82016-11-16 19:56:19 -0800191 public void testWakeLock_releasedAfterPulse() {
192 mMachine.requestState(INITIALIZED);
193
194 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700195 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800196 mMachine.requestState(DOZE_PULSING);
197 mMachine.requestState(DOZE_PULSE_DONE);
198
199 assertFalse(mWakeLockFake.isHeld());
200 }
201
202 @Test
Adrian Roosb84dc182016-12-02 09:01:09 -0800203 public void testPulseDuringPulse_doesntCrash() {
204 mMachine.requestState(INITIALIZED);
205
206 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700207 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800208 mMachine.requestState(DOZE_PULSING);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700209 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800210 mMachine.requestState(DOZE_PULSE_DONE);
211 }
212
213 @Test
Adrian Rooscd139a62016-12-16 12:23:51 -0800214 public void testSuppressingPulse_doesntCrash() {
215 mMachine.requestState(INITIALIZED);
216
217 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700218 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Rooscd139a62016-12-16 12:23:51 -0800219 mMachine.requestState(DOZE_PULSE_DONE);
220 }
221
222 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700223 public void testTransitions_canRequestTransitions() {
224 mMachine.requestState(INITIALIZED);
225 mMachine.requestState(DOZE);
226 doAnswer(inv -> {
227 mMachine.requestState(DOZE_PULSING);
228 return null;
229 }).when(mPartMock).transitionTo(any(), eq(DOZE_REQUEST_PULSE));
230
Adrian Roosd7b9d102017-04-28 15:42:58 -0700231 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700232
233 assertEquals(DOZE_PULSING, mMachine.getState());
234 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100235
236 @Test
Adrian Roosd7b9d102017-04-28 15:42:58 -0700237 public void testPulseReason_getMatchesRequest() {
238 mMachine.requestState(INITIALIZED);
239 mMachine.requestState(DOZE);
Lucas Dupin3d053532019-01-29 12:35:22 -0800240 mMachine.requestPulse(DozeLog.REASON_SENSOR_DOUBLE_TAP);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700241
Lucas Dupin3d053532019-01-29 12:35:22 -0800242 assertEquals(DozeLog.REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
Adrian Roosd7b9d102017-04-28 15:42:58 -0700243 }
244
245 @Test
246 public void testPulseReason_getFromTransition() {
247 mMachine.requestState(INITIALIZED);
248 mMachine.requestState(DOZE);
249 doAnswer(inv -> {
250 DozeMachine.State newState = inv.getArgument(1);
251 if (newState == DOZE_REQUEST_PULSE
252 || newState == DOZE_PULSING
253 || newState == DOZE_PULSE_DONE) {
254 assertEquals(DozeLog.PULSE_REASON_NOTIFICATION, mMachine.getPulseReason());
255 } else {
256 assertTrue("unexpected state " + newState,
257 newState == DOZE || newState == DOZE_AOD);
258 }
259 return null;
260 }).when(mPartMock).transitionTo(any(), any());
261
262 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
263 mMachine.requestState(DOZE_PULSING);
264 mMachine.requestState(DOZE_PULSE_DONE);
265 }
266
267 @Test
Adrian Roos4fb1f512017-02-14 14:01:32 +0100268 public void testWakeUp_wakesUp() {
269 mMachine.wakeUp();
270
271 assertTrue(mServiceFake.requestedWakeup);
272 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700273}