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

Commit 45a0ff7a authored by Rupesh Bansal's avatar Rupesh Bansal
Browse files

Fixed the failing tests in com.android.server.dreams

We were seeing the issue where
DreamManagerServiceMockingTest::testSettingsQueryUserChange was failing because of
lack of a permission, but when that permission was given, other tests in
the same module started to fail. The issue was we were not waiting for
the handler thread to be successfully executed before moving onto the
next test. This is now being fixed using a TestHandler. However, given
DreamManagerService is using DreamHandler, we had to change the handler
type in DreamManagerService to Handler. This means we might face an
issue with this test if we were to add a new utility in DreamHandler,
but given we have not seen any such usecases soo far, lets defer to
solving for that scenario for the future.

Test: atest com.android.server.dreams
Bug: 293443309
Change-Id: I19d235021077ca39cf14d0593ac5a8f5aa0f98a9
parent 4c1bdc28
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -67,6 +67,7 @@ import android.view.Display;

import com.android.internal.R;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.UiEventLogger;
import com.android.internal.logging.UiEventLoggerImpl;
import com.android.internal.util.DumpUtils;
@@ -112,7 +113,7 @@ public final class DreamManagerService extends SystemService {
    private final Object mLock = new Object();

    private final Context mContext;
    private final DreamHandler mHandler;
    private final Handler mHandler;
    private final DreamController mController;
    private final PowerManager mPowerManager;
    private final PowerManagerInternal mPowerManagerInternal;
@@ -211,9 +212,14 @@ public final class DreamManagerService extends SystemService {
    }

    public DreamManagerService(Context context) {
        this(context, new DreamHandler(FgThread.get().getLooper()));
    }

    @VisibleForTesting
    DreamManagerService(Context context, Handler handler) {
        super(context);
        mContext = context;
        mHandler = new DreamHandler(FgThread.get().getLooper());
        mHandler = handler;
        mController = new DreamController(context, mHandler, mControllerListener);

        mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
@@ -244,7 +250,6 @@ public final class DreamManagerService extends SystemService {
                com.android.internal.R.bool.config_keepDreamingWhenUnplugging);
        mDreamsDisabledByAmbientModeSuppressionConfig = mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_dreamsDisabledByAmbientModeSuppressionConfig);

    }

    @Override
+5 −0
Original line number Diff line number Diff line
@@ -17,6 +17,11 @@ android_test {
        "mockito-target-minus-junit4",
        "services.core",
        "mockingservicestests-utils-mockito",
        "servicestests-utils",
    ],

    defaults: [
        "modules-utils-testable-device-config-defaults",
    ],

    platform_apis: true,
+1 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
    Insert permissions here. eg:
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
    -->
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />

    <application android:debuggable="true"
                 android:testOnly="true">
+13 −17
Original line number Diff line number Diff line
@@ -36,13 +36,14 @@ import android.os.UserManager;
import android.provider.Settings;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.FlakyTest;

import com.android.server.LocalServices;
import com.android.internal.util.test.LocalServiceKeeperRule;
import com.android.server.SystemService;
import com.android.server.testutils.TestHandler;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -65,23 +66,24 @@ public class DreamManagerServiceMockingTest {
    @Mock
    private UserManager mUserManagerMock;

    private MockitoSession mMockitoSession;
    @Rule
    public LocalServiceKeeperRule mLocalServiceKeeperRule = new LocalServiceKeeperRule();

    private static <T> void addLocalServiceMock(Class<T> clazz, T mock) {
        LocalServices.removeServiceForTest(clazz);
        LocalServices.addService(clazz, mock);
    }
    private TestHandler mTestHandler;
    private MockitoSession mMockitoSession;

    @Before
    public void setUp() throws Exception {
        mTestHandler = new TestHandler(/* callback= */ null);
        MockitoAnnotations.initMocks(this);

        mContextSpy = spy(new ContextWrapper(InstrumentationRegistry.getContext()));
        mResourcesSpy = spy(mContextSpy.getResources());
        when(mContextSpy.getResources()).thenReturn(mResourcesSpy);

        addLocalServiceMock(ActivityManagerInternal.class, mActivityManagerInternalMock);
        addLocalServiceMock(PowerManagerInternal.class, mPowerManagerInternalMock);
        mLocalServiceKeeperRule.overrideLocalService(
                ActivityManagerInternal.class, mActivityManagerInternalMock);
        mLocalServiceKeeperRule.overrideLocalService(
                PowerManagerInternal.class, mPowerManagerInternalMock);

        when(mContextSpy.getSystemService(UserManager.class)).thenReturn(mUserManagerMock);
        mMockitoSession = mockitoSession()
@@ -94,26 +96,20 @@ public class DreamManagerServiceMockingTest {
    @After
    public void tearDown() throws Exception {
        mMockitoSession.finishMocking();
        LocalServices.removeServiceForTest(ActivityManagerInternal.class);
        LocalServices.removeServiceForTest(PowerManagerInternal.class);
    }

    private DreamManagerService createService() {
        return new DreamManagerService(mContextSpy);
        return new DreamManagerService(mContextSpy, mTestHandler);
    }

    @Test
    @FlakyTest(bugId = 293443309)
    public void testSettingsQueryUserChange() {
        final DreamManagerService service = createService();

        final SystemService.TargetUser from =
                new SystemService.TargetUser(mock(UserInfo.class));
        final SystemService.TargetUser to =
                new SystemService.TargetUser(mock(UserInfo.class));

        service.onUserSwitching(from, to);

        verify(() -> Settings.Secure.getIntForUser(any(),
                eq(Settings.Secure.SCREENSAVER_ENABLED),
                anyInt(),