diff options
| author | 2018-12-20 06:34:39 -0800 | |
|---|---|---|
| committer | 2018-12-20 06:42:57 -0800 | |
| commit | 9da2bb93c89e0f26a437abc42d2e01e115d6b3af (patch) | |
| tree | 6172a174b3031654313f4dfebc5fcd048639ae13 | |
| parent | 9507158846fae4be1a7f90adf5b8dad0475d482a (diff) | |
Fix flaking DeviceIdleController tests.
The mock DeviceIdleController.Constants object had all of its values set
to 0, so setAlarmSoon wasn't working properly because it would set the
alarm to now + 0, which would have passed by the time the next line in
the test came around.
Bug: 118639768
Test: atest com.android.server.DeviceIdleControllerTest
Change-Id: Ie485e9319d478e4887dc49e4715ddd6ccce9e768
| -rw-r--r-- | services/core/java/com/android/server/DeviceIdleController.java | 10 | ||||
| -rw-r--r-- | services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java | 25 |
2 files changed, 22 insertions, 13 deletions
diff --git a/services/core/java/com/android/server/DeviceIdleController.java b/services/core/java/com/android/server/DeviceIdleController.java index 08cb7a2f5047..ac17d878684d 100644 --- a/services/core/java/com/android/server/DeviceIdleController.java +++ b/services/core/java/com/android/server/DeviceIdleController.java @@ -1031,9 +1031,9 @@ public class DeviceIdleController extends SystemService INACTIVE_TIMEOUT = mParser.getDurationMillis(KEY_INACTIVE_TIMEOUT, !COMPRESS_TIME ? inactiveTimeoutDefault : (inactiveTimeoutDefault / 10)); SENSING_TIMEOUT = mParser.getDurationMillis(KEY_SENSING_TIMEOUT, - !DEBUG ? 4 * 60 * 1000L : 60 * 1000L); + !COMPRESS_TIME ? 4 * 60 * 1000L : 60 * 1000L); LOCATING_TIMEOUT = mParser.getDurationMillis(KEY_LOCATING_TIMEOUT, - !DEBUG ? 30 * 1000L : 15 * 1000L); + !COMPRESS_TIME ? 30 * 1000L : 15 * 1000L); LOCATION_ACCURACY = mParser.getFloat(KEY_LOCATION_ACCURACY, 20); MOTION_INACTIVE_TIMEOUT = mParser.getDurationMillis(KEY_MOTION_INACTIVE_TIMEOUT, !COMPRESS_TIME ? 10 * 60 * 1000L : 60 * 1000L); @@ -1562,6 +1562,7 @@ public class DeviceIdleController extends SystemService static class Injector { private final Context mContext; private ConnectivityService mConnectivityService; + private Constants mConstants; private LocationManager mLocationManager; Injector(Context ctx) { @@ -1591,7 +1592,10 @@ public class DeviceIdleController extends SystemService Constants getConstants(DeviceIdleController controller, Handler handler, ContentResolver resolver) { - return controller.new Constants(handler, resolver); + if (mConstants == null) { + mConstants = controller.new Constants(handler, resolver); + } + return mConstants; } LocationManager getLocationManager() { diff --git a/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java b/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java index cff0521bba50..7ff10bf26392 100644 --- a/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java +++ b/services/tests/mockingservicestests/src/com/android/server/DeviceIdleControllerTest.java @@ -46,6 +46,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; @@ -89,6 +90,7 @@ public class DeviceIdleControllerTest { private DeviceIdleController mDeviceIdleController; private AnyMotionDetectorForTest mAnyMotionDetector; private AppStateTrackerForTest mAppStateTracker; + private DeviceIdleController.Constants mConstants; private InjectorForTest mInjector; private MockitoSession mMockingSession; @@ -97,7 +99,7 @@ public class DeviceIdleControllerTest { @Mock private ConnectivityService mConnectivityService; @Mock - private DeviceIdleController.Constants mConstants; + private ContentResolver mContentResolver; @Mock private IActivityManager mIActivityManager; @Mock @@ -139,13 +141,6 @@ public class DeviceIdleControllerTest { } @Override - DeviceIdleController.Constants getConstants(DeviceIdleController controller, - Handler handler, - ContentResolver resolver) { - return mConstants; - } - - @Override LocationManager getLocationManager() { return locationManager; } @@ -170,6 +165,11 @@ public class DeviceIdleControllerTest { } @Override + public boolean hasSensor() { + return true; + } + + @Override public void checkForAnyMotion() { isMonitoring = true; } @@ -221,6 +221,7 @@ public class DeviceIdleControllerTest { mAppStateTracker = new AppStateTrackerForTest(getContext(), Looper.getMainLooper()); mAnyMotionDetector = new AnyMotionDetectorForTest(); mInjector = new InjectorForTest(getContext()); + doNothing().when(mContentResolver).registerContentObserver(any(), anyBoolean(), any()); mDeviceIdleController = new DeviceIdleController(getContext(), mInjector); spyOn(mDeviceIdleController); doNothing().when(mDeviceIdleController).publishBinderService(any(), any()); @@ -228,6 +229,10 @@ public class DeviceIdleControllerTest { mDeviceIdleController.onBootPhase(SystemService.PHASE_SYSTEM_SERVICES_READY); mDeviceIdleController.setDeepEnabledForTest(true); mDeviceIdleController.setLightEnabledForTest(true); + + // Get the same Constants object that mDeviceIdleController got. + mConstants = mInjector.getConstants(mDeviceIdleController, + mInjector.getHandler(mDeviceIdleController), mContentResolver); } @After @@ -1456,8 +1461,8 @@ public class DeviceIdleControllerTest { private void setAlarmSoon(boolean isSoon) { if (isSoon) { - doReturn(SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM / 2).when( - mAlarmManager).getNextWakeFromIdleTime(); + doReturn(SystemClock.elapsedRealtime() + mConstants.MIN_TIME_TO_ALARM / 2) + .when(mAlarmManager).getNextWakeFromIdleTime(); } else { doReturn(Long.MAX_VALUE).when(mAlarmManager).getNextWakeFromIdleTime(); } |