Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9da2bb93 authored by Kweku Adams's avatar Kweku Adams
Browse files

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
parent 95071588
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -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() {
+15 −10
Original line number Diff line number Diff line
@@ -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
@@ -138,13 +140,6 @@ public class DeviceIdleControllerTest {
            return connectivityService;
        }

        @Override
        DeviceIdleController.Constants getConstants(DeviceIdleController controller,
                Handler handler,
                ContentResolver resolver) {
            return mConstants;
        }

        @Override
        LocationManager getLocationManager() {
            return locationManager;
@@ -169,6 +164,11 @@ public class DeviceIdleControllerTest {
                    mock(DeviceIdleCallback.class), 0.5f);
        }

        @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();
        }