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

Commit dbcf8a0a authored by Robert Horvath's avatar Robert Horvath
Browse files

Set DreamsActivateOnSleepSetting and handle Sandman in tests

Two PowerManagerServiceTest were failing on TV:
- testRemovedDisplayGroupWakeLock_affectsNoDisplayGroups
- testWakeLock_affectsProperDisplayGroup

These tests expected the device to be DOZING at the end of the test,
but the devices was found to be DREAMING.
This difference came from `mDreamsActivateOnSleepSetting`, which was
not set for phones, but is set on TV.
Two tests are added to test the behaviour of this setting.

This CL also changes a call to `Handler#sendMessage` to
`Handler#sendMessageAtTime`, which schedules the message according to
the time of the test clock, instead of the real SystemClock.
This causes `handleSandman` to actually run during the test,
which would put the device to sleep if DOZING and dream couldn't be
started, so when #startDream is called on the DreamManager mock,
we set #isDreaming on the mock.

Bug: 205837239
Test: atest PowerManagerServiceTest
Change-Id: I6f27ea6a5cf8ddb1b7735c8d6ef67f27a5eb3747
parent b8bfc0a0
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -1998,7 +1998,7 @@ public final class PowerManagerService extends SystemService
    private boolean dozePowerGroupLocked(final PowerGroup powerGroup, long eventTime,
            int reason, int uid) {
        if (DEBUG_SPEW) {
            Slog.d(TAG, "sleepDisplayGroupNoUpdateLocked: eventTime=" + eventTime
            Slog.d(TAG, "dozePowerGroup: eventTime=" + eventTime
                    + ", groupId=" + powerGroup.getGroupId() + ", reason=" + reason
                    + ", uid=" + uid);
        }
@@ -3127,7 +3127,7 @@ public final class PowerManagerService extends SystemService
                    Message msg = mHandler.obtainMessage(MSG_SANDMAN);
                    msg.arg1 = powerGroup.getGroupId();
                    msg.setAsynchronous(true);
                    mHandler.sendMessage(msg);
                    mHandler.sendMessageAtTime(msg, mClock.uptimeMillis());
                }
            }
        }
+49 −0
Original line number Diff line number Diff line
@@ -213,6 +213,8 @@ public class PowerManagerServiceTest {

        Settings.Global.putInt(mContextSpy.getContentResolver(),
                Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);
        Settings.Secure.putInt(mContextSpy.getContentResolver(),
                Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP, 0);

        mClock = new OffsettableClock.Stopped();
        mTestLooper = new TestLooper(mClock::now);
@@ -725,6 +727,48 @@ public class PowerManagerServiceTest {
        assertThat(mService.getBinderServiceInstance().forceSuspend()).isFalse();
    }

    @SuppressWarnings("GuardedBy")
    @Test
    public void testScreensaverActivateOnSleepDisabled_powered_afterTimeout_goesToDozing() {
        when(mBatteryManagerInternalMock.isPowered(anyInt())).thenReturn(true);

        doAnswer(inv -> {
            when(mDreamManagerInternalMock.isDreaming()).thenReturn(true);
            return null;
        }).when(mDreamManagerInternalMock).startDream(anyBoolean());

        setMinimumScreenOffTimeoutConfig(5);
        createService();
        startSystem();

        assertThat(mService.getGlobalWakefulnessLocked()).isEqualTo(WAKEFULNESS_AWAKE);

        advanceTime(15000);
        assertThat(mService.getGlobalWakefulnessLocked()).isEqualTo(WAKEFULNESS_DOZING);
    }

    @SuppressWarnings("GuardedBy")
    @Test
    public void testScreensaverActivateOnSleepEnabled_powered_afterTimeout_goesToDreaming() {
        when(mBatteryManagerInternalMock.isPowered(anyInt())).thenReturn(true);
        Settings.Secure.putInt(mContextSpy.getContentResolver(),
                Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP, 1);

        doAnswer(inv -> {
            when(mDreamManagerInternalMock.isDreaming()).thenReturn(true);
            return null;
        }).when(mDreamManagerInternalMock).startDream(anyBoolean());

        setMinimumScreenOffTimeoutConfig(5);
        createService();
        startSystem();

        assertThat(mService.getGlobalWakefulnessLocked()).isEqualTo(WAKEFULNESS_AWAKE);

        advanceTime(15000);
        assertThat(mService.getGlobalWakefulnessLocked()).isEqualTo(WAKEFULNESS_DREAMING);
    }

    @Test
    public void testSetDozeOverrideFromDreamManager_triggersSuspendBlocker() {
        final String suspendBlockerName = "PowerManagerService.Display";
@@ -1152,6 +1196,11 @@ public class PowerManagerServiceTest {
        info.displayGroupId = nonDefaultDisplayGroupId;
        when(mDisplayManagerInternalMock.getDisplayInfo(nonDefaultDisplay)).thenReturn(info);

        doAnswer(inv -> {
            when(mDreamManagerInternalMock.isDreaming()).thenReturn(true);
            return null;
        }).when(mDreamManagerInternalMock).startDream(anyBoolean());

        final String pkg = mContextSpy.getOpPackageName();
        final Binder token = new Binder();
        final String tag = "testRemovedDisplayGroupWakeLock_affectsNoDisplayGroups";