blob: f972508c03753f1b43fa97396031ed3a40ae8b5b [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;
Adrian Roosff2c4562016-11-03 12:13:36 -070042import android.support.test.filters.SmallTest;
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
Jason Monkfba8faf2017-05-23 10:42:59 -040046import com.android.systemui.SysuiTestCase;
Adrian Roosc1b50322017-02-27 21:07:58 +010047import com.android.systemui.util.wakelock.WakeLockFake;
Adrian Roosff2c4562016-11-03 12:13:36 -070048
49import org.junit.Before;
50import org.junit.Test;
51import org.junit.runner.RunWith;
52
53@SmallTest
Jason Monk340b0e52017-03-08 14:57:56 -050054@RunWith(AndroidTestingRunner.class)
Jason Monkba364322017-03-06 11:19:20 -050055@UiThreadTest
Jason Monkfba8faf2017-05-23 10:42:59 -040056public class DozeMachineTest extends SysuiTestCase {
Adrian Roosff2c4562016-11-03 12:13:36 -070057
58 DozeMachine mMachine;
59
60 private DozeServiceFake mServiceFake;
61 private WakeLockFake mWakeLockFake;
Adrian Roos0261fb22017-03-07 20:20:35 +000062 private AmbientDisplayConfiguration mConfigMock;
Adrian Roosff2c4562016-11-03 12:13:36 -070063 private DozeMachine.Part mPartMock;
64
65 @Before
66 public void setUp() {
67 mServiceFake = new DozeServiceFake();
68 mWakeLockFake = new WakeLockFake();
Adrian Roos0261fb22017-03-07 20:20:35 +000069 mConfigMock = mock(AmbientDisplayConfiguration.class);
Adrian Roosff2c4562016-11-03 12:13:36 -070070 mPartMock = mock(DozeMachine.Part.class);
71
Adrian Roos0261fb22017-03-07 20:20:35 +000072 mMachine = new DozeMachine(mServiceFake, mConfigMock, mWakeLockFake);
Adrian Roosff2c4562016-11-03 12:13:36 -070073
74 mMachine.setParts(new DozeMachine.Part[]{mPartMock});
75 }
76
77 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070078 public void testInitialize_initializesParts() {
79 mMachine.requestState(INITIALIZED);
80
81 verify(mPartMock).transitionTo(UNINITIALIZED, INITIALIZED);
82 }
83
84 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070085 public void testInitialize_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +000086 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -070087
88 mMachine.requestState(INITIALIZED);
89
90 verify(mPartMock).transitionTo(INITIALIZED, DOZE);
91 assertEquals(DOZE, mMachine.getState());
92 }
93
94 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -070095 public void testInitialize_goesToAod() {
Adrian Roos0261fb22017-03-07 20:20:35 +000096 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -070097
98 mMachine.requestState(INITIALIZED);
99
100 verify(mPartMock).transitionTo(INITIALIZED, DOZE_AOD);
101 assertEquals(DOZE_AOD, mMachine.getState());
102 }
103
104 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700105 public void testPulseDone_goesToDoze() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000106 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(false);
Adrian Roosff2c4562016-11-03 12:13:36 -0700107 mMachine.requestState(INITIALIZED);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700108 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700109 mMachine.requestState(DOZE_PULSING);
110
111 mMachine.requestState(DOZE_PULSE_DONE);
112
113 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE);
114 assertEquals(DOZE, mMachine.getState());
115 }
116
117 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700118 public void testPulseDone_goesToAoD() {
Adrian Roos0261fb22017-03-07 20:20:35 +0000119 when(mConfigMock.alwaysOnEnabled(anyInt())).thenReturn(true);
Adrian Roosff2c4562016-11-03 12:13:36 -0700120 mMachine.requestState(INITIALIZED);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700121 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700122 mMachine.requestState(DOZE_PULSING);
123
124 mMachine.requestState(DOZE_PULSE_DONE);
125
126 verify(mPartMock).transitionTo(DOZE_PULSE_DONE, DOZE_AOD);
127 assertEquals(DOZE_AOD, mMachine.getState());
128 }
129
130 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700131 public void testFinished_staysFinished() {
132 mMachine.requestState(INITIALIZED);
133 mMachine.requestState(FINISH);
134 reset(mPartMock);
135
136 mMachine.requestState(DOZE);
137
138 verify(mPartMock, never()).transitionTo(any(), any());
139 assertEquals(FINISH, mMachine.getState());
140 }
141
142 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700143 public void testFinish_finishesService() {
144 mMachine.requestState(INITIALIZED);
145
146 mMachine.requestState(FINISH);
147
148 assertTrue(mServiceFake.finished);
149 }
150
151 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700152 public void testWakeLock_heldInTransition() {
153 doAnswer((inv) -> {
154 assertTrue(mWakeLockFake.isHeld());
155 return null;
156 }).when(mPartMock).transitionTo(any(), any());
157
158 mMachine.requestState(INITIALIZED);
159 }
160
161 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700162 public void testWakeLock_heldInPulseStates() {
163 mMachine.requestState(INITIALIZED);
164
Adrian Roosd7b9d102017-04-28 15:42:58 -0700165 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700166 assertTrue(mWakeLockFake.isHeld());
167
168 mMachine.requestState(DOZE_PULSING);
169 assertTrue(mWakeLockFake.isHeld());
170 }
171
172 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700173 public void testWakeLock_notHeldInDozeStates() {
174 mMachine.requestState(INITIALIZED);
175
176 mMachine.requestState(DOZE);
177 assertFalse(mWakeLockFake.isHeld());
178
179 mMachine.requestState(DOZE_AOD);
180 assertFalse(mWakeLockFake.isHeld());
181 }
182
183 @Test
Adrian Roosae0c5e82016-11-16 19:56:19 -0800184 public void testWakeLock_releasedAfterPulse() {
185 mMachine.requestState(INITIALIZED);
186
187 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700188 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosae0c5e82016-11-16 19:56:19 -0800189 mMachine.requestState(DOZE_PULSING);
190 mMachine.requestState(DOZE_PULSE_DONE);
191
192 assertFalse(mWakeLockFake.isHeld());
193 }
194
195 @Test
Adrian Roosb84dc182016-12-02 09:01:09 -0800196 public void testPulseDuringPulse_doesntCrash() {
197 mMachine.requestState(INITIALIZED);
198
199 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700200 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800201 mMachine.requestState(DOZE_PULSING);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700202 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosb84dc182016-12-02 09:01:09 -0800203 mMachine.requestState(DOZE_PULSE_DONE);
204 }
205
206 @Test
Adrian Rooscd139a62016-12-16 12:23:51 -0800207 public void testSuppressingPulse_doesntCrash() {
208 mMachine.requestState(INITIALIZED);
209
210 mMachine.requestState(DOZE);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700211 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Rooscd139a62016-12-16 12:23:51 -0800212 mMachine.requestState(DOZE_PULSE_DONE);
213 }
214
215 @Test
Adrian Roosff2c4562016-11-03 12:13:36 -0700216 public void testTransitions_canRequestTransitions() {
217 mMachine.requestState(INITIALIZED);
218 mMachine.requestState(DOZE);
219 doAnswer(inv -> {
220 mMachine.requestState(DOZE_PULSING);
221 return null;
222 }).when(mPartMock).transitionTo(any(), eq(DOZE_REQUEST_PULSE));
223
Adrian Roosd7b9d102017-04-28 15:42:58 -0700224 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
Adrian Roosff2c4562016-11-03 12:13:36 -0700225
226 assertEquals(DOZE_PULSING, mMachine.getState());
227 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100228
229 @Test
Adrian Roosd7b9d102017-04-28 15:42:58 -0700230 public void testPulseReason_getMatchesRequest() {
231 mMachine.requestState(INITIALIZED);
232 mMachine.requestState(DOZE);
Lucas Dupin3d053532019-01-29 12:35:22 -0800233 mMachine.requestPulse(DozeLog.REASON_SENSOR_DOUBLE_TAP);
Adrian Roosd7b9d102017-04-28 15:42:58 -0700234
Lucas Dupin3d053532019-01-29 12:35:22 -0800235 assertEquals(DozeLog.REASON_SENSOR_DOUBLE_TAP, mMachine.getPulseReason());
Adrian Roosd7b9d102017-04-28 15:42:58 -0700236 }
237
238 @Test
239 public void testPulseReason_getFromTransition() {
240 mMachine.requestState(INITIALIZED);
241 mMachine.requestState(DOZE);
242 doAnswer(inv -> {
243 DozeMachine.State newState = inv.getArgument(1);
244 if (newState == DOZE_REQUEST_PULSE
245 || newState == DOZE_PULSING
246 || newState == DOZE_PULSE_DONE) {
247 assertEquals(DozeLog.PULSE_REASON_NOTIFICATION, mMachine.getPulseReason());
248 } else {
249 assertTrue("unexpected state " + newState,
250 newState == DOZE || newState == DOZE_AOD);
251 }
252 return null;
253 }).when(mPartMock).transitionTo(any(), any());
254
255 mMachine.requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
256 mMachine.requestState(DOZE_PULSING);
257 mMachine.requestState(DOZE_PULSE_DONE);
258 }
259
260 @Test
Adrian Roos4fb1f512017-02-14 14:01:32 +0100261 public void testWakeUp_wakesUp() {
262 mMachine.wakeUp();
263
264 assertTrue(mServiceFake.requestedWakeup);
265 }
Adrian Roosff2c4562016-11-03 12:13:36 -0700266}