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

Commit 6862c2d6 authored by brycelee's avatar brycelee
Browse files

Allow LocalServices to be overridden more than once.

This changelist allows LocalServices to be overridden multiple
times, which allows default LocalService setup to be changed within
tests.

This change also adds an additional test for PhoneWindowManager
to prevent

Bug: 401205442
Test: atest LocalServiceKeeperRuleTest
Test: atest PhoneWindowManagerTests#powerPress_hubOrDreamOrSleep_noDreamManager_noCrash
Flag: EXEMPT bugfix
Change-Id: I10cfd96757f7dd9953e3967a85bf43e3ef2d7f3c
parent 1f0f41a6
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -283,6 +283,25 @@ public class PhoneWindowManagerTests {
        verify(mPowerManager).goToSleep(eventTime, PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON, 0);
    }

    @Test
    public void powerPress_hubOrDreamOrSleep_noDreamManager_noCrash() {
        mLocalServiceKeeperRule.overrideLocalService(DreamManagerInternal.class,
                null);

        when(mDisplayPolicy.isAwake()).thenReturn(true);
        when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false);
        initPhoneWindowManager();

        // Set power button behavior.
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.POWER_BUTTON_SHORT_PRESS, SHORT_PRESS_POWER_HUB_OR_DREAM_OR_SLEEP);
        mPhoneWindowManager.updateSettings(null);

        // Power button pressed. Make sure no crash occurs
        int eventTime = 0;
        mPhoneWindowManager.powerPress(eventTime, 1, 0);
    }

    @Test
    public void powerPress_hubOrDreamOrSleep_hubAvailableLocks() {
        when(mDisplayPolicy.isAwake()).thenReturn(true);
+21 −7
Original line number Diff line number Diff line
@@ -44,17 +44,31 @@ public class LocalServiceKeeperRule implements TestRule {
        if (!mRuleApplied) {
            throw new IllegalStateException("Can't override service without applying rule");
        }
        if (mOverriddenServices.containsKey(type) || mAddedServices.contains(type)) {
            throw new IllegalArgumentException("Type already overridden: " + type);
        }

        T currentService = LocalServices.getService(type);
        final boolean alreadyOverridden =
                mOverriddenServices.containsKey(type) || mAddedServices.contains(type);

        if (!alreadyOverridden) {
            final T currentService = LocalServices.getService(type);

            if (currentService != null) {
                mOverriddenServices.put(type, currentService);
            }
        }


        // Remove service from LocalServices if present.
        LocalServices.removeServiceForTest(type);
        } else {

        // Remove from tracked AddedServices if present.
        mAddedServices.remove(type);

        // If there is no stored value to restore, then this service is being set for the first
        // time.
        if (!mOverriddenServices.containsKey(type)) {
            mAddedServices.add(type);
        }

        LocalServices.addService(type, service);
    }

+10 −4
Original line number Diff line number Diff line
@@ -73,18 +73,24 @@ public class LocalServiceKeeperRuleTest {
    }

    @Test
    public void testDoesNotAllowToOverrideSameServiceTwice() throws Throwable {
    public void testAllowsOverrideSameServiceTwice() throws Throwable {
        TestService expectedService = new TestService() {};
        LocalServices.addService(TestService.class, expectedService);

        TestService service = new TestService() {};
        TestService secondService = new TestService() {};

        runInRuleApply(() -> {
            mRule.overrideLocalService(TestService.class, service);
            assertThrows(IllegalArgumentException.class,
                    () -> mRule.overrideLocalService(TestService.class, service));
            mRule.overrideLocalService(TestService.class, secondService);
            assertEquals(secondService, LocalServices.getService(TestService.class));
        });

        assertEquals(expectedService, LocalServices.getService(TestService.class));
    }

    @Test
    public void testRestroresLocalServiceAfterTestIfPresent() throws Throwable {
    public void testRestoresLocalServiceAfterTestIfPresent() throws Throwable {
        TestService expectedService = new TestService() {};
        LocalServices.addService(TestService.class, expectedService);
        TestService overriddenService = new TestService() {};