Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.server; |
| 17 | |
| 18 | import static androidx.test.InstrumentationRegistry.getContext; |
| 19 | |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 20 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 21 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing; |
| 22 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 23 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.inOrder; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 24 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; |
| 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; |
| 27 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; |
| 28 | import static com.android.server.DeviceIdleController.LIGHT_STATE_ACTIVE; |
| 29 | import static com.android.server.DeviceIdleController.LIGHT_STATE_IDLE; |
| 30 | import static com.android.server.DeviceIdleController.LIGHT_STATE_IDLE_MAINTENANCE; |
| 31 | import static com.android.server.DeviceIdleController.LIGHT_STATE_INACTIVE; |
| 32 | import static com.android.server.DeviceIdleController.LIGHT_STATE_OVERRIDE; |
| 33 | import static com.android.server.DeviceIdleController.LIGHT_STATE_PRE_IDLE; |
| 34 | import static com.android.server.DeviceIdleController.LIGHT_STATE_WAITING_FOR_NETWORK; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 35 | import static com.android.server.DeviceIdleController.MSG_REPORT_STATIONARY_STATUS; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 36 | import static com.android.server.DeviceIdleController.STATE_ACTIVE; |
| 37 | import static com.android.server.DeviceIdleController.STATE_IDLE; |
| 38 | import static com.android.server.DeviceIdleController.STATE_IDLE_MAINTENANCE; |
| 39 | import static com.android.server.DeviceIdleController.STATE_IDLE_PENDING; |
| 40 | import static com.android.server.DeviceIdleController.STATE_INACTIVE; |
| 41 | import static com.android.server.DeviceIdleController.STATE_LOCATING; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 42 | import static com.android.server.DeviceIdleController.STATE_QUICK_DOZE_DELAY; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 43 | import static com.android.server.DeviceIdleController.STATE_SENSING; |
| 44 | import static com.android.server.DeviceIdleController.lightStateToString; |
| 45 | import static com.android.server.DeviceIdleController.stateToString; |
| 46 | |
| 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertFalse; |
| 49 | import static org.junit.Assert.assertTrue; |
| 50 | import static org.junit.Assert.fail; |
| 51 | import static org.mockito.ArgumentMatchers.any; |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 52 | import static org.mockito.ArgumentMatchers.anyBoolean; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 53 | import static org.mockito.ArgumentMatchers.anyInt; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 54 | import static org.mockito.ArgumentMatchers.anyLong; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 55 | import static org.mockito.ArgumentMatchers.anyString; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 56 | import static org.mockito.ArgumentMatchers.argThat; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 57 | import static org.mockito.ArgumentMatchers.eq; |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 58 | import static org.mockito.Mockito.never; |
| 59 | import static org.mockito.Mockito.reset; |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 60 | import static org.mockito.Mockito.timeout; |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 61 | import static org.mockito.Mockito.verify; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 62 | |
| 63 | import android.app.ActivityManagerInternal; |
| 64 | import android.app.AlarmManager; |
| 65 | import android.app.IActivityManager; |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 66 | import android.content.ContentResolver; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 67 | import android.content.Context; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 68 | import android.content.Intent; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 69 | import android.hardware.Sensor; |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 70 | import android.hardware.SensorEvent; |
| 71 | import android.hardware.SensorEventListener; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 72 | import android.hardware.SensorManager; |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 73 | import android.hardware.TriggerEvent; |
| 74 | import android.hardware.TriggerEventListener; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 75 | import android.location.LocationManager; |
| 76 | import android.location.LocationProvider; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 77 | import android.net.ConnectivityManager; |
| 78 | import android.net.NetworkInfo; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 79 | import android.os.Handler; |
| 80 | import android.os.Looper; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 81 | import android.os.Message; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 82 | import android.os.PowerManager; |
| 83 | import android.os.PowerManagerInternal; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 84 | import android.os.PowerSaveState; |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 85 | import android.os.SystemClock; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 86 | |
| 87 | import androidx.test.runner.AndroidJUnit4; |
| 88 | |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 89 | import com.android.server.deviceidle.ConstraintController; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 90 | import com.android.server.net.NetworkPolicyManagerInternal; |
| 91 | import com.android.server.wm.ActivityTaskManagerInternal; |
| 92 | |
| 93 | import org.junit.After; |
| 94 | import org.junit.Before; |
| 95 | import org.junit.Test; |
| 96 | import org.junit.runner.RunWith; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 97 | import org.mockito.ArgumentCaptor; |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 98 | import org.mockito.InOrder; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 99 | import org.mockito.Mock; |
| 100 | import org.mockito.MockitoSession; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 101 | import org.mockito.invocation.InvocationOnMock; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 102 | import org.mockito.quality.Strictness; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 103 | import org.mockito.stubbing.Answer; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Tests for {@link com.android.server.DeviceIdleController}. |
| 107 | */ |
| 108 | @RunWith(AndroidJUnit4.class) |
| 109 | public class DeviceIdleControllerTest { |
| 110 | private DeviceIdleController mDeviceIdleController; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 111 | private DeviceIdleController.MyHandler mHandler; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 112 | private AnyMotionDetectorForTest mAnyMotionDetector; |
| 113 | private AppStateTrackerForTest mAppStateTracker; |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 114 | private DeviceIdleController.Constants mConstants; |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 115 | private InjectorForTest mInjector; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 116 | |
| 117 | private MockitoSession mMockingSession; |
| 118 | @Mock |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 119 | private AlarmManager mAlarmManager; |
| 120 | @Mock |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 121 | private ConnectivityService mConnectivityService; |
| 122 | @Mock |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 123 | private ContentResolver mContentResolver; |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 124 | @Mock |
| 125 | private IActivityManager mIActivityManager; |
| 126 | @Mock |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 127 | private LocationManager mLocationManager; |
| 128 | @Mock |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 129 | private PowerManager mPowerManager; |
| 130 | @Mock |
| 131 | private PowerManager.WakeLock mWakeLock; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 132 | @Mock |
| 133 | private PowerManagerInternal mPowerManagerInternal; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 134 | @Mock |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 135 | private Sensor mMotionSensor; |
| 136 | @Mock |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 137 | private SensorManager mSensorManager; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 138 | |
| 139 | class InjectorForTest extends DeviceIdleController.Injector { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 140 | ConnectivityService connectivityService; |
| 141 | LocationManager locationManager; |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 142 | ConstraintController constraintController; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 143 | // Freeze time for testing. |
| 144 | long nowElapsed; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 145 | |
| 146 | InjectorForTest(Context ctx) { |
| 147 | super(ctx); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | AlarmManager getAlarmManager() { |
| 152 | return mAlarmManager; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | AnyMotionDetector getAnyMotionDetector(Handler handler, SensorManager sm, |
| 157 | AnyMotionDetector.DeviceIdleCallback callback, float angleThreshold) { |
| 158 | return mAnyMotionDetector; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | AppStateTracker getAppStateTracker(Context ctx, Looper loop) { |
| 163 | return mAppStateTracker; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | ConnectivityService getConnectivityService() { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 168 | return connectivityService; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 172 | long getElapsedRealtime() { |
| 173 | return nowElapsed; |
| 174 | } |
| 175 | |
| 176 | @Override |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 177 | LocationManager getLocationManager() { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 178 | return locationManager; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 182 | DeviceIdleController.MyHandler getHandler(DeviceIdleController controller) { |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 183 | if (mHandler == null) { |
| 184 | mHandler = controller.new MyHandler(getContext().getMainLooper()); |
| 185 | spyOn(mHandler); |
| 186 | doNothing().when(mHandler).handleMessage(argThat((message) -> |
| 187 | message.what != MSG_REPORT_STATIONARY_STATUS)); |
| 188 | doAnswer(new Answer<Boolean>() { |
| 189 | @Override |
| 190 | public Boolean answer(InvocationOnMock invocation) throws Throwable { |
| 191 | Message msg = invocation.getArgument(0); |
| 192 | mHandler.handleMessage(msg); |
| 193 | return true; |
| 194 | } |
| 195 | }).when(mHandler).sendMessageDelayed( |
| 196 | argThat((message) -> message.what == MSG_REPORT_STATIONARY_STATUS), |
| 197 | anyLong()); |
| 198 | } |
| 199 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 200 | return mHandler; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 204 | Sensor getMotionSensor() { |
| 205 | return mMotionSensor; |
| 206 | } |
| 207 | |
| 208 | @Override |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 209 | PowerManager getPowerManager() { |
| 210 | return mPowerManager; |
| 211 | } |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 212 | |
| 213 | @Override |
| 214 | SensorManager getSensorManager() { |
| 215 | return mSensorManager; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | ConstraintController getConstraintController( |
| 220 | Handler handler, DeviceIdleController.LocalService localService) { |
| 221 | return constraintController; |
| 222 | } |
| 223 | |
| 224 | @Override |
| 225 | boolean useMotionSensor() { |
| 226 | return true; |
| 227 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | private class AnyMotionDetectorForTest extends AnyMotionDetector { |
| 231 | boolean isMonitoring = false; |
| 232 | |
| 233 | AnyMotionDetectorForTest() { |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 234 | super(mPowerManager, mock(Handler.class), mSensorManager, |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 235 | mock(DeviceIdleCallback.class), 0.5f); |
| 236 | } |
| 237 | |
| 238 | @Override |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 239 | public boolean hasSensor() { |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | @Override |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 244 | public void checkForAnyMotion() { |
| 245 | isMonitoring = true; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void stop() { |
| 250 | isMonitoring = false; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | private class AppStateTrackerForTest extends AppStateTracker { |
| 255 | AppStateTrackerForTest(Context ctx, Looper looper) { |
| 256 | super(ctx, looper); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void onSystemServicesReady() { |
| 261 | // Do nothing. |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | IActivityManager injectIActivityManager() { |
| 266 | return mIActivityManager; |
| 267 | } |
| 268 | } |
| 269 | |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 270 | private class StationaryListenerForTest implements DeviceIdleController.StationaryListener { |
| 271 | boolean motionExpected = false; |
| 272 | boolean isStationary = false; |
| 273 | |
| 274 | @Override |
| 275 | public void onDeviceStationaryChanged(boolean isStationary) { |
| 276 | if (isStationary == motionExpected) { |
| 277 | fail("Unexpected device stationary status: " + isStationary); |
| 278 | } |
| 279 | this.isStationary = isStationary; |
| 280 | } |
| 281 | } |
| 282 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 283 | @Before |
| 284 | public void setUp() { |
| 285 | mMockingSession = mockitoSession() |
| 286 | .initMocks(this) |
| 287 | .strictness(Strictness.LENIENT) |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 288 | .spyStatic(LocalServices.class) |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 289 | .startMocking(); |
Robin Lee | 54d4496 | 2018-12-17 17:43:57 +0100 | [diff] [blame] | 290 | spyOn(getContext()); |
| 291 | doReturn(null).when(getContext()).registerReceiver(any(), any()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 292 | doReturn(mock(ActivityManagerInternal.class)) |
| 293 | .when(() -> LocalServices.getService(ActivityManagerInternal.class)); |
| 294 | doReturn(mock(ActivityTaskManagerInternal.class)) |
| 295 | .when(() -> LocalServices.getService(ActivityTaskManagerInternal.class)); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 296 | doReturn(mock(AlarmManagerInternal.class)) |
| 297 | .when(() -> LocalServices.getService(AlarmManagerInternal.class)); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 298 | doReturn(mPowerManagerInternal) |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 299 | .when(() -> LocalServices.getService(PowerManagerInternal.class)); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 300 | when(mPowerManagerInternal.getLowPowerState(anyInt())) |
| 301 | .thenReturn(mock(PowerSaveState.class)); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 302 | doReturn(mock(NetworkPolicyManagerInternal.class)) |
| 303 | .when(() -> LocalServices.getService(NetworkPolicyManagerInternal.class)); |
| 304 | when(mPowerManager.newWakeLock(anyInt(), anyString())).thenReturn(mWakeLock); |
| 305 | doNothing().when(mWakeLock).acquire(); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 306 | doNothing().when(mAlarmManager).set(anyInt(), anyLong(), anyString(), any(), any()); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 307 | doReturn(mock(Sensor.class)).when(mSensorManager) |
| 308 | .getDefaultSensor(eq(Sensor.TYPE_SIGNIFICANT_MOTION), eq(true)); |
| 309 | doReturn(true).when(mSensorManager).registerListener(any(), any(), anyInt()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 310 | mAppStateTracker = new AppStateTrackerForTest(getContext(), Looper.getMainLooper()); |
| 311 | mAnyMotionDetector = new AnyMotionDetectorForTest(); |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 312 | mInjector = new InjectorForTest(getContext()); |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 313 | doNothing().when(mContentResolver).registerContentObserver(any(), anyBoolean(), any()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 314 | |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 315 | mDeviceIdleController = new DeviceIdleController(getContext(), mInjector); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 316 | spyOn(mDeviceIdleController); |
| 317 | doNothing().when(mDeviceIdleController).publishBinderService(any(), any()); |
| 318 | mDeviceIdleController.onStart(); |
| 319 | mDeviceIdleController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); |
| 320 | mDeviceIdleController.setDeepEnabledForTest(true); |
| 321 | mDeviceIdleController.setLightEnabledForTest(true); |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 322 | |
| 323 | // Get the same Constants object that mDeviceIdleController got. |
| 324 | mConstants = mInjector.getConstants(mDeviceIdleController, |
| 325 | mInjector.getHandler(mDeviceIdleController), mContentResolver); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @After |
| 329 | public void tearDown() { |
| 330 | if (mMockingSession != null) { |
| 331 | mMockingSession.finishMocking(); |
| 332 | } |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 333 | // DeviceIdleController adds these to LocalServices in the constructor, so we have to remove |
| 334 | // them after each test, otherwise, subsequent tests will fail. |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 335 | LocalServices.removeServiceForTest(AppStateTracker.class); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 336 | LocalServices.removeServiceForTest(DeviceIdleController.LocalService.class); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Test |
| 340 | public void testUpdateInteractivityLocked() { |
| 341 | doReturn(false).when(mPowerManager).isInteractive(); |
| 342 | mDeviceIdleController.updateInteractivityLocked(); |
| 343 | assertFalse(mDeviceIdleController.isScreenOn()); |
| 344 | |
| 345 | // Make sure setting false when screen is already off doesn't change anything. |
| 346 | doReturn(false).when(mPowerManager).isInteractive(); |
| 347 | mDeviceIdleController.updateInteractivityLocked(); |
| 348 | assertFalse(mDeviceIdleController.isScreenOn()); |
| 349 | |
| 350 | // Test changing from screen off to screen on. |
| 351 | doReturn(true).when(mPowerManager).isInteractive(); |
| 352 | mDeviceIdleController.updateInteractivityLocked(); |
| 353 | assertTrue(mDeviceIdleController.isScreenOn()); |
| 354 | |
| 355 | // Make sure setting true when screen is already on doesn't change anything. |
| 356 | doReturn(true).when(mPowerManager).isInteractive(); |
| 357 | mDeviceIdleController.updateInteractivityLocked(); |
| 358 | assertTrue(mDeviceIdleController.isScreenOn()); |
| 359 | |
| 360 | // Test changing from screen on to screen off. |
| 361 | doReturn(false).when(mPowerManager).isInteractive(); |
| 362 | mDeviceIdleController.updateInteractivityLocked(); |
| 363 | assertFalse(mDeviceIdleController.isScreenOn()); |
| 364 | } |
| 365 | |
| 366 | @Test |
| 367 | public void testUpdateChargingLocked() { |
| 368 | mDeviceIdleController.updateChargingLocked(false); |
| 369 | assertFalse(mDeviceIdleController.isCharging()); |
| 370 | |
| 371 | // Make sure setting false when charging is already off doesn't change anything. |
| 372 | mDeviceIdleController.updateChargingLocked(false); |
| 373 | assertFalse(mDeviceIdleController.isCharging()); |
| 374 | |
| 375 | // Test changing from charging off to charging on. |
| 376 | mDeviceIdleController.updateChargingLocked(true); |
| 377 | assertTrue(mDeviceIdleController.isCharging()); |
| 378 | |
| 379 | // Make sure setting true when charging is already on doesn't change anything. |
| 380 | mDeviceIdleController.updateChargingLocked(true); |
| 381 | assertTrue(mDeviceIdleController.isCharging()); |
| 382 | |
| 383 | // Test changing from charging on to charging off. |
| 384 | mDeviceIdleController.updateChargingLocked(false); |
| 385 | assertFalse(mDeviceIdleController.isCharging()); |
| 386 | } |
| 387 | |
| 388 | @Test |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 389 | public void testUpdateConnectivityState() { |
| 390 | // No connectivity service |
| 391 | final boolean isConnected = mDeviceIdleController.isNetworkConnected(); |
| 392 | mInjector.connectivityService = null; |
| 393 | mDeviceIdleController.updateConnectivityState(null); |
| 394 | assertEquals(isConnected, mDeviceIdleController.isNetworkConnected()); |
| 395 | |
| 396 | // No active network info |
| 397 | mInjector.connectivityService = mConnectivityService; |
| 398 | doReturn(null).when(mConnectivityService).getActiveNetworkInfo(); |
| 399 | mDeviceIdleController.updateConnectivityState(null); |
| 400 | assertFalse(mDeviceIdleController.isNetworkConnected()); |
| 401 | |
| 402 | // Active network info says connected. |
| 403 | final NetworkInfo ani = mock(NetworkInfo.class); |
| 404 | doReturn(ani).when(mConnectivityService).getActiveNetworkInfo(); |
| 405 | doReturn(true).when(ani).isConnected(); |
| 406 | mDeviceIdleController.updateConnectivityState(null); |
| 407 | assertTrue(mDeviceIdleController.isNetworkConnected()); |
| 408 | |
| 409 | // Active network info says not connected. |
| 410 | doReturn(false).when(ani).isConnected(); |
| 411 | mDeviceIdleController.updateConnectivityState(null); |
| 412 | assertFalse(mDeviceIdleController.isNetworkConnected()); |
| 413 | |
| 414 | // Wrong intent passed (false). |
| 415 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 416 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3); |
| 417 | doReturn(true).when(ani).isConnected(); |
| 418 | doReturn(1).when(ani).getType(); |
| 419 | mDeviceIdleController.updateConnectivityState(intent); |
| 420 | // Wrong intent means we shouldn't update the connected state. |
| 421 | assertFalse(mDeviceIdleController.isNetworkConnected()); |
| 422 | |
| 423 | // Intent says connected. |
| 424 | doReturn(1).when(ani).getType(); |
| 425 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1); |
| 426 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); |
| 427 | mDeviceIdleController.updateConnectivityState(intent); |
| 428 | assertTrue(mDeviceIdleController.isNetworkConnected()); |
| 429 | |
| 430 | // Wrong intent passed (true). |
| 431 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 3); |
| 432 | // Wrong intent means we shouldn't update the connected state. |
| 433 | assertTrue(mDeviceIdleController.isNetworkConnected()); |
| 434 | |
| 435 | // Intent says not connected. |
| 436 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, 1); |
| 437 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 438 | mDeviceIdleController.updateConnectivityState(intent); |
| 439 | assertFalse(mDeviceIdleController.isNetworkConnected()); |
| 440 | } |
| 441 | |
| 442 | @Test |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 443 | public void testUpdateQuickDozeFlagLocked() { |
| 444 | mDeviceIdleController.updateQuickDozeFlagLocked(false); |
| 445 | assertFalse(mDeviceIdleController.isQuickDozeEnabled()); |
| 446 | |
| 447 | // Make sure setting false when quick doze is already off doesn't change anything. |
| 448 | mDeviceIdleController.updateQuickDozeFlagLocked(false); |
| 449 | assertFalse(mDeviceIdleController.isQuickDozeEnabled()); |
| 450 | |
| 451 | // Test changing from quick doze off to quick doze on. |
| 452 | mDeviceIdleController.updateQuickDozeFlagLocked(true); |
| 453 | assertTrue(mDeviceIdleController.isQuickDozeEnabled()); |
| 454 | |
| 455 | // Make sure setting true when quick doze is already on doesn't change anything. |
| 456 | mDeviceIdleController.updateQuickDozeFlagLocked(true); |
| 457 | assertTrue(mDeviceIdleController.isQuickDozeEnabled()); |
| 458 | |
| 459 | // Test changing from quick doze on to quick doze off. |
| 460 | mDeviceIdleController.updateQuickDozeFlagLocked(false); |
| 461 | assertFalse(mDeviceIdleController.isQuickDozeEnabled()); |
| 462 | } |
| 463 | |
| 464 | @Test |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 465 | public void testStateActiveToStateInactive_ConditionsNotMet() { |
| 466 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 467 | verifyStateConditions(STATE_ACTIVE); |
| 468 | |
| 469 | // State should stay ACTIVE with screen on and charging. |
| 470 | setChargingOn(true); |
| 471 | setScreenOn(true); |
| 472 | |
| 473 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 474 | verifyStateConditions(STATE_ACTIVE); |
| 475 | |
| 476 | // State should stay ACTIVE with charging on. |
| 477 | setChargingOn(true); |
| 478 | setScreenOn(false); |
| 479 | |
| 480 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 481 | verifyStateConditions(STATE_ACTIVE); |
| 482 | |
| 483 | // State should stay ACTIVE with screen on. |
| 484 | // Note the different operation order here makes sure the state doesn't change before test. |
| 485 | setScreenOn(true); |
| 486 | setChargingOn(false); |
| 487 | |
| 488 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 489 | verifyStateConditions(STATE_ACTIVE); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 490 | |
| 491 | mConstants.WAIT_FOR_UNLOCK = false; |
| 492 | setScreenLocked(true); |
| 493 | setScreenOn(true); |
| 494 | setChargingOn(false); |
| 495 | |
| 496 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 497 | verifyStateConditions(STATE_ACTIVE); |
| 498 | |
| 499 | setScreenLocked(false); |
| 500 | setScreenOn(true); |
| 501 | setChargingOn(false); |
| 502 | |
| 503 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 504 | verifyStateConditions(STATE_ACTIVE); |
| 505 | |
| 506 | mConstants.WAIT_FOR_UNLOCK = true; |
| 507 | setScreenLocked(false); |
| 508 | setScreenOn(true); |
| 509 | setChargingOn(false); |
| 510 | |
| 511 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 512 | verifyStateConditions(STATE_ACTIVE); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | @Test |
| 516 | public void testLightStateActiveToLightStateInactive_ConditionsNotMet() { |
| 517 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 518 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 519 | |
| 520 | // State should stay ACTIVE with screen on and charging. |
| 521 | setChargingOn(true); |
| 522 | setScreenOn(true); |
| 523 | |
| 524 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 525 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 526 | |
| 527 | // State should stay ACTIVE with charging on. |
| 528 | setChargingOn(true); |
| 529 | setScreenOn(false); |
| 530 | |
| 531 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 532 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 533 | |
| 534 | // State should stay ACTIVE with screen on. |
| 535 | // Note the different operation order here makes sure the state doesn't change before test. |
| 536 | setScreenOn(true); |
| 537 | setChargingOn(false); |
| 538 | |
| 539 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 540 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 541 | } |
| 542 | |
| 543 | @Test |
| 544 | public void testStateActiveToStateInactive_ConditionsMet() { |
| 545 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 546 | verifyStateConditions(STATE_ACTIVE); |
| 547 | |
| 548 | setChargingOn(false); |
| 549 | setScreenOn(false); |
| 550 | |
| 551 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 552 | verifyStateConditions(STATE_INACTIVE); |
| 553 | } |
| 554 | |
| 555 | @Test |
| 556 | public void testLightStateActiveToLightStateInactive_ConditionsMet() { |
| 557 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 558 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 559 | |
| 560 | setChargingOn(false); |
| 561 | setScreenOn(false); |
| 562 | |
| 563 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 564 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 565 | } |
| 566 | |
| 567 | @Test |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 568 | public void testTransitionFromAnyStateToStateQuickDozeDelay() { |
| 569 | enterDeepState(STATE_ACTIVE); |
| 570 | setQuickDozeEnabled(true); |
| 571 | setChargingOn(false); |
| 572 | setScreenOn(false); |
| 573 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 574 | |
| 575 | enterDeepState(STATE_INACTIVE); |
| 576 | setQuickDozeEnabled(true); |
| 577 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 578 | |
| 579 | enterDeepState(STATE_IDLE_PENDING); |
| 580 | setQuickDozeEnabled(true); |
| 581 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 582 | |
| 583 | enterDeepState(STATE_SENSING); |
| 584 | setQuickDozeEnabled(true); |
| 585 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 586 | |
| 587 | enterDeepState(STATE_LOCATING); |
| 588 | setQuickDozeEnabled(true); |
| 589 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 590 | |
| 591 | // IDLE should stay as IDLE. |
| 592 | enterDeepState(STATE_IDLE); |
| 593 | setQuickDozeEnabled(true); |
| 594 | verifyStateConditions(STATE_IDLE); |
| 595 | |
| 596 | // IDLE_MAINTENANCE should stay as IDLE_MAINTENANCE. |
| 597 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 598 | setQuickDozeEnabled(true); |
| 599 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 600 | |
| 601 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 602 | setQuickDozeEnabled(true); |
| 603 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 604 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 605 | } |
| 606 | |
| 607 | @Test |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 608 | public void testStepIdleStateLocked_InvalidStates() { |
| 609 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 610 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 611 | // mDeviceIdleController.stepIdleStateLocked doesn't handle the ACTIVE case, so the state |
| 612 | // should stay as ACTIVE. |
| 613 | verifyStateConditions(STATE_ACTIVE); |
| 614 | } |
| 615 | |
| 616 | @Test |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 617 | public void testStepIdleStateLocked_ValidStates_QuickDoze() { |
| 618 | setAlarmSoon(false); |
| 619 | |
| 620 | // Quick doze should go directly into IDLE. |
| 621 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 622 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 623 | verifyStateConditions(STATE_IDLE); |
| 624 | |
| 625 | // Should just alternate between IDLE and IDLE_MAINTENANCE now. |
| 626 | |
| 627 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 628 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 629 | |
| 630 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 631 | verifyStateConditions(STATE_IDLE); |
| 632 | |
| 633 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 634 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 635 | } |
| 636 | |
| 637 | @Test |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 638 | public void testStepIdleStateLocked_ValidStates_WithWakeFromIdleAlarmSoon() { |
| 639 | enterDeepState(STATE_ACTIVE); |
| 640 | // Return that there's an alarm coming soon. |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 641 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 642 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 643 | verifyStateConditions(STATE_ACTIVE); |
| 644 | |
| 645 | // Everything besides ACTIVE should end up as INACTIVE since the screen would be off. |
| 646 | |
| 647 | enterDeepState(STATE_INACTIVE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 648 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 649 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 650 | verifyStateConditions(STATE_INACTIVE); |
| 651 | |
| 652 | enterDeepState(STATE_IDLE_PENDING); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 653 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 654 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 655 | verifyStateConditions(STATE_INACTIVE); |
| 656 | |
| 657 | enterDeepState(STATE_SENSING); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 658 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 659 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 660 | verifyStateConditions(STATE_INACTIVE); |
| 661 | |
| 662 | enterDeepState(STATE_LOCATING); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 663 | setAlarmSoon(true); |
| 664 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 665 | verifyStateConditions(STATE_INACTIVE); |
| 666 | |
| 667 | // With quick doze enabled, we should end up in QUICK_DOZE_DELAY instead of INACTIVE. |
| 668 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 669 | setQuickDozeEnabled(true); |
| 670 | setAlarmSoon(true); |
| 671 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 672 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 673 | |
| 674 | // With quick doze disabled, we should end up in INACTIVE instead of QUICK_DOZE_DELAY. |
| 675 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 676 | setQuickDozeEnabled(false); |
| 677 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 678 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 679 | verifyStateConditions(STATE_INACTIVE); |
| 680 | |
| 681 | enterDeepState(STATE_IDLE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 682 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 683 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 684 | verifyStateConditions(STATE_INACTIVE); |
| 685 | |
| 686 | enterDeepState(STATE_IDLE_MAINTENANCE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 687 | setAlarmSoon(true); |
Kweku Adams | bf86938 | 2018-10-09 18:26:56 -0700 | [diff] [blame] | 688 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 689 | verifyStateConditions(STATE_INACTIVE); |
| 690 | } |
| 691 | |
| 692 | @Test |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 693 | public void testStepIdleStateLocked_ValidStates_NoLocationManager() { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 694 | mInjector.locationManager = null; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 695 | // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon. |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 696 | setAlarmSoon(false); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 697 | // Set state to INACTIVE. |
| 698 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 699 | setChargingOn(false); |
| 700 | setScreenOn(false); |
| 701 | verifyStateConditions(STATE_INACTIVE); |
| 702 | |
| 703 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 704 | verifyStateConditions(STATE_IDLE_PENDING); |
| 705 | |
| 706 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 707 | verifyStateConditions(STATE_SENSING); |
| 708 | |
| 709 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 710 | // No location manager, so SENSING should go straight to IDLE. |
| 711 | verifyStateConditions(STATE_IDLE); |
| 712 | |
| 713 | // Should just alternate between IDLE and IDLE_MAINTENANCE now. |
| 714 | |
| 715 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 716 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 717 | |
| 718 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 719 | verifyStateConditions(STATE_IDLE); |
| 720 | |
| 721 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 722 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 723 | } |
| 724 | |
| 725 | @Test |
| 726 | public void testStepIdleStateLocked_ValidStates_WithLocationManager_NoProviders() { |
| 727 | // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon. |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 728 | setAlarmSoon(false); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 729 | // Set state to INACTIVE. |
| 730 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 731 | setChargingOn(false); |
| 732 | setScreenOn(false); |
| 733 | verifyStateConditions(STATE_INACTIVE); |
| 734 | |
| 735 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 736 | verifyStateConditions(STATE_IDLE_PENDING); |
| 737 | |
| 738 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 739 | verifyStateConditions(STATE_SENSING); |
| 740 | |
| 741 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 742 | // Location manager exists but there isn't a network or GPS provider, |
| 743 | // so SENSING should go straight to IDLE. |
| 744 | verifyStateConditions(STATE_IDLE); |
| 745 | |
| 746 | // Should just alternate between IDLE and IDLE_MAINTENANCE now. |
| 747 | |
| 748 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 749 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 750 | |
| 751 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 752 | verifyStateConditions(STATE_IDLE); |
| 753 | |
| 754 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 755 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 756 | } |
| 757 | |
| 758 | @Test |
| 759 | public void testStepIdleStateLocked_ValidStates_WithLocationManager_WithProviders() { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 760 | mInjector.locationManager = mLocationManager; |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 761 | doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider(anyString()); |
| 762 | // Make sure the controller doesn't think there's a wake-from-idle alarm coming soon. |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 763 | setAlarmSoon(false); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 764 | // Set state to INACTIVE. |
| 765 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 766 | setChargingOn(false); |
| 767 | setScreenOn(false); |
| 768 | verifyStateConditions(STATE_INACTIVE); |
| 769 | |
| 770 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 771 | verifyStateConditions(STATE_IDLE_PENDING); |
| 772 | |
| 773 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 774 | verifyStateConditions(STATE_SENSING); |
| 775 | |
| 776 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 777 | // Location manager exists with a provider, so SENSING should go to LOCATING. |
| 778 | verifyStateConditions(STATE_LOCATING); |
| 779 | |
| 780 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 781 | verifyStateConditions(STATE_IDLE); |
| 782 | |
| 783 | // Should just alternate between IDLE and IDLE_MAINTENANCE now. |
| 784 | |
| 785 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 786 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 787 | |
| 788 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 789 | verifyStateConditions(STATE_IDLE); |
| 790 | |
| 791 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 792 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
| 793 | } |
| 794 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 795 | @Test |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 796 | public void testLightStepIdleStateLocked_InvalidStates() { |
| 797 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 798 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 799 | // stepLightIdleStateLocked doesn't handle the ACTIVE case, so the state |
| 800 | // should stay as ACTIVE. |
| 801 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Make sure stepLightIdleStateLocked doesn't change state when the state is |
| 806 | * LIGHT_STATE_OVERRIDE. |
| 807 | */ |
| 808 | @Test |
| 809 | public void testLightStepIdleStateLocked_Overriden() { |
| 810 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 811 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 812 | verifyLightStateConditions(LIGHT_STATE_OVERRIDE); |
| 813 | } |
| 814 | |
| 815 | @Test |
| 816 | public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NetworkConnected() { |
| 817 | setNetworkConnected(true); |
| 818 | mDeviceIdleController.setJobsActive(false); |
| 819 | mDeviceIdleController.setAlarmsActive(false); |
| 820 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 821 | |
| 822 | // Set state to INACTIVE. |
| 823 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 824 | setChargingOn(false); |
| 825 | setScreenOn(false); |
| 826 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 827 | |
| 828 | // No active ops means INACTIVE should go straight to IDLE. |
| 829 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 830 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 831 | |
| 832 | // Should just alternate between IDLE and IDLE_MAINTENANCE now. |
| 833 | |
| 834 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 835 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 836 | |
| 837 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 838 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 839 | |
| 840 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 841 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 842 | } |
| 843 | |
| 844 | @Test |
| 845 | public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NetworkConnected() { |
| 846 | setNetworkConnected(true); |
| 847 | // Set state to INACTIVE. |
| 848 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 849 | setChargingOn(false); |
| 850 | setScreenOn(false); |
| 851 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 852 | |
| 853 | // Active ops means INACTIVE should go to PRE_IDLE to wait. |
| 854 | mDeviceIdleController.setJobsActive(true); |
| 855 | mDeviceIdleController.setAlarmsActive(true); |
| 856 | mDeviceIdleController.setActiveIdleOpsForTest(1); |
| 857 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 858 | verifyLightStateConditions(LIGHT_STATE_PRE_IDLE); |
| 859 | |
| 860 | // Even with active ops, PRE_IDLE should go to IDLE. |
| 861 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 862 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 863 | |
| 864 | // Should just alternate between IDLE and IDLE_MAINTENANCE now. |
| 865 | |
| 866 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 867 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 868 | |
| 869 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 870 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 871 | |
| 872 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 873 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 874 | } |
| 875 | |
| 876 | @Test |
| 877 | public void testLightStepIdleStateLocked_ValidStates_NoActiveOps_NoNetworkConnected() { |
| 878 | setNetworkConnected(false); |
| 879 | mDeviceIdleController.setJobsActive(false); |
| 880 | mDeviceIdleController.setAlarmsActive(false); |
| 881 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 882 | |
| 883 | // Set state to INACTIVE. |
| 884 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 885 | setChargingOn(false); |
| 886 | setScreenOn(false); |
| 887 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 888 | |
| 889 | // No active ops means INACTIVE should go straight to IDLE. |
| 890 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 891 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 892 | |
| 893 | // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now. |
| 894 | |
| 895 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 896 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 897 | |
| 898 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 899 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 900 | |
| 901 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 902 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 903 | |
| 904 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 905 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 906 | |
| 907 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 908 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 909 | } |
| 910 | |
| 911 | @Test |
| 912 | public void testLightStepIdleStateLocked_ValidStates_ActiveOps_NoNetworkConnected() { |
| 913 | setNetworkConnected(false); |
| 914 | // Set state to INACTIVE. |
| 915 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 916 | setChargingOn(false); |
| 917 | setScreenOn(false); |
| 918 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 919 | |
| 920 | // Active ops means INACTIVE should go to PRE_IDLE to wait. |
| 921 | mDeviceIdleController.setJobsActive(true); |
| 922 | mDeviceIdleController.setAlarmsActive(true); |
| 923 | mDeviceIdleController.setActiveIdleOpsForTest(1); |
| 924 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 925 | verifyLightStateConditions(LIGHT_STATE_PRE_IDLE); |
| 926 | |
| 927 | // Even with active ops, PRE_IDLE should go to IDLE. |
| 928 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 929 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 930 | |
| 931 | // Should cycle between IDLE, WAITING_FOR_NETWORK, and IDLE_MAINTENANCE now. |
| 932 | |
| 933 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 934 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 935 | |
| 936 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 937 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 938 | |
| 939 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 940 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 941 | |
| 942 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 943 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 944 | |
| 945 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 946 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 947 | } |
| 948 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 949 | ///////////////// EXIT conditions /////////////////// |
| 950 | |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 951 | @Test |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 952 | public void testExitMaintenanceEarlyIfNeededLocked_deep_noActiveOps() { |
| 953 | mDeviceIdleController.setJobsActive(false); |
| 954 | mDeviceIdleController.setAlarmsActive(false); |
| 955 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 956 | |
| 957 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 958 | |
| 959 | enterDeepState(STATE_ACTIVE); |
| 960 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 961 | verifyStateConditions(STATE_ACTIVE); |
| 962 | |
| 963 | enterDeepState(STATE_INACTIVE); |
| 964 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 965 | verifyStateConditions(STATE_INACTIVE); |
| 966 | |
| 967 | enterDeepState(STATE_IDLE_PENDING); |
| 968 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 969 | verifyStateConditions(STATE_IDLE_PENDING); |
| 970 | |
| 971 | enterDeepState(STATE_SENSING); |
| 972 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 973 | verifyStateConditions(STATE_SENSING); |
| 974 | |
| 975 | enterDeepState(STATE_LOCATING); |
| 976 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 977 | verifyStateConditions(STATE_LOCATING); |
| 978 | |
| 979 | enterDeepState(STATE_IDLE); |
| 980 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 981 | verifyStateConditions(STATE_IDLE); |
| 982 | |
| 983 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 984 | // Going into IDLE_MAINTENANCE increments the active idle op count. |
| 985 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 986 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 987 | verifyStateConditions(STATE_IDLE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 988 | |
| 989 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 990 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 991 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | @Test |
| 995 | public void testExitMaintenanceEarlyIfNeededLocked_deep_activeJobs() { |
| 996 | mDeviceIdleController.setJobsActive(true); |
| 997 | mDeviceIdleController.setAlarmsActive(false); |
| 998 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 999 | |
| 1000 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1001 | |
| 1002 | enterDeepState(STATE_ACTIVE); |
| 1003 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1004 | verifyStateConditions(STATE_ACTIVE); |
| 1005 | |
| 1006 | enterDeepState(STATE_INACTIVE); |
| 1007 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1008 | verifyStateConditions(STATE_INACTIVE); |
| 1009 | |
| 1010 | enterDeepState(STATE_IDLE_PENDING); |
| 1011 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1012 | verifyStateConditions(STATE_IDLE_PENDING); |
| 1013 | |
| 1014 | enterDeepState(STATE_SENSING); |
| 1015 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1016 | verifyStateConditions(STATE_SENSING); |
| 1017 | |
| 1018 | enterDeepState(STATE_LOCATING); |
| 1019 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1020 | verifyStateConditions(STATE_LOCATING); |
| 1021 | |
| 1022 | enterDeepState(STATE_IDLE); |
| 1023 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1024 | verifyStateConditions(STATE_IDLE); |
| 1025 | |
| 1026 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 1027 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1028 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1029 | |
| 1030 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1031 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1032 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | @Test |
| 1036 | public void testExitMaintenanceEarlyIfNeededLocked_deep_activeAlarms() { |
| 1037 | mDeviceIdleController.setJobsActive(false); |
| 1038 | mDeviceIdleController.setAlarmsActive(true); |
| 1039 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 1040 | |
| 1041 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1042 | |
| 1043 | enterDeepState(STATE_ACTIVE); |
| 1044 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1045 | verifyStateConditions(STATE_ACTIVE); |
| 1046 | |
| 1047 | enterDeepState(STATE_INACTIVE); |
| 1048 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1049 | verifyStateConditions(STATE_INACTIVE); |
| 1050 | |
| 1051 | enterDeepState(STATE_IDLE_PENDING); |
| 1052 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1053 | verifyStateConditions(STATE_IDLE_PENDING); |
| 1054 | |
| 1055 | enterDeepState(STATE_SENSING); |
| 1056 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1057 | verifyStateConditions(STATE_SENSING); |
| 1058 | |
| 1059 | enterDeepState(STATE_LOCATING); |
| 1060 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1061 | verifyStateConditions(STATE_LOCATING); |
| 1062 | |
| 1063 | enterDeepState(STATE_IDLE); |
| 1064 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1065 | verifyStateConditions(STATE_IDLE); |
| 1066 | |
| 1067 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 1068 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1069 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1070 | |
| 1071 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1072 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1073 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | @Test |
| 1077 | public void testExitMaintenanceEarlyIfNeededLocked_deep_activeOps() { |
| 1078 | mDeviceIdleController.setJobsActive(false); |
| 1079 | mDeviceIdleController.setAlarmsActive(false); |
| 1080 | mDeviceIdleController.setActiveIdleOpsForTest(1); |
| 1081 | |
| 1082 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1083 | |
| 1084 | enterDeepState(STATE_ACTIVE); |
| 1085 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1086 | verifyStateConditions(STATE_ACTIVE); |
| 1087 | |
| 1088 | enterDeepState(STATE_INACTIVE); |
| 1089 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1090 | verifyStateConditions(STATE_INACTIVE); |
| 1091 | |
| 1092 | enterDeepState(STATE_IDLE_PENDING); |
| 1093 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1094 | verifyStateConditions(STATE_IDLE_PENDING); |
| 1095 | |
| 1096 | enterDeepState(STATE_SENSING); |
| 1097 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1098 | verifyStateConditions(STATE_SENSING); |
| 1099 | |
| 1100 | enterDeepState(STATE_LOCATING); |
| 1101 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1102 | verifyStateConditions(STATE_LOCATING); |
| 1103 | |
| 1104 | enterDeepState(STATE_IDLE); |
| 1105 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1106 | verifyStateConditions(STATE_IDLE); |
| 1107 | |
| 1108 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 1109 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1110 | verifyStateConditions(STATE_IDLE_MAINTENANCE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1111 | |
| 1112 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1113 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1114 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | @Test |
| 1118 | public void testExitMaintenanceEarlyIfNeededLocked_light_noActiveOps() { |
| 1119 | mDeviceIdleController.setJobsActive(false); |
| 1120 | mDeviceIdleController.setAlarmsActive(false); |
| 1121 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 1122 | |
| 1123 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1124 | |
| 1125 | enterLightState(LIGHT_STATE_ACTIVE); |
| 1126 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1127 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1128 | |
| 1129 | enterLightState(LIGHT_STATE_INACTIVE); |
| 1130 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1131 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1132 | |
| 1133 | enterLightState(LIGHT_STATE_PRE_IDLE); |
| 1134 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1135 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1136 | |
| 1137 | enterLightState(LIGHT_STATE_IDLE); |
| 1138 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1139 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1140 | |
| 1141 | enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1142 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1143 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1144 | |
| 1145 | enterLightState(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1146 | // Going into IDLE_MAINTENANCE increments the active idle op count. |
| 1147 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 1148 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1149 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1150 | |
| 1151 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 1152 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1153 | verifyLightStateConditions(LIGHT_STATE_OVERRIDE); |
| 1154 | } |
| 1155 | |
| 1156 | @Test |
| 1157 | public void testExitMaintenanceEarlyIfNeededLocked_light_activeJobs() { |
| 1158 | mDeviceIdleController.setJobsActive(true); |
| 1159 | mDeviceIdleController.setAlarmsActive(false); |
| 1160 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 1161 | |
| 1162 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1163 | |
| 1164 | enterLightState(LIGHT_STATE_ACTIVE); |
| 1165 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1166 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1167 | |
| 1168 | enterLightState(LIGHT_STATE_INACTIVE); |
| 1169 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1170 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1171 | |
| 1172 | enterLightState(LIGHT_STATE_PRE_IDLE); |
| 1173 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1174 | verifyLightStateConditions(LIGHT_STATE_PRE_IDLE); |
| 1175 | |
| 1176 | enterLightState(LIGHT_STATE_IDLE); |
| 1177 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1178 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1179 | |
| 1180 | enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1181 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1182 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1183 | |
| 1184 | enterLightState(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1185 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1186 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1187 | |
| 1188 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 1189 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1190 | verifyLightStateConditions(LIGHT_STATE_OVERRIDE); |
| 1191 | } |
| 1192 | |
| 1193 | @Test |
| 1194 | public void testExitMaintenanceEarlyIfNeededLocked_light_activeAlarms() { |
| 1195 | mDeviceIdleController.setJobsActive(false); |
| 1196 | mDeviceIdleController.setAlarmsActive(true); |
| 1197 | mDeviceIdleController.setActiveIdleOpsForTest(0); |
| 1198 | |
| 1199 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1200 | |
| 1201 | enterLightState(LIGHT_STATE_ACTIVE); |
| 1202 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1203 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1204 | |
| 1205 | enterLightState(LIGHT_STATE_INACTIVE); |
| 1206 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1207 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1208 | |
| 1209 | enterLightState(LIGHT_STATE_PRE_IDLE); |
| 1210 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1211 | verifyLightStateConditions(LIGHT_STATE_PRE_IDLE); |
| 1212 | |
| 1213 | enterLightState(LIGHT_STATE_IDLE); |
| 1214 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1215 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1216 | |
| 1217 | enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1218 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1219 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1220 | |
| 1221 | enterLightState(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1222 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1223 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1224 | |
| 1225 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 1226 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1227 | verifyLightStateConditions(LIGHT_STATE_OVERRIDE); |
| 1228 | } |
| 1229 | |
| 1230 | @Test |
| 1231 | public void testExitMaintenanceEarlyIfNeededLocked_light_activeOps() { |
| 1232 | mDeviceIdleController.setJobsActive(false); |
| 1233 | mDeviceIdleController.setAlarmsActive(false); |
| 1234 | mDeviceIdleController.setActiveIdleOpsForTest(1); |
| 1235 | |
| 1236 | // This method should only change things if in IDLE_MAINTENANCE or PRE_IDLE states. |
| 1237 | |
| 1238 | enterLightState(LIGHT_STATE_ACTIVE); |
| 1239 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1240 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1241 | |
| 1242 | enterLightState(LIGHT_STATE_INACTIVE); |
| 1243 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1244 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1245 | |
| 1246 | enterLightState(LIGHT_STATE_PRE_IDLE); |
| 1247 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1248 | verifyLightStateConditions(LIGHT_STATE_PRE_IDLE); |
| 1249 | |
| 1250 | enterLightState(LIGHT_STATE_IDLE); |
| 1251 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1252 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1253 | |
| 1254 | enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1255 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1256 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1257 | |
| 1258 | enterLightState(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1259 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1260 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1261 | |
| 1262 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 1263 | mDeviceIdleController.exitMaintenanceEarlyIfNeededLocked(); |
| 1264 | verifyLightStateConditions(LIGHT_STATE_OVERRIDE); |
| 1265 | } |
| 1266 | |
| 1267 | @Test |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1268 | public void testHandleMotionDetectedLocked_deep_quickDoze_off() { |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1269 | enterDeepState(STATE_ACTIVE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1270 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1271 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1272 | verifyStateConditions(STATE_ACTIVE); |
| 1273 | |
| 1274 | // Anything that wasn't ACTIVE before motion detection should end up in the INACTIVE state. |
| 1275 | |
| 1276 | enterDeepState(STATE_INACTIVE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1277 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1278 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1279 | verifyStateConditions(STATE_INACTIVE); |
| 1280 | |
| 1281 | enterDeepState(STATE_IDLE_PENDING); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1282 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1283 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1284 | verifyStateConditions(STATE_INACTIVE); |
| 1285 | |
| 1286 | enterDeepState(STATE_SENSING); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1287 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1288 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1289 | verifyStateConditions(STATE_INACTIVE); |
| 1290 | |
| 1291 | enterDeepState(STATE_LOCATING); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1292 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1293 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1294 | verifyStateConditions(STATE_INACTIVE); |
| 1295 | |
| 1296 | enterDeepState(STATE_IDLE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1297 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1298 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1299 | verifyStateConditions(STATE_INACTIVE); |
| 1300 | |
| 1301 | enterDeepState(STATE_IDLE_MAINTENANCE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1302 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1303 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1304 | verifyStateConditions(STATE_INACTIVE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1305 | |
| 1306 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1307 | setQuickDozeEnabled(false); |
| 1308 | // Disabling quick doze doesn't immediately change the state as coming out is harder than |
| 1309 | // going in. |
| 1310 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1311 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1312 | verifyStateConditions(STATE_INACTIVE); |
| 1313 | } |
| 1314 | |
| 1315 | @Test |
| 1316 | public void testHandleMotionDetectedLocked_deep_quickDoze_on() { |
| 1317 | enterDeepState(STATE_ACTIVE); |
| 1318 | setQuickDozeEnabled(true); |
| 1319 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1320 | verifyStateConditions(STATE_ACTIVE); |
| 1321 | |
| 1322 | // Anything that wasn't ACTIVE before motion detection should end up in the |
| 1323 | // QUICK_DOZE_DELAY state since quick doze is enabled. |
| 1324 | |
| 1325 | enterDeepState(STATE_INACTIVE); |
| 1326 | setQuickDozeEnabled(true); |
| 1327 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1328 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1329 | |
| 1330 | enterDeepState(STATE_IDLE_PENDING); |
| 1331 | setQuickDozeEnabled(true); |
| 1332 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1333 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1334 | |
| 1335 | enterDeepState(STATE_SENSING); |
| 1336 | setQuickDozeEnabled(true); |
| 1337 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1338 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1339 | |
| 1340 | enterDeepState(STATE_LOCATING); |
| 1341 | setQuickDozeEnabled(true); |
| 1342 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1343 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1344 | |
| 1345 | enterDeepState(STATE_IDLE); |
| 1346 | setQuickDozeEnabled(true); |
| 1347 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1348 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1349 | |
| 1350 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 1351 | setQuickDozeEnabled(true); |
| 1352 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1353 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
| 1354 | |
| 1355 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1356 | setQuickDozeEnabled(true); |
| 1357 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1358 | verifyStateConditions(STATE_QUICK_DOZE_DELAY); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | @Test |
| 1362 | public void testHandleMotionDetectedLocked_light() { |
| 1363 | enterLightState(LIGHT_STATE_ACTIVE); |
| 1364 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1365 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1366 | |
| 1367 | // Motion shouldn't affect light idle, so LIGHT states should stay as they were except for |
| 1368 | // OVERRIDE. OVERRIDE means deep was active, so if motion was detected, |
| 1369 | // LIGHT_STATE_OVERRIDE should end up as LIGHT_STATE_INACTIVE. |
| 1370 | |
| 1371 | enterLightState(LIGHT_STATE_INACTIVE); |
| 1372 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1373 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1374 | |
| 1375 | enterLightState(LIGHT_STATE_PRE_IDLE); |
| 1376 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1377 | verifyLightStateConditions(LIGHT_STATE_PRE_IDLE); |
| 1378 | |
| 1379 | enterLightState(LIGHT_STATE_IDLE); |
| 1380 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1381 | verifyLightStateConditions(LIGHT_STATE_IDLE); |
| 1382 | |
| 1383 | enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1384 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1385 | verifyLightStateConditions(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1386 | |
| 1387 | enterLightState(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1388 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1389 | verifyLightStateConditions(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1390 | |
| 1391 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 1392 | mDeviceIdleController.handleMotionDetectedLocked(50, "test"); |
| 1393 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1394 | } |
| 1395 | |
| 1396 | @Test |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 1397 | public void testBecomeActiveLocked_deep() { |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1398 | // becomeActiveLocked should put everything into ACTIVE. |
| 1399 | |
| 1400 | enterDeepState(STATE_ACTIVE); |
| 1401 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1402 | verifyStateConditions(STATE_ACTIVE); |
| 1403 | |
| 1404 | enterDeepState(STATE_INACTIVE); |
| 1405 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1406 | verifyStateConditions(STATE_ACTIVE); |
| 1407 | |
| 1408 | enterDeepState(STATE_IDLE_PENDING); |
| 1409 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1410 | verifyStateConditions(STATE_ACTIVE); |
| 1411 | |
| 1412 | enterDeepState(STATE_SENSING); |
| 1413 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1414 | verifyStateConditions(STATE_ACTIVE); |
| 1415 | |
| 1416 | enterDeepState(STATE_LOCATING); |
| 1417 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1418 | verifyStateConditions(STATE_ACTIVE); |
| 1419 | |
| 1420 | enterDeepState(STATE_IDLE); |
| 1421 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1422 | verifyStateConditions(STATE_ACTIVE); |
| 1423 | |
| 1424 | enterDeepState(STATE_IDLE_MAINTENANCE); |
| 1425 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1426 | verifyStateConditions(STATE_ACTIVE); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1427 | |
| 1428 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1429 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1430 | verifyStateConditions(STATE_ACTIVE); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | @Test |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 1434 | public void testBecomeActiveLocked_light() { |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1435 | // becomeActiveLocked should put everything into ACTIVE. |
| 1436 | |
| 1437 | enterLightState(LIGHT_STATE_ACTIVE); |
| 1438 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1439 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1440 | |
| 1441 | enterLightState(LIGHT_STATE_INACTIVE); |
| 1442 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1443 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1444 | |
| 1445 | enterLightState(LIGHT_STATE_PRE_IDLE); |
| 1446 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1447 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1448 | |
| 1449 | enterLightState(LIGHT_STATE_IDLE); |
| 1450 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1451 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1452 | |
| 1453 | enterLightState(LIGHT_STATE_WAITING_FOR_NETWORK); |
| 1454 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1455 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1456 | |
| 1457 | enterLightState(LIGHT_STATE_IDLE_MAINTENANCE); |
| 1458 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1459 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1460 | |
| 1461 | enterLightState(LIGHT_STATE_OVERRIDE); |
| 1462 | mDeviceIdleController.becomeActiveLocked("test", 1000); |
| 1463 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1464 | } |
| 1465 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 1466 | /** Test based on b/119058625. */ |
| 1467 | @Test |
| 1468 | public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_ScreenThenMotion() { |
| 1469 | mConstants.WAIT_FOR_UNLOCK = true; |
| 1470 | enterDeepState(STATE_IDLE); |
| 1471 | reset(mAlarmManager); |
| 1472 | spyOn(mDeviceIdleController); |
| 1473 | |
| 1474 | mDeviceIdleController.keyguardShowingLocked(true); |
| 1475 | setScreenOn(true); |
| 1476 | // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself |
| 1477 | // shouldn't bring the device out of deep IDLE. |
| 1478 | verifyStateConditions(STATE_IDLE); |
| 1479 | mDeviceIdleController.handleMotionDetectedLocked(1000, "test"); |
| 1480 | // Motion should bring the device out of Doze. Since the screen is still locked (albeit |
| 1481 | // on), the states should go back into INACTIVE. |
| 1482 | verifyStateConditions(STATE_INACTIVE); |
| 1483 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1484 | verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1485 | verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt()); |
| 1486 | } |
| 1487 | |
| 1488 | /** Test based on b/119058625. */ |
| 1489 | @Test |
| 1490 | public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_ScreenThenMotion() { |
| 1491 | mConstants.WAIT_FOR_UNLOCK = true; |
| 1492 | enterDeepState(STATE_IDLE); |
| 1493 | reset(mAlarmManager); |
| 1494 | spyOn(mDeviceIdleController); |
| 1495 | |
| 1496 | mDeviceIdleController.keyguardShowingLocked(false); |
| 1497 | setScreenOn(true); |
| 1498 | // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself |
| 1499 | // should bring the device out of deep IDLE. |
| 1500 | verifyStateConditions(STATE_ACTIVE); |
| 1501 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1502 | verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1503 | verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt()); |
| 1504 | } |
| 1505 | |
| 1506 | /** Test based on b/119058625. */ |
| 1507 | @Test |
| 1508 | public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOn_MotionThenScreen() { |
| 1509 | mConstants.WAIT_FOR_UNLOCK = true; |
| 1510 | enterDeepState(STATE_IDLE); |
| 1511 | reset(mAlarmManager); |
| 1512 | spyOn(mDeviceIdleController); |
| 1513 | |
| 1514 | InOrder alarmManagerInOrder = inOrder(mAlarmManager); |
| 1515 | InOrder controllerInOrder = inOrder(mDeviceIdleController); |
| 1516 | |
| 1517 | mDeviceIdleController.keyguardShowingLocked(true); |
| 1518 | mDeviceIdleController.handleMotionDetectedLocked(1000, "test"); |
| 1519 | // The screen is still off, so motion should result in the INACTIVE state. |
| 1520 | verifyStateConditions(STATE_INACTIVE); |
| 1521 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1522 | alarmManagerInOrder.verify(mAlarmManager) |
| 1523 | .cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1524 | controllerInOrder.verify(mDeviceIdleController) |
| 1525 | .scheduleReportActiveLocked(anyString(), anyInt()); |
| 1526 | |
| 1527 | setScreenOn(true); |
| 1528 | // With WAIT_FOR_UNLOCK = true and the screen locked, turning the screen on by itself |
| 1529 | // shouldn't bring the device all the way to ACTIVE. |
| 1530 | verifyStateConditions(STATE_INACTIVE); |
| 1531 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1532 | alarmManagerInOrder.verify(mAlarmManager, never()).cancel( |
| 1533 | eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1534 | |
| 1535 | // User finally unlocks the device. Device should be fully active. |
| 1536 | mDeviceIdleController.keyguardShowingLocked(false); |
| 1537 | verifyStateConditions(STATE_ACTIVE); |
| 1538 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1539 | alarmManagerInOrder.verify(mAlarmManager) |
| 1540 | .cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1541 | controllerInOrder.verify(mDeviceIdleController) |
| 1542 | .scheduleReportActiveLocked(anyString(), anyInt()); |
| 1543 | } |
| 1544 | |
| 1545 | /** Test based on b/119058625. */ |
| 1546 | @Test |
| 1547 | public void testExitNotifiesDependencies_WaitForUnlockOn_KeyguardOff_MotionThenScreen() { |
| 1548 | mConstants.WAIT_FOR_UNLOCK = true; |
| 1549 | enterDeepState(STATE_IDLE); |
| 1550 | reset(mAlarmManager); |
| 1551 | spyOn(mDeviceIdleController); |
| 1552 | |
| 1553 | InOrder alarmManagerInOrder = inOrder(mAlarmManager); |
| 1554 | InOrder controllerInOrder = inOrder(mDeviceIdleController); |
| 1555 | |
| 1556 | mDeviceIdleController.keyguardShowingLocked(false); |
| 1557 | mDeviceIdleController.handleMotionDetectedLocked(1000, "test"); |
| 1558 | // The screen is still off, so motion should result in the INACTIVE state. |
| 1559 | verifyStateConditions(STATE_INACTIVE); |
| 1560 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1561 | alarmManagerInOrder.verify(mAlarmManager) |
| 1562 | .cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1563 | controllerInOrder.verify(mDeviceIdleController) |
| 1564 | .scheduleReportActiveLocked(anyString(), anyInt()); |
| 1565 | |
| 1566 | setScreenOn(true); |
| 1567 | // With WAIT_FOR_UNLOCK = true and the screen unlocked, turning the screen on by itself |
| 1568 | // should bring the device out of deep IDLE. |
| 1569 | verifyStateConditions(STATE_ACTIVE); |
| 1570 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1571 | alarmManagerInOrder.verify(mAlarmManager) |
| 1572 | .cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1573 | controllerInOrder.verify(mDeviceIdleController) |
| 1574 | .scheduleReportActiveLocked(anyString(), anyInt()); |
| 1575 | } |
| 1576 | |
| 1577 | @Test |
| 1578 | public void testExitNotifiesDependencies_WaitForUnlockOff_Screen() { |
| 1579 | mConstants.WAIT_FOR_UNLOCK = false; |
| 1580 | enterDeepState(STATE_IDLE); |
| 1581 | reset(mAlarmManager); |
| 1582 | spyOn(mDeviceIdleController); |
| 1583 | |
| 1584 | setScreenOn(true); |
| 1585 | // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself |
| 1586 | // should bring the device out of deep IDLE. |
| 1587 | verifyStateConditions(STATE_ACTIVE); |
| 1588 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1589 | verify(mAlarmManager).cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1590 | verify(mDeviceIdleController).scheduleReportActiveLocked(anyString(), anyInt()); |
| 1591 | } |
| 1592 | |
| 1593 | @Test |
| 1594 | public void testExitNotifiesDependencies_WaitForUnlockOff_MotionThenScreen() { |
| 1595 | mConstants.WAIT_FOR_UNLOCK = false; |
| 1596 | enterDeepState(STATE_IDLE); |
| 1597 | reset(mAlarmManager); |
| 1598 | spyOn(mDeviceIdleController); |
| 1599 | |
| 1600 | InOrder alarmManagerInOrder = inOrder(mAlarmManager); |
| 1601 | InOrder controllerInOrder = inOrder(mDeviceIdleController); |
| 1602 | |
| 1603 | mDeviceIdleController.handleMotionDetectedLocked(1000, "test"); |
| 1604 | // The screen is still off, so motion should result in the INACTIVE state. |
| 1605 | verifyStateConditions(STATE_INACTIVE); |
| 1606 | verifyLightStateConditions(LIGHT_STATE_INACTIVE); |
| 1607 | alarmManagerInOrder.verify(mAlarmManager) |
| 1608 | .cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1609 | controllerInOrder.verify(mDeviceIdleController) |
| 1610 | .scheduleReportActiveLocked(anyString(), anyInt()); |
| 1611 | |
| 1612 | setScreenOn(true); |
| 1613 | // With WAIT_FOR_UNLOCK = false and the screen locked, turning the screen on by itself |
| 1614 | // should bring the device out of deep IDLE. |
| 1615 | verifyStateConditions(STATE_ACTIVE); |
| 1616 | verifyLightStateConditions(LIGHT_STATE_ACTIVE); |
| 1617 | alarmManagerInOrder.verify(mAlarmManager) |
| 1618 | .cancel(eq(mDeviceIdleController.mDeepAlarmListener)); |
| 1619 | controllerInOrder.verify(mDeviceIdleController) |
| 1620 | .scheduleReportActiveLocked(anyString(), anyInt()); |
| 1621 | } |
| 1622 | |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 1623 | @Test |
| 1624 | public void testStepToIdleMode() { |
| 1625 | float delta = mDeviceIdleController.MIN_PRE_IDLE_FACTOR_CHANGE; |
| 1626 | for (int mode = PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL; |
| 1627 | mode <= PowerManager.PRE_IDLE_TIMEOUT_MODE_LONG; |
| 1628 | mode++) { |
| 1629 | int ret = mDeviceIdleController.setPreIdleTimeoutMode(mode); |
| 1630 | if (mode == PowerManager.PRE_IDLE_TIMEOUT_MODE_NORMAL) { |
| 1631 | assertEquals("setPreIdleTimeoutMode: " + mode + " failed.", |
| 1632 | mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret); |
| 1633 | } else { |
| 1634 | assertEquals("setPreIdleTimeoutMode: " + mode + " failed.", |
| 1635 | mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret); |
| 1636 | } |
| 1637 | //TODO(b/123045185): Mocked Handler of DeviceIdleController to make message loop |
| 1638 | //workable in this test class |
| 1639 | mDeviceIdleController.updatePreIdleFactor(); |
| 1640 | float expectedfactor = mDeviceIdleController.getPreIdleTimeoutByMode(mode); |
| 1641 | float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor(); |
| 1642 | assertEquals("Pre idle time factor of mode [" + mode + "].", |
| 1643 | expectedfactor, curfactor, delta); |
| 1644 | mDeviceIdleController.resetPreIdleTimeoutMode(); |
| 1645 | mDeviceIdleController.updatePreIdleFactor(); |
| 1646 | |
| 1647 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_INACTIVE); |
| 1648 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_PENDING); |
| 1649 | |
| 1650 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_SENSING); |
| 1651 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_LOCATING); |
| 1652 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_QUICK_DOZE_DELAY); |
| 1653 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE_MAINTENANCE); |
| 1654 | checkNextAlarmTimeWithNewPreIdleFactor(expectedfactor, STATE_IDLE); |
| 1655 | checkMaybeDoAnImmediateMaintenance(expectedfactor); |
| 1656 | } |
| 1657 | float curfactor = mDeviceIdleController.getPreIdleTimeoutFactor(); |
| 1658 | assertEquals("Pre idle time factor of mode default.", |
| 1659 | 1.0f, curfactor, delta); |
| 1660 | } |
| 1661 | |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1662 | @Test |
| 1663 | public void testStationaryDetection_QuickDozeOff() { |
| 1664 | setQuickDozeEnabled(false); |
| 1665 | enterDeepState(STATE_IDLE); |
| 1666 | // Regular progression through states, so time should have increased appropriately. |
| 1667 | mInjector.nowElapsed += mConstants.IDLE_AFTER_INACTIVE_TIMEOUT + mConstants.SENSING_TIMEOUT |
| 1668 | + mConstants.LOCATING_TIMEOUT; |
| 1669 | |
| 1670 | StationaryListenerForTest stationaryListener = new StationaryListenerForTest(); |
| 1671 | |
| 1672 | mDeviceIdleController.registerStationaryListener(stationaryListener); |
| 1673 | |
| 1674 | // Go to IDLE_MAINTENANCE |
| 1675 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1676 | |
| 1677 | // Back to IDLE |
| 1678 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1679 | assertTrue(stationaryListener.isStationary); |
| 1680 | |
| 1681 | // Test motion |
| 1682 | stationaryListener.motionExpected = true; |
| 1683 | mDeviceIdleController.mMotionListener.onTrigger(null); |
| 1684 | assertFalse(stationaryListener.isStationary); |
| 1685 | } |
| 1686 | |
| 1687 | @Test |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1688 | public void testStationaryDetection_QuickDozeOn_NoMotion() { |
| 1689 | // Short timeout for testing. |
| 1690 | mConstants.MOTION_INACTIVE_TIMEOUT = 6000L; |
| 1691 | doReturn(Sensor.REPORTING_MODE_ONE_SHOT).when(mMotionSensor).getReportingMode(); |
| 1692 | doReturn(true).when(mSensorManager) |
| 1693 | .requestTriggerSensor(eq(mDeviceIdleController.mMotionListener), eq(mMotionSensor)); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1694 | setAlarmSoon(false); |
| 1695 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1696 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1697 | verifyStateConditions(STATE_IDLE); |
| 1698 | // Quick doze progression through states, so time should have increased appropriately. |
| 1699 | mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT; |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1700 | final ArgumentCaptor<AlarmManager.OnAlarmListener> motionAlarmListener = ArgumentCaptor |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1701 | .forClass(AlarmManager.OnAlarmListener.class); |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1702 | final ArgumentCaptor<AlarmManager.OnAlarmListener> motionRegistrationAlarmListener = |
| 1703 | ArgumentCaptor.forClass(AlarmManager.OnAlarmListener.class); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1704 | doNothing().when(mAlarmManager).set(anyInt(), anyLong(), eq("DeviceIdleController.motion"), |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1705 | motionAlarmListener.capture(), any()); |
| 1706 | doNothing().when(mAlarmManager).set(anyInt(), anyLong(), |
| 1707 | eq("DeviceIdleController.motion_registration"), |
| 1708 | motionRegistrationAlarmListener.capture(), any()); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1709 | |
| 1710 | StationaryListenerForTest stationaryListener = new StationaryListenerForTest(); |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1711 | spyOn(stationaryListener); |
| 1712 | InOrder inOrder = inOrder(stationaryListener); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1713 | |
| 1714 | stationaryListener.motionExpected = true; |
| 1715 | mDeviceIdleController.registerStationaryListener(stationaryListener); |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1716 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1717 | .onDeviceStationaryChanged(eq(false)); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1718 | assertFalse(stationaryListener.isStationary); |
| 1719 | |
| 1720 | // Go to IDLE_MAINTENANCE |
| 1721 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1722 | |
| 1723 | mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2; |
| 1724 | |
| 1725 | // Back to IDLE |
| 1726 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1727 | |
| 1728 | // Now enough time has passed. |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1729 | mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1730 | stationaryListener.motionExpected = false; |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1731 | motionAlarmListener.getValue().onAlarm(); |
| 1732 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1733 | .onDeviceStationaryChanged(eq(true)); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1734 | assertTrue(stationaryListener.isStationary); |
| 1735 | |
| 1736 | stationaryListener.motionExpected = true; |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1737 | mDeviceIdleController.mMotionListener.onTrigger(null); |
| 1738 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1739 | .onDeviceStationaryChanged(eq(false)); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1740 | assertFalse(stationaryListener.isStationary); |
| 1741 | |
| 1742 | // Since we're in quick doze, the device shouldn't stop idling. |
| 1743 | verifyStateConditions(STATE_IDLE); |
| 1744 | |
| 1745 | // Go to IDLE_MAINTENANCE |
| 1746 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1747 | |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1748 | motionRegistrationAlarmListener.getValue().onAlarm(); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1749 | mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT / 2; |
| 1750 | |
| 1751 | // Back to IDLE |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1752 | stationaryListener.motionExpected = false; |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1753 | mDeviceIdleController.stepIdleStateLocked("testing"); |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1754 | verify(mSensorManager, |
| 1755 | timeout(mConstants.MOTION_INACTIVE_TIMEOUT).times(2)) |
| 1756 | .requestTriggerSensor(eq(mDeviceIdleController.mMotionListener), eq(mMotionSensor)); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1757 | |
| 1758 | // Now enough time has passed. |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1759 | mInjector.nowElapsed += mConstants.MOTION_INACTIVE_TIMEOUT; |
| 1760 | motionAlarmListener.getValue().onAlarm(); |
| 1761 | inOrder.verify(stationaryListener, |
| 1762 | timeout(mConstants.MOTION_INACTIVE_TIMEOUT).times(1)) |
| 1763 | .onDeviceStationaryChanged(eq(true)); |
Kweku Adams | b8bb9fb | 2019-08-28 07:45:00 -0700 | [diff] [blame] | 1764 | assertTrue(stationaryListener.isStationary); |
| 1765 | } |
| 1766 | |
Kweku Adams | 265b835 | 2019-12-19 18:59:42 -0800 | [diff] [blame] | 1767 | @Test |
| 1768 | public void testStationaryDetection_QuickDozeOn_OneShot() { |
| 1769 | // Short timeout for testing. |
| 1770 | mConstants.MOTION_INACTIVE_TIMEOUT = 6000L; |
| 1771 | doReturn(Sensor.REPORTING_MODE_ONE_SHOT).when(mMotionSensor).getReportingMode(); |
| 1772 | setAlarmSoon(false); |
| 1773 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1774 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1775 | verifyStateConditions(STATE_IDLE); |
| 1776 | // Quick doze progression through states, so time should have increased appropriately. |
| 1777 | mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT; |
| 1778 | final ArgumentCaptor<AlarmManager.OnAlarmListener> alarmListener = ArgumentCaptor |
| 1779 | .forClass(AlarmManager.OnAlarmListener.class); |
| 1780 | doNothing().when(mAlarmManager) |
| 1781 | .set(anyInt(), anyLong(), eq("DeviceIdleController.motion"), any(), any()); |
| 1782 | doNothing().when(mAlarmManager).set(anyInt(), anyLong(), |
| 1783 | eq("DeviceIdleController.motion_registration"), |
| 1784 | alarmListener.capture(), any()); |
| 1785 | ArgumentCaptor<TriggerEventListener> listenerCaptor = |
| 1786 | ArgumentCaptor.forClass(TriggerEventListener.class); |
| 1787 | |
| 1788 | StationaryListenerForTest stationaryListener = new StationaryListenerForTest(); |
| 1789 | spyOn(stationaryListener); |
| 1790 | InOrder inOrder = inOrder(stationaryListener, mSensorManager); |
| 1791 | |
| 1792 | stationaryListener.motionExpected = true; |
| 1793 | mDeviceIdleController.registerStationaryListener(stationaryListener); |
| 1794 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1795 | .onDeviceStationaryChanged(eq(false)); |
| 1796 | assertFalse(stationaryListener.isStationary); |
| 1797 | inOrder.verify(mSensorManager) |
| 1798 | .requestTriggerSensor(listenerCaptor.capture(), eq(mMotionSensor)); |
| 1799 | final TriggerEventListener listener = listenerCaptor.getValue(); |
| 1800 | |
| 1801 | // Trigger motion |
| 1802 | listener.onTrigger(mock(TriggerEvent.class)); |
| 1803 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1804 | .onDeviceStationaryChanged(eq(false)); |
| 1805 | |
| 1806 | // Make sure the listener is re-registered. |
| 1807 | alarmListener.getValue().onAlarm(); |
| 1808 | inOrder.verify(mSensorManager).requestTriggerSensor(eq(listener), eq(mMotionSensor)); |
| 1809 | } |
| 1810 | |
| 1811 | @Test |
| 1812 | public void testStationaryDetection_QuickDozeOn_MultiShot() { |
| 1813 | // Short timeout for testing. |
| 1814 | mConstants.MOTION_INACTIVE_TIMEOUT = 6000L; |
| 1815 | doReturn(Sensor.REPORTING_MODE_CONTINUOUS).when(mMotionSensor).getReportingMode(); |
| 1816 | setAlarmSoon(false); |
| 1817 | enterDeepState(STATE_QUICK_DOZE_DELAY); |
| 1818 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1819 | verifyStateConditions(STATE_IDLE); |
| 1820 | // Quick doze progression through states, so time should have increased appropriately. |
| 1821 | mInjector.nowElapsed += mConstants.QUICK_DOZE_DELAY_TIMEOUT; |
| 1822 | final ArgumentCaptor<AlarmManager.OnAlarmListener> alarmListener = ArgumentCaptor |
| 1823 | .forClass(AlarmManager.OnAlarmListener.class); |
| 1824 | doNothing().when(mAlarmManager) |
| 1825 | .set(anyInt(), anyLong(), eq("DeviceIdleController.motion"), any(), any()); |
| 1826 | doNothing().when(mAlarmManager).set(anyInt(), anyLong(), |
| 1827 | eq("DeviceIdleController.motion_registration"), |
| 1828 | alarmListener.capture(), any()); |
| 1829 | ArgumentCaptor<SensorEventListener> listenerCaptor = |
| 1830 | ArgumentCaptor.forClass(SensorEventListener.class); |
| 1831 | |
| 1832 | StationaryListenerForTest stationaryListener = new StationaryListenerForTest(); |
| 1833 | spyOn(stationaryListener); |
| 1834 | InOrder inOrder = inOrder(stationaryListener, mSensorManager); |
| 1835 | |
| 1836 | stationaryListener.motionExpected = true; |
| 1837 | mDeviceIdleController.registerStationaryListener(stationaryListener); |
| 1838 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1839 | .onDeviceStationaryChanged(eq(false)); |
| 1840 | assertFalse(stationaryListener.isStationary); |
| 1841 | inOrder.verify(mSensorManager) |
| 1842 | .registerListener(listenerCaptor.capture(), eq(mMotionSensor), |
| 1843 | eq(SensorManager.SENSOR_DELAY_NORMAL)); |
| 1844 | final SensorEventListener listener = listenerCaptor.getValue(); |
| 1845 | |
| 1846 | // Trigger motion |
| 1847 | listener.onSensorChanged(mock(SensorEvent.class)); |
| 1848 | inOrder.verify(stationaryListener, timeout(1000L).times(1)) |
| 1849 | .onDeviceStationaryChanged(eq(false)); |
| 1850 | |
| 1851 | // Make sure the listener is re-registered. |
| 1852 | alarmListener.getValue().onAlarm(); |
| 1853 | inOrder.verify(mSensorManager) |
| 1854 | .registerListener(eq(listener), eq(mMotionSensor), |
| 1855 | eq(SensorManager.SENSOR_DELAY_NORMAL)); |
| 1856 | } |
| 1857 | |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1858 | private void enterDeepState(int state) { |
| 1859 | switch (state) { |
| 1860 | case STATE_ACTIVE: |
| 1861 | setScreenOn(true); |
| 1862 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 1863 | break; |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1864 | case STATE_QUICK_DOZE_DELAY: |
| 1865 | // Start off from ACTIVE in case we're already past the desired state. |
| 1866 | enterDeepState(STATE_ACTIVE); |
| 1867 | setQuickDozeEnabled(true); |
| 1868 | setScreenOn(false); |
| 1869 | setChargingOn(false); |
| 1870 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 1871 | break; |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1872 | case STATE_LOCATING: |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1873 | mInjector.locationManager = mLocationManager; |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1874 | doReturn(mock(LocationProvider.class)).when(mLocationManager).getProvider( |
| 1875 | anyString()); |
| 1876 | // Fallthrough to step loop. |
| 1877 | case STATE_IDLE_PENDING: |
| 1878 | case STATE_SENSING: |
| 1879 | case STATE_IDLE: |
| 1880 | case STATE_IDLE_MAINTENANCE: |
| 1881 | // Make sure the controller doesn't think there's a wake-from-idle alarm coming |
| 1882 | // soon. |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1883 | setAlarmSoon(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1884 | case STATE_INACTIVE: |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1885 | // Start off from ACTIVE in case we're already past the desired state. |
| 1886 | enterDeepState(STATE_ACTIVE); |
| 1887 | setQuickDozeEnabled(false); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1888 | setScreenOn(false); |
| 1889 | setChargingOn(false); |
| 1890 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1891 | int count = 0; |
| 1892 | while (mDeviceIdleController.getState() != state) { |
| 1893 | // Stepping through each state ensures that the proper features are turned |
| 1894 | // on/off. |
| 1895 | mDeviceIdleController.stepIdleStateLocked("testing"); |
| 1896 | count++; |
| 1897 | if (count > 10) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1898 | fail("Infinite loop. Check test configuration. Currently at " + |
| 1899 | stateToString(mDeviceIdleController.getState())); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | break; |
| 1903 | default: |
| 1904 | fail("Unknown deep state " + stateToString(state)); |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | private void enterLightState(int lightState) { |
| 1909 | switch (lightState) { |
| 1910 | case LIGHT_STATE_ACTIVE: |
| 1911 | setScreenOn(true); |
| 1912 | mDeviceIdleController.becomeActiveLocked("testing", 0); |
| 1913 | break; |
| 1914 | case LIGHT_STATE_INACTIVE: |
| 1915 | case LIGHT_STATE_IDLE: |
| 1916 | case LIGHT_STATE_IDLE_MAINTENANCE: |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1917 | // Start off from ACTIVE in case we're already past the desired state. |
| 1918 | enterLightState(LIGHT_STATE_ACTIVE); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1919 | setScreenOn(false); |
| 1920 | setChargingOn(false); |
| 1921 | int count = 0; |
| 1922 | mDeviceIdleController.becomeInactiveIfAppropriateLocked(); |
| 1923 | while (mDeviceIdleController.getLightState() != lightState) { |
| 1924 | // Stepping through each state ensures that the proper features are turned |
| 1925 | // on/off. |
| 1926 | mDeviceIdleController.stepLightIdleStateLocked("testing"); |
| 1927 | |
| 1928 | count++; |
| 1929 | if (count > 10) { |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1930 | fail("Infinite loop. Check test configuration. Currently at " + |
| 1931 | lightStateToString(mDeviceIdleController.getLightState())); |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 1932 | } |
| 1933 | } |
| 1934 | break; |
| 1935 | case LIGHT_STATE_PRE_IDLE: |
| 1936 | case LIGHT_STATE_WAITING_FOR_NETWORK: |
| 1937 | case LIGHT_STATE_OVERRIDE: |
| 1938 | setScreenOn(false); |
| 1939 | setChargingOn(false); |
| 1940 | mDeviceIdleController.setLightStateForTest(lightState); |
| 1941 | break; |
| 1942 | default: |
| 1943 | fail("Unknown light state " + lightStateToString(lightState)); |
| 1944 | } |
| 1945 | } |
| 1946 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1947 | private void setChargingOn(boolean on) { |
| 1948 | mDeviceIdleController.updateChargingLocked(on); |
| 1949 | } |
| 1950 | |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 1951 | private void setScreenLocked(boolean locked) { |
| 1952 | mDeviceIdleController.keyguardShowingLocked(locked); |
| 1953 | } |
| 1954 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1955 | private void setScreenOn(boolean on) { |
| 1956 | doReturn(on).when(mPowerManager).isInteractive(); |
| 1957 | mDeviceIdleController.updateInteractivityLocked(); |
| 1958 | } |
| 1959 | |
Kweku Adams | 799858b | 2018-10-08 17:19:08 -0700 | [diff] [blame] | 1960 | private void setNetworkConnected(boolean connected) { |
| 1961 | mInjector.connectivityService = mConnectivityService; |
| 1962 | final NetworkInfo ani = mock(NetworkInfo.class); |
| 1963 | doReturn(connected).when(ani).isConnected(); |
| 1964 | doReturn(ani).when(mConnectivityService).getActiveNetworkInfo(); |
| 1965 | mDeviceIdleController.updateConnectivityState(null); |
| 1966 | } |
| 1967 | |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1968 | private void setQuickDozeEnabled(boolean on) { |
| 1969 | mDeviceIdleController.updateQuickDozeFlagLocked(on); |
| 1970 | } |
| 1971 | |
| 1972 | private void setAlarmSoon(boolean isSoon) { |
| 1973 | if (isSoon) { |
Kweku Adams | 9da2bb9 | 2018-12-20 06:34:39 -0800 | [diff] [blame] | 1974 | doReturn(SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM / 2) |
| 1975 | .when(mAlarmManager).getNextWakeFromIdleTime(); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 1976 | } else { |
| 1977 | doReturn(Long.MAX_VALUE).when(mAlarmManager).getNextWakeFromIdleTime(); |
| 1978 | } |
| 1979 | } |
| 1980 | |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1981 | private void verifyStateConditions(int expectedState) { |
| 1982 | int curState = mDeviceIdleController.getState(); |
| 1983 | assertEquals( |
| 1984 | "Expected " + stateToString(expectedState) + " but was " + stateToString(curState), |
| 1985 | expectedState, curState); |
| 1986 | |
| 1987 | switch (expectedState) { |
| 1988 | case STATE_ACTIVE: |
| 1989 | assertFalse(mDeviceIdleController.mMotionListener.isActive()); |
| 1990 | assertFalse(mAnyMotionDetector.isMonitoring); |
| 1991 | break; |
| 1992 | case STATE_INACTIVE: |
| 1993 | assertFalse(mDeviceIdleController.mMotionListener.isActive()); |
| 1994 | assertFalse(mAnyMotionDetector.isMonitoring); |
| 1995 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 1996 | assertFalse(mDeviceIdleController.isScreenOn() |
| 1997 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 1998 | break; |
| 1999 | case STATE_IDLE_PENDING: |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2000 | assertEquals( |
| 2001 | mDeviceIdleController.hasMotionSensor(), |
| 2002 | mDeviceIdleController.mMotionListener.isActive()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2003 | assertFalse(mAnyMotionDetector.isMonitoring); |
| 2004 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2005 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2006 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2007 | break; |
| 2008 | case STATE_SENSING: |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2009 | assertEquals( |
| 2010 | mDeviceIdleController.hasMotionSensor(), |
| 2011 | mDeviceIdleController.mMotionListener.isActive()); |
| 2012 | assertEquals( |
| 2013 | mDeviceIdleController.hasMotionSensor(), |
| 2014 | mAnyMotionDetector.isMonitoring); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2015 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2016 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2017 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2018 | break; |
| 2019 | case STATE_LOCATING: |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2020 | assertEquals( |
| 2021 | mDeviceIdleController.hasMotionSensor(), |
| 2022 | mDeviceIdleController.mMotionListener.isActive()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2023 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2024 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2025 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2026 | break; |
| 2027 | case STATE_IDLE: |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2028 | if (mDeviceIdleController.hasMotionSensor()) { |
| 2029 | assertTrue(mDeviceIdleController.mMotionListener.isActive() |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2030 | // If quick doze is enabled, the motion listener should NOT be active. |
| 2031 | || mDeviceIdleController.isQuickDozeEnabled()); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2032 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2033 | assertFalse(mAnyMotionDetector.isMonitoring); |
| 2034 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2035 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2036 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2037 | // Light state should be OVERRIDE at this point. |
| 2038 | verifyLightStateConditions(LIGHT_STATE_OVERRIDE); |
| 2039 | break; |
| 2040 | case STATE_IDLE_MAINTENANCE: |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2041 | if (mDeviceIdleController.hasMotionSensor()) { |
| 2042 | assertTrue(mDeviceIdleController.mMotionListener.isActive() |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2043 | // If quick doze is enabled, the motion listener should NOT be active. |
| 2044 | || mDeviceIdleController.isQuickDozeEnabled()); |
Robin Lee | 876b8854 | 2018-11-13 17:22:24 +0100 | [diff] [blame] | 2045 | } |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2046 | assertFalse(mAnyMotionDetector.isMonitoring); |
| 2047 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2048 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2049 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | b396ccf | 2018-09-17 16:37:15 -0700 | [diff] [blame] | 2050 | break; |
| 2051 | case STATE_QUICK_DOZE_DELAY: |
| 2052 | // If quick doze is enabled, the motion listener should NOT be active. |
| 2053 | assertFalse(mDeviceIdleController.mMotionListener.isActive()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2054 | assertFalse(mAnyMotionDetector.isMonitoring); |
| 2055 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2056 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2057 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2058 | break; |
| 2059 | default: |
| 2060 | fail("Conditions for " + stateToString(expectedState) + " unknown."); |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | private void verifyLightStateConditions(int expectedLightState) { |
| 2065 | int curLightState = mDeviceIdleController.getLightState(); |
| 2066 | assertEquals( |
| 2067 | "Expected " + lightStateToString(expectedLightState) |
| 2068 | + " but was " + lightStateToString(curLightState), |
| 2069 | expectedLightState, curLightState); |
| 2070 | |
| 2071 | switch (expectedLightState) { |
| 2072 | case LIGHT_STATE_ACTIVE: |
| 2073 | assertTrue( |
Kweku Adams | a457f4e | 2018-10-03 15:56:06 -0700 | [diff] [blame] | 2074 | mDeviceIdleController.isCharging() || mDeviceIdleController.isScreenOn() |
| 2075 | // Or there's an alarm coming up soon. |
| 2076 | || SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM |
| 2077 | > mAlarmManager.getNextWakeFromIdleTime()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2078 | break; |
| 2079 | case LIGHT_STATE_INACTIVE: |
| 2080 | case LIGHT_STATE_PRE_IDLE: |
| 2081 | case LIGHT_STATE_IDLE: |
| 2082 | case LIGHT_STATE_WAITING_FOR_NETWORK: |
| 2083 | case LIGHT_STATE_IDLE_MAINTENANCE: |
| 2084 | case LIGHT_STATE_OVERRIDE: |
| 2085 | assertFalse(mDeviceIdleController.isCharging()); |
Kweku Adams | b7ce190 | 2019-01-30 10:55:34 -0800 | [diff] [blame] | 2086 | assertFalse(mDeviceIdleController.isScreenOn() |
| 2087 | && !mDeviceIdleController.isKeyguardShowing()); |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2088 | break; |
| 2089 | default: |
| 2090 | fail("Conditions for " + lightStateToString(expectedLightState) + " unknown."); |
| 2091 | } |
| 2092 | } |
Denny cy Lee | c5a7c29 | 2019-01-01 17:37:55 +0800 | [diff] [blame] | 2093 | |
| 2094 | private void checkNextAlarmTimeWithNewPreIdleFactor(float factor, int state) { |
| 2095 | final long errorTolerance = 1000; |
| 2096 | enterDeepState(state); |
| 2097 | long now = SystemClock.elapsedRealtime(); |
| 2098 | long alarm = mDeviceIdleController.getNextAlarmTime(); |
| 2099 | if (state == STATE_INACTIVE || state == STATE_IDLE_PENDING) { |
| 2100 | int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor); |
| 2101 | if (Float.compare(factor, 1.0f) == 0) { |
| 2102 | assertEquals("setPreIdleTimeoutMode: " + factor + " failed.", |
| 2103 | mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret); |
| 2104 | } else { |
| 2105 | assertEquals("setPreIdleTimeoutMode: " + factor + " failed.", |
| 2106 | mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret); |
| 2107 | } |
| 2108 | if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) { |
| 2109 | mDeviceIdleController.updatePreIdleFactor(); |
| 2110 | long newAlarm = mDeviceIdleController.getNextAlarmTime(); |
| 2111 | long newDelay = (long) ((alarm - now) * factor); |
| 2112 | assertTrue("setPreIdleTimeoutFactor: " + factor, |
| 2113 | Math.abs(newDelay - (newAlarm - now)) < errorTolerance); |
| 2114 | mDeviceIdleController.resetPreIdleTimeoutMode(); |
| 2115 | mDeviceIdleController.updatePreIdleFactor(); |
| 2116 | mDeviceIdleController.maybeDoImmediateMaintenance(); |
| 2117 | newAlarm = mDeviceIdleController.getNextAlarmTime(); |
| 2118 | assertTrue("resetPreIdleTimeoutMode from: " + factor, |
| 2119 | Math.abs(newAlarm - alarm) < errorTolerance); |
| 2120 | mDeviceIdleController.setPreIdleTimeoutFactor(factor); |
| 2121 | now = SystemClock.elapsedRealtime(); |
| 2122 | enterDeepState(state); |
| 2123 | newAlarm = mDeviceIdleController.getNextAlarmTime(); |
| 2124 | assertTrue("setPreIdleTimeoutFactor: " + factor + " before step to idle", |
| 2125 | Math.abs(newDelay - (newAlarm - now)) < errorTolerance); |
| 2126 | mDeviceIdleController.resetPreIdleTimeoutMode(); |
| 2127 | mDeviceIdleController.updatePreIdleFactor(); |
| 2128 | mDeviceIdleController.maybeDoImmediateMaintenance(); |
| 2129 | } |
| 2130 | } else { |
| 2131 | mDeviceIdleController.setPreIdleTimeoutFactor(factor); |
| 2132 | mDeviceIdleController.updatePreIdleFactor(); |
| 2133 | long newAlarm = mDeviceIdleController.getNextAlarmTime(); |
| 2134 | assertTrue("setPreIdleTimeoutFactor: " + factor |
| 2135 | + " shounld not change next alarm" , |
| 2136 | (newAlarm == alarm)); |
| 2137 | mDeviceIdleController.resetPreIdleTimeoutMode(); |
| 2138 | mDeviceIdleController.updatePreIdleFactor(); |
| 2139 | mDeviceIdleController.maybeDoImmediateMaintenance(); |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | private void checkMaybeDoAnImmediateMaintenance(float factor) { |
| 2144 | int ret = mDeviceIdleController.setPreIdleTimeoutFactor(factor); |
| 2145 | final long minuteInMillis = 60 * 1000; |
| 2146 | if (Float.compare(factor, 1.0f) == 0) { |
| 2147 | assertEquals("setPreIdleTimeoutMode: " + factor + " failed.", |
| 2148 | mDeviceIdleController.SET_IDLE_FACTOR_RESULT_IGNORED, ret); |
| 2149 | } else { |
| 2150 | assertEquals("setPreIdleTimeoutMode: " + factor + " failed.", |
| 2151 | mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK, ret); |
| 2152 | } |
| 2153 | if (ret == mDeviceIdleController.SET_IDLE_FACTOR_RESULT_OK) { |
| 2154 | enterDeepState(STATE_IDLE); |
| 2155 | long now = SystemClock.elapsedRealtime(); |
| 2156 | long alarm = mDeviceIdleController.getNextAlarmTime(); |
| 2157 | mDeviceIdleController.setIdleStartTimeForTest( |
| 2158 | now - (long) (mConstants.IDLE_TIMEOUT * 0.6)); |
| 2159 | mDeviceIdleController.maybeDoImmediateMaintenance(); |
| 2160 | long newAlarm = mDeviceIdleController.getNextAlarmTime(); |
| 2161 | assertTrue("maintenance not reschedule IDLE_TIMEOUT * 0.6", |
| 2162 | newAlarm == alarm); |
| 2163 | mDeviceIdleController.setIdleStartTimeForTest( |
| 2164 | now - (long) (mConstants.IDLE_TIMEOUT * 1.2)); |
| 2165 | mDeviceIdleController.maybeDoImmediateMaintenance(); |
| 2166 | newAlarm = mDeviceIdleController.getNextAlarmTime(); |
| 2167 | assertTrue("maintenance not reschedule IDLE_TIMEOUT * 1.2", |
| 2168 | (newAlarm - now) < minuteInMillis); |
| 2169 | mDeviceIdleController.resetPreIdleTimeoutMode(); |
| 2170 | mDeviceIdleController.updatePreIdleFactor(); |
| 2171 | } |
| 2172 | } |
Kweku Adams | 00e3a37 | 2018-09-28 16:57:09 -0700 | [diff] [blame] | 2173 | } |