blob: 6dfb19ea075a4833909dc19fde11784090919add [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;
Adrian Roosc1b50322017-02-27 21:07:58 +010048import com.android.systemui.util.wakelock.WakeLockFake;
Adrian Roosff2c4562016-11-03 12:13:36 -070049
50import org.junit.Before;
51import org.junit.Test;
52import org.junit.runner.RunWith;
53
54@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050055@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050056@UiThreadTest
Jason Monkfba8faf2017-05-23 10:42:59 -040057public class DozeMachineTest extends SysuiTestCase {
Adrian Roosff2c4562016-11-03 12:13:36 -070058
59 DozeMachine mMachine;
60
61 private DozeServiceFake mServiceFake;
62 private WakeLockFake mWakeLockFake;
Adrian Roos0261fb22017-03-07 20:20:35 +000063 private AmbientDisplayConfiguration mConfigMock;
Adrian Roosff2c4562016-11-03 12:13:36 -070064 private DozeMachine.Part mPartMock;
65
66 @Before
67 public void setUp() {
68 mServiceFake = new DozeServiceFake();
69 mWakeLockFake = new WakeLockFake();
Adrian Roos0261fb22017-03-07 20:20:35 +000070 mConfigMock = mock(AmbientDisplayConfiguration.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070071 mPartMock = mock(DozeMachine.Part.class);
72
Adrian Roos0261fb22017-03-07 20:20:35 +000073 mMachine = new DozeMachine(mServiceFake, mConfigMock, mWakeLockFake);
Adrian Roosff2c4562016-11-03 12:13:36 -070074
75 mMachine.setParts(new DozeMachine.Part[]{mPartMock});
76 }
77
78 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070079 public void testInitialize_initializesParts() {
80 mMachine.requestState(INITIALIZED);
81
82 verify(mPartMock).transitionTo(UNINITIALIZED, INITIALIZED);
83 }
84
85 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070086 public void testInitialize_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +000087 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -070088
89 mMachine.requestState(INITIALIZED);
90
91 verify(mPartMock).transitionTo(INITIALIZED, DOZE);
92 assertEquals(DOZE, mMachine.getState());
93 }
94
95 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070096 public void testInitialize_goesToAod() {
Adrian Roos0261fb22017-03-07 20:20:35 +000097 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -070098
99 mMachine.requestState(INITIALIZED);
100
101 verify(mPartMock).transitionTo(INITIALIZED, DOZE_AOD);
102 assertEquals(DOZE_AOD, mMachine.getState());
103 }
104
105 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700106 public void testPulseDone_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000107 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700108 mMachine.requestState(INITIALIZED);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700109 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700110 mMachine.requestState(DOZE_PULSING);
111
112 mMachine.requestState(DOZE_PULSE_DONE);
113
114 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE);
115 assertEquals(DOZE, mMachine.getState());
116 }
117
118 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700119 public void testPulseDone_goesToAoD() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000120 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -0700121 mMachine.requestState(INITIALIZED);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700122 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700123 mMachine.requestState(DOZE_PULSING);
124
125 mMachine.requestState(DOZE_PULSE_DONE);
126
127 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE_AOD);
128 assertEquals(DOZE_AOD, mMachine.getState());
129 }
130
131 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700132 public void testFinished_staysFinished() {
133 mMachine.requestState(INITIALIZED);
134 mMachine.requestState(FINISH);
135 reset(mPartMock);
136
137 mMachine.requestState(DOZE);
138
139 verify(mPartMock, never()).transitionTo(any(), any());
140 assertEquals(FINISH, mMachine.getState());
141 }
142
143 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700144 public void testFinish_finishesService() {
145 mMachine.requestState(INITIALIZED);
146
147 mMachine.requestState(FINISH);
148
149 assertTrue(mServiceFake.finished);
150 }
151
152 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700153 public void testWakeLock_heldInTransition() {
154 doAnswer((inv) -> {
155 assertTrue(mWakeLockFake.isHeld());
156 return null;
157 }).when(mPartMock).transitionTo(any(), any());
158
159 mMachine.requestState(INITIALIZED);
160 }
161
162 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700163 public void testWakeLock_heldInPulseStates() {
164 mMachine.requestState(INITIALIZED);
165
Adrian Roosd7b9d102017-04-28 15:42:58 -0700166 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700167 assertTrue(mWakeLockFake.isHeld());
168
169 mMachine.requestState(DOZE_PULSING);
170 assertTrue(mWakeLockFake.isHeld());
171 }
172
173 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700174 public void testWakeLock_notHeldInDozeStates() {
175 mMachine.requestState(INITIALIZED);
176
177 mMachine.requestState(DOZE);
178 assertFalse(mWakeLockFake.isHeld());
179
180 mMachine.requestState(DOZE_AOD);
181 assertFalse(mWakeLockFake.isHeld());
182 }
183
184 @Test
Adrian Roosae0c5e82016-11-16 19:56:19 -0800185 public void testWakeLock_releasedAfterPulse() {
186 mMachine.requestState(INITIALIZED);
187
188 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700189 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800190 mMachine.requestState(DOZE_PULSING);
191 mMachine.requestState(DOZE_PULSE_DONE);
192
193 assertFalse(mWakeLockFake.isHeld());
194 }
195
196 @Test
Adrian Roosb84dc182016-12-02 09:01:09 -0800197 public void testPulseDuringPulse_doesntCrash() {
198 mMachine.requestState(INITIALIZED);
199
200 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700201 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800202 mMachine.requestState(DOZE_PULSING);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700203 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800204 mMachine.requestState(DOZE_PULSE_DONE);
205 }
206
207 @Test
Adrian Rooscd139a62016-12-16 12:23:51 -0800208 public void testSuppressingPulse_doesntCrash() {
209 mMachine.requestState(INITIALIZED);
210
211 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700212 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Rooscd139a62016-12-16 12:23:51 -0800213 mMachine.requestState(DOZE_PULSE_DONE);
214 }
215
216 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700217 public void testTransitions_canRequestTransitions() {
218 mMachine.requestState(INITIALIZED);
219 mMachine.requestState(DOZE);
220 doAnswer(inv -> {
221 mMachine.requestState(DOZE_PULSING);
222 return null;
223 }).when(mPartMock).transitionTo(any(), eq(DOZE_REQUEST_PULSE));
224
Adrian Roosd7b9d102017-04-28 15:42:58 -0700225 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700226
227 assertEquals(DOZE_PULSING, mMachine.getState());
228 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100229
230 @Test
Adrian Roosd7b9d102017-04-28 15:42:58 -0700231 public void testPulseReason_getMatchesRequest() {
232 mMachine.requestState(INITIALIZED);
233 mMachine.requestState(DOZE);
Lucas Dupin3d053532019-01-29 12:35:22 -0800234 mMachine.requestPulse(DozeLog.REASON_SENSOR_DOUBLE_TAP);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700235
Lucas Dupin3d053532019-01-29 12:35:22 -0800236 assertEquals(DozeLog.REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
Adrian Roosd7b9d102017-04-28 15:42:58 -0700237 }
238
239 @Test
240 public void testPulseReason_getFromTransition() {
241 mMachine.requestState(INITIALIZED);
242 mMachine.requestState(DOZE);
243 doAnswer(inv -> {
244 DozeMachine.State newState = inv.getArgument(1);
245 if (newState == DOZE_REQUEST_PULSE
246 || newState == DOZE_PULSING
247 || newState == DOZE_PULSE_DONE) {
248 assertEquals(DozeLog.PULSE_REASON_NOTIFICATION, mMachine.getPulseReason());
249 } else {
250 assertTrue("unexpected state " + newState,
251 newState == DOZE || newState == DOZE_AOD);
252 }
253 return null;
254 }).when(mPartMock).transitionTo(any(), any());
255
256 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
257 mMachine.requestState(DOZE_PULSING);
258 mMachine.requestState(DOZE_PULSE_DONE);
259 }
260
261 @Test
Adrian Roos4fb1f512017-02-14 14:01:32 +0100262 public void testWakeUp_wakesUp() {
263 mMachine.wakeUp();
264
265 assertTrue(mServiceFake.requestedWakeup);
266 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700267}