blob: 88273b7bc2cf43005c63332f15b2058b2f6a3e96 [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
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070020import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer;
Kweku Adams00e3a372018-09-28 16:57:09 -070021import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
22import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080023import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder;
Kweku Adams00e3a372018-09-28 16:57:09 -070024import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
25import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession;
26import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
27import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
28import static com.android.server.DeviceIdleController.LIGHT_STATE_ACTIVE;
29import static com.android.server.DeviceIdleController.LIGHT_STATE_IDLE;
30import static com.android.server.DeviceIdleController.LIGHT_STATE_IDLE_MAINTENANCE;
31import static com.android.server.DeviceIdleController.LIGHT_STATE_INACTIVE;
32import static com.android.server.DeviceIdleController.LIGHT_STATE_OVERRIDE;
33import static com.android.server.DeviceIdleController.LIGHT_STATE_PRE_IDLE;
34import static com.android.server.DeviceIdleController.LIGHT_STATE_WAITING_FOR_NETWORK;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070035import static com.android.server.DeviceIdleController.MSG_REPORT_STATIONARY_STATUS;
Kweku Adams00e3a372018-09-28 16:57:09 -070036import static com.android.server.DeviceIdleController.STATE_ACTIVE;
37import static com.android.server.DeviceIdleController.STATE_IDLE;
38import static com.android.server.DeviceIdleController.STATE_IDLE_MAINTENANCE;
39import static com.android.server.DeviceIdleController.STATE_IDLE_PENDING;
40import static com.android.server.DeviceIdleController.STATE_INACTIVE;
41import static com.android.server.DeviceIdleController.STATE_LOCATING;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070042import static com.android.server.DeviceIdleController.STATE_QUICK_DOZE_DELAY;
Kweku Adams00e3a372018-09-28 16:57:09 -070043import static com.android.server.DeviceIdleController.STATE_SENSING;
44import static com.android.server.DeviceIdleController.lightStateToString;
45import static com.android.server.DeviceIdleController.stateToString;
46
47import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertFalse;
49import static org.junit.Assert.assertTrue;
50import static org.junit.Assert.fail;
51import static org.mockito.ArgumentMatchers.any;
Kweku Adams9da2bb92018-12-20 06:34:39 -080052import static org.mockito.ArgumentMatchers.anyBoolean;
Kweku Adams00e3a372018-09-28 16:57:09 -070053import static org.mockito.ArgumentMatchers.anyInt;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070054import static org.mockito.ArgumentMatchers.anyLong;
Kweku Adams00e3a372018-09-28 16:57:09 -070055import static org.mockito.ArgumentMatchers.anyString;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070056import static org.mockito.ArgumentMatchers.argThat;
Robin Lee876b88542018-11-13 17:22:24 +010057import static org.mockito.ArgumentMatchers.eq;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080058import static org.mockito.Mockito.never;
59import static org.mockito.Mockito.reset;
60import static org.mockito.Mockito.verify;
Kweku Adams00e3a372018-09-28 16:57:09 -070061
62import android.app.ActivityManagerInternal;
63import android.app.AlarmManager;
64import android.app.IActivityManager;
Kweku Adamsa457f4e2018-10-03 15:56:06 -070065import android.content.ContentResolver;
Kweku Adams00e3a372018-09-28 16:57:09 -070066import android.content.Context;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070067import android.content.Intent;
Robin Lee876b88542018-11-13 17:22:24 +010068import android.hardware.Sensor;
Kweku Adams00e3a372018-09-28 16:57:09 -070069import android.hardware.SensorManager;
70import android.location.LocationManager;
71import android.location.LocationProvider;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070072import android.net.ConnectivityManager;
73import android.net.NetworkInfo;
Kweku Adams00e3a372018-09-28 16:57:09 -070074import android.os.Handler;
75import android.os.Looper;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070076import android.os.Message;
Kweku Adams00e3a372018-09-28 16:57:09 -070077import android.os.PowerManager;
78import android.os.PowerManagerInternal;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070079import android.os.PowerSaveState;
Kweku Adamsa457f4e2018-10-03 15:56:06 -070080import android.os.SystemClock;
Kweku Adams00e3a372018-09-28 16:57:09 -070081
82import androidx.test.runner.AndroidJUnit4;
83
Robin Lee876b88542018-11-13 17:22:24 +010084import com.android.server.deviceidle.ConstraintController;
Kweku Adams00e3a372018-09-28 16:57:09 -070085import com.android.server.net.NetworkPolicyManagerInternal;
86import com.android.server.wm.ActivityTaskManagerInternal;
87
88import org.junit.After;
89import org.junit.Before;
90import org.junit.Test;
91import org.junit.runner.RunWith;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070092import org.mockito.ArgumentCaptor;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080093import org.mockito.InOrder;
Kweku Adams00e3a372018-09-28 16:57:09 -070094import org.mockito.Mock;
95import org.mockito.MockitoSession;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070096import org.mockito.invocation.InvocationOnMock;
Kweku Adams00e3a372018-09-28 16:57:09 -070097import org.mockito.quality.Strictness;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -070098import org.mockito.stubbing.Answer;
Kweku Adams00e3a372018-09-28 16:57:09 -070099
100/**
101 * Tests for {@link com.android.server.DeviceIdleController}.
102 */
103@RunWith(AndroidJUnit4.class)
104public class DeviceIdleControllerTest {
105 private DeviceIdleController mDeviceIdleController;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -0700106 private DeviceIdleController.MyHandler mHandler;
Kweku Adams00e3a372018-09-28 16:57:09 -0700107 private AnyMotionDetectorForTest mAnyMotionDetector;
108 private AppStateTrackerForTest mAppStateTracker;
Kweku Adams9da2bb92018-12-20 06:34:39 -0800109 private DeviceIdleController.Constants mConstants;
Kweku Adams799858b2018-10-08 17:19:08 -0700110 private InjectorForTest mInjector;
Kweku Adams00e3a372018-09-28 16:57:09 -0700111
112 private MockitoSession mMockingSession;
113 @Mock
Kweku Adams00e3a372018-09-28 16:57:09 -0700114 private AlarmManager mAlarmManager;
115 @Mock
Kweku Adams799858b2018-10-08 17:19:08 -0700116 private ConnectivityService mConnectivityService;
117 @Mock
Kweku Adams9da2bb92018-12-20 06:34:39 -0800118 private ContentResolver mContentResolver;
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700119 @Mock
120 private IActivityManager mIActivityManager;
121 @Mock
Kweku Adams00e3a372018-09-28 16:57:09 -0700122 private LocationManager mLocationManager;
123 @Mock
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700124 private PowerManager mPowerManager;
125 @Mock
126 private PowerManager.WakeLock mWakeLock;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700127 @Mock
128 private PowerManagerInternal mPowerManagerInternal;
Robin Lee876b88542018-11-13 17:22:24 +0100129 @Mock
130 private SensorManager mSensorManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700131
132 class InjectorForTest extends DeviceIdleController.Injector {
Kweku Adams799858b2018-10-08 17:19:08 -0700133 ConnectivityService connectivityService;
134 LocationManager locationManager;
Robin Lee876b88542018-11-13 17:22:24 +0100135 ConstraintController constraintController;
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -0700136 // Freeze time for testing.
137 long nowElapsed;
Kweku Adams00e3a372018-09-28 16:57:09 -0700138
139 InjectorForTest(Context ctx) {
140 super(ctx);
141 }
142
143 @Override
144 AlarmManager getAlarmManager() {
145 return mAlarmManager;
146 }
147
148 @Override
149 AnyMotionDetector getAnyMotionDetector(Handler handler, SensorManager sm,
150 AnyMotionDetector.DeviceIdleCallback callback, float angleThreshold) {
151 return mAnyMotionDetector;
152 }
153
154 @Override
155 AppStateTracker getAppStateTracker(Context ctx, Looper loop) {
156 return mAppStateTracker;
157 }
158
159 @Override
160 ConnectivityService getConnectivityService() {
Kweku Adams799858b2018-10-08 17:19:08 -0700161 return connectivityService;
Kweku Adams00e3a372018-09-28 16:57:09 -0700162 }
163
164 @Override
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -0700165 long getElapsedRealtime() {
166 return nowElapsed;
167 }
168
169 @Override
Kweku Adams00e3a372018-09-28 16:57:09 -0700170 LocationManager getLocationManager() {
Kweku Adams799858b2018-10-08 17:19:08 -0700171 return locationManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700172 }
173
174 @Override
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700175 DeviceIdleController.MyHandler getHandler(DeviceIdleController controller) {
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -0700176 if (mHandler == null) {
177 mHandler = controller.new MyHandler(getContext().getMainLooper());
178 spyOn(mHandler);
179 doNothing().when(mHandler).handleMessage(argThat((message) ->
180 message.what != MSG_REPORT_STATIONARY_STATUS));
181 doAnswer(new Answer<Boolean>() {
182 @Override
183 public Boolean answer(InvocationOnMock invocation) throws Throwable {
184 Message msg = invocation.getArgument(0);
185 mHandler.handleMessage(msg);
186 return true;
187 }
188 }).when(mHandler).sendMessageDelayed(
189 argThat((message) -> message.what == MSG_REPORT_STATIONARY_STATUS),
190 anyLong());
191 }
192
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800193 return mHandler;
Kweku Adams00e3a372018-09-28 16:57:09 -0700194 }
195
196 @Override
197 PowerManager getPowerManager() {
198 return mPowerManager;
199 }
Robin Lee876b88542018-11-13 17:22:24 +0100200
201 @Override
202 SensorManager getSensorManager() {
203 return mSensorManager;
204 }
205
206 @Override
207 ConstraintController getConstraintController(
208 Handler handler, DeviceIdleController.LocalService localService) {
209 return constraintController;
210 }
211
212 @Override
213 boolean useMotionSensor() {
214 return true;
215 }
Kweku Adams00e3a372018-09-28 16:57:09 -0700216 }
217
218 private class AnyMotionDetectorForTest extends AnyMotionDetector {
219 boolean isMonitoring = false;
220
221 AnyMotionDetectorForTest() {
Robin Lee876b88542018-11-13 17:22:24 +0100222 super(mPowerManager, mock(Handler.class), mSensorManager,
Kweku Adams00e3a372018-09-28 16:57:09 -0700223 mock(DeviceIdleCallback.class), 0.5f);
224 }
225
226 @Override
Kweku Adams9da2bb92018-12-20 06:34:39 -0800227 public boolean hasSensor() {
228 return true;
229 }
230
231 @Override
Kweku Adams00e3a372018-09-28 16:57:09 -0700232 public void checkForAnyMotion() {
233 isMonitoring = true;
234 }
235
236 @Override
237 public void stop() {
238 isMonitoring = false;
239 }
240 }
241
242 private class AppStateTrackerForTest extends AppStateTracker {
243 AppStateTrackerForTest(Context ctx, Looper looper) {
244 super(ctx, looper);
245 }
246
247 @Override
248 public void onSystemServicesReady() {
249 // Do nothing.
250 }
251
252 @Override
253 IActivityManager injectIActivityManager() {
254 return mIActivityManager;
255 }
256 }
257
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -0700258 private class StationaryListenerForTest implements DeviceIdleController.StationaryListener {
259 boolean motionExpected = false;
260 boolean isStationary = false;
261
262 @Override
263 public void onDeviceStationaryChanged(boolean isStationary) {
264 if (isStationary == motionExpected) {
265 fail("Unexpected device stationary status: " + isStationary);
266 }
267 this.isStationary = isStationary;
268 }
269 }
270
Kweku Adams00e3a372018-09-28 16:57:09 -0700271 @Before
272 public void setUp() {
273 mMockingSession = mockitoSession()
274 .initMocks(this)
275 .strictness(Strictness.LENIENT)
Robin Lee876b88542018-11-13 17:22:24 +0100276 .spyStatic(LocalServices.class)
Kweku Adams00e3a372018-09-28 16:57:09 -0700277 .startMocking();
Robin Lee54d44962018-12-17 17:43:57 +0100278 spyOn(getContext());
279 doReturn(null).when(getContext()).registerReceiver(any(), any());
Kweku Adams00e3a372018-09-28 16:57:09 -0700280 doReturn(mock(ActivityManagerInternal.class))
281 .when(() -> LocalServices.getService(ActivityManagerInternal.class));
282 doReturn(mock(ActivityTaskManagerInternal.class))
283 .when(() -> LocalServices.getService(ActivityTaskManagerInternal.class));
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800284 doReturn(mock(AlarmManagerInternal.class))
285 .when(() -> LocalServices.getService(AlarmManagerInternal.class));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700286 doReturn(mPowerManagerInternal)
Kweku Adams00e3a372018-09-28 16:57:09 -0700287 .when(() -> LocalServices.getService(PowerManagerInternal.class));
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800288 when(mPowerManagerInternal.getLowPowerState(anyInt()))
289 .thenReturn(mock(PowerSaveState.class));
Kweku Adams00e3a372018-09-28 16:57:09 -0700290 doReturn(mock(NetworkPolicyManagerInternal.class))
291 .when(() -> LocalServices.getService(NetworkPolicyManagerInternal.class));
292 when(mPowerManager.newWakeLock(anyInt(), anyString())).thenReturn(mWakeLock);
293 doNothing().when(mWakeLock).acquire();
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700294 doNothing().when(mAlarmManager).set(anyInt(), anyLong(), anyString(), any(), any());
Robin Lee876b88542018-11-13 17:22:24 +0100295 doReturn(mock(Sensor.class)).when(mSensorManager)
296 .getDefaultSensor(eq(Sensor.TYPE_SIGNIFICANT_MOTION), eq(true));
297 doReturn(true).when(mSensorManager).registerListener(any(), any(), anyInt());
Kweku Adams00e3a372018-09-28 16:57:09 -0700298 mAppStateTracker = new AppStateTrackerForTest(getContext(), Looper.getMainLooper());
299 mAnyMotionDetector = new AnyMotionDetectorForTest();
Kweku Adams799858b2018-10-08 17:19:08 -0700300 mInjector = new InjectorForTest(getContext());
Kweku Adams9da2bb92018-12-20 06:34:39 -0800301 doNothing().when(mContentResolver).registerContentObserver(any(), anyBoolean(), any());
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800302
Kweku Adams799858b2018-10-08 17:19:08 -0700303 mDeviceIdleController = new DeviceIdleController(getContext(), mInjector);
Kweku Adams00e3a372018-09-28 16:57:09 -0700304 spyOn(mDeviceIdleController);
305 doNothing().when(mDeviceIdleController).publishBinderService(any(), any());
306 mDeviceIdleController.onStart();
307 mDeviceIdleController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
308 mDeviceIdleController.setDeepEnabledForTest(true);
309 mDeviceIdleController.setLightEnabledForTest(true);
Kweku Adams9da2bb92018-12-20 06:34:39 -0800310
311 // Get the same Constants object that mDeviceIdleController got.
312 mConstants = mInjector.getConstants(mDeviceIdleController,
313 mInjector.getHandler(mDeviceIdleController), mContentResolver);
Kweku Adams00e3a372018-09-28 16:57:09 -0700314 }
315
316 @After
317 public void tearDown() {
318 if (mMockingSession != null) {
319 mMockingSession.finishMocking();
320 }
Robin Lee876b88542018-11-13 17:22:24 +0100321 // DeviceIdleController adds these to LocalServices in the constructor, so we have to remove
322 // them after each test, otherwise, subsequent tests will fail.
Kweku Adams00e3a372018-09-28 16:57:09 -0700323 LocalServices.removeServiceForTest(AppStateTracker.class);
Robin Lee876b88542018-11-13 17:22:24 +0100324 LocalServices.removeServiceForTest(DeviceIdleController.LocalService.class);
Kweku Adams00e3a372018-09-28 16:57:09 -0700325 }
326
327 @Test
328 public void testUpdateInteractivityLocked() {
329 doReturn(false).when(mPowerManager).isInteractive();
330 mDeviceIdleController.updateInteractivityLocked();
331 assertFalse(mDeviceIdleController.isScreenOn());
332
333 // Make sure setting false when screen is already off doesn't change anything.
334 doReturn(false).when(mPowerManager).isInteractive();
335 mDeviceIdleController.updateInteractivityLocked();
336 assertFalse(mDeviceIdleController.isScreenOn());
337
338 // Test changing from screen off to screen on.
339 doReturn(true).when(mPowerManager).isInteractive();
340 mDeviceIdleController.updateInteractivityLocked();
341 assertTrue(mDeviceIdleController.isScreenOn());
342
343 // Make sure setting true when screen is already on doesn't change anything.
344 doReturn(true).when(mPowerManager).isInteractive();
345 mDeviceIdleController.updateInteractivityLocked();
346 assertTrue(mDeviceIdleController.isScreenOn());
347
348 // Test changing from screen on to screen off.
349 doReturn(false).when(mPowerManager).isInteractive();
350 mDeviceIdleController.updateInteractivityLocked();
351 assertFalse(mDeviceIdleController.isScreenOn());
352 }
353
354 @Test
355 public void testUpdateChargingLocked() {
356 mDeviceIdleController.updateChargingLocked(false);
357 assertFalse(mDeviceIdleController.isCharging());
358
359 // Make sure setting false when charging is already off doesn't change anything.
360 mDeviceIdleController.updateChargingLocked(false);
361 assertFalse(mDeviceIdleController.isCharging());
362
363 // Test changing from charging off to charging on.
364 mDeviceIdleController.updateChargingLocked(true);
365 assertTrue(mDeviceIdleController.isCharging());
366
367 // Make sure setting true when charging is already on doesn't change anything.
368 mDeviceIdleController.updateChargingLocked(true);
369 assertTrue(mDeviceIdleController.isCharging());
370
371 // Test changing from charging on to charging off.
372 mDeviceIdleController.updateChargingLocked(false);
373 assertFalse(mDeviceIdleController.isCharging());
374 }
375
376 @Test
Kweku Adams799858b2018-10-08 17:19:08 -0700377 public void testUpdateConnectivityState() {
378 // No connectivity service
379 final boolean isConnected = mDeviceIdleController.isNetworkConnected();
380 mInjector.connectivityService = null;
381 mDeviceIdleController.updateConnectivityState(null);
382 assertEquals(isConnected, mDeviceIdleController.isNetworkConnected());
383
384 // No active network info
385 mInjector.connectivityService = mConnectivityService;
386 doReturn(null).when(mConnectivityService).getActiveNetworkInfo();
387 mDeviceIdleController.updateConnectivityState(null);
388 assertFalse(mDeviceIdleController.isNetworkConnected());
389
390 // Active network info says connected.
391 final NetworkInfo ani = mock(NetworkInfo.class);
392 doReturn(ani).when(mConnectivityService).getActiveNetworkInfo();
393 doReturn(true).when(ani).isConnected();
394 mDeviceIdleController.updateConnectivityState(null);
395 assertTrue(mDeviceIdleController.isNetworkConnected());
396
397 // Active network info says not connected.
398 doReturn(false).when(ani).isConnected();
399 mDeviceIdleController.updateConnectivityState(null);
400 assertFalse(mDeviceIdleController.isNetworkConnected());
401
402 // Wrong intent passed (false).
403 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
404 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3);
405 doReturn(true).when(ani).isConnected();
406 doReturn(1).when(ani).getType();
407 mDeviceIdleController.updateConnectivityState(intent);
408 // Wrong intent means we shouldn't update the connected state.
409 assertFalse(mDeviceIdleController.isNetworkConnected());
410
411 // Intent says connected.
412 doReturn(1).when(ani).getType();
413 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1);
414 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
415 mDeviceIdleController.updateConnectivityState(intent);
416 assertTrue(mDeviceIdleController.isNetworkConnected());
417
418 // Wrong intent passed (true).
419 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3);
420 // Wrong intent means we shouldn't update the connected state.
421 assertTrue(mDeviceIdleController.isNetworkConnected());
422
423 // Intent says not connected.
424 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1);
425 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
426 mDeviceIdleController.updateConnectivityState(intent);
427 assertFalse(mDeviceIdleController.isNetworkConnected());
428 }
429
430 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700431 public void testUpdateQuickDozeFlagLocked() {
432 mDeviceIdleController.updateQuickDozeFlagLocked(false);
433 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
434
435 // Make sure setting false when quick doze is already off doesn't change anything.
436 mDeviceIdleController.updateQuickDozeFlagLocked(false);
437 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
438
439 // Test changing from quick doze off to quick doze on.
440 mDeviceIdleController.updateQuickDozeFlagLocked(true);
441 assertTrue(mDeviceIdleController.isQuickDozeEnabled());
442
443 // Make sure setting true when quick doze is already on doesn't change anything.
444 mDeviceIdleController.updateQuickDozeFlagLocked(true);
445 assertTrue(mDeviceIdleController.isQuickDozeEnabled());
446
447 // Test changing from quick doze on to quick doze off.
448 mDeviceIdleController.updateQuickDozeFlagLocked(false);
449 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
450 }
451
452 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700453 public void testStateActiveToStateInactive_ConditionsNotMet() {
454 mDeviceIdleController.becomeActiveLocked("testing", 0);
455 verifyStateConditions(STATE_ACTIVE);
456
457 // State should stay ACTIVE with screen on and charging.
458 setChargingOn(true);
459 setScreenOn(true);
460
461 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
462 verifyStateConditions(STATE_ACTIVE);
463
464 // State should stay ACTIVE with charging on.
465 setChargingOn(true);
466 setScreenOn(false);
467
468 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
469 verifyStateConditions(STATE_ACTIVE);
470
471 // State should stay ACTIVE with screen on.
472 // Note the different operation order here makes sure the state doesn't change before test.
473 setScreenOn(true);
474 setChargingOn(false);
475
476 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
477 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800478
479 mConstants.WAIT_FOR_UNLOCK = false;
480 setScreenLocked(true);
481 setScreenOn(true);
482 setChargingOn(false);
483
484 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
485 verifyStateConditions(STATE_ACTIVE);
486
487 setScreenLocked(false);
488 setScreenOn(true);
489 setChargingOn(false);
490
491 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
492 verifyStateConditions(STATE_ACTIVE);
493
494 mConstants.WAIT_FOR_UNLOCK = true;
495 setScreenLocked(false);
496 setScreenOn(true);
497 setChargingOn(false);
498
499 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
500 verifyStateConditions(STATE_ACTIVE);
Kweku Adams00e3a372018-09-28 16:57:09 -0700501 }
502
503 @Test
504 public void testLightStateActiveToLightStateInactive_ConditionsNotMet() {
505 mDeviceIdleController.becomeActiveLocked("testing", 0);
506 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
507
508 // State should stay ACTIVE with screen on and charging.
509 setChargingOn(true);
510 setScreenOn(true);
511
512 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
513 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
514
515 // State should stay ACTIVE with charging on.
516 setChargingOn(true);
517 setScreenOn(false);
518
519 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
520 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
521
522 // State should stay ACTIVE with screen on.
523 // Note the different operation order here makes sure the state doesn't change before test.
524 setScreenOn(true);
525 setChargingOn(false);
526
527 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
528 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
529 }
530
531 @Test
532 public void testStateActiveToStateInactive_ConditionsMet() {
533 mDeviceIdleController.becomeActiveLocked("testing", 0);
534 verifyStateConditions(STATE_ACTIVE);
535
536 setChargingOn(false);
537 setScreenOn(false);
538
539 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
540 verifyStateConditions(STATE_INACTIVE);
541 }
542
543 @Test
544 public void testLightStateActiveToLightStateInactive_ConditionsMet() {
545 mDeviceIdleController.becomeActiveLocked("testing", 0);
546 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
547
548 setChargingOn(false);
549 setScreenOn(false);
550
551 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
552 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
553 }
554
555 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700556 public void testTransitionFromAnyStateToStateQuickDozeDelay() {
557 enterDeepState(STATE_ACTIVE);
558 setQuickDozeEnabled(true);
559 setChargingOn(false);
560 setScreenOn(false);
561 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
562
563 enterDeepState(STATE_INACTIVE);
564 setQuickDozeEnabled(true);
565 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
566
567 enterDeepState(STATE_IDLE_PENDING);
568 setQuickDozeEnabled(true);
569 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
570
571 enterDeepState(STATE_SENSING);
572 setQuickDozeEnabled(true);
573 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
574
575 enterDeepState(STATE_LOCATING);
576 setQuickDozeEnabled(true);
577 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
578
579 // IDLE should stay as IDLE.
580 enterDeepState(STATE_IDLE);
581 setQuickDozeEnabled(true);
582 verifyStateConditions(STATE_IDLE);
583
584 // IDLE_MAINTENANCE should stay as IDLE_MAINTENANCE.
585 enterDeepState(STATE_IDLE_MAINTENANCE);
586 setQuickDozeEnabled(true);
587 verifyStateConditions(STATE_IDLE_MAINTENANCE);
588
589 enterDeepState(STATE_QUICK_DOZE_DELAY);
590 setQuickDozeEnabled(true);
591 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
592 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
593 }
594
595 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700596 public void testStepIdleStateLocked_InvalidStates() {
597 mDeviceIdleController.becomeActiveLocked("testing", 0);
598 mDeviceIdleController.stepIdleStateLocked("testing");
599 // mDeviceIdleController.stepIdleStateLocked doesn't handle the ACTIVE case, so the state
600 // should stay as ACTIVE.
601 verifyStateConditions(STATE_ACTIVE);
602 }
603
604 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700605 public void testStepIdleStateLocked_ValidStates_QuickDoze() {
606 setAlarmSoon(false);
607
608 // Quick doze should go directly into IDLE.
609 enterDeepState(STATE_QUICK_DOZE_DELAY);
610 mDeviceIdleController.stepIdleStateLocked("testing");
611 verifyStateConditions(STATE_IDLE);
612
613 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
614
615 mDeviceIdleController.stepIdleStateLocked("testing");
616 verifyStateConditions(STATE_IDLE_MAINTENANCE);
617
618 mDeviceIdleController.stepIdleStateLocked("testing");
619 verifyStateConditions(STATE_IDLE);
620
621 mDeviceIdleController.stepIdleStateLocked("testing");
622 verifyStateConditions(STATE_IDLE_MAINTENANCE);
623 }
624
625 @Test
Kweku Adamsbf869382018-10-09 18:26:56 -0700626 public void testStepIdleStateLocked_ValidStates_WithWakeFromIdleAlarmSoon() {
627 enterDeepState(STATE_ACTIVE);
628 // Return that there's an alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700629 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700630 mDeviceIdleController.stepIdleStateLocked("testing");
631 verifyStateConditions(STATE_ACTIVE);
632
633 // Everything besides ACTIVE should end up as INACTIVE since the screen would be off.
634
635 enterDeepState(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700636 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700637 mDeviceIdleController.stepIdleStateLocked("testing");
638 verifyStateConditions(STATE_INACTIVE);
639
640 enterDeepState(STATE_IDLE_PENDING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700641 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700642 mDeviceIdleController.stepIdleStateLocked("testing");
643 verifyStateConditions(STATE_INACTIVE);
644
645 enterDeepState(STATE_SENSING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700646 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700647 mDeviceIdleController.stepIdleStateLocked("testing");
648 verifyStateConditions(STATE_INACTIVE);
649
650 enterDeepState(STATE_LOCATING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700651 setAlarmSoon(true);
652 mDeviceIdleController.stepIdleStateLocked("testing");
653 verifyStateConditions(STATE_INACTIVE);
654
655 // With quick doze enabled, we should end up in QUICK_DOZE_DELAY instead of INACTIVE.
656 enterDeepState(STATE_QUICK_DOZE_DELAY);
657 setQuickDozeEnabled(true);
658 setAlarmSoon(true);
659 mDeviceIdleController.stepIdleStateLocked("testing");
660 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
661
662 // With quick doze disabled, we should end up in INACTIVE instead of QUICK_DOZE_DELAY.
663 enterDeepState(STATE_QUICK_DOZE_DELAY);
664 setQuickDozeEnabled(false);
665 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700666 mDeviceIdleController.stepIdleStateLocked("testing");
667 verifyStateConditions(STATE_INACTIVE);
668
669 enterDeepState(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700670 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700671 mDeviceIdleController.stepIdleStateLocked("testing");
672 verifyStateConditions(STATE_INACTIVE);
673
674 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700675 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700676 mDeviceIdleController.stepIdleStateLocked("testing");
677 verifyStateConditions(STATE_INACTIVE);
678 }
679
680 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700681 public void testStepIdleStateLocked_ValidStates_NoLocationManager() {
Kweku Adams799858b2018-10-08 17:19:08 -0700682 mInjector.locationManager = null;
Kweku Adams00e3a372018-09-28 16:57:09 -0700683 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700684 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700685 // Set state to INACTIVE.
686 mDeviceIdleController.becomeActiveLocked("testing", 0);
687 setChargingOn(false);
688 setScreenOn(false);
689 verifyStateConditions(STATE_INACTIVE);
690
691 mDeviceIdleController.stepIdleStateLocked("testing");
692 verifyStateConditions(STATE_IDLE_PENDING);
693
694 mDeviceIdleController.stepIdleStateLocked("testing");
695 verifyStateConditions(STATE_SENSING);
696
697 mDeviceIdleController.stepIdleStateLocked("testing");
698 // No location manager, so SENSING should go straight to IDLE.
699 verifyStateConditions(STATE_IDLE);
700
701 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
702
703 mDeviceIdleController.stepIdleStateLocked("testing");
704 verifyStateConditions(STATE_IDLE_MAINTENANCE);
705
706 mDeviceIdleController.stepIdleStateLocked("testing");
707 verifyStateConditions(STATE_IDLE);
708
709 mDeviceIdleController.stepIdleStateLocked("testing");
710 verifyStateConditions(STATE_IDLE_MAINTENANCE);
711 }
712
713 @Test
714 public void testStepIdleStateLocked_ValidStates_WithLocationManager_NoProviders() {
715 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700716 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700717 // Set state to INACTIVE.
718 mDeviceIdleController.becomeActiveLocked("testing", 0);
719 setChargingOn(false);
720 setScreenOn(false);
721 verifyStateConditions(STATE_INACTIVE);
722
723 mDeviceIdleController.stepIdleStateLocked("testing");
724 verifyStateConditions(STATE_IDLE_PENDING);
725
726 mDeviceIdleController.stepIdleStateLocked("testing");
727 verifyStateConditions(STATE_SENSING);
728
729 mDeviceIdleController.stepIdleStateLocked("testing");
730 // Location manager exists but there isn't a network or GPS provider,
731 // so SENSING should go straight to IDLE.
732 verifyStateConditions(STATE_IDLE);
733
734 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
735
736 mDeviceIdleController.stepIdleStateLocked("testing");
737 verifyStateConditions(STATE_IDLE_MAINTENANCE);
738
739 mDeviceIdleController.stepIdleStateLocked("testing");
740 verifyStateConditions(STATE_IDLE);
741
742 mDeviceIdleController.stepIdleStateLocked("testing");
743 verifyStateConditions(STATE_IDLE_MAINTENANCE);
744 }
745
746 @Test
747 public void testStepIdleStateLocked_ValidStates_WithLocationManager_WithProviders() {
Kweku Adams799858b2018-10-08 17:19:08 -0700748 mInjector.locationManager = mLocationManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700749 doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(anyString());
750 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700751 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700752 // Set state to INACTIVE.
753 mDeviceIdleController.becomeActiveLocked("testing", 0);
754 setChargingOn(false);
755 setScreenOn(false);
756 verifyStateConditions(STATE_INACTIVE);
757
758 mDeviceIdleController.stepIdleStateLocked("testing");
759 verifyStateConditions(STATE_IDLE_PENDING);
760
761 mDeviceIdleController.stepIdleStateLocked("testing");
762 verifyStateConditions(STATE_SENSING);
763
764 mDeviceIdleController.stepIdleStateLocked("testing");
765 // Location manager exists with a provider, so SENSING should go to LOCATING.
766 verifyStateConditions(STATE_LOCATING);
767
768 mDeviceIdleController.stepIdleStateLocked("testing");
769 verifyStateConditions(STATE_IDLE);
770
771 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
772
773 mDeviceIdleController.stepIdleStateLocked("testing");
774 verifyStateConditions(STATE_IDLE_MAINTENANCE);
775
776 mDeviceIdleController.stepIdleStateLocked("testing");
777 verifyStateConditions(STATE_IDLE);
778
779 mDeviceIdleController.stepIdleStateLocked("testing");
780 verifyStateConditions(STATE_IDLE_MAINTENANCE);
781 }
782
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700783 @Test
Kweku Adams799858b2018-10-08 17:19:08 -0700784 public void testLightStepIdleStateLocked_InvalidStates() {
785 mDeviceIdleController.becomeActiveLocked("testing", 0);
786 mDeviceIdleController.stepLightIdleStateLocked("testing");
787 // stepLightIdleStateLocked doesn't handle the ACTIVE case, so the state
788 // should stay as ACTIVE.
789 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
790 }
791
792 /**
793 * Make sure stepLightIdleStateLocked doesn't change state when the state is
794 * LIGHT_STATE_OVERRIDE.
795 */
796 @Test
797 public void testLightStepIdleStateLocked_Overriden() {
798 enterLightState(LIGHT_STATE_OVERRIDE);
799 mDeviceIdleController.stepLightIdleStateLocked("testing");
800 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
801 }
802
803 @Test
804 public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NetworkConnected() {
805 setNetworkConnected(true);
806 mDeviceIdleController.setJobsActive(false);
807 mDeviceIdleController.setAlarmsActive(false);
808 mDeviceIdleController.setActiveIdleOpsForTest(0);
809
810 // Set state to INACTIVE.
811 mDeviceIdleController.becomeActiveLocked("testing", 0);
812 setChargingOn(false);
813 setScreenOn(false);
814 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
815
816 // No active ops means INACTIVE should go straight to IDLE.
817 mDeviceIdleController.stepLightIdleStateLocked("testing");
818 verifyLightStateConditions(LIGHT_STATE_IDLE);
819
820 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
821
822 mDeviceIdleController.stepLightIdleStateLocked("testing");
823 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
824
825 mDeviceIdleController.stepLightIdleStateLocked("testing");
826 verifyLightStateConditions(LIGHT_STATE_IDLE);
827
828 mDeviceIdleController.stepLightIdleStateLocked("testing");
829 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
830 }
831
832 @Test
833 public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NetworkConnected() {
834 setNetworkConnected(true);
835 // Set state to INACTIVE.
836 mDeviceIdleController.becomeActiveLocked("testing", 0);
837 setChargingOn(false);
838 setScreenOn(false);
839 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
840
841 // Active ops means INACTIVE should go to PRE_IDLE to wait.
842 mDeviceIdleController.setJobsActive(true);
843 mDeviceIdleController.setAlarmsActive(true);
844 mDeviceIdleController.setActiveIdleOpsForTest(1);
845 mDeviceIdleController.stepLightIdleStateLocked("testing");
846 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
847
848 // Even with active ops, PRE_IDLE should go to IDLE.
849 mDeviceIdleController.stepLightIdleStateLocked("testing");
850 verifyLightStateConditions(LIGHT_STATE_IDLE);
851
852 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
853
854 mDeviceIdleController.stepLightIdleStateLocked("testing");
855 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
856
857 mDeviceIdleController.stepLightIdleStateLocked("testing");
858 verifyLightStateConditions(LIGHT_STATE_IDLE);
859
860 mDeviceIdleController.stepLightIdleStateLocked("testing");
861 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
862 }
863
864 @Test
865 public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NoNetworkConnected() {
866 setNetworkConnected(false);
867 mDeviceIdleController.setJobsActive(false);
868 mDeviceIdleController.setAlarmsActive(false);
869 mDeviceIdleController.setActiveIdleOpsForTest(0);
870
871 // Set state to INACTIVE.
872 mDeviceIdleController.becomeActiveLocked("testing", 0);
873 setChargingOn(false);
874 setScreenOn(false);
875 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
876
877 // No active ops means INACTIVE should go straight to IDLE.
878 mDeviceIdleController.stepLightIdleStateLocked("testing");
879 verifyLightStateConditions(LIGHT_STATE_IDLE);
880
881 // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now.
882
883 mDeviceIdleController.stepLightIdleStateLocked("testing");
884 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
885
886 mDeviceIdleController.stepLightIdleStateLocked("testing");
887 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
888
889 mDeviceIdleController.stepLightIdleStateLocked("testing");
890 verifyLightStateConditions(LIGHT_STATE_IDLE);
891
892 mDeviceIdleController.stepLightIdleStateLocked("testing");
893 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
894
895 mDeviceIdleController.stepLightIdleStateLocked("testing");
896 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
897 }
898
899 @Test
900 public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NoNetworkConnected() {
901 setNetworkConnected(false);
902 // Set state to INACTIVE.
903 mDeviceIdleController.becomeActiveLocked("testing", 0);
904 setChargingOn(false);
905 setScreenOn(false);
906 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
907
908 // Active ops means INACTIVE should go to PRE_IDLE to wait.
909 mDeviceIdleController.setJobsActive(true);
910 mDeviceIdleController.setAlarmsActive(true);
911 mDeviceIdleController.setActiveIdleOpsForTest(1);
912 mDeviceIdleController.stepLightIdleStateLocked("testing");
913 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
914
915 // Even with active ops, PRE_IDLE should go to IDLE.
916 mDeviceIdleController.stepLightIdleStateLocked("testing");
917 verifyLightStateConditions(LIGHT_STATE_IDLE);
918
919 // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now.
920
921 mDeviceIdleController.stepLightIdleStateLocked("testing");
922 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
923
924 mDeviceIdleController.stepLightIdleStateLocked("testing");
925 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
926
927 mDeviceIdleController.stepLightIdleStateLocked("testing");
928 verifyLightStateConditions(LIGHT_STATE_IDLE);
929
930 mDeviceIdleController.stepLightIdleStateLocked("testing");
931 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
932
933 mDeviceIdleController.stepLightIdleStateLocked("testing");
934 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
935 }
936
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700937 ///////////////// EXIT conditions ///////////////////
938
Kweku Adams799858b2018-10-08 17:19:08 -0700939 @Test
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700940 public void testExitMaintenanceEarlyIfNeededLocked_deep_noActiveOps() {
941 mDeviceIdleController.setJobsActive(false);
942 mDeviceIdleController.setAlarmsActive(false);
943 mDeviceIdleController.setActiveIdleOpsForTest(0);
944
945 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
946
947 enterDeepState(STATE_ACTIVE);
948 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
949 verifyStateConditions(STATE_ACTIVE);
950
951 enterDeepState(STATE_INACTIVE);
952 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
953 verifyStateConditions(STATE_INACTIVE);
954
955 enterDeepState(STATE_IDLE_PENDING);
956 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
957 verifyStateConditions(STATE_IDLE_PENDING);
958
959 enterDeepState(STATE_SENSING);
960 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
961 verifyStateConditions(STATE_SENSING);
962
963 enterDeepState(STATE_LOCATING);
964 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
965 verifyStateConditions(STATE_LOCATING);
966
967 enterDeepState(STATE_IDLE);
968 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
969 verifyStateConditions(STATE_IDLE);
970
971 enterDeepState(STATE_IDLE_MAINTENANCE);
972 // Going into IDLE_MAINTENANCE increments the active idle op count.
973 mDeviceIdleController.setActiveIdleOpsForTest(0);
974 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
975 verifyStateConditions(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700976
977 enterDeepState(STATE_QUICK_DOZE_DELAY);
978 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
979 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700980 }
981
982 @Test
983 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeJobs() {
984 mDeviceIdleController.setJobsActive(true);
985 mDeviceIdleController.setAlarmsActive(false);
986 mDeviceIdleController.setActiveIdleOpsForTest(0);
987
988 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
989
990 enterDeepState(STATE_ACTIVE);
991 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
992 verifyStateConditions(STATE_ACTIVE);
993
994 enterDeepState(STATE_INACTIVE);
995 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
996 verifyStateConditions(STATE_INACTIVE);
997
998 enterDeepState(STATE_IDLE_PENDING);
999 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1000 verifyStateConditions(STATE_IDLE_PENDING);
1001
1002 enterDeepState(STATE_SENSING);
1003 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1004 verifyStateConditions(STATE_SENSING);
1005
1006 enterDeepState(STATE_LOCATING);
1007 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1008 verifyStateConditions(STATE_LOCATING);
1009
1010 enterDeepState(STATE_IDLE);
1011 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1012 verifyStateConditions(STATE_IDLE);
1013
1014 enterDeepState(STATE_IDLE_MAINTENANCE);
1015 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1016 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001017
1018 enterDeepState(STATE_QUICK_DOZE_DELAY);
1019 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1020 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001021 }
1022
1023 @Test
1024 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeAlarms() {
1025 mDeviceIdleController.setJobsActive(false);
1026 mDeviceIdleController.setAlarmsActive(true);
1027 mDeviceIdleController.setActiveIdleOpsForTest(0);
1028
1029 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1030
1031 enterDeepState(STATE_ACTIVE);
1032 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1033 verifyStateConditions(STATE_ACTIVE);
1034
1035 enterDeepState(STATE_INACTIVE);
1036 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1037 verifyStateConditions(STATE_INACTIVE);
1038
1039 enterDeepState(STATE_IDLE_PENDING);
1040 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1041 verifyStateConditions(STATE_IDLE_PENDING);
1042
1043 enterDeepState(STATE_SENSING);
1044 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1045 verifyStateConditions(STATE_SENSING);
1046
1047 enterDeepState(STATE_LOCATING);
1048 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1049 verifyStateConditions(STATE_LOCATING);
1050
1051 enterDeepState(STATE_IDLE);
1052 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1053 verifyStateConditions(STATE_IDLE);
1054
1055 enterDeepState(STATE_IDLE_MAINTENANCE);
1056 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1057 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001058
1059 enterDeepState(STATE_QUICK_DOZE_DELAY);
1060 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1061 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001062 }
1063
1064 @Test
1065 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeOps() {
1066 mDeviceIdleController.setJobsActive(false);
1067 mDeviceIdleController.setAlarmsActive(false);
1068 mDeviceIdleController.setActiveIdleOpsForTest(1);
1069
1070 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1071
1072 enterDeepState(STATE_ACTIVE);
1073 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1074 verifyStateConditions(STATE_ACTIVE);
1075
1076 enterDeepState(STATE_INACTIVE);
1077 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1078 verifyStateConditions(STATE_INACTIVE);
1079
1080 enterDeepState(STATE_IDLE_PENDING);
1081 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1082 verifyStateConditions(STATE_IDLE_PENDING);
1083
1084 enterDeepState(STATE_SENSING);
1085 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1086 verifyStateConditions(STATE_SENSING);
1087
1088 enterDeepState(STATE_LOCATING);
1089 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1090 verifyStateConditions(STATE_LOCATING);
1091
1092 enterDeepState(STATE_IDLE);
1093 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1094 verifyStateConditions(STATE_IDLE);
1095
1096 enterDeepState(STATE_IDLE_MAINTENANCE);
1097 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1098 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001099
1100 enterDeepState(STATE_QUICK_DOZE_DELAY);
1101 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1102 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001103 }
1104
1105 @Test
1106 public void testExitMaintenanceEarlyIfNeededLocked_light_noActiveOps() {
1107 mDeviceIdleController.setJobsActive(false);
1108 mDeviceIdleController.setAlarmsActive(false);
1109 mDeviceIdleController.setActiveIdleOpsForTest(0);
1110
1111 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1112
1113 enterLightState(LIGHT_STATE_ACTIVE);
1114 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1115 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1116
1117 enterLightState(LIGHT_STATE_INACTIVE);
1118 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1119 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1120
1121 enterLightState(LIGHT_STATE_PRE_IDLE);
1122 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1123 verifyLightStateConditions(LIGHT_STATE_IDLE);
1124
1125 enterLightState(LIGHT_STATE_IDLE);
1126 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1127 verifyLightStateConditions(LIGHT_STATE_IDLE);
1128
1129 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1130 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1131 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1132
1133 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1134 // Going into IDLE_MAINTENANCE increments the active idle op count.
1135 mDeviceIdleController.setActiveIdleOpsForTest(0);
1136 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1137 verifyLightStateConditions(LIGHT_STATE_IDLE);
1138
1139 enterLightState(LIGHT_STATE_OVERRIDE);
1140 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1141 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1142 }
1143
1144 @Test
1145 public void testExitMaintenanceEarlyIfNeededLocked_light_activeJobs() {
1146 mDeviceIdleController.setJobsActive(true);
1147 mDeviceIdleController.setAlarmsActive(false);
1148 mDeviceIdleController.setActiveIdleOpsForTest(0);
1149
1150 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1151
1152 enterLightState(LIGHT_STATE_ACTIVE);
1153 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1154 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1155
1156 enterLightState(LIGHT_STATE_INACTIVE);
1157 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1158 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1159
1160 enterLightState(LIGHT_STATE_PRE_IDLE);
1161 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1162 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1163
1164 enterLightState(LIGHT_STATE_IDLE);
1165 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1166 verifyLightStateConditions(LIGHT_STATE_IDLE);
1167
1168 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1169 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1170 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1171
1172 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1173 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1174 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1175
1176 enterLightState(LIGHT_STATE_OVERRIDE);
1177 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1178 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1179 }
1180
1181 @Test
1182 public void testExitMaintenanceEarlyIfNeededLocked_light_activeAlarms() {
1183 mDeviceIdleController.setJobsActive(false);
1184 mDeviceIdleController.setAlarmsActive(true);
1185 mDeviceIdleController.setActiveIdleOpsForTest(0);
1186
1187 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1188
1189 enterLightState(LIGHT_STATE_ACTIVE);
1190 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1191 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1192
1193 enterLightState(LIGHT_STATE_INACTIVE);
1194 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1195 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1196
1197 enterLightState(LIGHT_STATE_PRE_IDLE);
1198 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1199 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1200
1201 enterLightState(LIGHT_STATE_IDLE);
1202 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1203 verifyLightStateConditions(LIGHT_STATE_IDLE);
1204
1205 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1206 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1207 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1208
1209 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1210 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1211 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1212
1213 enterLightState(LIGHT_STATE_OVERRIDE);
1214 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1215 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1216 }
1217
1218 @Test
1219 public void testExitMaintenanceEarlyIfNeededLocked_light_activeOps() {
1220 mDeviceIdleController.setJobsActive(false);
1221 mDeviceIdleController.setAlarmsActive(false);
1222 mDeviceIdleController.setActiveIdleOpsForTest(1);
1223
1224 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1225
1226 enterLightState(LIGHT_STATE_ACTIVE);
1227 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1228 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1229
1230 enterLightState(LIGHT_STATE_INACTIVE);
1231 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1232 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1233
1234 enterLightState(LIGHT_STATE_PRE_IDLE);
1235 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1236 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1237
1238 enterLightState(LIGHT_STATE_IDLE);
1239 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1240 verifyLightStateConditions(LIGHT_STATE_IDLE);
1241
1242 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1243 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1244 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1245
1246 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1247 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1248 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1249
1250 enterLightState(LIGHT_STATE_OVERRIDE);
1251 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1252 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1253 }
1254
1255 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001256 public void testHandleMotionDetectedLocked_deep_quickDoze_off() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001257 enterDeepState(STATE_ACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001258 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001259 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1260 verifyStateConditions(STATE_ACTIVE);
1261
1262 // Anything that wasn't ACTIVE before motion detection should end up in the INACTIVE state.
1263
1264 enterDeepState(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001265 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001266 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1267 verifyStateConditions(STATE_INACTIVE);
1268
1269 enterDeepState(STATE_IDLE_PENDING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001270 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001271 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1272 verifyStateConditions(STATE_INACTIVE);
1273
1274 enterDeepState(STATE_SENSING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001275 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001276 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1277 verifyStateConditions(STATE_INACTIVE);
1278
1279 enterDeepState(STATE_LOCATING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001280 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001281 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1282 verifyStateConditions(STATE_INACTIVE);
1283
1284 enterDeepState(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001285 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001286 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1287 verifyStateConditions(STATE_INACTIVE);
1288
1289 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001290 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001291 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1292 verifyStateConditions(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001293
1294 enterDeepState(STATE_QUICK_DOZE_DELAY);
1295 setQuickDozeEnabled(false);
1296 // Disabling quick doze doesn't immediately change the state as coming out is harder than
1297 // going in.
1298 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1299 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1300 verifyStateConditions(STATE_INACTIVE);
1301 }
1302
1303 @Test
1304 public void testHandleMotionDetectedLocked_deep_quickDoze_on() {
1305 enterDeepState(STATE_ACTIVE);
1306 setQuickDozeEnabled(true);
1307 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1308 verifyStateConditions(STATE_ACTIVE);
1309
1310 // Anything that wasn't ACTIVE before motion detection should end up in the
1311 // QUICK_DOZE_DELAY state since quick doze is enabled.
1312
1313 enterDeepState(STATE_INACTIVE);
1314 setQuickDozeEnabled(true);
1315 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1316 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1317
1318 enterDeepState(STATE_IDLE_PENDING);
1319 setQuickDozeEnabled(true);
1320 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1321 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1322
1323 enterDeepState(STATE_SENSING);
1324 setQuickDozeEnabled(true);
1325 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1326 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1327
1328 enterDeepState(STATE_LOCATING);
1329 setQuickDozeEnabled(true);
1330 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1331 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1332
1333 enterDeepState(STATE_IDLE);
1334 setQuickDozeEnabled(true);
1335 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1336 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1337
1338 enterDeepState(STATE_IDLE_MAINTENANCE);
1339 setQuickDozeEnabled(true);
1340 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1341 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1342
1343 enterDeepState(STATE_QUICK_DOZE_DELAY);
1344 setQuickDozeEnabled(true);
1345 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1346 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001347 }
1348
1349 @Test
1350 public void testHandleMotionDetectedLocked_light() {
1351 enterLightState(LIGHT_STATE_ACTIVE);
1352 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1353 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1354
1355 // Motion shouldn't affect light idle, so LIGHT states should stay as they were except for
1356 // OVERRIDE. OVERRIDE means deep was active, so if motion was detected,
1357 // LIGHT_STATE_OVERRIDE should end up as LIGHT_STATE_INACTIVE.
1358
1359 enterLightState(LIGHT_STATE_INACTIVE);
1360 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1361 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1362
1363 enterLightState(LIGHT_STATE_PRE_IDLE);
1364 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1365 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1366
1367 enterLightState(LIGHT_STATE_IDLE);
1368 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1369 verifyLightStateConditions(LIGHT_STATE_IDLE);
1370
1371 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1372 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1373 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1374
1375 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1376 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1377 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1378
1379 enterLightState(LIGHT_STATE_OVERRIDE);
1380 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1381 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1382 }
1383
1384 @Test
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001385 public void testBecomeActiveLocked_deep() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001386 // becomeActiveLocked should put everything into ACTIVE.
1387
1388 enterDeepState(STATE_ACTIVE);
1389 mDeviceIdleController.becomeActiveLocked("test", 1000);
1390 verifyStateConditions(STATE_ACTIVE);
1391
1392 enterDeepState(STATE_INACTIVE);
1393 mDeviceIdleController.becomeActiveLocked("test", 1000);
1394 verifyStateConditions(STATE_ACTIVE);
1395
1396 enterDeepState(STATE_IDLE_PENDING);
1397 mDeviceIdleController.becomeActiveLocked("test", 1000);
1398 verifyStateConditions(STATE_ACTIVE);
1399
1400 enterDeepState(STATE_SENSING);
1401 mDeviceIdleController.becomeActiveLocked("test", 1000);
1402 verifyStateConditions(STATE_ACTIVE);
1403
1404 enterDeepState(STATE_LOCATING);
1405 mDeviceIdleController.becomeActiveLocked("test", 1000);
1406 verifyStateConditions(STATE_ACTIVE);
1407
1408 enterDeepState(STATE_IDLE);
1409 mDeviceIdleController.becomeActiveLocked("test", 1000);
1410 verifyStateConditions(STATE_ACTIVE);
1411
1412 enterDeepState(STATE_IDLE_MAINTENANCE);
1413 mDeviceIdleController.becomeActiveLocked("test", 1000);
1414 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001415
1416 enterDeepState(STATE_QUICK_DOZE_DELAY);
1417 mDeviceIdleController.becomeActiveLocked("test", 1000);
1418 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001419 }
1420
1421 @Test
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001422 public void testBecomeActiveLocked_light() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001423 // becomeActiveLocked should put everything into ACTIVE.
1424
1425 enterLightState(LIGHT_STATE_ACTIVE);
1426 mDeviceIdleController.becomeActiveLocked("test", 1000);
1427 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1428
1429 enterLightState(LIGHT_STATE_INACTIVE);
1430 mDeviceIdleController.becomeActiveLocked("test", 1000);
1431 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1432
1433 enterLightState(LIGHT_STATE_PRE_IDLE);
1434 mDeviceIdleController.becomeActiveLocked("test", 1000);
1435 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1436
1437 enterLightState(LIGHT_STATE_IDLE);
1438 mDeviceIdleController.becomeActiveLocked("test", 1000);
1439 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1440
1441 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1442 mDeviceIdleController.becomeActiveLocked("test", 1000);
1443 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1444
1445 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1446 mDeviceIdleController.becomeActiveLocked("test", 1000);
1447 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1448
1449 enterLightState(LIGHT_STATE_OVERRIDE);
1450 mDeviceIdleController.becomeActiveLocked("test", 1000);
1451 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1452 }
1453
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001454 /** Test based on b/119058625. */
1455 @Test
1456 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_ScreenThenMotion() {
1457 mConstants.WAIT_FOR_UNLOCK = true;
1458 enterDeepState(STATE_IDLE);
1459 reset(mAlarmManager);
1460 spyOn(mDeviceIdleController);
1461
1462 mDeviceIdleController.keyguardShowingLocked(true);
1463 setScreenOn(true);
1464 // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself
1465 // shouldn't bring the device out of deep IDLE.
1466 verifyStateConditions(STATE_IDLE);
1467 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1468 // Motion should bring the device out of Doze. Since the screen is still locked (albeit
1469 // on), the states should go back into INACTIVE.
1470 verifyStateConditions(STATE_INACTIVE);
1471 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1472 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1473 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1474 }
1475
1476 /** Test based on b/119058625. */
1477 @Test
1478 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_ScreenThenMotion() {
1479 mConstants.WAIT_FOR_UNLOCK = true;
1480 enterDeepState(STATE_IDLE);
1481 reset(mAlarmManager);
1482 spyOn(mDeviceIdleController);
1483
1484 mDeviceIdleController.keyguardShowingLocked(false);
1485 setScreenOn(true);
1486 // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself
1487 // should bring the device out of deep IDLE.
1488 verifyStateConditions(STATE_ACTIVE);
1489 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1490 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1491 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1492 }
1493
1494 /** Test based on b/119058625. */
1495 @Test
1496 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_MotionThenScreen() {
1497 mConstants.WAIT_FOR_UNLOCK = true;
1498 enterDeepState(STATE_IDLE);
1499 reset(mAlarmManager);
1500 spyOn(mDeviceIdleController);
1501
1502 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1503 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1504
1505 mDeviceIdleController.keyguardShowingLocked(true);
1506 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1507 // The screen is still off, so motion should result in the INACTIVE state.
1508 verifyStateConditions(STATE_INACTIVE);
1509 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1510 alarmManagerInOrder.verify(mAlarmManager)
1511 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1512 controllerInOrder.verify(mDeviceIdleController)
1513 .scheduleReportActiveLocked(anyString(), anyInt());
1514
1515 setScreenOn(true);
1516 // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself
1517 // shouldn't bring the device all the way to ACTIVE.
1518 verifyStateConditions(STATE_INACTIVE);
1519 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1520 alarmManagerInOrder.verify(mAlarmManager, never()).cancel(
1521 eq(mDeviceIdleController.mDeepAlarmListener));
1522
1523 // User finally unlocks the device. Device should be fully active.
1524 mDeviceIdleController.keyguardShowingLocked(false);
1525 verifyStateConditions(STATE_ACTIVE);
1526 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1527 alarmManagerInOrder.verify(mAlarmManager)
1528 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1529 controllerInOrder.verify(mDeviceIdleController)
1530 .scheduleReportActiveLocked(anyString(), anyInt());
1531 }
1532
1533 /** Test based on b/119058625. */
1534 @Test
1535 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_MotionThenScreen() {
1536 mConstants.WAIT_FOR_UNLOCK = true;
1537 enterDeepState(STATE_IDLE);
1538 reset(mAlarmManager);
1539 spyOn(mDeviceIdleController);
1540
1541 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1542 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1543
1544 mDeviceIdleController.keyguardShowingLocked(false);
1545 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1546 // The screen is still off, so motion should result in the INACTIVE state.
1547 verifyStateConditions(STATE_INACTIVE);
1548 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1549 alarmManagerInOrder.verify(mAlarmManager)
1550 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1551 controllerInOrder.verify(mDeviceIdleController)
1552 .scheduleReportActiveLocked(anyString(), anyInt());
1553
1554 setScreenOn(true);
1555 // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself
1556 // should bring the device out of deep IDLE.
1557 verifyStateConditions(STATE_ACTIVE);
1558 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1559 alarmManagerInOrder.verify(mAlarmManager)
1560 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1561 controllerInOrder.verify(mDeviceIdleController)
1562 .scheduleReportActiveLocked(anyString(), anyInt());
1563 }
1564
1565 @Test
1566 public void testExitNotifiesDependencies_WaitForUnlockOff_Screen() {
1567 mConstants.WAIT_FOR_UNLOCK = false;
1568 enterDeepState(STATE_IDLE);
1569 reset(mAlarmManager);
1570 spyOn(mDeviceIdleController);
1571
1572 setScreenOn(true);
1573 // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself
1574 // should bring the device out of deep IDLE.
1575 verifyStateConditions(STATE_ACTIVE);
1576 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1577 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1578 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1579 }
1580
1581 @Test
1582 public void testExitNotifiesDependencies_WaitForUnlockOff_MotionThenScreen() {
1583 mConstants.WAIT_FOR_UNLOCK = false;
1584 enterDeepState(STATE_IDLE);
1585 reset(mAlarmManager);
1586 spyOn(mDeviceIdleController);
1587
1588 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1589 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1590
1591 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1592 // The screen is still off, so motion should result in the INACTIVE state.
1593 verifyStateConditions(STATE_INACTIVE);
1594 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1595 alarmManagerInOrder.verify(mAlarmManager)
1596 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1597 controllerInOrder.verify(mDeviceIdleController)
1598 .scheduleReportActiveLocked(anyString(), anyInt());
1599
1600 setScreenOn(true);
1601 // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself
1602 // should bring the device out of deep IDLE.
1603 verifyStateConditions(STATE_ACTIVE);
1604 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1605 alarmManagerInOrder.verify(mAlarmManager)
1606 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1607 controllerInOrder.verify(mDeviceIdleController)
1608 .scheduleReportActiveLocked(anyString(), anyInt());
1609 }
1610
Denny cy Leec5a7c292019-01-01 17:37:55 +08001611 @Test
1612 public void testStepToIdleMode() {
1613 float delta = mDeviceIdleController.MIN_PRE_IDLE_FACTOR_CHANGE;
1614 for (int mode = PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL;
1615 mode <= PowerManager.PRE_IDLE_TIMEOUT_MODE_LONG;
1616 mode++) {
1617 int ret = mDeviceIdleController.setPreIdleTimeoutMode(mode);
1618 if (mode == PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL) {
1619 assertEquals("setPreIdleTimeoutMode: " + mode + " failed.",
1620 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
1621 } else {
1622 assertEquals("setPreIdleTimeoutMode: " + mode + " failed.",
1623 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
1624 }
1625 //TODO(b/123045185): Mocked Handler of DeviceIdleController to make message loop
1626 //workable in this test class
1627 mDeviceIdleController.updatePreIdleFactor();
1628 float expectedfactor = mDeviceIdleController.getPreIdleTimeoutByMode(mode);
1629 float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor();
1630 assertEquals("Pre idle time factor of mode [" + mode + "].",
1631 expectedfactor, curfactor, delta);
1632 mDeviceIdleController.resetPreIdleTimeoutMode();
1633 mDeviceIdleController.updatePreIdleFactor();
1634
1635 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_INACTIVE);
1636 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_PENDING);
1637
1638 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_SENSING);
1639 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_LOCATING);
1640 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_QUICK_DOZE_DELAY);
1641 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_MAINTENANCE);
1642 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE);
1643 checkMaybeDoAnImmediateMaintenance(expectedfactor);
1644 }
1645 float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor();
1646 assertEquals("Pre idle time factor of mode default.",
1647 1.0f, curfactor, delta);
1648 }
1649
Kweku Adamsb8bb9fb2019-08-28 07:45:00 -07001650 @Test
1651 public void testStationaryDetection_QuickDozeOff() {
1652 setQuickDozeEnabled(false);
1653 enterDeepState(STATE_IDLE);
1654 // Regular progression through states, so time should have increased appropriately.
1655 mInjector.nowElapsed += mConstants.IDLE_AFTER_INACTIVE_TIMEOUT + mConstants.SENSING_TIMEOUT
1656 + mConstants.LOCATING_TIMEOUT;
1657
1658 StationaryListenerForTest stationaryListener = new StationaryListenerForTest();
1659
1660 mDeviceIdleController.registerStationaryListener(stationaryListener);
1661
1662 // Go to IDLE_MAINTENANCE
1663 mDeviceIdleController.stepIdleStateLocked("testing");
1664
1665 // Back to IDLE
1666 mDeviceIdleController.stepIdleStateLocked("testing");
1667 assertTrue(stationaryListener.isStationary);
1668
1669 // Test motion
1670 stationaryListener.motionExpected = true;
1671 mDeviceIdleController.mMotionListener.onTrigger(null);
1672 assertFalse(stationaryListener.isStationary);
1673 }
1674
1675 @Test
1676 public void testStationaryDetection_QuickDozeOn() {
1677 setAlarmSoon(false);
1678 enterDeepState(STATE_QUICK_DOZE_DELAY);
1679 mDeviceIdleController.stepIdleStateLocked("testing");
1680 verifyStateConditions(STATE_IDLE);
1681 // Quick doze progression through states, so time should have increased appropriately.
1682 mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT;
1683 final ArgumentCaptor<AlarmManager.OnAlarmListener> alarmListener = ArgumentCaptor
1684 .forClass(AlarmManager.OnAlarmListener.class);
1685 doNothing().when(mAlarmManager).set(anyInt(), anyLong(), eq("DeviceIdleController.motion"),
1686 alarmListener.capture(), any());
1687
1688 StationaryListenerForTest stationaryListener = new StationaryListenerForTest();
1689
1690 stationaryListener.motionExpected = true;
1691 mDeviceIdleController.registerStationaryListener(stationaryListener);
1692 assertFalse(stationaryListener.isStationary);
1693
1694 // Go to IDLE_MAINTENANCE
1695 mDeviceIdleController.stepIdleStateLocked("testing");
1696
1697 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2;
1698
1699 // Back to IDLE
1700 mDeviceIdleController.stepIdleStateLocked("testing");
1701
1702 // Now enough time has passed.
1703 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2;
1704 stationaryListener.motionExpected = false;
1705 alarmListener.getValue().onAlarm();
1706 assertTrue(stationaryListener.isStationary);
1707
1708 stationaryListener.motionExpected = true;
1709 mDeviceIdleController.mMotionListener.onSensorChanged(null);
1710 assertFalse(stationaryListener.isStationary);
1711
1712 // Since we're in quick doze, the device shouldn't stop idling.
1713 verifyStateConditions(STATE_IDLE);
1714
1715 // Go to IDLE_MAINTENANCE
1716 mDeviceIdleController.stepIdleStateLocked("testing");
1717
1718 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2;
1719
1720 // Back to IDLE
1721 mDeviceIdleController.stepIdleStateLocked("testing");
1722
1723 // Now enough time has passed.
1724 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2;
1725 stationaryListener.motionExpected = false;
1726 alarmListener.getValue().onAlarm();
1727 assertTrue(stationaryListener.isStationary);
1728 }
1729
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001730 private void enterDeepState(int state) {
1731 switch (state) {
1732 case STATE_ACTIVE:
1733 setScreenOn(true);
1734 mDeviceIdleController.becomeActiveLocked("testing", 0);
1735 break;
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001736 case STATE_QUICK_DOZE_DELAY:
1737 // Start off from ACTIVE in case we're already past the desired state.
1738 enterDeepState(STATE_ACTIVE);
1739 setQuickDozeEnabled(true);
1740 setScreenOn(false);
1741 setChargingOn(false);
1742 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
1743 break;
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001744 case STATE_LOCATING:
Kweku Adams799858b2018-10-08 17:19:08 -07001745 mInjector.locationManager = mLocationManager;
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001746 doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(
1747 anyString());
1748 // Fallthrough to step loop.
1749 case STATE_IDLE_PENDING:
1750 case STATE_SENSING:
1751 case STATE_IDLE:
1752 case STATE_IDLE_MAINTENANCE:
1753 // Make sure the controller doesn't think there's a wake-from-idle alarm coming
1754 // soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001755 setAlarmSoon(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001756 case STATE_INACTIVE:
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001757 // Start off from ACTIVE in case we're already past the desired state.
1758 enterDeepState(STATE_ACTIVE);
1759 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001760 setScreenOn(false);
1761 setChargingOn(false);
1762 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001763 int count = 0;
1764 while (mDeviceIdleController.getState() != state) {
1765 // Stepping through each state ensures that the proper features are turned
1766 // on/off.
1767 mDeviceIdleController.stepIdleStateLocked("testing");
1768 count++;
1769 if (count > 10) {
Kweku Adams799858b2018-10-08 17:19:08 -07001770 fail("Infinite loop. Check test configuration. Currently at " +
1771 stateToString(mDeviceIdleController.getState()));
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001772 }
1773 }
1774 break;
1775 default:
1776 fail("Unknown deep state " + stateToString(state));
1777 }
1778 }
1779
1780 private void enterLightState(int lightState) {
1781 switch (lightState) {
1782 case LIGHT_STATE_ACTIVE:
1783 setScreenOn(true);
1784 mDeviceIdleController.becomeActiveLocked("testing", 0);
1785 break;
1786 case LIGHT_STATE_INACTIVE:
1787 case LIGHT_STATE_IDLE:
1788 case LIGHT_STATE_IDLE_MAINTENANCE:
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001789 // Start off from ACTIVE in case we're already past the desired state.
1790 enterLightState(LIGHT_STATE_ACTIVE);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001791 setScreenOn(false);
1792 setChargingOn(false);
1793 int count = 0;
1794 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
1795 while (mDeviceIdleController.getLightState() != lightState) {
1796 // Stepping through each state ensures that the proper features are turned
1797 // on/off.
1798 mDeviceIdleController.stepLightIdleStateLocked("testing");
1799
1800 count++;
1801 if (count > 10) {
Kweku Adams799858b2018-10-08 17:19:08 -07001802 fail("Infinite loop. Check test configuration. Currently at " +
1803 lightStateToString(mDeviceIdleController.getLightState()));
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001804 }
1805 }
1806 break;
1807 case LIGHT_STATE_PRE_IDLE:
1808 case LIGHT_STATE_WAITING_FOR_NETWORK:
1809 case LIGHT_STATE_OVERRIDE:
1810 setScreenOn(false);
1811 setChargingOn(false);
1812 mDeviceIdleController.setLightStateForTest(lightState);
1813 break;
1814 default:
1815 fail("Unknown light state " + lightStateToString(lightState));
1816 }
1817 }
1818
Kweku Adams00e3a372018-09-28 16:57:09 -07001819 private void setChargingOn(boolean on) {
1820 mDeviceIdleController.updateChargingLocked(on);
1821 }
1822
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001823 private void setScreenLocked(boolean locked) {
1824 mDeviceIdleController.keyguardShowingLocked(locked);
1825 }
1826
Kweku Adams00e3a372018-09-28 16:57:09 -07001827 private void setScreenOn(boolean on) {
1828 doReturn(on).when(mPowerManager).isInteractive();
1829 mDeviceIdleController.updateInteractivityLocked();
1830 }
1831
Kweku Adams799858b2018-10-08 17:19:08 -07001832 private void setNetworkConnected(boolean connected) {
1833 mInjector.connectivityService = mConnectivityService;
1834 final NetworkInfo ani = mock(NetworkInfo.class);
1835 doReturn(connected).when(ani).isConnected();
1836 doReturn(ani).when(mConnectivityService).getActiveNetworkInfo();
1837 mDeviceIdleController.updateConnectivityState(null);
1838 }
1839
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001840 private void setQuickDozeEnabled(boolean on) {
1841 mDeviceIdleController.updateQuickDozeFlagLocked(on);
1842 }
1843
1844 private void setAlarmSoon(boolean isSoon) {
1845 if (isSoon) {
Kweku Adams9da2bb92018-12-20 06:34:39 -08001846 doReturn(SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM / 2)
1847 .when(mAlarmManager).getNextWakeFromIdleTime();
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001848 } else {
1849 doReturn(Long.MAX_VALUE).when(mAlarmManager).getNextWakeFromIdleTime();
1850 }
1851 }
1852
Kweku Adams00e3a372018-09-28 16:57:09 -07001853 private void verifyStateConditions(int expectedState) {
1854 int curState = mDeviceIdleController.getState();
1855 assertEquals(
1856 "Expected " + stateToString(expectedState) + " but was " + stateToString(curState),
1857 expectedState, curState);
1858
1859 switch (expectedState) {
1860 case STATE_ACTIVE:
1861 assertFalse(mDeviceIdleController.mMotionListener.isActive());
1862 assertFalse(mAnyMotionDetector.isMonitoring);
1863 break;
1864 case STATE_INACTIVE:
1865 assertFalse(mDeviceIdleController.mMotionListener.isActive());
1866 assertFalse(mAnyMotionDetector.isMonitoring);
1867 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001868 assertFalse(mDeviceIdleController.isScreenOn()
1869 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001870 break;
1871 case STATE_IDLE_PENDING:
Robin Lee876b88542018-11-13 17:22:24 +01001872 assertEquals(
1873 mDeviceIdleController.hasMotionSensor(),
1874 mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07001875 assertFalse(mAnyMotionDetector.isMonitoring);
1876 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001877 assertFalse(mDeviceIdleController.isScreenOn()
1878 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001879 break;
1880 case STATE_SENSING:
Robin Lee876b88542018-11-13 17:22:24 +01001881 assertEquals(
1882 mDeviceIdleController.hasMotionSensor(),
1883 mDeviceIdleController.mMotionListener.isActive());
1884 assertEquals(
1885 mDeviceIdleController.hasMotionSensor(),
1886 mAnyMotionDetector.isMonitoring);
Kweku Adams00e3a372018-09-28 16:57:09 -07001887 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001888 assertFalse(mDeviceIdleController.isScreenOn()
1889 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001890 break;
1891 case STATE_LOCATING:
Robin Lee876b88542018-11-13 17:22:24 +01001892 assertEquals(
1893 mDeviceIdleController.hasMotionSensor(),
1894 mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07001895 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001896 assertFalse(mDeviceIdleController.isScreenOn()
1897 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001898 break;
1899 case STATE_IDLE:
Robin Lee876b88542018-11-13 17:22:24 +01001900 if (mDeviceIdleController.hasMotionSensor()) {
1901 assertTrue(mDeviceIdleController.mMotionListener.isActive()
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001902 // If quick doze is enabled, the motion listener should NOT be active.
1903 || mDeviceIdleController.isQuickDozeEnabled());
Robin Lee876b88542018-11-13 17:22:24 +01001904 }
Kweku Adams00e3a372018-09-28 16:57:09 -07001905 assertFalse(mAnyMotionDetector.isMonitoring);
1906 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001907 assertFalse(mDeviceIdleController.isScreenOn()
1908 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001909 // Light state should be OVERRIDE at this point.
1910 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1911 break;
1912 case STATE_IDLE_MAINTENANCE:
Robin Lee876b88542018-11-13 17:22:24 +01001913 if (mDeviceIdleController.hasMotionSensor()) {
1914 assertTrue(mDeviceIdleController.mMotionListener.isActive()
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001915 // If quick doze is enabled, the motion listener should NOT be active.
1916 || mDeviceIdleController.isQuickDozeEnabled());
Robin Lee876b88542018-11-13 17:22:24 +01001917 }
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001918 assertFalse(mAnyMotionDetector.isMonitoring);
1919 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001920 assertFalse(mDeviceIdleController.isScreenOn()
1921 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001922 break;
1923 case STATE_QUICK_DOZE_DELAY:
1924 // If quick doze is enabled, the motion listener should NOT be active.
1925 assertFalse(mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07001926 assertFalse(mAnyMotionDetector.isMonitoring);
1927 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001928 assertFalse(mDeviceIdleController.isScreenOn()
1929 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001930 break;
1931 default:
1932 fail("Conditions for " + stateToString(expectedState) + " unknown.");
1933 }
1934 }
1935
1936 private void verifyLightStateConditions(int expectedLightState) {
1937 int curLightState = mDeviceIdleController.getLightState();
1938 assertEquals(
1939 "Expected " + lightStateToString(expectedLightState)
1940 + " but was " + lightStateToString(curLightState),
1941 expectedLightState, curLightState);
1942
1943 switch (expectedLightState) {
1944 case LIGHT_STATE_ACTIVE:
1945 assertTrue(
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001946 mDeviceIdleController.isCharging() || mDeviceIdleController.isScreenOn()
1947 // Or there's an alarm coming up soon.
1948 || SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM
1949 > mAlarmManager.getNextWakeFromIdleTime());
Kweku Adams00e3a372018-09-28 16:57:09 -07001950 break;
1951 case LIGHT_STATE_INACTIVE:
1952 case LIGHT_STATE_PRE_IDLE:
1953 case LIGHT_STATE_IDLE:
1954 case LIGHT_STATE_WAITING_FOR_NETWORK:
1955 case LIGHT_STATE_IDLE_MAINTENANCE:
1956 case LIGHT_STATE_OVERRIDE:
1957 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001958 assertFalse(mDeviceIdleController.isScreenOn()
1959 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07001960 break;
1961 default:
1962 fail("Conditions for " + lightStateToString(expectedLightState) + " unknown.");
1963 }
1964 }
Denny cy Leec5a7c292019-01-01 17:37:55 +08001965
1966 private void checkNextAlarmTimeWithNewPreIdleFactor(float factor, int state) {
1967 final long errorTolerance = 1000;
1968 enterDeepState(state);
1969 long now = SystemClock.elapsedRealtime();
1970 long alarm = mDeviceIdleController.getNextAlarmTime();
1971 if (state == STATE_INACTIVE || state == STATE_IDLE_PENDING) {
1972 int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor);
1973 if (Float.compare(factor, 1.0f) == 0) {
1974 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
1975 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
1976 } else {
1977 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
1978 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
1979 }
1980 if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) {
1981 mDeviceIdleController.updatePreIdleFactor();
1982 long newAlarm = mDeviceIdleController.getNextAlarmTime();
1983 long newDelay = (long) ((alarm - now) * factor);
1984 assertTrue("setPreIdleTimeoutFactor: " + factor,
1985 Math.abs(newDelay - (newAlarm - now)) < errorTolerance);
1986 mDeviceIdleController.resetPreIdleTimeoutMode();
1987 mDeviceIdleController.updatePreIdleFactor();
1988 mDeviceIdleController.maybeDoImmediateMaintenance();
1989 newAlarm = mDeviceIdleController.getNextAlarmTime();
1990 assertTrue("resetPreIdleTimeoutMode from: " + factor,
1991 Math.abs(newAlarm - alarm) < errorTolerance);
1992 mDeviceIdleController.setPreIdleTimeoutFactor(factor);
1993 now = SystemClock.elapsedRealtime();
1994 enterDeepState(state);
1995 newAlarm = mDeviceIdleController.getNextAlarmTime();
1996 assertTrue("setPreIdleTimeoutFactor: " + factor + " before step to idle",
1997 Math.abs(newDelay - (newAlarm - now)) < errorTolerance);
1998 mDeviceIdleController.resetPreIdleTimeoutMode();
1999 mDeviceIdleController.updatePreIdleFactor();
2000 mDeviceIdleController.maybeDoImmediateMaintenance();
2001 }
2002 } else {
2003 mDeviceIdleController.setPreIdleTimeoutFactor(factor);
2004 mDeviceIdleController.updatePreIdleFactor();
2005 long newAlarm = mDeviceIdleController.getNextAlarmTime();
2006 assertTrue("setPreIdleTimeoutFactor: " + factor
2007 + " shounld not change next alarm" ,
2008 (newAlarm == alarm));
2009 mDeviceIdleController.resetPreIdleTimeoutMode();
2010 mDeviceIdleController.updatePreIdleFactor();
2011 mDeviceIdleController.maybeDoImmediateMaintenance();
2012 }
2013 }
2014
2015 private void checkMaybeDoAnImmediateMaintenance(float factor) {
2016 int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor);
2017 final long minuteInMillis = 60 * 1000;
2018 if (Float.compare(factor, 1.0f) == 0) {
2019 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
2020 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
2021 } else {
2022 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
2023 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
2024 }
2025 if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) {
2026 enterDeepState(STATE_IDLE);
2027 long now = SystemClock.elapsedRealtime();
2028 long alarm = mDeviceIdleController.getNextAlarmTime();
2029 mDeviceIdleController.setIdleStartTimeForTest(
2030 now - (long) (mConstants.IDLE_TIMEOUT * 0.6));
2031 mDeviceIdleController.maybeDoImmediateMaintenance();
2032 long newAlarm = mDeviceIdleController.getNextAlarmTime();
2033 assertTrue("maintenance not reschedule IDLE_TIMEOUT * 0.6",
2034 newAlarm == alarm);
2035 mDeviceIdleController.setIdleStartTimeForTest(
2036 now - (long) (mConstants.IDLE_TIMEOUT * 1.2));
2037 mDeviceIdleController.maybeDoImmediateMaintenance();
2038 newAlarm = mDeviceIdleController.getNextAlarmTime();
2039 assertTrue("maintenance not reschedule IDLE_TIMEOUT * 1.2",
2040 (newAlarm - now) < minuteInMillis);
2041 mDeviceIdleController.resetPreIdleTimeoutMode();
2042 mDeviceIdleController.updatePreIdleFactor();
2043 }
2044 }
Kweku Adams00e3a372018-09-28 16:57:09 -07002045}