blob: 9251031eb5cd317c85ce5a624394633d56c2969a [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 Adams810c77d2019-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 Adams810c77d2019-08-28 07:45:00 -070035import static com.android.server.DeviceIdleController.MSG_REPORT_STATIONARY_STATUS;
Denny cy Lee990ca332019-10-22 16:21:20 +080036import static com.android.server.DeviceIdleController.MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR;
37import static com.android.server.DeviceIdleController.MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR;
Kweku Adams00e3a372018-09-28 16:57:09 -070038import static com.android.server.DeviceIdleController.STATE_ACTIVE;
39import static com.android.server.DeviceIdleController.STATE_IDLE;
40import static com.android.server.DeviceIdleController.STATE_IDLE_MAINTENANCE;
41import static com.android.server.DeviceIdleController.STATE_IDLE_PENDING;
42import static com.android.server.DeviceIdleController.STATE_INACTIVE;
43import static com.android.server.DeviceIdleController.STATE_LOCATING;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070044import static com.android.server.DeviceIdleController.STATE_QUICK_DOZE_DELAY;
Kweku Adams00e3a372018-09-28 16:57:09 -070045import static com.android.server.DeviceIdleController.STATE_SENSING;
46import static com.android.server.DeviceIdleController.lightStateToString;
47import static com.android.server.DeviceIdleController.stateToString;
48
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertFalse;
51import static org.junit.Assert.assertTrue;
52import static org.junit.Assert.fail;
53import static org.mockito.ArgumentMatchers.any;
Kweku Adams9da2bb92018-12-20 06:34:39 -080054import static org.mockito.ArgumentMatchers.anyBoolean;
Kweku Adams00e3a372018-09-28 16:57:09 -070055import static org.mockito.ArgumentMatchers.anyInt;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070056import static org.mockito.ArgumentMatchers.anyLong;
Kweku Adams00e3a372018-09-28 16:57:09 -070057import static org.mockito.ArgumentMatchers.anyString;
Kweku Adams810c77d2019-08-28 07:45:00 -070058import static org.mockito.ArgumentMatchers.argThat;
Robin Lee876b88542018-11-13 17:22:24 +010059import static org.mockito.ArgumentMatchers.eq;
Kweku Adams7c96f5182019-06-04 16:32:49 -070060import static org.mockito.ArgumentMatchers.longThat;
Kweku Adamsecf98fb2019-07-22 17:12:41 -070061import static org.mockito.Mockito.atLeastOnce;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080062import static org.mockito.Mockito.never;
63import static org.mockito.Mockito.reset;
Kweku Adamsa890d282019-12-19 16:08:43 -080064import static org.mockito.Mockito.timeout;
Kweku Adamsb7ce1902019-01-30 10:55:34 -080065import static org.mockito.Mockito.verify;
Kweku Adams00e3a372018-09-28 16:57:09 -070066
67import android.app.ActivityManagerInternal;
68import android.app.AlarmManager;
69import android.app.IActivityManager;
Kweku Adamsa457f4e2018-10-03 15:56:06 -070070import android.content.ContentResolver;
Kweku Adams00e3a372018-09-28 16:57:09 -070071import android.content.Context;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070072import android.content.Intent;
Robin Lee876b88542018-11-13 17:22:24 +010073import android.hardware.Sensor;
Kweku Adamsa890d282019-12-19 16:08:43 -080074import android.hardware.SensorEvent;
75import android.hardware.SensorEventListener;
Kweku Adams00e3a372018-09-28 16:57:09 -070076import android.hardware.SensorManager;
Kweku Adamsa890d282019-12-19 16:08:43 -080077import android.hardware.TriggerEvent;
78import android.hardware.TriggerEventListener;
Kweku Adams00e3a372018-09-28 16:57:09 -070079import android.location.LocationManager;
80import android.location.LocationProvider;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070081import android.net.ConnectivityManager;
82import android.net.NetworkInfo;
Kweku Adams00e3a372018-09-28 16:57:09 -070083import android.os.Handler;
84import android.os.Looper;
Kweku Adams810c77d2019-08-28 07:45:00 -070085import android.os.Message;
Kweku Adams00e3a372018-09-28 16:57:09 -070086import android.os.PowerManager;
87import android.os.PowerManagerInternal;
Kweku Adamsb396ccf2018-09-17 16:37:15 -070088import android.os.PowerSaveState;
Kweku Adamsa457f4e2018-10-03 15:56:06 -070089import android.os.SystemClock;
Kweku Adams00e3a372018-09-28 16:57:09 -070090
91import androidx.test.runner.AndroidJUnit4;
92
Robin Lee876b88542018-11-13 17:22:24 +010093import com.android.server.deviceidle.ConstraintController;
Kweku Adams00e3a372018-09-28 16:57:09 -070094import com.android.server.net.NetworkPolicyManagerInternal;
95import com.android.server.wm.ActivityTaskManagerInternal;
96
97import org.junit.After;
98import org.junit.Before;
99import org.junit.Test;
100import org.junit.runner.RunWith;
Kweku Adams810c77d2019-08-28 07:45:00 -0700101import org.mockito.ArgumentCaptor;
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800102import org.mockito.InOrder;
Kweku Adams00e3a372018-09-28 16:57:09 -0700103import org.mockito.Mock;
104import org.mockito.MockitoSession;
Kweku Adams810c77d2019-08-28 07:45:00 -0700105import org.mockito.invocation.InvocationOnMock;
Kweku Adams00e3a372018-09-28 16:57:09 -0700106import org.mockito.quality.Strictness;
Kweku Adams810c77d2019-08-28 07:45:00 -0700107import org.mockito.stubbing.Answer;
Kweku Adams00e3a372018-09-28 16:57:09 -0700108
109/**
110 * Tests for {@link com.android.server.DeviceIdleController}.
111 */
112@RunWith(AndroidJUnit4.class)
113public class DeviceIdleControllerTest {
114 private DeviceIdleController mDeviceIdleController;
Kweku Adams810c77d2019-08-28 07:45:00 -0700115 private DeviceIdleController.MyHandler mHandler;
Kweku Adams00e3a372018-09-28 16:57:09 -0700116 private AnyMotionDetectorForTest mAnyMotionDetector;
117 private AppStateTrackerForTest mAppStateTracker;
Kweku Adams9da2bb92018-12-20 06:34:39 -0800118 private DeviceIdleController.Constants mConstants;
Kweku Adams799858b2018-10-08 17:19:08 -0700119 private InjectorForTest mInjector;
Kweku Adams00e3a372018-09-28 16:57:09 -0700120
121 private MockitoSession mMockingSession;
122 @Mock
Kweku Adams00e3a372018-09-28 16:57:09 -0700123 private AlarmManager mAlarmManager;
124 @Mock
Makoto Onukifa8b0ba2019-10-07 16:49:04 -0700125 private ConnectivityManager mConnectivityManager;
Kweku Adams799858b2018-10-08 17:19:08 -0700126 @Mock
Kweku Adams9da2bb92018-12-20 06:34:39 -0800127 private ContentResolver mContentResolver;
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700128 @Mock
129 private IActivityManager mIActivityManager;
130 @Mock
Kweku Adams00e3a372018-09-28 16:57:09 -0700131 private LocationManager mLocationManager;
132 @Mock
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700133 private PowerManager mPowerManager;
134 @Mock
135 private PowerManager.WakeLock mWakeLock;
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700136 @Mock
137 private PowerManagerInternal mPowerManagerInternal;
Robin Lee876b88542018-11-13 17:22:24 +0100138 @Mock
Kweku Adamsa890d282019-12-19 16:08:43 -0800139 private Sensor mMotionSensor;
140 @Mock
Robin Lee876b88542018-11-13 17:22:24 +0100141 private SensorManager mSensorManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700142
143 class InjectorForTest extends DeviceIdleController.Injector {
Makoto Onukifa8b0ba2019-10-07 16:49:04 -0700144 ConnectivityManager connectivityManager;
Kweku Adams799858b2018-10-08 17:19:08 -0700145 LocationManager locationManager;
Robin Lee876b88542018-11-13 17:22:24 +0100146 ConstraintController constraintController;
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700147 // Freeze time for testing.
148 long nowElapsed;
Kweku Adams00e3a372018-09-28 16:57:09 -0700149
150 InjectorForTest(Context ctx) {
151 super(ctx);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700152 nowElapsed = SystemClock.elapsedRealtime();
Kweku Adams00e3a372018-09-28 16:57:09 -0700153 }
154
155 @Override
156 AlarmManager getAlarmManager() {
157 return mAlarmManager;
158 }
159
160 @Override
161 AnyMotionDetector getAnyMotionDetector(Handler handler, SensorManager sm,
162 AnyMotionDetector.DeviceIdleCallback callback, float angleThreshold) {
163 return mAnyMotionDetector;
164 }
165
166 @Override
167 AppStateTracker getAppStateTracker(Context ctx, Looper loop) {
168 return mAppStateTracker;
169 }
170
171 @Override
Makoto Onukifa8b0ba2019-10-07 16:49:04 -0700172 ConnectivityManager getConnectivityManager() {
173 return connectivityManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700174 }
175
176 @Override
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700177 long getElapsedRealtime() {
178 return nowElapsed;
179 }
180
181 @Override
Kweku Adams00e3a372018-09-28 16:57:09 -0700182 LocationManager getLocationManager() {
Kweku Adams799858b2018-10-08 17:19:08 -0700183 return locationManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700184 }
185
186 @Override
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700187 DeviceIdleController.MyHandler getHandler(DeviceIdleController controller) {
Kweku Adams810c77d2019-08-28 07:45:00 -0700188 if (mHandler == null) {
189 mHandler = controller.new MyHandler(getContext().getMainLooper());
190 spyOn(mHandler);
191 doNothing().when(mHandler).handleMessage(argThat((message) ->
Denny cy Lee990ca332019-10-22 16:21:20 +0800192 message.what != MSG_REPORT_STATIONARY_STATUS
193 && message.what != MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR
194 && message.what != MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR));
Kweku Adams810c77d2019-08-28 07:45:00 -0700195 doAnswer(new Answer<Boolean>() {
196 @Override
197 public Boolean answer(InvocationOnMock invocation) throws Throwable {
198 Message msg = invocation.getArgument(0);
199 mHandler.handleMessage(msg);
200 return true;
201 }
202 }).when(mHandler).sendMessageDelayed(
Denny cy Lee990ca332019-10-22 16:21:20 +0800203 argThat((message) -> message.what == MSG_REPORT_STATIONARY_STATUS
204 || message.what == MSG_UPDATE_PRE_IDLE_TIMEOUT_FACTOR
205 || message.what == MSG_RESET_PRE_IDLE_TIMEOUT_FACTOR),
Kweku Adams810c77d2019-08-28 07:45:00 -0700206 anyLong());
207 }
208
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800209 return mHandler;
Kweku Adams00e3a372018-09-28 16:57:09 -0700210 }
211
212 @Override
Kweku Adamsa890d282019-12-19 16:08:43 -0800213 Sensor getMotionSensor() {
214 return mMotionSensor;
215 }
216
217 @Override
Kweku Adams00e3a372018-09-28 16:57:09 -0700218 PowerManager getPowerManager() {
219 return mPowerManager;
220 }
Robin Lee876b88542018-11-13 17:22:24 +0100221
222 @Override
223 SensorManager getSensorManager() {
224 return mSensorManager;
225 }
226
227 @Override
228 ConstraintController getConstraintController(
Makoto Onukieaf7eed2019-08-12 15:27:15 -0700229 Handler handler, DeviceIdleInternal localService) {
Robin Lee876b88542018-11-13 17:22:24 +0100230 return constraintController;
231 }
232
233 @Override
234 boolean useMotionSensor() {
235 return true;
236 }
Kweku Adams00e3a372018-09-28 16:57:09 -0700237 }
238
239 private class AnyMotionDetectorForTest extends AnyMotionDetector {
240 boolean isMonitoring = false;
241
242 AnyMotionDetectorForTest() {
Robin Lee876b88542018-11-13 17:22:24 +0100243 super(mPowerManager, mock(Handler.class), mSensorManager,
Kweku Adams00e3a372018-09-28 16:57:09 -0700244 mock(DeviceIdleCallback.class), 0.5f);
245 }
246
247 @Override
Kweku Adams9da2bb92018-12-20 06:34:39 -0800248 public boolean hasSensor() {
249 return true;
250 }
251
252 @Override
Kweku Adams00e3a372018-09-28 16:57:09 -0700253 public void checkForAnyMotion() {
254 isMonitoring = true;
255 }
256
257 @Override
258 public void stop() {
259 isMonitoring = false;
260 }
261 }
262
263 private class AppStateTrackerForTest extends AppStateTracker {
264 AppStateTrackerForTest(Context ctx, Looper looper) {
265 super(ctx, looper);
266 }
267
268 @Override
269 public void onSystemServicesReady() {
270 // Do nothing.
271 }
272
273 @Override
274 IActivityManager injectIActivityManager() {
275 return mIActivityManager;
276 }
277 }
278
Kweku Adams810c77d2019-08-28 07:45:00 -0700279 private class StationaryListenerForTest implements DeviceIdleInternal.StationaryListener {
280 boolean motionExpected = false;
281 boolean isStationary = false;
282
283 @Override
284 public void onDeviceStationaryChanged(boolean isStationary) {
285 if (isStationary == motionExpected) {
286 fail("Unexpected device stationary status: " + isStationary);
287 }
288 this.isStationary = isStationary;
289 }
290 }
291
Kweku Adams00e3a372018-09-28 16:57:09 -0700292 @Before
293 public void setUp() {
294 mMockingSession = mockitoSession()
295 .initMocks(this)
296 .strictness(Strictness.LENIENT)
Robin Lee876b88542018-11-13 17:22:24 +0100297 .spyStatic(LocalServices.class)
Kweku Adams00e3a372018-09-28 16:57:09 -0700298 .startMocking();
Robin Lee54d44962018-12-17 17:43:57 +0100299 spyOn(getContext());
300 doReturn(null).when(getContext()).registerReceiver(any(), any());
Kweku Adams00e3a372018-09-28 16:57:09 -0700301 doReturn(mock(ActivityManagerInternal.class))
302 .when(() -> LocalServices.getService(ActivityManagerInternal.class));
303 doReturn(mock(ActivityTaskManagerInternal.class))
304 .when(() -> LocalServices.getService(ActivityTaskManagerInternal.class));
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800305 doReturn(mock(AlarmManagerInternal.class))
306 .when(() -> LocalServices.getService(AlarmManagerInternal.class));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700307 doReturn(mPowerManagerInternal)
Kweku Adams00e3a372018-09-28 16:57:09 -0700308 .when(() -> LocalServices.getService(PowerManagerInternal.class));
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800309 when(mPowerManagerInternal.getLowPowerState(anyInt()))
310 .thenReturn(mock(PowerSaveState.class));
Kweku Adams00e3a372018-09-28 16:57:09 -0700311 doReturn(mock(NetworkPolicyManagerInternal.class))
312 .when(() -> LocalServices.getService(NetworkPolicyManagerInternal.class));
313 when(mPowerManager.newWakeLock(anyInt(), anyString())).thenReturn(mWakeLock);
314 doNothing().when(mWakeLock).acquire();
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700315 doNothing().when(mAlarmManager).set(anyInt(), anyLong(), anyString(), any(), any());
Robin Lee876b88542018-11-13 17:22:24 +0100316 doReturn(mock(Sensor.class)).when(mSensorManager)
317 .getDefaultSensor(eq(Sensor.TYPE_SIGNIFICANT_MOTION), eq(true));
318 doReturn(true).when(mSensorManager).registerListener(any(), any(), anyInt());
Kweku Adams00e3a372018-09-28 16:57:09 -0700319 mAppStateTracker = new AppStateTrackerForTest(getContext(), Looper.getMainLooper());
320 mAnyMotionDetector = new AnyMotionDetectorForTest();
Kweku Adams799858b2018-10-08 17:19:08 -0700321 mInjector = new InjectorForTest(getContext());
Kweku Adams9da2bb92018-12-20 06:34:39 -0800322 doNothing().when(mContentResolver).registerContentObserver(any(), anyBoolean(), any());
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800323
Kweku Adams799858b2018-10-08 17:19:08 -0700324 mDeviceIdleController = new DeviceIdleController(getContext(), mInjector);
Kweku Adams00e3a372018-09-28 16:57:09 -0700325 spyOn(mDeviceIdleController);
326 doNothing().when(mDeviceIdleController).publishBinderService(any(), any());
327 mDeviceIdleController.onStart();
328 mDeviceIdleController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY);
329 mDeviceIdleController.setDeepEnabledForTest(true);
330 mDeviceIdleController.setLightEnabledForTest(true);
Kweku Adams9da2bb92018-12-20 06:34:39 -0800331
332 // Get the same Constants object that mDeviceIdleController got.
333 mConstants = mInjector.getConstants(mDeviceIdleController,
334 mInjector.getHandler(mDeviceIdleController), mContentResolver);
Kweku Adams00e3a372018-09-28 16:57:09 -0700335 }
336
337 @After
338 public void tearDown() {
339 if (mMockingSession != null) {
340 mMockingSession.finishMocking();
341 }
Robin Lee876b88542018-11-13 17:22:24 +0100342 // DeviceIdleController adds these to LocalServices in the constructor, so we have to remove
343 // them after each test, otherwise, subsequent tests will fail.
Kweku Adams00e3a372018-09-28 16:57:09 -0700344 LocalServices.removeServiceForTest(AppStateTracker.class);
Makoto Onukieaf7eed2019-08-12 15:27:15 -0700345 LocalServices.removeServiceForTest(DeviceIdleInternal.class);
Kweku Adams00e3a372018-09-28 16:57:09 -0700346 }
347
348 @Test
349 public void testUpdateInteractivityLocked() {
350 doReturn(false).when(mPowerManager).isInteractive();
351 mDeviceIdleController.updateInteractivityLocked();
352 assertFalse(mDeviceIdleController.isScreenOn());
353
354 // Make sure setting false when screen is already off doesn't change anything.
355 doReturn(false).when(mPowerManager).isInteractive();
356 mDeviceIdleController.updateInteractivityLocked();
357 assertFalse(mDeviceIdleController.isScreenOn());
358
359 // Test changing from screen off to screen on.
360 doReturn(true).when(mPowerManager).isInteractive();
361 mDeviceIdleController.updateInteractivityLocked();
362 assertTrue(mDeviceIdleController.isScreenOn());
363
364 // Make sure setting true when screen is already on doesn't change anything.
365 doReturn(true).when(mPowerManager).isInteractive();
366 mDeviceIdleController.updateInteractivityLocked();
367 assertTrue(mDeviceIdleController.isScreenOn());
368
369 // Test changing from screen on to screen off.
370 doReturn(false).when(mPowerManager).isInteractive();
371 mDeviceIdleController.updateInteractivityLocked();
372 assertFalse(mDeviceIdleController.isScreenOn());
373 }
374
375 @Test
376 public void testUpdateChargingLocked() {
377 mDeviceIdleController.updateChargingLocked(false);
378 assertFalse(mDeviceIdleController.isCharging());
379
380 // Make sure setting false when charging is already off doesn't change anything.
381 mDeviceIdleController.updateChargingLocked(false);
382 assertFalse(mDeviceIdleController.isCharging());
383
384 // Test changing from charging off to charging on.
385 mDeviceIdleController.updateChargingLocked(true);
386 assertTrue(mDeviceIdleController.isCharging());
387
388 // Make sure setting true when charging is already on doesn't change anything.
389 mDeviceIdleController.updateChargingLocked(true);
390 assertTrue(mDeviceIdleController.isCharging());
391
392 // Test changing from charging on to charging off.
393 mDeviceIdleController.updateChargingLocked(false);
394 assertFalse(mDeviceIdleController.isCharging());
395 }
396
397 @Test
Kweku Adams799858b2018-10-08 17:19:08 -0700398 public void testUpdateConnectivityState() {
399 // No connectivity service
400 final boolean isConnected = mDeviceIdleController.isNetworkConnected();
Makoto Onukifa8b0ba2019-10-07 16:49:04 -0700401 mInjector.connectivityManager = null;
Kweku Adams799858b2018-10-08 17:19:08 -0700402 mDeviceIdleController.updateConnectivityState(null);
403 assertEquals(isConnected, mDeviceIdleController.isNetworkConnected());
404
405 // No active network info
Makoto Onukifa8b0ba2019-10-07 16:49:04 -0700406 mInjector.connectivityManager = mConnectivityManager;
407 doReturn(null).when(mConnectivityManager).getActiveNetworkInfo();
Kweku Adams799858b2018-10-08 17:19:08 -0700408 mDeviceIdleController.updateConnectivityState(null);
409 assertFalse(mDeviceIdleController.isNetworkConnected());
410
411 // Active network info says connected.
412 final NetworkInfo ani = mock(NetworkInfo.class);
Makoto Onukifa8b0ba2019-10-07 16:49:04 -0700413 doReturn(ani).when(mConnectivityManager).getActiveNetworkInfo();
Kweku Adams799858b2018-10-08 17:19:08 -0700414 doReturn(true).when(ani).isConnected();
415 mDeviceIdleController.updateConnectivityState(null);
416 assertTrue(mDeviceIdleController.isNetworkConnected());
417
418 // Active network info says not connected.
419 doReturn(false).when(ani).isConnected();
420 mDeviceIdleController.updateConnectivityState(null);
421 assertFalse(mDeviceIdleController.isNetworkConnected());
422
423 // Wrong intent passed (false).
424 Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
425 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3);
426 doReturn(true).when(ani).isConnected();
427 doReturn(1).when(ani).getType();
428 mDeviceIdleController.updateConnectivityState(intent);
429 // Wrong intent means we shouldn't update the connected state.
430 assertFalse(mDeviceIdleController.isNetworkConnected());
431
432 // Intent says connected.
433 doReturn(1).when(ani).getType();
434 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1);
435 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
436 mDeviceIdleController.updateConnectivityState(intent);
437 assertTrue(mDeviceIdleController.isNetworkConnected());
438
439 // Wrong intent passed (true).
440 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3);
441 // Wrong intent means we shouldn't update the connected state.
442 assertTrue(mDeviceIdleController.isNetworkConnected());
443
444 // Intent says not connected.
445 intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1);
446 intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true);
447 mDeviceIdleController.updateConnectivityState(intent);
448 assertFalse(mDeviceIdleController.isNetworkConnected());
449 }
450
451 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700452 public void testUpdateQuickDozeFlagLocked() {
453 mDeviceIdleController.updateQuickDozeFlagLocked(false);
454 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
455
456 // Make sure setting false when quick doze is already off doesn't change anything.
457 mDeviceIdleController.updateQuickDozeFlagLocked(false);
458 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
459
460 // Test changing from quick doze off to quick doze on.
461 mDeviceIdleController.updateQuickDozeFlagLocked(true);
462 assertTrue(mDeviceIdleController.isQuickDozeEnabled());
463
464 // Make sure setting true when quick doze is already on doesn't change anything.
465 mDeviceIdleController.updateQuickDozeFlagLocked(true);
466 assertTrue(mDeviceIdleController.isQuickDozeEnabled());
467
468 // Test changing from quick doze on to quick doze off.
469 mDeviceIdleController.updateQuickDozeFlagLocked(false);
470 assertFalse(mDeviceIdleController.isQuickDozeEnabled());
471 }
472
473 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700474 public void testStateActiveToStateInactive_ConditionsNotMet() {
475 mDeviceIdleController.becomeActiveLocked("testing", 0);
476 verifyStateConditions(STATE_ACTIVE);
477
478 // State should stay ACTIVE with screen on and charging.
479 setChargingOn(true);
480 setScreenOn(true);
481
482 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
483 verifyStateConditions(STATE_ACTIVE);
484
485 // State should stay ACTIVE with charging on.
486 setChargingOn(true);
487 setScreenOn(false);
488
489 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
490 verifyStateConditions(STATE_ACTIVE);
491
492 // State should stay ACTIVE with screen on.
493 // Note the different operation order here makes sure the state doesn't change before test.
494 setScreenOn(true);
495 setChargingOn(false);
496
497 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
498 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsb7ce1902019-01-30 10:55:34 -0800499
500 mConstants.WAIT_FOR_UNLOCK = false;
501 setScreenLocked(true);
502 setScreenOn(true);
503 setChargingOn(false);
504
505 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
506 verifyStateConditions(STATE_ACTIVE);
507
508 setScreenLocked(false);
509 setScreenOn(true);
510 setChargingOn(false);
511
512 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
513 verifyStateConditions(STATE_ACTIVE);
514
515 mConstants.WAIT_FOR_UNLOCK = true;
516 setScreenLocked(false);
517 setScreenOn(true);
518 setChargingOn(false);
519
520 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
521 verifyStateConditions(STATE_ACTIVE);
Kweku Adams00e3a372018-09-28 16:57:09 -0700522 }
523
524 @Test
525 public void testLightStateActiveToLightStateInactive_ConditionsNotMet() {
526 mDeviceIdleController.becomeActiveLocked("testing", 0);
527 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
528
529 // State should stay ACTIVE with screen on and charging.
530 setChargingOn(true);
531 setScreenOn(true);
532
533 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
534 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
535
536 // State should stay ACTIVE with charging on.
537 setChargingOn(true);
538 setScreenOn(false);
539
540 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
541 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
542
543 // State should stay ACTIVE with screen on.
544 // Note the different operation order here makes sure the state doesn't change before test.
545 setScreenOn(true);
546 setChargingOn(false);
547
548 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
549 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
550 }
551
552 @Test
553 public void testStateActiveToStateInactive_ConditionsMet() {
554 mDeviceIdleController.becomeActiveLocked("testing", 0);
555 verifyStateConditions(STATE_ACTIVE);
556
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700557 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700558 setChargingOn(false);
559 setScreenOn(false);
560
561 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
562 verifyStateConditions(STATE_INACTIVE);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700563 verify(mDeviceIdleController)
564 .scheduleAlarmLocked(eq(mConstants.INACTIVE_TIMEOUT), eq(false));
565 }
566
567 @Test
568 public void testStateActiveToStateInactive_UpcomingAlarm() {
569 final long timeUntilAlarm = mConstants.MIN_TIME_TO_ALARM / 2;
570 // Set an upcoming alarm that will prevent full idle.
571 doReturn(mInjector.getElapsedRealtime() + timeUntilAlarm)
572 .when(mAlarmManager).getNextWakeFromIdleTime();
573
574 InOrder inOrder = inOrder(mDeviceIdleController);
575
576 enterDeepState(STATE_ACTIVE);
577 setQuickDozeEnabled(false);
578 setChargingOn(false);
579 setScreenOn(false);
580
581 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
582 verifyStateConditions(STATE_INACTIVE);
583 inOrder.verify(mDeviceIdleController)
584 .scheduleAlarmLocked(eq(timeUntilAlarm + mConstants.INACTIVE_TIMEOUT), eq(false));
585
586 enterDeepState(STATE_ACTIVE);
587 setQuickDozeEnabled(true);
588 setChargingOn(false);
589 setScreenOn(false);
590
591 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
592 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
593 inOrder.verify(mDeviceIdleController).scheduleAlarmLocked(
594 eq(timeUntilAlarm + mConstants.QUICK_DOZE_DELAY_TIMEOUT), eq(false));
Kweku Adams00e3a372018-09-28 16:57:09 -0700595 }
596
597 @Test
598 public void testLightStateActiveToLightStateInactive_ConditionsMet() {
599 mDeviceIdleController.becomeActiveLocked("testing", 0);
600 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
601
602 setChargingOn(false);
603 setScreenOn(false);
604
605 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
606 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
607 }
608
609 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700610 public void testTransitionFromAnyStateToStateQuickDozeDelay() {
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700611 setAlarmSoon(false);
612 InOrder inOrder = inOrder(mDeviceIdleController);
613
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700614 enterDeepState(STATE_ACTIVE);
615 setQuickDozeEnabled(true);
616 setChargingOn(false);
617 setScreenOn(false);
618 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700619 inOrder.verify(mDeviceIdleController)
620 .scheduleAlarmLocked(eq(mConstants.QUICK_DOZE_DELAY_TIMEOUT), eq(false));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700621
622 enterDeepState(STATE_INACTIVE);
623 setQuickDozeEnabled(true);
624 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700625 inOrder.verify(mDeviceIdleController)
626 .scheduleAlarmLocked(eq(mConstants.QUICK_DOZE_DELAY_TIMEOUT), eq(false));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700627
628 enterDeepState(STATE_IDLE_PENDING);
629 setQuickDozeEnabled(true);
630 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700631 inOrder.verify(mDeviceIdleController)
632 .scheduleAlarmLocked(eq(mConstants.QUICK_DOZE_DELAY_TIMEOUT), eq(false));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700633
634 enterDeepState(STATE_SENSING);
635 setQuickDozeEnabled(true);
636 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700637 inOrder.verify(mDeviceIdleController)
638 .scheduleAlarmLocked(eq(mConstants.QUICK_DOZE_DELAY_TIMEOUT), eq(false));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700639
640 enterDeepState(STATE_LOCATING);
641 setQuickDozeEnabled(true);
642 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700643 inOrder.verify(mDeviceIdleController)
644 .scheduleAlarmLocked(eq(mConstants.QUICK_DOZE_DELAY_TIMEOUT), eq(false));
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700645
646 // IDLE should stay as IDLE.
647 enterDeepState(STATE_IDLE);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700648 // Clear out any alarm setting from the order before checking for this section.
649 inOrder.verify(mDeviceIdleController, atLeastOnce())
650 .scheduleAlarmLocked(anyLong(), anyBoolean());
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700651 setQuickDozeEnabled(true);
652 verifyStateConditions(STATE_IDLE);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700653 inOrder.verify(mDeviceIdleController, never()).scheduleAlarmLocked(anyLong(), anyBoolean());
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700654
655 // IDLE_MAINTENANCE should stay as IDLE_MAINTENANCE.
656 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700657 // Clear out any alarm setting from the order before checking for this section.
658 inOrder.verify(mDeviceIdleController, atLeastOnce())
659 .scheduleAlarmLocked(anyLong(), anyBoolean());
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700660 setQuickDozeEnabled(true);
661 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700662 inOrder.verify(mDeviceIdleController, never()).scheduleAlarmLocked(anyLong(), anyBoolean());
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700663
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700664 // State is already QUICK_DOZE_DELAY. No work should be done.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700665 enterDeepState(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700666 // Clear out any alarm setting from the order before checking for this section.
667 inOrder.verify(mDeviceIdleController, atLeastOnce())
668 .scheduleAlarmLocked(anyLong(), anyBoolean());
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700669 setQuickDozeEnabled(true);
670 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
671 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsecf98fb2019-07-22 17:12:41 -0700672 inOrder.verify(mDeviceIdleController, never()).scheduleAlarmLocked(anyLong(), anyBoolean());
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700673 }
674
675 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700676 public void testStepIdleStateLocked_InvalidStates() {
677 mDeviceIdleController.becomeActiveLocked("testing", 0);
678 mDeviceIdleController.stepIdleStateLocked("testing");
679 // mDeviceIdleController.stepIdleStateLocked doesn't handle the ACTIVE case, so the state
680 // should stay as ACTIVE.
681 verifyStateConditions(STATE_ACTIVE);
682 }
683
684 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700685 public void testStepIdleStateLocked_ValidStates_QuickDoze() {
686 setAlarmSoon(false);
687
688 // Quick doze should go directly into IDLE.
689 enterDeepState(STATE_QUICK_DOZE_DELAY);
690 mDeviceIdleController.stepIdleStateLocked("testing");
691 verifyStateConditions(STATE_IDLE);
692
693 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
694
695 mDeviceIdleController.stepIdleStateLocked("testing");
696 verifyStateConditions(STATE_IDLE_MAINTENANCE);
697
698 mDeviceIdleController.stepIdleStateLocked("testing");
699 verifyStateConditions(STATE_IDLE);
700
701 mDeviceIdleController.stepIdleStateLocked("testing");
702 verifyStateConditions(STATE_IDLE_MAINTENANCE);
703 }
704
705 @Test
Kweku Adamsbf869382018-10-09 18:26:56 -0700706 public void testStepIdleStateLocked_ValidStates_WithWakeFromIdleAlarmSoon() {
707 enterDeepState(STATE_ACTIVE);
708 // Return that there's an alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700709 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700710 mDeviceIdleController.stepIdleStateLocked("testing");
711 verifyStateConditions(STATE_ACTIVE);
712
713 // Everything besides ACTIVE should end up as INACTIVE since the screen would be off.
714
715 enterDeepState(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700716 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700717 mDeviceIdleController.stepIdleStateLocked("testing");
718 verifyStateConditions(STATE_INACTIVE);
719
720 enterDeepState(STATE_IDLE_PENDING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700721 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700722 mDeviceIdleController.stepIdleStateLocked("testing");
723 verifyStateConditions(STATE_INACTIVE);
724
725 enterDeepState(STATE_SENSING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700726 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700727 mDeviceIdleController.stepIdleStateLocked("testing");
728 verifyStateConditions(STATE_INACTIVE);
729
730 enterDeepState(STATE_LOCATING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700731 setAlarmSoon(true);
732 mDeviceIdleController.stepIdleStateLocked("testing");
733 verifyStateConditions(STATE_INACTIVE);
734
735 // With quick doze enabled, we should end up in QUICK_DOZE_DELAY instead of INACTIVE.
736 enterDeepState(STATE_QUICK_DOZE_DELAY);
737 setQuickDozeEnabled(true);
738 setAlarmSoon(true);
739 mDeviceIdleController.stepIdleStateLocked("testing");
740 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
741
742 // With quick doze disabled, we should end up in INACTIVE instead of QUICK_DOZE_DELAY.
743 enterDeepState(STATE_QUICK_DOZE_DELAY);
744 setQuickDozeEnabled(false);
745 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700746 mDeviceIdleController.stepIdleStateLocked("testing");
747 verifyStateConditions(STATE_INACTIVE);
748
749 enterDeepState(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700750 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700751 mDeviceIdleController.stepIdleStateLocked("testing");
752 verifyStateConditions(STATE_INACTIVE);
753
754 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700755 setAlarmSoon(true);
Kweku Adamsbf869382018-10-09 18:26:56 -0700756 mDeviceIdleController.stepIdleStateLocked("testing");
757 verifyStateConditions(STATE_INACTIVE);
758 }
759
760 @Test
Kweku Adams00e3a372018-09-28 16:57:09 -0700761 public void testStepIdleStateLocked_ValidStates_NoLocationManager() {
Kweku Adams799858b2018-10-08 17:19:08 -0700762 mInjector.locationManager = null;
Kweku Adams00e3a372018-09-28 16:57:09 -0700763 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700764 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700765 // Set state to INACTIVE.
766 mDeviceIdleController.becomeActiveLocked("testing", 0);
767 setChargingOn(false);
768 setScreenOn(false);
769 verifyStateConditions(STATE_INACTIVE);
770
771 mDeviceIdleController.stepIdleStateLocked("testing");
772 verifyStateConditions(STATE_IDLE_PENDING);
773
774 mDeviceIdleController.stepIdleStateLocked("testing");
775 verifyStateConditions(STATE_SENSING);
776
777 mDeviceIdleController.stepIdleStateLocked("testing");
778 // No location manager, so SENSING should go straight to IDLE.
779 verifyStateConditions(STATE_IDLE);
780
781 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
782
783 mDeviceIdleController.stepIdleStateLocked("testing");
784 verifyStateConditions(STATE_IDLE_MAINTENANCE);
785
786 mDeviceIdleController.stepIdleStateLocked("testing");
787 verifyStateConditions(STATE_IDLE);
788
789 mDeviceIdleController.stepIdleStateLocked("testing");
790 verifyStateConditions(STATE_IDLE_MAINTENANCE);
791 }
792
793 @Test
794 public void testStepIdleStateLocked_ValidStates_WithLocationManager_NoProviders() {
795 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700796 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700797 // Set state to INACTIVE.
798 mDeviceIdleController.becomeActiveLocked("testing", 0);
799 setChargingOn(false);
800 setScreenOn(false);
801 verifyStateConditions(STATE_INACTIVE);
802
803 mDeviceIdleController.stepIdleStateLocked("testing");
804 verifyStateConditions(STATE_IDLE_PENDING);
805
806 mDeviceIdleController.stepIdleStateLocked("testing");
807 verifyStateConditions(STATE_SENSING);
808
809 mDeviceIdleController.stepIdleStateLocked("testing");
810 // Location manager exists but there isn't a network or GPS provider,
811 // so SENSING should go straight to IDLE.
812 verifyStateConditions(STATE_IDLE);
813
814 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
815
816 mDeviceIdleController.stepIdleStateLocked("testing");
817 verifyStateConditions(STATE_IDLE_MAINTENANCE);
818
819 mDeviceIdleController.stepIdleStateLocked("testing");
820 verifyStateConditions(STATE_IDLE);
821
822 mDeviceIdleController.stepIdleStateLocked("testing");
823 verifyStateConditions(STATE_IDLE_MAINTENANCE);
824 }
825
826 @Test
827 public void testStepIdleStateLocked_ValidStates_WithLocationManager_WithProviders() {
Kweku Adams799858b2018-10-08 17:19:08 -0700828 mInjector.locationManager = mLocationManager;
Kweku Adams00e3a372018-09-28 16:57:09 -0700829 doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(anyString());
830 // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700831 setAlarmSoon(false);
Kweku Adams00e3a372018-09-28 16:57:09 -0700832 // Set state to INACTIVE.
833 mDeviceIdleController.becomeActiveLocked("testing", 0);
834 setChargingOn(false);
835 setScreenOn(false);
836 verifyStateConditions(STATE_INACTIVE);
837
838 mDeviceIdleController.stepIdleStateLocked("testing");
839 verifyStateConditions(STATE_IDLE_PENDING);
840
841 mDeviceIdleController.stepIdleStateLocked("testing");
842 verifyStateConditions(STATE_SENSING);
843
844 mDeviceIdleController.stepIdleStateLocked("testing");
845 // Location manager exists with a provider, so SENSING should go to LOCATING.
846 verifyStateConditions(STATE_LOCATING);
847
848 mDeviceIdleController.stepIdleStateLocked("testing");
849 verifyStateConditions(STATE_IDLE);
850
851 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
852
853 mDeviceIdleController.stepIdleStateLocked("testing");
854 verifyStateConditions(STATE_IDLE_MAINTENANCE);
855
856 mDeviceIdleController.stepIdleStateLocked("testing");
857 verifyStateConditions(STATE_IDLE);
858
859 mDeviceIdleController.stepIdleStateLocked("testing");
860 verifyStateConditions(STATE_IDLE_MAINTENANCE);
861 }
862
Kweku Adamsa457f4e2018-10-03 15:56:06 -0700863 @Test
Kweku Adams799858b2018-10-08 17:19:08 -0700864 public void testLightStepIdleStateLocked_InvalidStates() {
865 mDeviceIdleController.becomeActiveLocked("testing", 0);
866 mDeviceIdleController.stepLightIdleStateLocked("testing");
867 // stepLightIdleStateLocked doesn't handle the ACTIVE case, so the state
868 // should stay as ACTIVE.
869 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
870 }
871
872 /**
873 * Make sure stepLightIdleStateLocked doesn't change state when the state is
874 * LIGHT_STATE_OVERRIDE.
875 */
876 @Test
877 public void testLightStepIdleStateLocked_Overriden() {
878 enterLightState(LIGHT_STATE_OVERRIDE);
879 mDeviceIdleController.stepLightIdleStateLocked("testing");
880 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
881 }
882
883 @Test
884 public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NetworkConnected() {
885 setNetworkConnected(true);
886 mDeviceIdleController.setJobsActive(false);
887 mDeviceIdleController.setAlarmsActive(false);
888 mDeviceIdleController.setActiveIdleOpsForTest(0);
889
890 // Set state to INACTIVE.
891 mDeviceIdleController.becomeActiveLocked("testing", 0);
892 setChargingOn(false);
893 setScreenOn(false);
894 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
895
896 // No active ops means INACTIVE should go straight to IDLE.
897 mDeviceIdleController.stepLightIdleStateLocked("testing");
898 verifyLightStateConditions(LIGHT_STATE_IDLE);
899
900 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
901
902 mDeviceIdleController.stepLightIdleStateLocked("testing");
903 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
904
905 mDeviceIdleController.stepLightIdleStateLocked("testing");
906 verifyLightStateConditions(LIGHT_STATE_IDLE);
907
908 mDeviceIdleController.stepLightIdleStateLocked("testing");
909 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
910 }
911
912 @Test
913 public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NetworkConnected() {
914 setNetworkConnected(true);
915 // Set state to INACTIVE.
916 mDeviceIdleController.becomeActiveLocked("testing", 0);
917 setChargingOn(false);
918 setScreenOn(false);
919 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
920
921 // Active ops means INACTIVE should go to PRE_IDLE to wait.
922 mDeviceIdleController.setJobsActive(true);
923 mDeviceIdleController.setAlarmsActive(true);
924 mDeviceIdleController.setActiveIdleOpsForTest(1);
925 mDeviceIdleController.stepLightIdleStateLocked("testing");
926 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
927
928 // Even with active ops, PRE_IDLE should go to IDLE.
929 mDeviceIdleController.stepLightIdleStateLocked("testing");
930 verifyLightStateConditions(LIGHT_STATE_IDLE);
931
932 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
933
934 mDeviceIdleController.stepLightIdleStateLocked("testing");
935 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
936
937 mDeviceIdleController.stepLightIdleStateLocked("testing");
938 verifyLightStateConditions(LIGHT_STATE_IDLE);
939
940 mDeviceIdleController.stepLightIdleStateLocked("testing");
941 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
942 }
943
944 @Test
945 public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NoNetworkConnected() {
946 setNetworkConnected(false);
947 mDeviceIdleController.setJobsActive(false);
948 mDeviceIdleController.setAlarmsActive(false);
949 mDeviceIdleController.setActiveIdleOpsForTest(0);
950
951 // Set state to INACTIVE.
952 mDeviceIdleController.becomeActiveLocked("testing", 0);
953 setChargingOn(false);
954 setScreenOn(false);
955 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
956
957 // No active ops means INACTIVE should go straight to IDLE.
958 mDeviceIdleController.stepLightIdleStateLocked("testing");
959 verifyLightStateConditions(LIGHT_STATE_IDLE);
960
961 // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now.
962
963 mDeviceIdleController.stepLightIdleStateLocked("testing");
964 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
965
966 mDeviceIdleController.stepLightIdleStateLocked("testing");
967 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
968
969 mDeviceIdleController.stepLightIdleStateLocked("testing");
970 verifyLightStateConditions(LIGHT_STATE_IDLE);
971
972 mDeviceIdleController.stepLightIdleStateLocked("testing");
973 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
974
975 mDeviceIdleController.stepLightIdleStateLocked("testing");
976 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
977 }
978
979 @Test
980 public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NoNetworkConnected() {
981 setNetworkConnected(false);
982 // Set state to INACTIVE.
983 mDeviceIdleController.becomeActiveLocked("testing", 0);
984 setChargingOn(false);
985 setScreenOn(false);
986 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
987
988 // Active ops means INACTIVE should go to PRE_IDLE to wait.
989 mDeviceIdleController.setJobsActive(true);
990 mDeviceIdleController.setAlarmsActive(true);
991 mDeviceIdleController.setActiveIdleOpsForTest(1);
992 mDeviceIdleController.stepLightIdleStateLocked("testing");
993 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
994
995 // Even with active ops, PRE_IDLE should go to IDLE.
996 mDeviceIdleController.stepLightIdleStateLocked("testing");
997 verifyLightStateConditions(LIGHT_STATE_IDLE);
998
999 // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now.
1000
1001 mDeviceIdleController.stepLightIdleStateLocked("testing");
1002 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1003
1004 mDeviceIdleController.stepLightIdleStateLocked("testing");
1005 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1006
1007 mDeviceIdleController.stepLightIdleStateLocked("testing");
1008 verifyLightStateConditions(LIGHT_STATE_IDLE);
1009
1010 mDeviceIdleController.stepLightIdleStateLocked("testing");
1011 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1012
1013 mDeviceIdleController.stepLightIdleStateLocked("testing");
1014 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1015 }
1016
Kweku Adams7c96f5182019-06-04 16:32:49 -07001017 @Test
1018 public void testLightIdleAlarmUnaffectedByMotion() {
1019 setNetworkConnected(true);
1020 mDeviceIdleController.setJobsActive(false);
1021 mDeviceIdleController.setAlarmsActive(false);
1022 mDeviceIdleController.setActiveIdleOpsForTest(0);
1023 spyOn(mDeviceIdleController);
1024
1025 InOrder inOrder = inOrder(mDeviceIdleController);
1026
1027 // Set state to INACTIVE.
1028 mDeviceIdleController.becomeActiveLocked("testing", 0);
1029 setChargingOn(false);
1030 setScreenOn(false);
1031 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1032
1033 // No active ops means INACTIVE should go straight to IDLE.
1034 mDeviceIdleController.stepLightIdleStateLocked("testing");
1035 verifyLightStateConditions(LIGHT_STATE_IDLE);
1036 inOrder.verify(mDeviceIdleController).scheduleLightAlarmLocked(
1037 longThat(l -> l == mConstants.LIGHT_IDLE_TIMEOUT));
1038
1039 // Should just alternate between IDLE and IDLE_MAINTENANCE now.
1040
1041 mDeviceIdleController.stepLightIdleStateLocked("testing");
1042 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1043 inOrder.verify(mDeviceIdleController).scheduleLightAlarmLocked(
1044 longThat(l -> l >= mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET));
1045
1046 mDeviceIdleController.stepLightIdleStateLocked("testing");
1047 verifyLightStateConditions(LIGHT_STATE_IDLE);
1048 inOrder.verify(mDeviceIdleController).scheduleLightAlarmLocked(
1049 longThat(l -> l > mConstants.LIGHT_IDLE_TIMEOUT));
1050
1051 mDeviceIdleController.stepLightIdleStateLocked("testing");
1052 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1053 inOrder.verify(mDeviceIdleController).scheduleLightAlarmLocked(
1054 longThat(l -> l >= mConstants.LIGHT_IDLE_MAINTENANCE_MIN_BUDGET));
1055
1056 // Test that motion doesn't reset the idle timeout.
1057 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1058
1059 mDeviceIdleController.stepLightIdleStateLocked("testing");
1060 verifyLightStateConditions(LIGHT_STATE_IDLE);
1061 inOrder.verify(mDeviceIdleController).scheduleLightAlarmLocked(
1062 longThat(l -> l > mConstants.LIGHT_IDLE_TIMEOUT));
1063 }
1064
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001065 ///////////////// EXIT conditions ///////////////////
1066
Kweku Adams799858b2018-10-08 17:19:08 -07001067 @Test
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001068 public void testExitMaintenanceEarlyIfNeededLocked_deep_noActiveOps() {
1069 mDeviceIdleController.setJobsActive(false);
1070 mDeviceIdleController.setAlarmsActive(false);
1071 mDeviceIdleController.setActiveIdleOpsForTest(0);
1072
1073 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1074
1075 enterDeepState(STATE_ACTIVE);
1076 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1077 verifyStateConditions(STATE_ACTIVE);
1078
1079 enterDeepState(STATE_INACTIVE);
1080 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1081 verifyStateConditions(STATE_INACTIVE);
1082
1083 enterDeepState(STATE_IDLE_PENDING);
1084 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1085 verifyStateConditions(STATE_IDLE_PENDING);
1086
1087 enterDeepState(STATE_SENSING);
1088 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1089 verifyStateConditions(STATE_SENSING);
1090
1091 enterDeepState(STATE_LOCATING);
1092 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1093 verifyStateConditions(STATE_LOCATING);
1094
1095 enterDeepState(STATE_IDLE);
1096 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1097 verifyStateConditions(STATE_IDLE);
1098
1099 enterDeepState(STATE_IDLE_MAINTENANCE);
1100 // Going into IDLE_MAINTENANCE increments the active idle op count.
1101 mDeviceIdleController.setActiveIdleOpsForTest(0);
1102 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1103 verifyStateConditions(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001104
1105 enterDeepState(STATE_QUICK_DOZE_DELAY);
1106 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1107 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001108 }
1109
1110 @Test
1111 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeJobs() {
1112 mDeviceIdleController.setJobsActive(true);
1113 mDeviceIdleController.setAlarmsActive(false);
1114 mDeviceIdleController.setActiveIdleOpsForTest(0);
1115
1116 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1117
1118 enterDeepState(STATE_ACTIVE);
1119 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1120 verifyStateConditions(STATE_ACTIVE);
1121
1122 enterDeepState(STATE_INACTIVE);
1123 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1124 verifyStateConditions(STATE_INACTIVE);
1125
1126 enterDeepState(STATE_IDLE_PENDING);
1127 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1128 verifyStateConditions(STATE_IDLE_PENDING);
1129
1130 enterDeepState(STATE_SENSING);
1131 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1132 verifyStateConditions(STATE_SENSING);
1133
1134 enterDeepState(STATE_LOCATING);
1135 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1136 verifyStateConditions(STATE_LOCATING);
1137
1138 enterDeepState(STATE_IDLE);
1139 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1140 verifyStateConditions(STATE_IDLE);
1141
1142 enterDeepState(STATE_IDLE_MAINTENANCE);
1143 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1144 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001145
1146 enterDeepState(STATE_QUICK_DOZE_DELAY);
1147 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1148 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001149 }
1150
1151 @Test
1152 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeAlarms() {
1153 mDeviceIdleController.setJobsActive(false);
1154 mDeviceIdleController.setAlarmsActive(true);
1155 mDeviceIdleController.setActiveIdleOpsForTest(0);
1156
1157 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1158
1159 enterDeepState(STATE_ACTIVE);
1160 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1161 verifyStateConditions(STATE_ACTIVE);
1162
1163 enterDeepState(STATE_INACTIVE);
1164 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1165 verifyStateConditions(STATE_INACTIVE);
1166
1167 enterDeepState(STATE_IDLE_PENDING);
1168 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1169 verifyStateConditions(STATE_IDLE_PENDING);
1170
1171 enterDeepState(STATE_SENSING);
1172 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1173 verifyStateConditions(STATE_SENSING);
1174
1175 enterDeepState(STATE_LOCATING);
1176 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1177 verifyStateConditions(STATE_LOCATING);
1178
1179 enterDeepState(STATE_IDLE);
1180 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1181 verifyStateConditions(STATE_IDLE);
1182
1183 enterDeepState(STATE_IDLE_MAINTENANCE);
1184 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1185 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001186
1187 enterDeepState(STATE_QUICK_DOZE_DELAY);
1188 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1189 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001190 }
1191
1192 @Test
1193 public void testExitMaintenanceEarlyIfNeededLocked_deep_activeOps() {
1194 mDeviceIdleController.setJobsActive(false);
1195 mDeviceIdleController.setAlarmsActive(false);
1196 mDeviceIdleController.setActiveIdleOpsForTest(1);
1197
1198 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1199
1200 enterDeepState(STATE_ACTIVE);
1201 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1202 verifyStateConditions(STATE_ACTIVE);
1203
1204 enterDeepState(STATE_INACTIVE);
1205 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1206 verifyStateConditions(STATE_INACTIVE);
1207
1208 enterDeepState(STATE_IDLE_PENDING);
1209 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1210 verifyStateConditions(STATE_IDLE_PENDING);
1211
1212 enterDeepState(STATE_SENSING);
1213 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1214 verifyStateConditions(STATE_SENSING);
1215
1216 enterDeepState(STATE_LOCATING);
1217 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1218 verifyStateConditions(STATE_LOCATING);
1219
1220 enterDeepState(STATE_IDLE);
1221 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1222 verifyStateConditions(STATE_IDLE);
1223
1224 enterDeepState(STATE_IDLE_MAINTENANCE);
1225 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1226 verifyStateConditions(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001227
1228 enterDeepState(STATE_QUICK_DOZE_DELAY);
1229 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1230 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001231 }
1232
1233 @Test
1234 public void testExitMaintenanceEarlyIfNeededLocked_light_noActiveOps() {
1235 mDeviceIdleController.setJobsActive(false);
1236 mDeviceIdleController.setAlarmsActive(false);
1237 mDeviceIdleController.setActiveIdleOpsForTest(0);
1238
1239 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1240
1241 enterLightState(LIGHT_STATE_ACTIVE);
1242 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1243 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1244
1245 enterLightState(LIGHT_STATE_INACTIVE);
1246 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1247 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1248
1249 enterLightState(LIGHT_STATE_PRE_IDLE);
1250 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1251 verifyLightStateConditions(LIGHT_STATE_IDLE);
1252
1253 enterLightState(LIGHT_STATE_IDLE);
1254 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1255 verifyLightStateConditions(LIGHT_STATE_IDLE);
1256
1257 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1258 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1259 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1260
1261 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1262 // Going into IDLE_MAINTENANCE increments the active idle op count.
1263 mDeviceIdleController.setActiveIdleOpsForTest(0);
1264 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1265 verifyLightStateConditions(LIGHT_STATE_IDLE);
1266
1267 enterLightState(LIGHT_STATE_OVERRIDE);
1268 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1269 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1270 }
1271
1272 @Test
1273 public void testExitMaintenanceEarlyIfNeededLocked_light_activeJobs() {
1274 mDeviceIdleController.setJobsActive(true);
1275 mDeviceIdleController.setAlarmsActive(false);
1276 mDeviceIdleController.setActiveIdleOpsForTest(0);
1277
1278 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1279
1280 enterLightState(LIGHT_STATE_ACTIVE);
1281 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1282 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1283
1284 enterLightState(LIGHT_STATE_INACTIVE);
1285 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1286 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1287
1288 enterLightState(LIGHT_STATE_PRE_IDLE);
1289 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1290 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1291
1292 enterLightState(LIGHT_STATE_IDLE);
1293 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1294 verifyLightStateConditions(LIGHT_STATE_IDLE);
1295
1296 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1297 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1298 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1299
1300 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1301 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1302 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1303
1304 enterLightState(LIGHT_STATE_OVERRIDE);
1305 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1306 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1307 }
1308
1309 @Test
1310 public void testExitMaintenanceEarlyIfNeededLocked_light_activeAlarms() {
1311 mDeviceIdleController.setJobsActive(false);
1312 mDeviceIdleController.setAlarmsActive(true);
1313 mDeviceIdleController.setActiveIdleOpsForTest(0);
1314
1315 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1316
1317 enterLightState(LIGHT_STATE_ACTIVE);
1318 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1319 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1320
1321 enterLightState(LIGHT_STATE_INACTIVE);
1322 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1323 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1324
1325 enterLightState(LIGHT_STATE_PRE_IDLE);
1326 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1327 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1328
1329 enterLightState(LIGHT_STATE_IDLE);
1330 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1331 verifyLightStateConditions(LIGHT_STATE_IDLE);
1332
1333 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1334 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1335 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1336
1337 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1338 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1339 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1340
1341 enterLightState(LIGHT_STATE_OVERRIDE);
1342 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1343 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1344 }
1345
1346 @Test
1347 public void testExitMaintenanceEarlyIfNeededLocked_light_activeOps() {
1348 mDeviceIdleController.setJobsActive(false);
1349 mDeviceIdleController.setAlarmsActive(false);
1350 mDeviceIdleController.setActiveIdleOpsForTest(1);
1351
1352 // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states.
1353
1354 enterLightState(LIGHT_STATE_ACTIVE);
1355 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1356 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1357
1358 enterLightState(LIGHT_STATE_INACTIVE);
1359 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1360 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1361
1362 enterLightState(LIGHT_STATE_PRE_IDLE);
1363 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1364 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1365
1366 enterLightState(LIGHT_STATE_IDLE);
1367 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1368 verifyLightStateConditions(LIGHT_STATE_IDLE);
1369
1370 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1371 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1372 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1373
1374 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1375 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1376 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1377
1378 enterLightState(LIGHT_STATE_OVERRIDE);
1379 mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked();
1380 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
1381 }
1382
1383 @Test
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001384 public void testHandleMotionDetectedLocked_deep_quickDoze_off() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001385 enterDeepState(STATE_ACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001386 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001387 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1388 verifyStateConditions(STATE_ACTIVE);
1389
1390 // Anything that wasn't ACTIVE before motion detection should end up in the INACTIVE state.
1391
1392 enterDeepState(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001393 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001394 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1395 verifyStateConditions(STATE_INACTIVE);
1396
1397 enterDeepState(STATE_IDLE_PENDING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001398 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001399 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1400 verifyStateConditions(STATE_INACTIVE);
1401
1402 enterDeepState(STATE_SENSING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001403 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001404 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1405 verifyStateConditions(STATE_INACTIVE);
1406
1407 enterDeepState(STATE_LOCATING);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001408 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001409 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1410 verifyStateConditions(STATE_INACTIVE);
1411
1412 enterDeepState(STATE_IDLE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001413 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001414 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1415 verifyStateConditions(STATE_INACTIVE);
1416
1417 enterDeepState(STATE_IDLE_MAINTENANCE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001418 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001419 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1420 verifyStateConditions(STATE_INACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001421
1422 enterDeepState(STATE_QUICK_DOZE_DELAY);
1423 setQuickDozeEnabled(false);
1424 // Disabling quick doze doesn't immediately change the state as coming out is harder than
1425 // going in.
1426 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1427 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1428 verifyStateConditions(STATE_INACTIVE);
1429 }
1430
1431 @Test
1432 public void testHandleMotionDetectedLocked_deep_quickDoze_on() {
1433 enterDeepState(STATE_ACTIVE);
1434 setQuickDozeEnabled(true);
1435 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1436 verifyStateConditions(STATE_ACTIVE);
1437
1438 // Anything that wasn't ACTIVE before motion detection should end up in the
1439 // QUICK_DOZE_DELAY state since quick doze is enabled.
1440
1441 enterDeepState(STATE_INACTIVE);
1442 setQuickDozeEnabled(true);
1443 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1444 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1445
1446 enterDeepState(STATE_IDLE_PENDING);
1447 setQuickDozeEnabled(true);
1448 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1449 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1450
1451 enterDeepState(STATE_SENSING);
1452 setQuickDozeEnabled(true);
1453 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1454 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1455
1456 enterDeepState(STATE_LOCATING);
1457 setQuickDozeEnabled(true);
1458 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1459 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1460
1461 enterDeepState(STATE_IDLE);
1462 setQuickDozeEnabled(true);
1463 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1464 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1465
1466 enterDeepState(STATE_IDLE_MAINTENANCE);
1467 setQuickDozeEnabled(true);
1468 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1469 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
1470
1471 enterDeepState(STATE_QUICK_DOZE_DELAY);
1472 setQuickDozeEnabled(true);
1473 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1474 verifyStateConditions(STATE_QUICK_DOZE_DELAY);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001475 }
1476
1477 @Test
1478 public void testHandleMotionDetectedLocked_light() {
1479 enterLightState(LIGHT_STATE_ACTIVE);
1480 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1481 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1482
1483 // Motion shouldn't affect light idle, so LIGHT states should stay as they were except for
1484 // OVERRIDE. OVERRIDE means deep was active, so if motion was detected,
1485 // LIGHT_STATE_OVERRIDE should end up as LIGHT_STATE_INACTIVE.
1486
1487 enterLightState(LIGHT_STATE_INACTIVE);
1488 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1489 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1490
1491 enterLightState(LIGHT_STATE_PRE_IDLE);
1492 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1493 verifyLightStateConditions(LIGHT_STATE_PRE_IDLE);
1494
1495 enterLightState(LIGHT_STATE_IDLE);
1496 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1497 verifyLightStateConditions(LIGHT_STATE_IDLE);
1498
1499 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1500 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1501 verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK);
1502
1503 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1504 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1505 verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE);
1506
1507 enterLightState(LIGHT_STATE_OVERRIDE);
1508 mDeviceIdleController.handleMotionDetectedLocked(50, "test");
1509 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1510 }
1511
1512 @Test
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001513 public void testBecomeActiveLocked_deep() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001514 // becomeActiveLocked should put everything into ACTIVE.
1515
1516 enterDeepState(STATE_ACTIVE);
1517 mDeviceIdleController.becomeActiveLocked("test", 1000);
1518 verifyStateConditions(STATE_ACTIVE);
1519
1520 enterDeepState(STATE_INACTIVE);
1521 mDeviceIdleController.becomeActiveLocked("test", 1000);
1522 verifyStateConditions(STATE_ACTIVE);
1523
1524 enterDeepState(STATE_IDLE_PENDING);
1525 mDeviceIdleController.becomeActiveLocked("test", 1000);
1526 verifyStateConditions(STATE_ACTIVE);
1527
1528 enterDeepState(STATE_SENSING);
1529 mDeviceIdleController.becomeActiveLocked("test", 1000);
1530 verifyStateConditions(STATE_ACTIVE);
1531
1532 enterDeepState(STATE_LOCATING);
1533 mDeviceIdleController.becomeActiveLocked("test", 1000);
1534 verifyStateConditions(STATE_ACTIVE);
1535
1536 enterDeepState(STATE_IDLE);
1537 mDeviceIdleController.becomeActiveLocked("test", 1000);
1538 verifyStateConditions(STATE_ACTIVE);
1539
1540 enterDeepState(STATE_IDLE_MAINTENANCE);
1541 mDeviceIdleController.becomeActiveLocked("test", 1000);
1542 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001543
1544 enterDeepState(STATE_QUICK_DOZE_DELAY);
1545 mDeviceIdleController.becomeActiveLocked("test", 1000);
1546 verifyStateConditions(STATE_ACTIVE);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001547 }
1548
1549 @Test
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001550 public void testBecomeActiveLocked_light() {
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001551 // becomeActiveLocked should put everything into ACTIVE.
1552
1553 enterLightState(LIGHT_STATE_ACTIVE);
1554 mDeviceIdleController.becomeActiveLocked("test", 1000);
1555 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1556
1557 enterLightState(LIGHT_STATE_INACTIVE);
1558 mDeviceIdleController.becomeActiveLocked("test", 1000);
1559 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1560
1561 enterLightState(LIGHT_STATE_PRE_IDLE);
1562 mDeviceIdleController.becomeActiveLocked("test", 1000);
1563 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1564
1565 enterLightState(LIGHT_STATE_IDLE);
1566 mDeviceIdleController.becomeActiveLocked("test", 1000);
1567 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1568
1569 enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK);
1570 mDeviceIdleController.becomeActiveLocked("test", 1000);
1571 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1572
1573 enterLightState(LIGHT_STATE_IDLE_MAINTENANCE);
1574 mDeviceIdleController.becomeActiveLocked("test", 1000);
1575 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1576
1577 enterLightState(LIGHT_STATE_OVERRIDE);
1578 mDeviceIdleController.becomeActiveLocked("test", 1000);
1579 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1580 }
1581
Kweku Adamsb7ce1902019-01-30 10:55:34 -08001582 /** Test based on b/119058625. */
1583 @Test
1584 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_ScreenThenMotion() {
1585 mConstants.WAIT_FOR_UNLOCK = true;
1586 enterDeepState(STATE_IDLE);
1587 reset(mAlarmManager);
1588 spyOn(mDeviceIdleController);
1589
1590 mDeviceIdleController.keyguardShowingLocked(true);
1591 setScreenOn(true);
1592 // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself
1593 // shouldn't bring the device out of deep IDLE.
1594 verifyStateConditions(STATE_IDLE);
1595 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1596 // Motion should bring the device out of Doze. Since the screen is still locked (albeit
1597 // on), the states should go back into INACTIVE.
1598 verifyStateConditions(STATE_INACTIVE);
1599 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1600 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1601 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1602 }
1603
1604 /** Test based on b/119058625. */
1605 @Test
1606 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_ScreenThenMotion() {
1607 mConstants.WAIT_FOR_UNLOCK = true;
1608 enterDeepState(STATE_IDLE);
1609 reset(mAlarmManager);
1610 spyOn(mDeviceIdleController);
1611
1612 mDeviceIdleController.keyguardShowingLocked(false);
1613 setScreenOn(true);
1614 // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself
1615 // should bring the device out of deep IDLE.
1616 verifyStateConditions(STATE_ACTIVE);
1617 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1618 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1619 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1620 }
1621
1622 /** Test based on b/119058625. */
1623 @Test
1624 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_MotionThenScreen() {
1625 mConstants.WAIT_FOR_UNLOCK = true;
1626 enterDeepState(STATE_IDLE);
1627 reset(mAlarmManager);
1628 spyOn(mDeviceIdleController);
1629
1630 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1631 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1632
1633 mDeviceIdleController.keyguardShowingLocked(true);
1634 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1635 // The screen is still off, so motion should result in the INACTIVE state.
1636 verifyStateConditions(STATE_INACTIVE);
1637 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1638 alarmManagerInOrder.verify(mAlarmManager)
1639 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1640 controllerInOrder.verify(mDeviceIdleController)
1641 .scheduleReportActiveLocked(anyString(), anyInt());
1642
1643 setScreenOn(true);
1644 // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself
1645 // shouldn't bring the device all the way to ACTIVE.
1646 verifyStateConditions(STATE_INACTIVE);
1647 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1648 alarmManagerInOrder.verify(mAlarmManager, never()).cancel(
1649 eq(mDeviceIdleController.mDeepAlarmListener));
1650
1651 // User finally unlocks the device. Device should be fully active.
1652 mDeviceIdleController.keyguardShowingLocked(false);
1653 verifyStateConditions(STATE_ACTIVE);
1654 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1655 alarmManagerInOrder.verify(mAlarmManager)
1656 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1657 controllerInOrder.verify(mDeviceIdleController)
1658 .scheduleReportActiveLocked(anyString(), anyInt());
1659 }
1660
1661 /** Test based on b/119058625. */
1662 @Test
1663 public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_MotionThenScreen() {
1664 mConstants.WAIT_FOR_UNLOCK = true;
1665 enterDeepState(STATE_IDLE);
1666 reset(mAlarmManager);
1667 spyOn(mDeviceIdleController);
1668
1669 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1670 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1671
1672 mDeviceIdleController.keyguardShowingLocked(false);
1673 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1674 // The screen is still off, so motion should result in the INACTIVE state.
1675 verifyStateConditions(STATE_INACTIVE);
1676 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1677 alarmManagerInOrder.verify(mAlarmManager)
1678 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1679 controllerInOrder.verify(mDeviceIdleController)
1680 .scheduleReportActiveLocked(anyString(), anyInt());
1681
1682 setScreenOn(true);
1683 // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself
1684 // should bring the device out of deep IDLE.
1685 verifyStateConditions(STATE_ACTIVE);
1686 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1687 alarmManagerInOrder.verify(mAlarmManager)
1688 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1689 controllerInOrder.verify(mDeviceIdleController)
1690 .scheduleReportActiveLocked(anyString(), anyInt());
1691 }
1692
1693 @Test
1694 public void testExitNotifiesDependencies_WaitForUnlockOff_Screen() {
1695 mConstants.WAIT_FOR_UNLOCK = false;
1696 enterDeepState(STATE_IDLE);
1697 reset(mAlarmManager);
1698 spyOn(mDeviceIdleController);
1699
1700 setScreenOn(true);
1701 // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself
1702 // should bring the device out of deep IDLE.
1703 verifyStateConditions(STATE_ACTIVE);
1704 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1705 verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1706 verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt());
1707 }
1708
1709 @Test
1710 public void testExitNotifiesDependencies_WaitForUnlockOff_MotionThenScreen() {
1711 mConstants.WAIT_FOR_UNLOCK = false;
1712 enterDeepState(STATE_IDLE);
1713 reset(mAlarmManager);
1714 spyOn(mDeviceIdleController);
1715
1716 InOrder alarmManagerInOrder = inOrder(mAlarmManager);
1717 InOrder controllerInOrder = inOrder(mDeviceIdleController);
1718
1719 mDeviceIdleController.handleMotionDetectedLocked(1000, "test");
1720 // The screen is still off, so motion should result in the INACTIVE state.
1721 verifyStateConditions(STATE_INACTIVE);
1722 verifyLightStateConditions(LIGHT_STATE_INACTIVE);
1723 alarmManagerInOrder.verify(mAlarmManager)
1724 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1725 controllerInOrder.verify(mDeviceIdleController)
1726 .scheduleReportActiveLocked(anyString(), anyInt());
1727
1728 setScreenOn(true);
1729 // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself
1730 // should bring the device out of deep IDLE.
1731 verifyStateConditions(STATE_ACTIVE);
1732 verifyLightStateConditions(LIGHT_STATE_ACTIVE);
1733 alarmManagerInOrder.verify(mAlarmManager)
1734 .cancel(eq(mDeviceIdleController.mDeepAlarmListener));
1735 controllerInOrder.verify(mDeviceIdleController)
1736 .scheduleReportActiveLocked(anyString(), anyInt());
1737 }
1738
Denny cy Leec5a7c292019-01-01 17:37:55 +08001739 @Test
1740 public void testStepToIdleMode() {
1741 float delta = mDeviceIdleController.MIN_PRE_IDLE_FACTOR_CHANGE;
1742 for (int mode = PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL;
1743 mode <= PowerManager.PRE_IDLE_TIMEOUT_MODE_LONG;
1744 mode++) {
1745 int ret = mDeviceIdleController.setPreIdleTimeoutMode(mode);
1746 if (mode == PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL) {
1747 assertEquals("setPreIdleTimeoutMode: " + mode + " failed.",
1748 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
1749 } else {
1750 assertEquals("setPreIdleTimeoutMode: " + mode + " failed.",
1751 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
1752 }
1753 //TODO(b/123045185): Mocked Handler of DeviceIdleController to make message loop
1754 //workable in this test class
Denny cy Leec5a7c292019-01-01 17:37:55 +08001755 float expectedfactor = mDeviceIdleController.getPreIdleTimeoutByMode(mode);
1756 float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor();
1757 assertEquals("Pre idle time factor of mode [" + mode + "].",
1758 expectedfactor, curfactor, delta);
1759 mDeviceIdleController.resetPreIdleTimeoutMode();
Denny cy Leec5a7c292019-01-01 17:37:55 +08001760
1761 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_INACTIVE);
1762 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_PENDING);
1763
1764 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_SENSING);
1765 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_LOCATING);
1766 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_QUICK_DOZE_DELAY);
1767 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_MAINTENANCE);
1768 checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE);
1769 checkMaybeDoAnImmediateMaintenance(expectedfactor);
1770 }
1771 float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor();
1772 assertEquals("Pre idle time factor of mode default.",
1773 1.0f, curfactor, delta);
1774 }
1775
Kweku Adams810c77d2019-08-28 07:45:00 -07001776 @Test
1777 public void testStationaryDetection_QuickDozeOff() {
1778 setQuickDozeEnabled(false);
1779 enterDeepState(STATE_IDLE);
1780 // Regular progression through states, so time should have increased appropriately.
1781 mInjector.nowElapsed += mConstants.IDLE_AFTER_INACTIVE_TIMEOUT + mConstants.SENSING_TIMEOUT
1782 + mConstants.LOCATING_TIMEOUT;
1783
1784 StationaryListenerForTest stationaryListener = new StationaryListenerForTest();
1785
1786 mDeviceIdleController.registerStationaryListener(stationaryListener);
1787
1788 // Go to IDLE_MAINTENANCE
1789 mDeviceIdleController.stepIdleStateLocked("testing");
1790
1791 // Back to IDLE
1792 mDeviceIdleController.stepIdleStateLocked("testing");
1793 assertTrue(stationaryListener.isStationary);
1794
1795 // Test motion
1796 stationaryListener.motionExpected = true;
1797 mDeviceIdleController.mMotionListener.onTrigger(null);
1798 assertFalse(stationaryListener.isStationary);
1799 }
1800
1801 @Test
Kweku Adamsa890d282019-12-19 16:08:43 -08001802 public void testStationaryDetection_QuickDozeOn_NoMotion() {
1803 // Short timeout for testing.
1804 mConstants.MOTION_INACTIVE_TIMEOUT = 6000L;
1805 doReturn(Sensor.REPORTING_MODE_ONE_SHOT).when(mMotionSensor).getReportingMode();
1806 doReturn(true).when(mSensorManager)
1807 .requestTriggerSensor(eq(mDeviceIdleController.mMotionListener), eq(mMotionSensor));
Kweku Adams810c77d2019-08-28 07:45:00 -07001808 setAlarmSoon(false);
1809 enterDeepState(STATE_QUICK_DOZE_DELAY);
1810 mDeviceIdleController.stepIdleStateLocked("testing");
1811 verifyStateConditions(STATE_IDLE);
1812 // Quick doze progression through states, so time should have increased appropriately.
1813 mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT;
Kweku Adamsa890d282019-12-19 16:08:43 -08001814 final ArgumentCaptor<AlarmManager.OnAlarmListener> motionAlarmListener = ArgumentCaptor
Kweku Adams810c77d2019-08-28 07:45:00 -07001815 .forClass(AlarmManager.OnAlarmListener.class);
Kweku Adamsa890d282019-12-19 16:08:43 -08001816 final ArgumentCaptor<AlarmManager.OnAlarmListener> motionRegistrationAlarmListener =
1817 ArgumentCaptor.forClass(AlarmManager.OnAlarmListener.class);
Kweku Adams810c77d2019-08-28 07:45:00 -07001818 doNothing().when(mAlarmManager).set(anyInt(), anyLong(), eq("DeviceIdleController.motion"),
Kweku Adamsa890d282019-12-19 16:08:43 -08001819 motionAlarmListener.capture(), any());
1820 doNothing().when(mAlarmManager).set(anyInt(), anyLong(),
1821 eq("DeviceIdleController.motion_registration"),
1822 motionRegistrationAlarmListener.capture(), any());
Kweku Adams810c77d2019-08-28 07:45:00 -07001823
1824 StationaryListenerForTest stationaryListener = new StationaryListenerForTest();
Kweku Adamsa890d282019-12-19 16:08:43 -08001825 spyOn(stationaryListener);
1826 InOrder inOrder = inOrder(stationaryListener);
Kweku Adams810c77d2019-08-28 07:45:00 -07001827
1828 stationaryListener.motionExpected = true;
1829 mDeviceIdleController.registerStationaryListener(stationaryListener);
Kweku Adamsa890d282019-12-19 16:08:43 -08001830 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1831 .onDeviceStationaryChanged(eq(false));
Kweku Adams810c77d2019-08-28 07:45:00 -07001832 assertFalse(stationaryListener.isStationary);
1833
1834 // Go to IDLE_MAINTENANCE
1835 mDeviceIdleController.stepIdleStateLocked("testing");
1836
1837 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2;
1838
1839 // Back to IDLE
1840 mDeviceIdleController.stepIdleStateLocked("testing");
1841
1842 // Now enough time has passed.
Kweku Adamsa890d282019-12-19 16:08:43 -08001843 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT;
Kweku Adams810c77d2019-08-28 07:45:00 -07001844 stationaryListener.motionExpected = false;
Kweku Adamsa890d282019-12-19 16:08:43 -08001845 motionAlarmListener.getValue().onAlarm();
1846 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1847 .onDeviceStationaryChanged(eq(true));
Kweku Adams810c77d2019-08-28 07:45:00 -07001848 assertTrue(stationaryListener.isStationary);
1849
1850 stationaryListener.motionExpected = true;
Kweku Adamsa890d282019-12-19 16:08:43 -08001851 mDeviceIdleController.mMotionListener.onTrigger(null);
1852 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1853 .onDeviceStationaryChanged(eq(false));
Kweku Adams810c77d2019-08-28 07:45:00 -07001854 assertFalse(stationaryListener.isStationary);
1855
1856 // Since we're in quick doze, the device shouldn't stop idling.
1857 verifyStateConditions(STATE_IDLE);
1858
1859 // Go to IDLE_MAINTENANCE
1860 mDeviceIdleController.stepIdleStateLocked("testing");
1861
Kweku Adamsa890d282019-12-19 16:08:43 -08001862 motionRegistrationAlarmListener.getValue().onAlarm();
Kweku Adams810c77d2019-08-28 07:45:00 -07001863 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2;
1864
1865 // Back to IDLE
Kweku Adamsa890d282019-12-19 16:08:43 -08001866 stationaryListener.motionExpected = false;
Kweku Adams810c77d2019-08-28 07:45:00 -07001867 mDeviceIdleController.stepIdleStateLocked("testing");
Kweku Adamsa890d282019-12-19 16:08:43 -08001868 verify(mSensorManager,
1869 timeout(mConstants.MOTION_INACTIVE_TIMEOUT).times(2))
1870 .requestTriggerSensor(eq(mDeviceIdleController.mMotionListener), eq(mMotionSensor));
Kweku Adams810c77d2019-08-28 07:45:00 -07001871
1872 // Now enough time has passed.
Kweku Adamsa890d282019-12-19 16:08:43 -08001873 mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT;
1874 motionAlarmListener.getValue().onAlarm();
1875 inOrder.verify(stationaryListener,
1876 timeout(mConstants.MOTION_INACTIVE_TIMEOUT).times(1))
1877 .onDeviceStationaryChanged(eq(true));
Kweku Adams810c77d2019-08-28 07:45:00 -07001878 assertTrue(stationaryListener.isStationary);
1879 }
1880
Kweku Adamsa890d282019-12-19 16:08:43 -08001881 @Test
1882 public void testStationaryDetection_QuickDozeOn_OneShot() {
1883 // Short timeout for testing.
1884 mConstants.MOTION_INACTIVE_TIMEOUT = 6000L;
1885 doReturn(Sensor.REPORTING_MODE_ONE_SHOT).when(mMotionSensor).getReportingMode();
1886 setAlarmSoon(false);
1887 enterDeepState(STATE_QUICK_DOZE_DELAY);
1888 mDeviceIdleController.stepIdleStateLocked("testing");
1889 verifyStateConditions(STATE_IDLE);
1890 // Quick doze progression through states, so time should have increased appropriately.
1891 mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT;
1892 final ArgumentCaptor<AlarmManager.OnAlarmListener> alarmListener = ArgumentCaptor
1893 .forClass(AlarmManager.OnAlarmListener.class);
1894 doNothing().when(mAlarmManager)
1895 .set(anyInt(), anyLong(), eq("DeviceIdleController.motion"), any(), any());
1896 doNothing().when(mAlarmManager).set(anyInt(), anyLong(),
1897 eq("DeviceIdleController.motion_registration"),
1898 alarmListener.capture(), any());
1899 ArgumentCaptor<TriggerEventListener> listenerCaptor =
1900 ArgumentCaptor.forClass(TriggerEventListener.class);
1901
1902 StationaryListenerForTest stationaryListener = new StationaryListenerForTest();
1903 spyOn(stationaryListener);
1904 InOrder inOrder = inOrder(stationaryListener, mSensorManager);
1905
1906 stationaryListener.motionExpected = true;
1907 mDeviceIdleController.registerStationaryListener(stationaryListener);
1908 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1909 .onDeviceStationaryChanged(eq(false));
1910 assertFalse(stationaryListener.isStationary);
1911 inOrder.verify(mSensorManager)
1912 .requestTriggerSensor(listenerCaptor.capture(), eq(mMotionSensor));
1913 final TriggerEventListener listener = listenerCaptor.getValue();
1914
1915 // Trigger motion
1916 listener.onTrigger(mock(TriggerEvent.class));
1917 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1918 .onDeviceStationaryChanged(eq(false));
1919
1920 // Make sure the listener is re-registered.
1921 alarmListener.getValue().onAlarm();
1922 inOrder.verify(mSensorManager).requestTriggerSensor(eq(listener), eq(mMotionSensor));
1923 }
1924
1925 @Test
1926 public void testStationaryDetection_QuickDozeOn_MultiShot() {
1927 // Short timeout for testing.
1928 mConstants.MOTION_INACTIVE_TIMEOUT = 6000L;
1929 doReturn(Sensor.REPORTING_MODE_CONTINUOUS).when(mMotionSensor).getReportingMode();
1930 setAlarmSoon(false);
1931 enterDeepState(STATE_QUICK_DOZE_DELAY);
1932 mDeviceIdleController.stepIdleStateLocked("testing");
1933 verifyStateConditions(STATE_IDLE);
1934 // Quick doze progression through states, so time should have increased appropriately.
1935 mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT;
1936 final ArgumentCaptor<AlarmManager.OnAlarmListener> alarmListener = ArgumentCaptor
1937 .forClass(AlarmManager.OnAlarmListener.class);
1938 doNothing().when(mAlarmManager)
1939 .set(anyInt(), anyLong(), eq("DeviceIdleController.motion"), any(), any());
1940 doNothing().when(mAlarmManager).set(anyInt(), anyLong(),
1941 eq("DeviceIdleController.motion_registration"),
1942 alarmListener.capture(), any());
1943 ArgumentCaptor<SensorEventListener> listenerCaptor =
1944 ArgumentCaptor.forClass(SensorEventListener.class);
1945
1946 StationaryListenerForTest stationaryListener = new StationaryListenerForTest();
1947 spyOn(stationaryListener);
1948 InOrder inOrder = inOrder(stationaryListener, mSensorManager);
1949
1950 stationaryListener.motionExpected = true;
1951 mDeviceIdleController.registerStationaryListener(stationaryListener);
1952 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1953 .onDeviceStationaryChanged(eq(false));
1954 assertFalse(stationaryListener.isStationary);
1955 inOrder.verify(mSensorManager)
1956 .registerListener(listenerCaptor.capture(), eq(mMotionSensor),
1957 eq(SensorManager.SENSOR_DELAY_NORMAL));
1958 final SensorEventListener listener = listenerCaptor.getValue();
1959
1960 // Trigger motion
1961 listener.onSensorChanged(mock(SensorEvent.class));
1962 inOrder.verify(stationaryListener, timeout(1000L).times(1))
1963 .onDeviceStationaryChanged(eq(false));
1964
1965 // Make sure the listener is re-registered.
1966 alarmListener.getValue().onAlarm();
1967 inOrder.verify(mSensorManager)
1968 .registerListener(eq(listener), eq(mMotionSensor),
1969 eq(SensorManager.SENSOR_DELAY_NORMAL));
1970 }
1971
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001972 private void enterDeepState(int state) {
1973 switch (state) {
1974 case STATE_ACTIVE:
1975 setScreenOn(true);
1976 mDeviceIdleController.becomeActiveLocked("testing", 0);
1977 break;
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001978 case STATE_QUICK_DOZE_DELAY:
1979 // Start off from ACTIVE in case we're already past the desired state.
1980 enterDeepState(STATE_ACTIVE);
1981 setQuickDozeEnabled(true);
1982 setScreenOn(false);
1983 setChargingOn(false);
1984 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
1985 break;
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001986 case STATE_LOCATING:
Kweku Adams799858b2018-10-08 17:19:08 -07001987 mInjector.locationManager = mLocationManager;
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001988 doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(
1989 anyString());
1990 // Fallthrough to step loop.
1991 case STATE_IDLE_PENDING:
1992 case STATE_SENSING:
1993 case STATE_IDLE:
1994 case STATE_IDLE_MAINTENANCE:
1995 // Make sure the controller doesn't think there's a wake-from-idle alarm coming
1996 // soon.
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001997 setAlarmSoon(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07001998 case STATE_INACTIVE:
Kweku Adamsb396ccf2018-09-17 16:37:15 -07001999 // Start off from ACTIVE in case we're already past the desired state.
2000 enterDeepState(STATE_ACTIVE);
2001 setQuickDozeEnabled(false);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07002002 setScreenOn(false);
2003 setChargingOn(false);
2004 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
Kweku Adamsa457f4e2018-10-03 15:56:06 -07002005 int count = 0;
2006 while (mDeviceIdleController.getState() != state) {
2007 // Stepping through each state ensures that the proper features are turned
2008 // on/off.
2009 mDeviceIdleController.stepIdleStateLocked("testing");
2010 count++;
2011 if (count > 10) {
Kweku Adams799858b2018-10-08 17:19:08 -07002012 fail("Infinite loop. Check test configuration. Currently at " +
2013 stateToString(mDeviceIdleController.getState()));
Kweku Adamsa457f4e2018-10-03 15:56:06 -07002014 }
2015 }
2016 break;
2017 default:
2018 fail("Unknown deep state " + stateToString(state));
2019 }
2020 }
2021
2022 private void enterLightState(int lightState) {
2023 switch (lightState) {
2024 case LIGHT_STATE_ACTIVE:
2025 setScreenOn(true);
2026 mDeviceIdleController.becomeActiveLocked("testing", 0);
2027 break;
2028 case LIGHT_STATE_INACTIVE:
2029 case LIGHT_STATE_IDLE:
2030 case LIGHT_STATE_IDLE_MAINTENANCE:
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002031 // Start off from ACTIVE in case we're already past the desired state.
2032 enterLightState(LIGHT_STATE_ACTIVE);
Kweku Adamsa457f4e2018-10-03 15:56:06 -07002033 setScreenOn(false);
2034 setChargingOn(false);
2035 int count = 0;
2036 mDeviceIdleController.becomeInactiveIfAppropriateLocked();
2037 while (mDeviceIdleController.getLightState() != lightState) {
2038 // Stepping through each state ensures that the proper features are turned
2039 // on/off.
2040 mDeviceIdleController.stepLightIdleStateLocked("testing");
2041
2042 count++;
2043 if (count > 10) {
Kweku Adams799858b2018-10-08 17:19:08 -07002044 fail("Infinite loop. Check test configuration. Currently at " +
2045 lightStateToString(mDeviceIdleController.getLightState()));
Kweku Adamsa457f4e2018-10-03 15:56:06 -07002046 }
2047 }
2048 break;
2049 case LIGHT_STATE_PRE_IDLE:
2050 case LIGHT_STATE_WAITING_FOR_NETWORK:
2051 case LIGHT_STATE_OVERRIDE:
2052 setScreenOn(false);
2053 setChargingOn(false);
2054 mDeviceIdleController.setLightStateForTest(lightState);
2055 break;
2056 default:
2057 fail("Unknown light state " + lightStateToString(lightState));
2058 }
2059 }
2060
Kweku Adams00e3a372018-09-28 16:57:09 -07002061 private void setChargingOn(boolean on) {
2062 mDeviceIdleController.updateChargingLocked(on);
2063 }
2064
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002065 private void setScreenLocked(boolean locked) {
2066 mDeviceIdleController.keyguardShowingLocked(locked);
2067 }
2068
Kweku Adams00e3a372018-09-28 16:57:09 -07002069 private void setScreenOn(boolean on) {
2070 doReturn(on).when(mPowerManager).isInteractive();
2071 mDeviceIdleController.updateInteractivityLocked();
2072 }
2073
Kweku Adams799858b2018-10-08 17:19:08 -07002074 private void setNetworkConnected(boolean connected) {
Makoto Onukifa8b0ba2019-10-07 16:49:04 -07002075 mInjector.connectivityManager = mConnectivityManager;
Kweku Adams799858b2018-10-08 17:19:08 -07002076 final NetworkInfo ani = mock(NetworkInfo.class);
2077 doReturn(connected).when(ani).isConnected();
Makoto Onukifa8b0ba2019-10-07 16:49:04 -07002078 doReturn(ani).when(mConnectivityManager).getActiveNetworkInfo();
Kweku Adams799858b2018-10-08 17:19:08 -07002079 mDeviceIdleController.updateConnectivityState(null);
2080 }
2081
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002082 private void setQuickDozeEnabled(boolean on) {
2083 mDeviceIdleController.updateQuickDozeFlagLocked(on);
2084 }
2085
2086 private void setAlarmSoon(boolean isSoon) {
2087 if (isSoon) {
Kweku Adams9da2bb92018-12-20 06:34:39 -08002088 doReturn(SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM / 2)
2089 .when(mAlarmManager).getNextWakeFromIdleTime();
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002090 } else {
2091 doReturn(Long.MAX_VALUE).when(mAlarmManager).getNextWakeFromIdleTime();
2092 }
2093 }
2094
Kweku Adams00e3a372018-09-28 16:57:09 -07002095 private void verifyStateConditions(int expectedState) {
2096 int curState = mDeviceIdleController.getState();
2097 assertEquals(
2098 "Expected " + stateToString(expectedState) + " but was " + stateToString(curState),
2099 expectedState, curState);
2100
2101 switch (expectedState) {
2102 case STATE_ACTIVE:
2103 assertFalse(mDeviceIdleController.mMotionListener.isActive());
2104 assertFalse(mAnyMotionDetector.isMonitoring);
2105 break;
2106 case STATE_INACTIVE:
2107 assertFalse(mDeviceIdleController.mMotionListener.isActive());
2108 assertFalse(mAnyMotionDetector.isMonitoring);
2109 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002110 assertFalse(mDeviceIdleController.isScreenOn()
2111 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002112 break;
2113 case STATE_IDLE_PENDING:
Robin Lee876b88542018-11-13 17:22:24 +01002114 assertEquals(
2115 mDeviceIdleController.hasMotionSensor(),
2116 mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07002117 assertFalse(mAnyMotionDetector.isMonitoring);
2118 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002119 assertFalse(mDeviceIdleController.isScreenOn()
2120 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002121 break;
2122 case STATE_SENSING:
Robin Lee876b88542018-11-13 17:22:24 +01002123 assertEquals(
2124 mDeviceIdleController.hasMotionSensor(),
2125 mDeviceIdleController.mMotionListener.isActive());
2126 assertEquals(
2127 mDeviceIdleController.hasMotionSensor(),
2128 mAnyMotionDetector.isMonitoring);
Kweku Adams00e3a372018-09-28 16:57:09 -07002129 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002130 assertFalse(mDeviceIdleController.isScreenOn()
2131 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002132 break;
2133 case STATE_LOCATING:
Robin Lee876b88542018-11-13 17:22:24 +01002134 assertEquals(
2135 mDeviceIdleController.hasMotionSensor(),
2136 mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07002137 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002138 assertFalse(mDeviceIdleController.isScreenOn()
2139 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002140 break;
2141 case STATE_IDLE:
Robin Lee876b88542018-11-13 17:22:24 +01002142 if (mDeviceIdleController.hasMotionSensor()) {
2143 assertTrue(mDeviceIdleController.mMotionListener.isActive()
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002144 // If quick doze is enabled, the motion listener should NOT be active.
2145 || mDeviceIdleController.isQuickDozeEnabled());
Robin Lee876b88542018-11-13 17:22:24 +01002146 }
Kweku Adams00e3a372018-09-28 16:57:09 -07002147 assertFalse(mAnyMotionDetector.isMonitoring);
2148 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002149 assertFalse(mDeviceIdleController.isScreenOn()
2150 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002151 // Light state should be OVERRIDE at this point.
2152 verifyLightStateConditions(LIGHT_STATE_OVERRIDE);
2153 break;
2154 case STATE_IDLE_MAINTENANCE:
Robin Lee876b88542018-11-13 17:22:24 +01002155 if (mDeviceIdleController.hasMotionSensor()) {
2156 assertTrue(mDeviceIdleController.mMotionListener.isActive()
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002157 // If quick doze is enabled, the motion listener should NOT be active.
2158 || mDeviceIdleController.isQuickDozeEnabled());
Robin Lee876b88542018-11-13 17:22:24 +01002159 }
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002160 assertFalse(mAnyMotionDetector.isMonitoring);
2161 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002162 assertFalse(mDeviceIdleController.isScreenOn()
2163 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adamsb396ccf2018-09-17 16:37:15 -07002164 break;
2165 case STATE_QUICK_DOZE_DELAY:
2166 // If quick doze is enabled, the motion listener should NOT be active.
2167 assertFalse(mDeviceIdleController.mMotionListener.isActive());
Kweku Adams00e3a372018-09-28 16:57:09 -07002168 assertFalse(mAnyMotionDetector.isMonitoring);
2169 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002170 assertFalse(mDeviceIdleController.isScreenOn()
2171 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002172 break;
2173 default:
2174 fail("Conditions for " + stateToString(expectedState) + " unknown.");
2175 }
2176 }
2177
2178 private void verifyLightStateConditions(int expectedLightState) {
2179 int curLightState = mDeviceIdleController.getLightState();
2180 assertEquals(
2181 "Expected " + lightStateToString(expectedLightState)
2182 + " but was " + lightStateToString(curLightState),
2183 expectedLightState, curLightState);
2184
2185 switch (expectedLightState) {
2186 case LIGHT_STATE_ACTIVE:
2187 assertTrue(
Kweku Adamsa457f4e2018-10-03 15:56:06 -07002188 mDeviceIdleController.isCharging() || mDeviceIdleController.isScreenOn()
2189 // Or there's an alarm coming up soon.
2190 || SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM
2191 > mAlarmManager.getNextWakeFromIdleTime());
Kweku Adams00e3a372018-09-28 16:57:09 -07002192 break;
2193 case LIGHT_STATE_INACTIVE:
2194 case LIGHT_STATE_PRE_IDLE:
2195 case LIGHT_STATE_IDLE:
2196 case LIGHT_STATE_WAITING_FOR_NETWORK:
2197 case LIGHT_STATE_IDLE_MAINTENANCE:
2198 case LIGHT_STATE_OVERRIDE:
2199 assertFalse(mDeviceIdleController.isCharging());
Kweku Adamsb7ce1902019-01-30 10:55:34 -08002200 assertFalse(mDeviceIdleController.isScreenOn()
2201 && !mDeviceIdleController.isKeyguardShowing());
Kweku Adams00e3a372018-09-28 16:57:09 -07002202 break;
2203 default:
2204 fail("Conditions for " + lightStateToString(expectedLightState) + " unknown.");
2205 }
2206 }
Denny cy Leec5a7c292019-01-01 17:37:55 +08002207
2208 private void checkNextAlarmTimeWithNewPreIdleFactor(float factor, int state) {
2209 final long errorTolerance = 1000;
2210 enterDeepState(state);
2211 long now = SystemClock.elapsedRealtime();
2212 long alarm = mDeviceIdleController.getNextAlarmTime();
2213 if (state == STATE_INACTIVE || state == STATE_IDLE_PENDING) {
2214 int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor);
2215 if (Float.compare(factor, 1.0f) == 0) {
2216 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
2217 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
2218 } else {
2219 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
2220 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
2221 }
2222 if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) {
Denny cy Leec5a7c292019-01-01 17:37:55 +08002223 long newAlarm = mDeviceIdleController.getNextAlarmTime();
2224 long newDelay = (long) ((alarm - now) * factor);
2225 assertTrue("setPreIdleTimeoutFactor: " + factor,
2226 Math.abs(newDelay - (newAlarm - now)) < errorTolerance);
2227 mDeviceIdleController.resetPreIdleTimeoutMode();
Denny cy Leec5a7c292019-01-01 17:37:55 +08002228 newAlarm = mDeviceIdleController.getNextAlarmTime();
2229 assertTrue("resetPreIdleTimeoutMode from: " + factor,
2230 Math.abs(newAlarm - alarm) < errorTolerance);
2231 mDeviceIdleController.setPreIdleTimeoutFactor(factor);
2232 now = SystemClock.elapsedRealtime();
2233 enterDeepState(state);
2234 newAlarm = mDeviceIdleController.getNextAlarmTime();
2235 assertTrue("setPreIdleTimeoutFactor: " + factor + " before step to idle",
2236 Math.abs(newDelay - (newAlarm - now)) < errorTolerance);
2237 mDeviceIdleController.resetPreIdleTimeoutMode();
Denny cy Leec5a7c292019-01-01 17:37:55 +08002238 }
2239 } else {
2240 mDeviceIdleController.setPreIdleTimeoutFactor(factor);
Denny cy Leec5a7c292019-01-01 17:37:55 +08002241 long newAlarm = mDeviceIdleController.getNextAlarmTime();
2242 assertTrue("setPreIdleTimeoutFactor: " + factor
2243 + " shounld not change next alarm" ,
2244 (newAlarm == alarm));
2245 mDeviceIdleController.resetPreIdleTimeoutMode();
Denny cy Leec5a7c292019-01-01 17:37:55 +08002246 }
2247 }
2248
2249 private void checkMaybeDoAnImmediateMaintenance(float factor) {
2250 int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor);
2251 final long minuteInMillis = 60 * 1000;
2252 if (Float.compare(factor, 1.0f) == 0) {
2253 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
2254 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret);
2255 } else {
2256 assertEquals("setPreIdleTimeoutMode: " + factor + " failed.",
2257 mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret);
2258 }
2259 if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) {
2260 enterDeepState(STATE_IDLE);
2261 long now = SystemClock.elapsedRealtime();
2262 long alarm = mDeviceIdleController.getNextAlarmTime();
2263 mDeviceIdleController.setIdleStartTimeForTest(
2264 now - (long) (mConstants.IDLE_TIMEOUT * 0.6));
Denny cy Leec5a7c292019-01-01 17:37:55 +08002265 long newAlarm = mDeviceIdleController.getNextAlarmTime();
2266 assertTrue("maintenance not reschedule IDLE_TIMEOUT * 0.6",
2267 newAlarm == alarm);
2268 mDeviceIdleController.setIdleStartTimeForTest(
2269 now - (long) (mConstants.IDLE_TIMEOUT * 1.2));
Denny cy Leec5a7c292019-01-01 17:37:55 +08002270 newAlarm = mDeviceIdleController.getNextAlarmTime();
2271 assertTrue("maintenance not reschedule IDLE_TIMEOUT * 1.2",
2272 (newAlarm - now) < minuteInMillis);
2273 mDeviceIdleController.resetPreIdleTimeoutMode();
Denny cy Leec5a7c292019-01-01 17:37:55 +08002274 }
2275 }
Kweku Adams00e3a372018-09-28 16:57:09 -07002276}