blob: e51ee947cba1edafe8daf2cb511eeb15ab5eca35 [file] [log] [blame]
Kweku Adams00e3a372018-09-28 16:57:09 -07001/*
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 */
16package com.android.server;
17
18import static androidx.test.InstrumentationRegistry.getContext;
19
20import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
21import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080022import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder;
Kweku Adams00e3a372018-09-28 16:57:09 -070023import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
24import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
25import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
26import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
27import static com.android.server.DeviceIdleController.LIGHT_STATE_ACTIVE;
28import static com.android.server.DeviceIdleController.LIGHT_STATE_IDLE;
29import static com.android.server.DeviceIdleController.LIGHT_STATE_IDLE_MAINTENANCE;
30import static com.android.server.DeviceIdleController.LIGHT_STATE_INACTIVE;
31import static com.android.server.DeviceIdleController.LIGHT_STATE_OVERRIDE;
32import static com.android.server.DeviceIdleController.LIGHT_STATE_PRE_IDLE;
33import static com.android.server.DeviceIdleController.LIGHT_STATE_WAITING_FOR_NETWORK;
34import static com.android.server.DeviceIdleController.STATE_ACTIVE;
35import static com.android.server.DeviceIdleController.STATE_IDLE;
36import static com.android.server.DeviceIdleController.STATE_IDLE_MAINTENANCE;
37import static com.android.server.DeviceIdleController.STATE_IDLE_PENDING;
38import static com.android.server.DeviceIdleController.STATE_INACTIVE;
39import static com.android.server.DeviceIdleController.STATE_LOCATING;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070040import static com.android.server.DeviceIdleController.STATE_QUICK_DOZE_DELAY;
Kweku Adams00e3a372018-09-28 16:57:09 -070041import static com.android.server.DeviceIdleController.STATE_SENSING;
42import static com.android.server.DeviceIdleController.lightStateToString;
43import static com.android.server.DeviceIdleController.stateToString;
44
45import static org.junit.Assert.assertEquals;
46import static org.junit.Assert.assertFalse;
47import static org.junit.Assert.assertTrue;
48import static org.junit.Assert.fail;
49import static org.mockito.ArgumentMatchers.any;
Kweku Adams9da2bb92018-12-20 06:34:39 -080050import static org.mockito.ArgumentMatchers.anyBoolean;
Kweku Adams00e3a372018-09-28 16:57:09 -070051import static org.mockito.ArgumentMatchers.anyInt;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070052import static org.mockito.ArgumentMatchers.anyLong;
Kweku Adams00e3a372018-09-28 16:57:09 -070053import static org.mockito.ArgumentMatchers.anyString;
Robin Lee876b88542018-11-13 17:22:24 +010054import static org.mockito.ArgumentMatchers.eq;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080055import static org.mockito.Mockito.never;
56import static org.mockito.Mockito.reset;
57import static org.mockito.Mockito.verify;
Kweku Adams00e3a372018-09-28 16:57:09 -070058
59import android.app.ActivityManagerInternal;
60import android.app.AlarmManager;
61import android.app.IActivityManager;
Kweku Adamsa457f4e2018-10-03 15:56:06 -070062import android.content.ContentResolver;
Kweku Adams00e3a372018-09-28 16:57:09 -070063import android.content.Context;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070064import android.content.Intent;
Robin Lee876b88542018-11-13 17:22:24 +010065import android.hardware.Sensor;
Kweku Adams00e3a372018-09-28 16:57:09 -070066import android.hardware.SensorManager;
67import android.location.LocationManager;
68import android.location.LocationProvider;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070069import android.net.ConnectivityManager;
70import android.net.NetworkInfo;
Kweku Adams00e3a372018-09-28 16:57:09 -070071import android.os.Handler;
72import android.os.Looper;
73import android.os.PowerManager;
74import android.os.PowerManagerInternal;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070075import android.os.PowerSaveState;
Kweku Adamsa457f4e2018-10-03 15:56:06 -070076import android.os.SystemClock;
Kweku Adams00e3a372018-09-28 16:57:09 -070077
78import androidx.test.runner.AndroidJUnit4;
79
Robin Lee876b88542018-11-13 17:22:24 +010080import com.android.server.deviceidle.ConstraintController;
Kweku Adams00e3a372018-09-28 16:57:09 -070081import com.android.server.net.NetworkPolicyManagerInternal;
82import com.android.server.wm.ActivityTaskManagerInternal;
83
84import org.junit.After;
85import org.junit.Before;
86import org.junit.Test;
87import org.junit.runner.RunWith;
Robin Lee876b88542018-11-13 17:22:24 +010088import org.mockito.Answers;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080089import org.mockito.InOrder;
Kweku Adams00e3a372018-09-28 16:57:09 -070090import org.mockito.Mock;
91import org.mockito.MockitoSession;
92import org.mockito.quality.Strictness;
93
94/**
95 * Tests for {@link com.android.server.DeviceIdleController}.
96 */
97@RunWith(AndroidJUnit4.class)
98public class DeviceIdleControllerTest {
99 private DeviceIdleController mDeviceIdleController;
100 private AnyMotionDetectorForTest mAnyMotionDetector;
101 private AppStateTrackerForTest mAppStateTracker;
Kweku Adams9da2bb92018-12-20 06:34:39 -0800102 private DeviceIdleController.Constants mConstants;
Kweku Adams799858b2018-10-08 17:19:08 -0700103 private InjectorForTest mInjector;
Kweku Adams00e3a372018-09-28 16:57:09 -0700104
105 private MockitoSession mMockingSession;
106 @Mock
Kweku Adams00e3a372018-09-28 16:57:09 -0700107 private AlarmManager mAlarmManager;
108 @Mock
Kweku Adams799858b2018-10-08 17:19:08 -0700109 private ConnectivityService mConnectivityService;
110 @Mock
Kweku Adams9da2bb92018-12-20 06:34:39 -0800111 private ContentResolver mContentResolver;
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700112 @Mock
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800113 private DeviceIdleController.MyHandler mHandler;
114 @Mock
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700115 private IActivityManager mIActivityManager;
116 @Mock
Kweku Adams00e3a372018-09-28 16:57:09 -0700117 private LocationManager mLocationManager;
118 @Mock
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700119 private PowerManager mPowerManager;
120 @Mock
121 private PowerManager.WakeLock mWakeLock;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700122 @Mock
123 private PowerManagerInternal mPowerManagerInternal;
Robin Lee876b88542018-11-13 17:22:24 +0100124 @Mock
125 private SensorManager mSensorManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700126
127 class InjectorForTest extends DeviceIdleController.Injector {
Kweku Adams799858b2018-10-08 17:19:08 -0700128 ConnectivityService connectivityService;
129 LocationManager locationManager;
Robin Lee876b88542018-11-13 17:22:24 +0100130 ConstraintController constraintController;
Kweku Adams00e3a372018-09-28 16:57:09 -0700131
132 InjectorForTest(Context ctx) {
133 super(ctx);
134 }
135
136 @Override
137 AlarmManager getAlarmManager() {
138 return mAlarmManager;
139 }
140
141 @Override
142 AnyMotionDetector getAnyMotionDetector(Handler handler, SensorManager sm,
143 AnyMotionDetector.DeviceIdleCallback callback, float angleThreshold) {
144 return mAnyMotionDetector;
145 }
146
147 @Override
148 AppStateTracker getAppStateTracker(Context ctx, Looper loop) {
149 return mAppStateTracker;
150 }
151
152 @Override
153 ConnectivityService getConnectivityService() {
Kweku Adams799858b2018-10-08 17:19:08 -0700154 return connectivityService;
Kweku Adams00e3a372018-09-28 16:57:09 -0700155 }
156
157 @Override
158 LocationManager getLocationManager() {
Kweku Adams799858b2018-10-08 17:19:08 -0700159 return locationManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700160 }
161
162 @Override
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700163 DeviceIdleController.MyHandler getHandler(DeviceIdleController controller) {
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800164 return mHandler;
Kweku Adams00e3a372018-09-28 16:57:09 -0700165 }
166
167 @Override
168 PowerManager getPowerManager() {
169 return mPowerManager;
170 }
Robin Lee876b88542018-11-13 17:22:24 +0100171
172 @Override
173 SensorManager getSensorManager() {
174 return mSensorManager;
175 }
176
177 @Override
178 ConstraintController getConstraintController(
179 Handler handler, DeviceIdleController.LocalService localService) {
180 return constraintController;
181 }
182
183 @Override
184 boolean useMotionSensor() {
185 return true;
186 }
Kweku Adams00e3a372018-09-28 16:57:09 -0700187 }
188
189 private class AnyMotionDetectorForTest extends AnyMotionDetector {
190 boolean isMonitoring = false;
191
192 AnyMotionDetectorForTest() {
Robin Lee876b88542018-11-13 17:22:24 +0100193 super(mPowerManager, mock(Handler.class), mSensorManager,
Kweku Adams00e3a372018-09-28 16:57:09 -0700194 mock(DeviceIdleCallback.class), 0.5f);
195 }
196
197 @Override
Kweku Adams9da2bb92018-12-20 06:34:39 -0800198 public boolean hasSensor() {
199 return true;
200 }
201
202 @Override
Kweku Adams00e3a372018-09-28 16:57:09 -0700203 public void checkForAnyMotion() {
204 isMonitoring = true;
205 }
206
207 @Override
208 public void stop() {
209 isMonitoring = false;
210 }
211 }
212
213 private class AppStateTrackerForTest extends AppStateTracker {
214 AppStateTrackerForTest(Context ctx, Looper looper) {
215 super(ctx, looper);
216 }
217
218 @Override
219 public void onSystemServicesReady() {
220 // Do nothing.
221 }
222
223 @Override
224 IActivityManager injectIActivityManager() {
225 return mIActivityManager;
226 }
227 }
228
229 @Before
230 public void setUp() {
231 mMockingSession = mockitoSession()
232 .initMocks(this)
233 .strictness(Strictness.LENIENT)
Robin Lee876b88542018-11-13 17:22:24 +0100234 .spyStatic(LocalServices.class)
Kweku Adams00e3a372018-09-28 16:57:09 -0700235 .startMocking();
Robin Lee54d44962018-12-17 17:43:57 +0100236 spyOn(getContext());
237 doReturn(null).when(getContext()).registerReceiver(any(), any());
Kweku Adams00e3a372018-09-28 16:57:09 -0700238 doReturn(mock(ActivityManagerInternal.class))
239 .when(() -> LocalServices.getService(ActivityManagerInternal.class));
240 doReturn(mock(ActivityTaskManagerInternal.class))
241 .when(() -> LocalServices.getService(ActivityTaskManagerInternal.class));
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800242 doReturn(mock(AlarmManagerInternal.class))
243 .when(() -> LocalServices.getService(AlarmManagerInternal.class));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700244 doReturn(mPowerManagerInternal)
Kweku Adams00e3a372018-09-28 16:57:09 -0700245 .when(() -> LocalServices.getService(PowerManagerInternal.class));
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800246 when(mPowerManagerInternal.getLowPowerState(anyInt()))
247 .thenReturn(mock(PowerSaveState.class));
Kweku Adams00e3a372018-09-28 16:57:09 -0700248 doReturn(mock(NetworkPolicyManagerInternal.class))
249 .when(() -> LocalServices.getService(NetworkPolicyManagerInternal.class));
250 when(mPowerManager.newWakeLock(anyInt(), anyString())).thenReturn(mWakeLock);
251 doNothing().when(mWakeLock).acquire();
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700252 doNothing().when(mAlarmManager).set(anyInt(), anyLong(), anyString(), any(), any());
Robin Lee876b88542018-11-13 17:22:24 +0100253 doReturn(mock(Sensor.class)).when(mSensorManager)
254 .getDefaultSensor(eq(Sensor.TYPE_SIGNIFICANT_MOTION), eq(true));
255 doReturn(true).when(mSensorManager).registerListener(any(), any(), anyInt());
Kweku Adams00e3a372018-09-28 16:57:09 -0700256 mAppStateTracker = new AppStateTrackerForTest(getContext(), Looper.getMainLooper());
257 mAnyMotionDetector = new AnyMotionDetectorForTest();
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800258 mHandler = mock(DeviceIdleController.MyHandler.class, Answers.RETURNS_DEEP_STUBS);
259 doNothing().when(mHandler).handleMessage(any());
Kweku Adams799858b2018-10-08 17:19:08 -0700260 mInjector = new InjectorForTest(getContext());
Kweku Adams9da2bb92018-12-20 06:34:39 -0800261 doNothing().when(mContentResolver).registerContentObserver(any(), anyBoolean(), any());
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800262
Kweku Adams799858b2018-10-08 17:19:08 -0700263 mDeviceIdleController = new DeviceIdleController(getContext(), mInjector);
Kweku Adams00e3a372018-09-28 16:57:09 -0700264 spyOn(mDeviceIdleController);
265 doNothing().when(mDeviceIdleController).publishBinderService(any(), any());
266 mDeviceIdleController.onStart();
267 mDeviceIdleController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
268 mDeviceIdleController.setDeepEnabledForTest(true);
269 mDeviceIdleController.setLightEnabledForTest(true);
Kweku Adams9da2bb92018-12-20 06:34:39 -0800270
271 // Get the same Constants object that mDeviceIdleController got.
272 mConstants = mInjector.getConstants(mDeviceIdleController,
273 mInjector.getHandler(mDeviceIdleController), mContentResolver);
Kweku Adams00e3a372018-09-28 16:57:09 -0700274 }
275
276 @After
277 public void tearDown() {
278 if (mMockingSession != null) {
279 mMockingSession.finishMocking();
280 }
Robin Lee876b88542018-11-13 17:22:24 +0100281 // DeviceIdleController adds these to LocalServices in the constructor, so we have to remove
282 // them after each test, otherwise, subsequent tests will fail.
Kweku Adams00e3a372018-09-28 16:57:09 -0700283 LocalServices.removeServiceForTest(AppStateTracker.class);
Robin Lee876b88542018-11-13 17:22:24 +0100284 LocalServices.removeServiceForTest(DeviceIdleController.LocalService.class);
Kweku Adams00e3a372018-09-28 16:57:09 -0700285 }
286
287 @Test
288 public void testUpdateInteractivityLocked() {
289 doReturn(false).when(mPowerManager).isInteractive();
290 mDeviceIdleController.updateInteractivityLocked();
291 assertFalse(mDeviceIdleController.isScreenOn());
292
293 // Make sure setting false when screen is already off doesn't change anything.
294 doReturn(false).when(mPowerManager).isInteractive();
295 mDeviceIdleController.updateInteractivityLocked();
296 assertFalse(mDeviceIdleController.isScreenOn());
297
298 // Test changing from screen off to screen on.
299 doReturn(true).when(mPowerManager).isInteractive();
300 mDeviceIdleController.updateInteractivityLocked();
301 assertTrue(mDeviceIdleController.isScreenOn());
302
303 // Make sure setting true when screen is already on doesn't change anything.
304 doReturn(true).when(mPowerManager).isInteractive();
305 mDeviceIdleController.updateInteractivityLocked();
306 assertTrue(mDeviceIdleController.isScreenOn());
307
308 // Test changing from screen on to screen off.
309 doReturn(false).when(mPowerManager).isInteractive();
310 mDeviceIdleController.updateInteractivityLocked();
311 assertFalse(mDeviceIdleController.isScreenOn());
312 }
313
314 @Test
315 public void testUpdateChargingLocked() {
316 mDeviceIdleController.updateChargingLocked(false);
317 assertFalse(mDeviceIdleController.isCharging());
318
319 // Make sure setting false when charging is already off doesn't change anything.
320 mDeviceIdleController.updateChargingLocked(false);
321 assertFalse(mDeviceIdleController.isCharging());
322
323 // Test changing from charging off to charging on.
324 mDeviceIdleController.updateChargingLocked(true);
325 assertTrue(mDeviceIdleController.isCharging());
326
327 // Make sure setting true when charging is already on doesn't change anything.
328 mDeviceIdleController.updateChargingLocked(true);
329 assertTrue(mDeviceIdleController.isCharging());
330
331 // Test changing from charging on to charging off.
332 mDeviceIdleController.updateChargingLocked(false);
333 assertFalse(mDeviceIdleController.isCharging());
334 }
335
336 @Test
Kweku Adams799858b2018-10-08 17:19:08 -0700337 public void testUpdateConnectivityState() {
338 // No connectivity service
339 final boolean isConnected = mDeviceIdleController.isNetworkConnected();
340 mInjector.connectivityService = null;
341 mDeviceIdleController.updateConnectivityState(null);
342 assertEquals(isConnected, mDeviceIdleController.isNetworkConnected());
343
344 // No active network info
345 mInjector.connectivityService = mConnectivityService;
346 doReturn(null).when(mConnectivityService).getActiveNetworkInfo();
347 mDeviceIdleController.updateConnectivityState(null);
348 assertFalse(mDeviceIdleController.isNetworkConnected());
349
350 // Active network info says connected.
351 final NetworkInfo ani = mock(NetworkInfo.class);
352 doReturn(ani).when(mConnectivityService).getActiveNetworkInfo();
353 doReturn(true).when(ani).isConnected();
354 mDeviceIdleController.updateConnectivityState(null);
355 assertTrue(mDeviceIdleController.isNetworkConnected());
356
357 // Active network info says not connected.
358 doReturn(false).when(ani).isConnected();
359 mDeviceIdleController.updateConnectivityState(null);
360 assertFalse(mDeviceIdleController.isNetworkConnected());
361
362 // Wrong intent passed (false).
363 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
364 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3);
365 doReturn(true).when(ani).isConnected();
366 doReturn(1).when(ani).getType();
367 mDeviceIdleController.updateConnectivityState(intent);
368 // Wrong intent means we shouldn't update the connected state.
369 assertFalse(mDeviceIdleController.isNetworkConnected());
370
371 // Intent says connected.
372 doReturn(1).when(ani).getType();
373 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1);
374 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
375 mDeviceIdleController.updateConnectivityState(intent);
376 assertTrue(mDeviceIdleController.isNetworkConnected());
377
378 // Wrong intent passed (true).
379 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3);
380 // Wrong intent means we shouldn't update the connected state.
381 assertTrue(mDeviceIdleController.isNetworkConnected());
382
383 // Intent says not connected.
384 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1);
385 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
386 mDeviceIdleController.updateConnectivityState(intent);
387 assertFalse(mDeviceIdleController.isNetworkConnected());
388 }
389
390 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700391 public void testUpdateQuickDozeFlagLocked() {
392 mDeviceIdleController.updateQuickDozeFlagLocked(false);
393 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
394
395 // Make sure setting false when quick doze is already off doesn't change anything.
396 mDeviceIdleController.updateQuickDozeFlagLocked(false);
397 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
398
399 // Test changing from quick doze off to quick doze on.
400 mDeviceIdleController.updateQuickDozeFlagLocked(true);
401 assertTrue(mDeviceIdleController.isQuickDozeEnabled());
402
403 // Make sure setting true when quick doze is already on doesn't change anything.
404 mDeviceIdleController.updateQuickDozeFlagLocked(true);
405 assertTrue(mDeviceIdleController.isQuickDozeEnabled());
406
407 // Test changing from quick doze on to quick doze off.
408 mDeviceIdleController.updateQuickDozeFlagLocked(false);
409 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
410 }
411
412 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700413 public void testStateActiveToStateInactive_ConditionsNotMet() {
414 mDeviceIdleController.becomeActiveLocked("testing", 0);
415 verifyStateConditions(STATE_ACTIVE);
416
417 // State should stay ACTIVE with screen on and charging.
418 setChargingOn(true);
419 setScreenOn(true);
420
421 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
422 verifyStateConditions(STATE_ACTIVE);
423
424 // State should stay ACTIVE with charging on.
425 setChargingOn(true);
426 setScreenOn(false);
427
428 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
429 verifyStateConditions(STATE_ACTIVE);
430
431 // State should stay ACTIVE with screen on.
432 // Note the different operation order here makes sure the state doesn't change before test.
433 setScreenOn(true);
434 setChargingOn(false);
435
436 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
437 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800438
439 mConstants.WAIT_FOR_UNLOCK = false;
440 setScreenLocked(true);
441 setScreenOn(true);
442 setChargingOn(false);
443
444 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
445 verifyStateConditions(STATE_ACTIVE);
446
447 setScreenLocked(false);
448 setScreenOn(true);
449 setChargingOn(false);
450
451 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
452 verifyStateConditions(STATE_ACTIVE);
453
454 mConstants.WAIT_FOR_UNLOCK = true;
455 setScreenLocked(false);
456 setScreenOn(true);
457 setChargingOn(false);
458
459 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
460 verifyStateConditions(STATE_ACTIVE);
Kweku Adams00e3a372018-09-28 16:57:09 -0700461 }
462
463 @Test
464 public void testLightStateActiveToLightStateInactive_ConditionsNotMet() {
465 mDeviceIdleController.becomeActiveLocked("testing", 0);
466 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
467
468 // State should stay ACTIVE with screen on and charging.
469 setChargingOn(true);
470 setScreenOn(true);
471
472 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
473 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
474
475 // State should stay ACTIVE with charging on.
476 setChargingOn(true);
477 setScreenOn(false);
478
479 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
480 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
481
482 // State should stay ACTIVE with screen on.
483 // Note the different operation order here makes sure the state doesn't change before test.
484 setScreenOn(true);
485 setChargingOn(false);
486
487 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
488 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
489 }
490
491 @Test
492 public void testStateActiveToStateInactive_ConditionsMet() {
493 mDeviceIdleController.becomeActiveLocked("testing", 0);
494 verifyStateConditions(STATE_ACTIVE);
495
496 setChargingOn(false);
497 setScreenOn(false);
498
499 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
500 verifyStateConditions(STATE_INACTIVE);
501 }
502
503 @Test
504 public void testLightStateActiveToLightStateInactive_ConditionsMet() {
505 mDeviceIdleController.becomeActiveLocked("testing", 0);
506 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
507
508 setChargingOn(false);
509 setScreenOn(false);
510
511 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
512 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
513 }
514
515 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700516 public void testTransitionFromAnyStateToStateQuickDozeDelay() {
517 enterDeepState(STATE_ACTIVE);
518 setQuickDozeEnabled(true);
519 setChargingOn(false);
520 setScreenOn(false);
521 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
522
523 enterDeepState(STATE_INACTIVE);
524 setQuickDozeEnabled(true);
525 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
526
527 enterDeepState(STATE_IDLE_PENDING);
528 setQuickDozeEnabled(true);
529 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
530
531 enterDeepState(STATE_SENSING);
532 setQuickDozeEnabled(true);
533 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
534
535 enterDeepState(STATE_LOCATING);
536 setQuickDozeEnabled(true);
537 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
538
539 // IDLE should stay as IDLE.
540 enterDeepState(STATE_IDLE);
541 setQuickDozeEnabled(true);
542 verifyStateConditions(STATE_IDLE);
543
544 // IDLE_MAINTENANCE should stay as IDLE_MAINTENANCE.
545 enterDeepState(STATE_IDLE_MAINTENANCE);
546 setQuickDozeEnabled(true);
547 verifyStateConditions(STATE_IDLE_MAINTENANCE);
548
549 enterDeepState(STATE_QUICK_DOZE_DELAY);
550 setQuickDozeEnabled(true);
551 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
552 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
553 }
554
555 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700556 public void testStepIdleStateLocked_InvalidStates() {
557 mDeviceIdleController.becomeActiveLocked("testing", 0);
558 mDeviceIdleController.stepIdleStateLocked("testing");
559 // mDeviceIdleController.stepIdleStateLocked doesn't handle the ACTIVE case, so the state
560 // should stay as ACTIVE.
561 verifyStateConditions(STATE_ACTIVE);
562 }
563
564 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700565 public void testStepIdleStateLocked_ValidStates_QuickDoze() {
566 setAlarmSoon(false);
567
568 // Quick doze should go directly into IDLE.
569 enterDeepState(STATE_QUICK_DOZE_DELAY);
570 mDeviceIdleController.stepIdleStateLocked("testing");
571 verifyStateConditions(STATE_IDLE);
572
573 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
574
575 mDeviceIdleController.stepIdleStateLocked("testing");
576 verifyStateConditions(STATE_IDLE_MAINTENANCE);
577
578 mDeviceIdleController.stepIdleStateLocked("testing");
579 verifyStateConditions(STATE_IDLE);
580
581 mDeviceIdleController.stepIdleStateLocked("testing");
582 verifyStateConditions(STATE_IDLE_MAINTENANCE);
583 }
584
585 @Test
Kweku Adamsbf869382018-10-09 18:26:56 -0700586 public void testStepIdleStateLocked_ValidStates_WithWakeFromIdleAlarmSoon() {
587 enterDeepState(STATE_ACTIVE);
588 // Return that there's an alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700589 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700590 mDeviceIdleController.stepIdleStateLocked("testing");
591 verifyStateConditions(STATE_ACTIVE);
592
593 // Everything besides ACTIVE should end up as INACTIVE since the screen would be off.
594
595 enterDeepState(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700596 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700597 mDeviceIdleController.stepIdleStateLocked("testing");
598 verifyStateConditions(STATE_INACTIVE);
599
600 enterDeepState(STATE_IDLE_PENDING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700601 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700602 mDeviceIdleController.stepIdleStateLocked("testing");
603 verifyStateConditions(STATE_INACTIVE);
604
605 enterDeepState(STATE_SENSING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700606 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700607 mDeviceIdleController.stepIdleStateLocked("testing");
608 verifyStateConditions(STATE_INACTIVE);
609
610 enterDeepState(STATE_LOCATING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700611 setAlarmSoon(true);
612 mDeviceIdleController.stepIdleStateLocked("testing");
613 verifyStateConditions(STATE_INACTIVE);
614
615 // With quick doze enabled, we should end up in QUICK_DOZE_DELAY instead of INACTIVE.
616 enterDeepState(STATE_QUICK_DOZE_DELAY);
617 setQuickDozeEnabled(true);
618 setAlarmSoon(true);
619 mDeviceIdleController.stepIdleStateLocked("testing");
620 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
621
622 // With quick doze disabled, we should end up in INACTIVE instead of QUICK_DOZE_DELAY.
623 enterDeepState(STATE_QUICK_DOZE_DELAY);
624 setQuickDozeEnabled(false);
625 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700626 mDeviceIdleController.stepIdleStateLocked("testing");
627 verifyStateConditions(STATE_INACTIVE);
628
629 enterDeepState(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700630 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700631 mDeviceIdleController.stepIdleStateLocked("testing");
632 verifyStateConditions(STATE_INACTIVE);
633
634 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700635 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700636 mDeviceIdleController.stepIdleStateLocked("testing");
637 verifyStateConditions(STATE_INACTIVE);
638 }
639
640 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700641 public void testStepIdleStateLocked_ValidStates_NoLocationManager() {
Kweku Adams799858b2018-10-08 17:19:08 -0700642 mInjector.locationManager = null;
Kweku Adams00e3a372018-09-28 16:57:09 -0700643 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700644 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700645 // Set state to INACTIVE.
646 mDeviceIdleController.becomeActiveLocked("testing", 0);
647 setChargingOn(false);
648 setScreenOn(false);
649 verifyStateConditions(STATE_INACTIVE);
650
651 mDeviceIdleController.stepIdleStateLocked("testing");
652 verifyStateConditions(STATE_IDLE_PENDING);
653
654 mDeviceIdleController.stepIdleStateLocked("testing");
655 verifyStateConditions(STATE_SENSING);
656
657 mDeviceIdleController.stepIdleStateLocked("testing");
658 // No location manager, so SENSING should go straight to IDLE.
659 verifyStateConditions(STATE_IDLE);
660
661 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
662
663 mDeviceIdleController.stepIdleStateLocked("testing");
664 verifyStateConditions(STATE_IDLE_MAINTENANCE);
665
666 mDeviceIdleController.stepIdleStateLocked("testing");
667 verifyStateConditions(STATE_IDLE);
668
669 mDeviceIdleController.stepIdleStateLocked("testing");
670 verifyStateConditions(STATE_IDLE_MAINTENANCE);
671 }
672
673 @Test
674 public void testStepIdleStateLocked_ValidStates_WithLocationManager_NoProviders() {
675 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700676 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700677 // Set state to INACTIVE.
678 mDeviceIdleController.becomeActiveLocked("testing", 0);
679 setChargingOn(false);
680 setScreenOn(false);
681 verifyStateConditions(STATE_INACTIVE);
682
683 mDeviceIdleController.stepIdleStateLocked("testing");
684 verifyStateConditions(STATE_IDLE_PENDING);
685
686 mDeviceIdleController.stepIdleStateLocked("testing");
687 verifyStateConditions(STATE_SENSING);
688
689 mDeviceIdleController.stepIdleStateLocked("testing");
690 // Location manager exists but there isn't a network or GPS provider,
691 // so SENSING should go straight to IDLE.
692 verifyStateConditions(STATE_IDLE);
693
694 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
695
696 mDeviceIdleController.stepIdleStateLocked("testing");
697 verifyStateConditions(STATE_IDLE_MAINTENANCE);
698
699 mDeviceIdleController.stepIdleStateLocked("testing");
700 verifyStateConditions(STATE_IDLE);
701
702 mDeviceIdleController.stepIdleStateLocked("testing");
703 verifyStateConditions(STATE_IDLE_MAINTENANCE);
704 }
705
706 @Test
707 public void testStepIdleStateLocked_ValidStates_WithLocationManager_WithProviders() {
Kweku Adams799858b2018-10-08 17:19:08 -0700708 mInjector.locationManager = mLocationManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700709 doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(anyString());
710 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700711 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700712 // Set state to INACTIVE.
713 mDeviceIdleController.becomeActiveLocked("testing", 0);
714 setChargingOn(false);
715 setScreenOn(false);
716 verifyStateConditions(STATE_INACTIVE);
717
718 mDeviceIdleController.stepIdleStateLocked("testing");
719 verifyStateConditions(STATE_IDLE_PENDING);
720
721 mDeviceIdleController.stepIdleStateLocked("testing");
722 verifyStateConditions(STATE_SENSING);
723
724 mDeviceIdleController.stepIdleStateLocked("testing");
725 // Location manager exists with a provider, so SENSING should go to LOCATING.
726 verifyStateConditions(STATE_LOCATING);
727
728 mDeviceIdleController.stepIdleStateLocked("testing");
729 verifyStateConditions(STATE_IDLE);
730
731 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
732
733 mDeviceIdleController.stepIdleStateLocked("testing");
734 verifyStateConditions(STATE_IDLE_MAINTENANCE);
735
736 mDeviceIdleController.stepIdleStateLocked("testing");
737 verifyStateConditions(STATE_IDLE);
738
739 mDeviceIdleController.stepIdleStateLocked("testing");
740 verifyStateConditions(STATE_IDLE_MAINTENANCE);
741 }
742
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700743 @Test
Kweku Adams799858b2018-10-08 17:19:08 -0700744 public void testLightStepIdleStateLocked_InvalidStates() {
745 mDeviceIdleController.becomeActiveLocked("testing", 0);
746 mDeviceIdleController.stepLightIdleStateLocked("testing");
747 // stepLightIdleStateLocked doesn't handle the ACTIVE case, so the state
748 // should stay as ACTIVE.
749 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
750 }
751
752 /**
753 * Make sure stepLightIdleStateLocked doesn't change state when the state is
754 * LIGHT_STATE_OVERRIDE.
755 */
756 @Test
757 public void testLightStepIdleStateLocked_Overriden() {
758 enterLightState(LIGHT_STATE_OVERRIDE);
759 mDeviceIdleController.stepLightIdleStateLocked("testing");
760 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
761 }
762
763 @Test
764 public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NetworkConnected() {
765 setNetworkConnected(true);
766 mDeviceIdleController.setJobsActive(false);
767 mDeviceIdleController.setAlarmsActive(false);
768 mDeviceIdleController.setActiveIdleOpsForTest(0);
769
770 // Set state to INACTIVE.
771 mDeviceIdleController.becomeActiveLocked("testing", 0);
772 setChargingOn(false);
773 setScreenOn(false);
774 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
775
776 // No active ops means INACTIVE should go straight to IDLE.
777 mDeviceIdleController.stepLightIdleStateLocked("testing");
778 verifyLightStateConditions(LIGHT_STATE_IDLE);
779
780 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
781
782 mDeviceIdleController.stepLightIdleStateLocked("testing");
783 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
784
785 mDeviceIdleController.stepLightIdleStateLocked("testing");
786 verifyLightStateConditions(LIGHT_STATE_IDLE);
787
788 mDeviceIdleController.stepLightIdleStateLocked("testing");
789 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
790 }
791
792 @Test
793 public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NetworkConnected() {
794 setNetworkConnected(true);
795 // Set state to INACTIVE.
796 mDeviceIdleController.becomeActiveLocked("testing", 0);
797 setChargingOn(false);
798 setScreenOn(false);
799 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
800
801 // Active ops means INACTIVE should go to PRE_IDLE to wait.
802 mDeviceIdleController.setJobsActive(true);
803 mDeviceIdleController.setAlarmsActive(true);
804 mDeviceIdleController.setActiveIdleOpsForTest(1);
805 mDeviceIdleController.stepLightIdleStateLocked("testing");
806 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
807
808 // Even with active ops, PRE_IDLE should go to IDLE.
809 mDeviceIdleController.stepLightIdleStateLocked("testing");
810 verifyLightStateConditions(LIGHT_STATE_IDLE);
811
812 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
813
814 mDeviceIdleController.stepLightIdleStateLocked("testing");
815 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
816
817 mDeviceIdleController.stepLightIdleStateLocked("testing");
818 verifyLightStateConditions(LIGHT_STATE_IDLE);
819
820 mDeviceIdleController.stepLightIdleStateLocked("testing");
821 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
822 }
823
824 @Test
825 public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NoNetworkConnected() {
826 setNetworkConnected(false);
827 mDeviceIdleController.setJobsActive(false);
828 mDeviceIdleController.setAlarmsActive(false);
829 mDeviceIdleController.setActiveIdleOpsForTest(0);
830
831 // Set state to INACTIVE.
832 mDeviceIdleController.becomeActiveLocked("testing", 0);
833 setChargingOn(false);
834 setScreenOn(false);
835 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
836
837 // No active ops means INACTIVE should go straight to IDLE.
838 mDeviceIdleController.stepLightIdleStateLocked("testing");
839 verifyLightStateConditions(LIGHT_STATE_IDLE);
840
841 // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now.
842
843 mDeviceIdleController.stepLightIdleStateLocked("testing");
844 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
845
846 mDeviceIdleController.stepLightIdleStateLocked("testing");
847 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
848
849 mDeviceIdleController.stepLightIdleStateLocked("testing");
850 verifyLightStateConditions(LIGHT_STATE_IDLE);
851
852 mDeviceIdleController.stepLightIdleStateLocked("testing");
853 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
854
855 mDeviceIdleController.stepLightIdleStateLocked("testing");
856 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
857 }
858
859 @Test
860 public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NoNetworkConnected() {
861 setNetworkConnected(false);
862 // Set state to INACTIVE.
863 mDeviceIdleController.becomeActiveLocked("testing", 0);
864 setChargingOn(false);
865 setScreenOn(false);
866 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
867
868 // Active ops means INACTIVE should go to PRE_IDLE to wait.
869 mDeviceIdleController.setJobsActive(true);
870 mDeviceIdleController.setAlarmsActive(true);
871 mDeviceIdleController.setActiveIdleOpsForTest(1);
872 mDeviceIdleController.stepLightIdleStateLocked("testing");
873 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
874
875 // Even with active ops, PRE_IDLE should go to IDLE.
876 mDeviceIdleController.stepLightIdleStateLocked("testing");
877 verifyLightStateConditions(LIGHT_STATE_IDLE);
878
879 // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now.
880
881 mDeviceIdleController.stepLightIdleStateLocked("testing");
882 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
883
884 mDeviceIdleController.stepLightIdleStateLocked("testing");
885 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
886
887 mDeviceIdleController.stepLightIdleStateLocked("testing");
888 verifyLightStateConditions(LIGHT_STATE_IDLE);
889
890 mDeviceIdleController.stepLightIdleStateLocked("testing");
891 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
892
893 mDeviceIdleController.stepLightIdleStateLocked("testing");
894 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
895 }
896
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700897 ///////////////// EXIT conditions ///////////////////
898
Kweku Adams799858b2018-10-08 17:19:08 -0700899 @Test
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700900 public void testExitMaintenanceEarlyIfNeededLocked_deep_noActiveOps() {
901 mDeviceIdleController.setJobsActive(false);
902 mDeviceIdleController.setAlarmsActive(false);
903 mDeviceIdleController.setActiveIdleOpsForTest(0);
904
905 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
906
907 enterDeepState(STATE_ACTIVE);
908 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
909 verifyStateConditions(STATE_ACTIVE);
910
911 enterDeepState(STATE_INACTIVE);
912 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
913 verifyStateConditions(STATE_INACTIVE);
914
915 enterDeepState(STATE_IDLE_PENDING);
916 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
917 verifyStateConditions(STATE_IDLE_PENDING);
918
919 enterDeepState(STATE_SENSING);
920 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
921 verifyStateConditions(STATE_SENSING);
922
923 enterDeepState(STATE_LOCATING);
924 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
925 verifyStateConditions(STATE_LOCATING);
926
927 enterDeepState(STATE_IDLE);
928 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
929 verifyStateConditions(STATE_IDLE);
930
931 enterDeepState(STATE_IDLE_MAINTENANCE);
932 // Going into IDLE_MAINTENANCE increments the active idle op count.
933 mDeviceIdleController.setActiveIdleOpsForTest(0);
934 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
935 verifyStateConditions(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700936
937 enterDeepState(STATE_QUICK_DOZE_DELAY);
938 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
939 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700940 }
941
942 @Test
943 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeJobs() {
944 mDeviceIdleController.setJobsActive(true);
945 mDeviceIdleController.setAlarmsActive(false);
946 mDeviceIdleController.setActiveIdleOpsForTest(0);
947
948 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
949
950 enterDeepState(STATE_ACTIVE);
951 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
952 verifyStateConditions(STATE_ACTIVE);
953
954 enterDeepState(STATE_INACTIVE);
955 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
956 verifyStateConditions(STATE_INACTIVE);
957
958 enterDeepState(STATE_IDLE_PENDING);
959 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
960 verifyStateConditions(STATE_IDLE_PENDING);
961
962 enterDeepState(STATE_SENSING);
963 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
964 verifyStateConditions(STATE_SENSING);
965
966 enterDeepState(STATE_LOCATING);
967 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
968 verifyStateConditions(STATE_LOCATING);
969
970 enterDeepState(STATE_IDLE);
971 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
972 verifyStateConditions(STATE_IDLE);
973
974 enterDeepState(STATE_IDLE_MAINTENANCE);
975 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
976 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700977
978 enterDeepState(STATE_QUICK_DOZE_DELAY);
979 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
980 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700981 }
982
983 @Test
984 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeAlarms() {
985 mDeviceIdleController.setJobsActive(false);
986 mDeviceIdleController.setAlarmsActive(true);
987 mDeviceIdleController.setActiveIdleOpsForTest(0);
988
989 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
990
991 enterDeepState(STATE_ACTIVE);
992 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
993 verifyStateConditions(STATE_ACTIVE);
994
995 enterDeepState(STATE_INACTIVE);
996 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
997 verifyStateConditions(STATE_INACTIVE);
998
999 enterDeepState(STATE_IDLE_PENDING);
1000 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1001 verifyStateConditions(STATE_IDLE_PENDING);
1002
1003 enterDeepState(STATE_SENSING);
1004 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1005 verifyStateConditions(STATE_SENSING);
1006
1007 enterDeepState(STATE_LOCATING);
1008 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1009 verifyStateConditions(STATE_LOCATING);
1010
1011 enterDeepState(STATE_IDLE);
1012 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1013 verifyStateConditions(STATE_IDLE);
1014
1015 enterDeepState(STATE_IDLE_MAINTENANCE);
1016 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1017 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001018
1019 enterDeepState(STATE_QUICK_DOZE_DELAY);
1020 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1021 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001022 }
1023
1024 @Test
1025 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeOps() {
1026 mDeviceIdleController.setJobsActive(false);
1027 mDeviceIdleController.setAlarmsActive(false);
1028 mDeviceIdleController.setActiveIdleOpsForTest(1);
1029
1030 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1031
1032 enterDeepState(STATE_ACTIVE);
1033 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1034 verifyStateConditions(STATE_ACTIVE);
1035
1036 enterDeepState(STATE_INACTIVE);
1037 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1038 verifyStateConditions(STATE_INACTIVE);
1039
1040 enterDeepState(STATE_IDLE_PENDING);
1041 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1042 verifyStateConditions(STATE_IDLE_PENDING);
1043
1044 enterDeepState(STATE_SENSING);
1045 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1046 verifyStateConditions(STATE_SENSING);
1047
1048 enterDeepState(STATE_LOCATING);
1049 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1050 verifyStateConditions(STATE_LOCATING);
1051
1052 enterDeepState(STATE_IDLE);
1053 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1054 verifyStateConditions(STATE_IDLE);
1055
1056 enterDeepState(STATE_IDLE_MAINTENANCE);
1057 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1058 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001059
1060 enterDeepState(STATE_QUICK_DOZE_DELAY);
1061 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1062 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001063 }
1064
1065 @Test
1066 public void testExitMaintenanceEarlyIfNeededLocked_light_noActiveOps() {
1067 mDeviceIdleController.setJobsActive(false);
1068 mDeviceIdleController.setAlarmsActive(false);
1069 mDeviceIdleController.setActiveIdleOpsForTest(0);
1070
1071 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1072
1073 enterLightState(LIGHT_STATE_ACTIVE);
1074 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1075 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1076
1077 enterLightState(LIGHT_STATE_INACTIVE);
1078 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1079 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1080
1081 enterLightState(LIGHT_STATE_PRE_IDLE);
1082 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1083 verifyLightStateConditions(LIGHT_STATE_IDLE);
1084
1085 enterLightState(LIGHT_STATE_IDLE);
1086 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1087 verifyLightStateConditions(LIGHT_STATE_IDLE);
1088
1089 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1090 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1091 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1092
1093 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1094 // Going into IDLE_MAINTENANCE increments the active idle op count.
1095 mDeviceIdleController.setActiveIdleOpsForTest(0);
1096 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1097 verifyLightStateConditions(LIGHT_STATE_IDLE);
1098
1099 enterLightState(LIGHT_STATE_OVERRIDE);
1100 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1101 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1102 }
1103
1104 @Test
1105 public void testExitMaintenanceEarlyIfNeededLocked_light_activeJobs() {
1106 mDeviceIdleController.setJobsActive(true);
1107 mDeviceIdleController.setAlarmsActive(false);
1108 mDeviceIdleController.setActiveIdleOpsForTest(0);
1109
1110 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1111
1112 enterLightState(LIGHT_STATE_ACTIVE);
1113 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1114 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1115
1116 enterLightState(LIGHT_STATE_INACTIVE);
1117 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1118 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1119
1120 enterLightState(LIGHT_STATE_PRE_IDLE);
1121 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1122 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1123
1124 enterLightState(LIGHT_STATE_IDLE);
1125 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1126 verifyLightStateConditions(LIGHT_STATE_IDLE);
1127
1128 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1129 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1130 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1131
1132 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1133 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1134 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1135
1136 enterLightState(LIGHT_STATE_OVERRIDE);
1137 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1138 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1139 }
1140
1141 @Test
1142 public void testExitMaintenanceEarlyIfNeededLocked_light_activeAlarms() {
1143 mDeviceIdleController.setJobsActive(false);
1144 mDeviceIdleController.setAlarmsActive(true);
1145 mDeviceIdleController.setActiveIdleOpsForTest(0);
1146
1147 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1148
1149 enterLightState(LIGHT_STATE_ACTIVE);
1150 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1151 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1152
1153 enterLightState(LIGHT_STATE_INACTIVE);
1154 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1155 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1156
1157 enterLightState(LIGHT_STATE_PRE_IDLE);
1158 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1159 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1160
1161 enterLightState(LIGHT_STATE_IDLE);
1162 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1163 verifyLightStateConditions(LIGHT_STATE_IDLE);
1164
1165 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1166 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1167 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1168
1169 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1170 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1171 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1172
1173 enterLightState(LIGHT_STATE_OVERRIDE);
1174 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1175 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1176 }
1177
1178 @Test
1179 public void testExitMaintenanceEarlyIfNeededLocked_light_activeOps() {
1180 mDeviceIdleController.setJobsActive(false);
1181 mDeviceIdleController.setAlarmsActive(false);
1182 mDeviceIdleController.setActiveIdleOpsForTest(1);
1183
1184 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1185
1186 enterLightState(LIGHT_STATE_ACTIVE);
1187 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1188 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1189
1190 enterLightState(LIGHT_STATE_INACTIVE);
1191 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1192 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1193
1194 enterLightState(LIGHT_STATE_PRE_IDLE);
1195 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1196 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1197
1198 enterLightState(LIGHT_STATE_IDLE);
1199 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1200 verifyLightStateConditions(LIGHT_STATE_IDLE);
1201
1202 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1203 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1204 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1205
1206 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1207 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1208 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1209
1210 enterLightState(LIGHT_STATE_OVERRIDE);
1211 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1212 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1213 }
1214
1215 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001216 public void testHandleMotionDetectedLocked_deep_quickDoze_off() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001217 enterDeepState(STATE_ACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001218 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001219 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1220 verifyStateConditions(STATE_ACTIVE);
1221
1222 // Anything that wasn't ACTIVE before motion detection should end up in the INACTIVE state.
1223
1224 enterDeepState(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001225 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001226 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1227 verifyStateConditions(STATE_INACTIVE);
1228
1229 enterDeepState(STATE_IDLE_PENDING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001230 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001231 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1232 verifyStateConditions(STATE_INACTIVE);
1233
1234 enterDeepState(STATE_SENSING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001235 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001236 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1237 verifyStateConditions(STATE_INACTIVE);
1238
1239 enterDeepState(STATE_LOCATING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001240 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001241 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1242 verifyStateConditions(STATE_INACTIVE);
1243
1244 enterDeepState(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001245 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001246 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1247 verifyStateConditions(STATE_INACTIVE);
1248
1249 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001250 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001251 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1252 verifyStateConditions(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001253
1254 enterDeepState(STATE_QUICK_DOZE_DELAY);
1255 setQuickDozeEnabled(false);
1256 // Disabling quick doze doesn't immediately change the state as coming out is harder than
1257 // going in.
1258 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1259 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1260 verifyStateConditions(STATE_INACTIVE);
1261 }
1262
1263 @Test
1264 public void testHandleMotionDetectedLocked_deep_quickDoze_on() {
1265 enterDeepState(STATE_ACTIVE);
1266 setQuickDozeEnabled(true);
1267 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1268 verifyStateConditions(STATE_ACTIVE);
1269
1270 // Anything that wasn't ACTIVE before motion detection should end up in the
1271 // QUICK_DOZE_DELAY state since quick doze is enabled.
1272
1273 enterDeepState(STATE_INACTIVE);
1274 setQuickDozeEnabled(true);
1275 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1276 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1277
1278 enterDeepState(STATE_IDLE_PENDING);
1279 setQuickDozeEnabled(true);
1280 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1281 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1282
1283 enterDeepState(STATE_SENSING);
1284 setQuickDozeEnabled(true);
1285 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1286 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1287
1288 enterDeepState(STATE_LOCATING);
1289 setQuickDozeEnabled(true);
1290 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1291 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1292
1293 enterDeepState(STATE_IDLE);
1294 setQuickDozeEnabled(true);
1295 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1296 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1297
1298 enterDeepState(STATE_IDLE_MAINTENANCE);
1299 setQuickDozeEnabled(true);
1300 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1301 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1302
1303 enterDeepState(STATE_QUICK_DOZE_DELAY);
1304 setQuickDozeEnabled(true);
1305 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1306 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001307 }
1308
1309 @Test
1310 public void testHandleMotionDetectedLocked_light() {
1311 enterLightState(LIGHT_STATE_ACTIVE);
1312 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1313 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1314
1315 // Motion shouldn't affect light idle, so LIGHT states should stay as they were except for
1316 // OVERRIDE. OVERRIDE means deep was active, so if motion was detected,
1317 // LIGHT_STATE_OVERRIDE should end up as LIGHT_STATE_INACTIVE.
1318
1319 enterLightState(LIGHT_STATE_INACTIVE);
1320 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1321 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1322
1323 enterLightState(LIGHT_STATE_PRE_IDLE);
1324 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1325 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1326
1327 enterLightState(LIGHT_STATE_IDLE);
1328 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1329 verifyLightStateConditions(LIGHT_STATE_IDLE);
1330
1331 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1332 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1333 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1334
1335 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1336 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1337 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1338
1339 enterLightState(LIGHT_STATE_OVERRIDE);
1340 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1341 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1342 }
1343
1344 @Test
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001345 public void testBecomeActiveLocked_deep() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001346 // becomeActiveLocked should put everything into ACTIVE.
1347
1348 enterDeepState(STATE_ACTIVE);
1349 mDeviceIdleController.becomeActiveLocked("test", 1000);
1350 verifyStateConditions(STATE_ACTIVE);
1351
1352 enterDeepState(STATE_INACTIVE);
1353 mDeviceIdleController.becomeActiveLocked("test", 1000);
1354 verifyStateConditions(STATE_ACTIVE);
1355
1356 enterDeepState(STATE_IDLE_PENDING);
1357 mDeviceIdleController.becomeActiveLocked("test", 1000);
1358 verifyStateConditions(STATE_ACTIVE);
1359
1360 enterDeepState(STATE_SENSING);
1361 mDeviceIdleController.becomeActiveLocked("test", 1000);
1362 verifyStateConditions(STATE_ACTIVE);
1363
1364 enterDeepState(STATE_LOCATING);
1365 mDeviceIdleController.becomeActiveLocked("test", 1000);
1366 verifyStateConditions(STATE_ACTIVE);
1367
1368 enterDeepState(STATE_IDLE);
1369 mDeviceIdleController.becomeActiveLocked("test", 1000);
1370 verifyStateConditions(STATE_ACTIVE);
1371
1372 enterDeepState(STATE_IDLE_MAINTENANCE);
1373 mDeviceIdleController.becomeActiveLocked("test", 1000);
1374 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001375
1376 enterDeepState(STATE_QUICK_DOZE_DELAY);
1377 mDeviceIdleController.becomeActiveLocked("test", 1000);
1378 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001379 }
1380
1381 @Test
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001382 public void testBecomeActiveLocked_light() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001383 // becomeActiveLocked should put everything into ACTIVE.
1384
1385 enterLightState(LIGHT_STATE_ACTIVE);
1386 mDeviceIdleController.becomeActiveLocked("test", 1000);
1387 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1388
1389 enterLightState(LIGHT_STATE_INACTIVE);
1390 mDeviceIdleController.becomeActiveLocked("test", 1000);
1391 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1392
1393 enterLightState(LIGHT_STATE_PRE_IDLE);
1394 mDeviceIdleController.becomeActiveLocked("test", 1000);
1395 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1396
1397 enterLightState(LIGHT_STATE_IDLE);
1398 mDeviceIdleController.becomeActiveLocked("test", 1000);
1399 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1400
1401 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1402 mDeviceIdleController.becomeActiveLocked("test", 1000);
1403 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1404
1405 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1406 mDeviceIdleController.becomeActiveLocked("test", 1000);
1407 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1408
1409 enterLightState(LIGHT_STATE_OVERRIDE);
1410 mDeviceIdleController.becomeActiveLocked("test", 1000);
1411 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1412 }
1413
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001414 /** Test based on b/119058625. */
1415 @Test
1416 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_ScreenThenMotion() {
1417 mConstants.WAIT_FOR_UNLOCK = true;
1418 enterDeepState(STATE_IDLE);
1419 reset(mAlarmManager);
1420 spyOn(mDeviceIdleController);
1421
1422 mDeviceIdleController.keyguardShowingLocked(true);
1423 setScreenOn(true);
1424 // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself
1425 // shouldn't bring the device out of deep IDLE.
1426 verifyStateConditions(STATE_IDLE);
1427 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1428 // Motion should bring the device out of Doze. Since the screen is still locked (albeit
1429 // on), the states should go back into INACTIVE.
1430 verifyStateConditions(STATE_INACTIVE);
1431 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1432 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1433 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1434 }
1435
1436 /** Test based on b/119058625. */
1437 @Test
1438 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_ScreenThenMotion() {
1439 mConstants.WAIT_FOR_UNLOCK = true;
1440 enterDeepState(STATE_IDLE);
1441 reset(mAlarmManager);
1442 spyOn(mDeviceIdleController);
1443
1444 mDeviceIdleController.keyguardShowingLocked(false);
1445 setScreenOn(true);
1446 // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself
1447 // should bring the device out of deep IDLE.
1448 verifyStateConditions(STATE_ACTIVE);
1449 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1450 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1451 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1452 }
1453
1454 /** Test based on b/119058625. */
1455 @Test
1456 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_MotionThenScreen() {
1457 mConstants.WAIT_FOR_UNLOCK = true;
1458 enterDeepState(STATE_IDLE);
1459 reset(mAlarmManager);
1460 spyOn(mDeviceIdleController);
1461
1462 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1463 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1464
1465 mDeviceIdleController.keyguardShowingLocked(true);
1466 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1467 // The screen is still off, so motion should result in the INACTIVE state.
1468 verifyStateConditions(STATE_INACTIVE);
1469 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1470 alarmManagerInOrder.verify(mAlarmManager)
1471 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1472 controllerInOrder.verify(mDeviceIdleController)
1473 .scheduleReportActiveLocked(anyString(), anyInt());
1474
1475 setScreenOn(true);
1476 // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself
1477 // shouldn't bring the device all the way to ACTIVE.
1478 verifyStateConditions(STATE_INACTIVE);
1479 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1480 alarmManagerInOrder.verify(mAlarmManager, never()).cancel(
1481 eq(mDeviceIdleController.mDeepAlarmListener));
1482
1483 // User finally unlocks the device. Device should be fully active.
1484 mDeviceIdleController.keyguardShowingLocked(false);
1485 verifyStateConditions(STATE_ACTIVE);
1486 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1487 alarmManagerInOrder.verify(mAlarmManager)
1488 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1489 controllerInOrder.verify(mDeviceIdleController)
1490 .scheduleReportActiveLocked(anyString(), anyInt());
1491 }
1492
1493 /** Test based on b/119058625. */
1494 @Test
1495 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_MotionThenScreen() {
1496 mConstants.WAIT_FOR_UNLOCK = true;
1497 enterDeepState(STATE_IDLE);
1498 reset(mAlarmManager);
1499 spyOn(mDeviceIdleController);
1500
1501 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1502 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1503
1504 mDeviceIdleController.keyguardShowingLocked(false);
1505 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1506 // The screen is still off, so motion should result in the INACTIVE state.
1507 verifyStateConditions(STATE_INACTIVE);
1508 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1509 alarmManagerInOrder.verify(mAlarmManager)
1510 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1511 controllerInOrder.verify(mDeviceIdleController)
1512 .scheduleReportActiveLocked(anyString(), anyInt());
1513
1514 setScreenOn(true);
1515 // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself
1516 // should bring the device out of deep IDLE.
1517 verifyStateConditions(STATE_ACTIVE);
1518 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1519 alarmManagerInOrder.verify(mAlarmManager)
1520 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1521 controllerInOrder.verify(mDeviceIdleController)
1522 .scheduleReportActiveLocked(anyString(), anyInt());
1523 }
1524
1525 @Test
1526 public void testExitNotifiesDependencies_WaitForUnlockOff_Screen() {
1527 mConstants.WAIT_FOR_UNLOCK = false;
1528 enterDeepState(STATE_IDLE);
1529 reset(mAlarmManager);
1530 spyOn(mDeviceIdleController);
1531
1532 setScreenOn(true);
1533 // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself
1534 // should bring the device out of deep IDLE.
1535 verifyStateConditions(STATE_ACTIVE);
1536 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1537 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1538 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1539 }
1540
1541 @Test
1542 public void testExitNotifiesDependencies_WaitForUnlockOff_MotionThenScreen() {
1543 mConstants.WAIT_FOR_UNLOCK = false;
1544 enterDeepState(STATE_IDLE);
1545 reset(mAlarmManager);
1546 spyOn(mDeviceIdleController);
1547
1548 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1549 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1550
1551 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1552 // The screen is still off, so motion should result in the INACTIVE state.
1553 verifyStateConditions(STATE_INACTIVE);
1554 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1555 alarmManagerInOrder.verify(mAlarmManager)
1556 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1557 controllerInOrder.verify(mDeviceIdleController)
1558 .scheduleReportActiveLocked(anyString(), anyInt());
1559
1560 setScreenOn(true);
1561 // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself
1562 // should bring the device out of deep IDLE.
1563 verifyStateConditions(STATE_ACTIVE);
1564 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1565 alarmManagerInOrder.verify(mAlarmManager)
1566 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1567 controllerInOrder.verify(mDeviceIdleController)
1568 .scheduleReportActiveLocked(anyString(), anyInt());
1569 }
1570
Denny cy Leec5a7c292019-01-01 17:37:55 +08001571 @Test
1572 public void testStepToIdleMode() {
1573 float delta = mDeviceIdleController.MIN_PRE_IDLE_FACTOR_CHANGE;
1574 for (int mode = PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL;
1575 mode <= PowerManager.PRE_IDLE_TIMEOUT_MODE_LONG;
1576 mode++) {
1577 int ret = mDeviceIdleController.setPreIdleTimeoutMode(mode);
1578 if (mode == PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL) {
1579 assertEquals("setPreIdleTimeoutMode: " + mode + " failed.",
1580 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
1581 } else {
1582 assertEquals("setPreIdleTimeoutMode: " + mode + " failed.",
1583 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
1584 }
1585 //TODO(b/123045185): Mocked Handler of DeviceIdleController to make message loop
1586 //workable in this test class
1587 mDeviceIdleController.updatePreIdleFactor();
1588 float expectedfactor = mDeviceIdleController.getPreIdleTimeoutByMode(mode);
1589 float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor();
1590 assertEquals("Pre idle time factor of mode [" + mode + "].",
1591 expectedfactor, curfactor, delta);
1592 mDeviceIdleController.resetPreIdleTimeoutMode();
1593 mDeviceIdleController.updatePreIdleFactor();
1594
1595 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_INACTIVE);
1596 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_PENDING);
1597
1598 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_SENSING);
1599 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_LOCATING);
1600 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_QUICK_DOZE_DELAY);
1601 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_MAINTENANCE);
1602 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE);
1603 checkMaybeDoAnImmediateMaintenance(expectedfactor);
1604 }
1605 float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor();
1606 assertEquals("Pre idle time factor of mode default.",
1607 1.0f, curfactor, delta);
1608 }
1609
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001610 private void enterDeepState(int state) {
1611 switch (state) {
1612 case STATE_ACTIVE:
1613 setScreenOn(true);
1614 mDeviceIdleController.becomeActiveLocked("testing", 0);
1615 break;
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001616 case STATE_QUICK_DOZE_DELAY:
1617 // Start off from ACTIVE in case we're already past the desired state.
1618 enterDeepState(STATE_ACTIVE);
1619 setQuickDozeEnabled(true);
1620 setScreenOn(false);
1621 setChargingOn(false);
1622 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
1623 break;
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001624 case STATE_LOCATING:
Kweku Adams799858b2018-10-08 17:19:08 -07001625 mInjector.locationManager = mLocationManager;
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001626 doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(
1627 anyString());
1628 // Fallthrough to step loop.
1629 case STATE_IDLE_PENDING:
1630 case STATE_SENSING:
1631 case STATE_IDLE:
1632 case STATE_IDLE_MAINTENANCE:
1633 // Make sure the controller doesn't think there's a wake-from-idle alarm coming
1634 // soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001635 setAlarmSoon(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001636 case STATE_INACTIVE:
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001637 // Start off from ACTIVE in case we're already past the desired state.
1638 enterDeepState(STATE_ACTIVE);
1639 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001640 setScreenOn(false);
1641 setChargingOn(false);
1642 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001643 int count = 0;
1644 while (mDeviceIdleController.getState() != state) {
1645 // Stepping through each state ensures that the proper features are turned
1646 // on/off.
1647 mDeviceIdleController.stepIdleStateLocked("testing");
1648 count++;
1649 if (count > 10) {
Kweku Adams799858b2018-10-08 17:19:08 -07001650 fail("Infinite loop. Check test configuration. Currently at " +
1651 stateToString(mDeviceIdleController.getState()));
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001652 }
1653 }
1654 break;
1655 default:
1656 fail("Unknown deep state " + stateToString(state));
1657 }
1658 }
1659
1660 private void enterLightState(int lightState) {
1661 switch (lightState) {
1662 case LIGHT_STATE_ACTIVE:
1663 setScreenOn(true);
1664 mDeviceIdleController.becomeActiveLocked("testing", 0);
1665 break;
1666 case LIGHT_STATE_INACTIVE:
1667 case LIGHT_STATE_IDLE:
1668 case LIGHT_STATE_IDLE_MAINTENANCE:
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001669 // Start off from ACTIVE in case we're already past the desired state.
1670 enterLightState(LIGHT_STATE_ACTIVE);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001671 setScreenOn(false);
1672 setChargingOn(false);
1673 int count = 0;
1674 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
1675 while (mDeviceIdleController.getLightState() != lightState) {
1676 // Stepping through each state ensures that the proper features are turned
1677 // on/off.
1678 mDeviceIdleController.stepLightIdleStateLocked("testing");
1679
1680 count++;
1681 if (count > 10) {
Kweku Adams799858b2018-10-08 17:19:08 -07001682 fail("Infinite loop. Check test configuration. Currently at " +
1683 lightStateToString(mDeviceIdleController.getLightState()));
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001684 }
1685 }
1686 break;
1687 case LIGHT_STATE_PRE_IDLE:
1688 case LIGHT_STATE_WAITING_FOR_NETWORK:
1689 case LIGHT_STATE_OVERRIDE:
1690 setScreenOn(false);
1691 setChargingOn(false);
1692 mDeviceIdleController.setLightStateForTest(lightState);
1693 break;
1694 default:
1695 fail("Unknown light state " + lightStateToString(lightState));
1696 }
1697 }
1698
Kweku Adams00e3a372018-09-28 16:57:09 -07001699 private void setChargingOn(boolean on) {
1700 mDeviceIdleController.updateChargingLocked(on);
1701 }
1702
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001703 private void setScreenLocked(boolean locked) {
1704 mDeviceIdleController.keyguardShowingLocked(locked);
1705 }
1706
Kweku Adams00e3a372018-09-28 16:57:09 -07001707 private void setScreenOn(boolean on) {
1708 doReturn(on).when(mPowerManager).isInteractive();
1709 mDeviceIdleController.updateInteractivityLocked();
1710 }
1711
Kweku Adams799858b2018-10-08 17:19:08 -07001712 private void setNetworkConnected(boolean connected) {
1713 mInjector.connectivityService = mConnectivityService;
1714 final NetworkInfo ani = mock(NetworkInfo.class);
1715 doReturn(connected).when(ani).isConnected();
1716 doReturn(ani).when(mConnectivityService).getActiveNetworkInfo();
1717 mDeviceIdleController.updateConnectivityState(null);
1718 }
1719
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001720 private void setQuickDozeEnabled(boolean on) {
1721 mDeviceIdleController.updateQuickDozeFlagLocked(on);
1722 }
1723
1724 private void setAlarmSoon(boolean isSoon) {
1725 if (isSoon) {
Kweku Adams9da2bb92018-12-20 06:34:39 -08001726 doReturn(SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM / 2)
1727 .when(mAlarmManager).getNextWakeFromIdleTime();
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001728 } else {
1729 doReturn(Long.MAX_VALUE).when(mAlarmManager).getNextWakeFromIdleTime();
1730 }
1731 }
1732
Kweku Adams00e3a372018-09-28 16:57:09 -07001733 private void verifyStateConditions(int expectedState) {
1734 int curState = mDeviceIdleController.getState();
1735 assertEquals(
1736 "Expected " + stateToString(expectedState) + " but was " + stateToString(curState),
1737 expectedState, curState);
1738
1739 switch (expectedState) {
1740 case STATE_ACTIVE:
1741 assertFalse(mDeviceIdleController.mMotionListener.isActive());
1742 assertFalse(mAnyMotionDetector.isMonitoring);
1743 break;
1744 case STATE_INACTIVE:
1745 assertFalse(mDeviceIdleController.mMotionListener.isActive());
1746 assertFalse(mAnyMotionDetector.isMonitoring);
1747 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001748 assertFalse(mDeviceIdleController.isScreenOn()
1749 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001750 break;
1751 case STATE_IDLE_PENDING:
Robin Lee876b88542018-11-13 17:22:24 +01001752 assertEquals(
1753 mDeviceIdleController.hasMotionSensor(),
1754 mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07001755 assertFalse(mAnyMotionDetector.isMonitoring);
1756 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001757 assertFalse(mDeviceIdleController.isScreenOn()
1758 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001759 break;
1760 case STATE_SENSING:
Robin Lee876b88542018-11-13 17:22:24 +01001761 assertEquals(
1762 mDeviceIdleController.hasMotionSensor(),
1763 mDeviceIdleController.mMotionListener.isActive());
1764 assertEquals(
1765 mDeviceIdleController.hasMotionSensor(),
1766 mAnyMotionDetector.isMonitoring);
Kweku Adams00e3a372018-09-28 16:57:09 -07001767 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001768 assertFalse(mDeviceIdleController.isScreenOn()
1769 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001770 break;
1771 case STATE_LOCATING:
Robin Lee876b88542018-11-13 17:22:24 +01001772 assertEquals(
1773 mDeviceIdleController.hasMotionSensor(),
1774 mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07001775 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001776 assertFalse(mDeviceIdleController.isScreenOn()
1777 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001778 break;
1779 case STATE_IDLE:
Robin Lee876b88542018-11-13 17:22:24 +01001780 if (mDeviceIdleController.hasMotionSensor()) {
1781 assertTrue(mDeviceIdleController.mMotionListener.isActive()
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001782 // If quick doze is enabled, the motion listener should NOT be active.
1783 || mDeviceIdleController.isQuickDozeEnabled());
Robin Lee876b88542018-11-13 17:22:24 +01001784 }
Kweku Adams00e3a372018-09-28 16:57:09 -07001785 assertFalse(mAnyMotionDetector.isMonitoring);
1786 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001787 assertFalse(mDeviceIdleController.isScreenOn()
1788 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001789 // Light state should be OVERRIDE at this point.
1790 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1791 break;
1792 case STATE_IDLE_MAINTENANCE:
Robin Lee876b88542018-11-13 17:22:24 +01001793 if (mDeviceIdleController.hasMotionSensor()) {
1794 assertTrue(mDeviceIdleController.mMotionListener.isActive()
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001795 // If quick doze is enabled, the motion listener should NOT be active.
1796 || mDeviceIdleController.isQuickDozeEnabled());
Robin Lee876b88542018-11-13 17:22:24 +01001797 }
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001798 assertFalse(mAnyMotionDetector.isMonitoring);
1799 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001800 assertFalse(mDeviceIdleController.isScreenOn()
1801 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001802 break;
1803 case STATE_QUICK_DOZE_DELAY:
1804 // If quick doze is enabled, the motion listener should NOT be active.
1805 assertFalse(mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07001806 assertFalse(mAnyMotionDetector.isMonitoring);
1807 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001808 assertFalse(mDeviceIdleController.isScreenOn()
1809 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001810 break;
1811 default:
1812 fail("Conditions for " + stateToString(expectedState) + " unknown.");
1813 }
1814 }
1815
1816 private void verifyLightStateConditions(int expectedLightState) {
1817 int curLightState = mDeviceIdleController.getLightState();
1818 assertEquals(
1819 "Expected " + lightStateToString(expectedLightState)
1820 + " but was " + lightStateToString(curLightState),
1821 expectedLightState, curLightState);
1822
1823 switch (expectedLightState) {
1824 case LIGHT_STATE_ACTIVE:
1825 assertTrue(
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001826 mDeviceIdleController.isCharging() || mDeviceIdleController.isScreenOn()
1827 // Or there's an alarm coming up soon.
1828 || SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM
1829 > mAlarmManager.getNextWakeFromIdleTime());
Kweku Adams00e3a372018-09-28 16:57:09 -07001830 break;
1831 case LIGHT_STATE_INACTIVE:
1832 case LIGHT_STATE_PRE_IDLE:
1833 case LIGHT_STATE_IDLE:
1834 case LIGHT_STATE_WAITING_FOR_NETWORK:
1835 case LIGHT_STATE_IDLE_MAINTENANCE:
1836 case LIGHT_STATE_OVERRIDE:
1837 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001838 assertFalse(mDeviceIdleController.isScreenOn()
1839 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001840 break;
1841 default:
1842 fail("Conditions for " + lightStateToString(expectedLightState) + " unknown.");
1843 }
1844 }
Denny cy Leec5a7c292019-01-01 17:37:55 +08001845
1846 private void checkNextAlarmTimeWithNewPreIdleFactor(float factor, int state) {
1847 final long errorTolerance = 1000;
1848 enterDeepState(state);
1849 long now = SystemClock.elapsedRealtime();
1850 long alarm = mDeviceIdleController.getNextAlarmTime();
1851 if (state == STATE_INACTIVE || state == STATE_IDLE_PENDING) {
1852 int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor);
1853 if (Float.compare(factor, 1.0f) == 0) {
1854 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
1855 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
1856 } else {
1857 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
1858 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
1859 }
1860 if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) {
1861 mDeviceIdleController.updatePreIdleFactor();
1862 long newAlarm = mDeviceIdleController.getNextAlarmTime();
1863 long newDelay = (long) ((alarm - now) * factor);
1864 assertTrue("setPreIdleTimeoutFactor: " + factor,
1865 Math.abs(newDelay - (newAlarm - now)) < errorTolerance);
1866 mDeviceIdleController.resetPreIdleTimeoutMode();
1867 mDeviceIdleController.updatePreIdleFactor();
1868 mDeviceIdleController.maybeDoImmediateMaintenance();
1869 newAlarm = mDeviceIdleController.getNextAlarmTime();
1870 assertTrue("resetPreIdleTimeoutMode from: " + factor,
1871 Math.abs(newAlarm - alarm) < errorTolerance);
1872 mDeviceIdleController.setPreIdleTimeoutFactor(factor);
1873 now = SystemClock.elapsedRealtime();
1874 enterDeepState(state);
1875 newAlarm = mDeviceIdleController.getNextAlarmTime();
1876 assertTrue("setPreIdleTimeoutFactor: " + factor + " before step to idle",
1877 Math.abs(newDelay - (newAlarm - now)) < errorTolerance);
1878 mDeviceIdleController.resetPreIdleTimeoutMode();
1879 mDeviceIdleController.updatePreIdleFactor();
1880 mDeviceIdleController.maybeDoImmediateMaintenance();
1881 }
1882 } else {
1883 mDeviceIdleController.setPreIdleTimeoutFactor(factor);
1884 mDeviceIdleController.updatePreIdleFactor();
1885 long newAlarm = mDeviceIdleController.getNextAlarmTime();
1886 assertTrue("setPreIdleTimeoutFactor: " + factor
1887 + " shounld not change next alarm" ,
1888 (newAlarm == alarm));
1889 mDeviceIdleController.resetPreIdleTimeoutMode();
1890 mDeviceIdleController.updatePreIdleFactor();
1891 mDeviceIdleController.maybeDoImmediateMaintenance();
1892 }
1893 }
1894
1895 private void checkMaybeDoAnImmediateMaintenance(float factor) {
1896 int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor);
1897 final long minuteInMillis = 60 * 1000;
1898 if (Float.compare(factor, 1.0f) == 0) {
1899 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
1900 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
1901 } else {
1902 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
1903 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
1904 }
1905 if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) {
1906 enterDeepState(STATE_IDLE);
1907 long now = SystemClock.elapsedRealtime();
1908 long alarm = mDeviceIdleController.getNextAlarmTime();
1909 mDeviceIdleController.setIdleStartTimeForTest(
1910 now - (long) (mConstants.IDLE_TIMEOUT * 0.6));
1911 mDeviceIdleController.maybeDoImmediateMaintenance();
1912 long newAlarm = mDeviceIdleController.getNextAlarmTime();
1913 assertTrue("maintenance not reschedule IDLE_TIMEOUT * 0.6",
1914 newAlarm == alarm);
1915 mDeviceIdleController.setIdleStartTimeForTest(
1916 now - (long) (mConstants.IDLE_TIMEOUT * 1.2));
1917 mDeviceIdleController.maybeDoImmediateMaintenance();
1918 newAlarm = mDeviceIdleController.getNextAlarmTime();
1919 assertTrue("maintenance not reschedule IDLE_TIMEOUT * 1.2",
1920 (newAlarm - now) < minuteInMillis);
1921 mDeviceIdleController.resetPreIdleTimeoutMode();
1922 mDeviceIdleController.updatePreIdleFactor();
1923 }
1924 }
Kweku Adams00e3a372018-09-28 16:57:09 -07001925}