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

Commit f0ae428f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Switch some objects in WmTest to use spyOn()"

parents 2a71dae5 b73f3965
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -315,7 +315,7 @@ public class ActivityStackSupervisor extends ConfigurationContainer implements D
    /** The number of distinct task ids that can be assigned to the tasks of a single user */
    private static final int MAX_TASK_IDS_PER_USER = UserHandle.PER_USER_RANGE;

    ActivityTaskManagerService mService;
    final ActivityTaskManagerService mService;

    /** The historial list of recent tasks including inactive tasks */
    RecentTasks mRecentTasks;
@@ -617,11 +617,6 @@ public class ActivityStackSupervisor extends ConfigurationContainer implements D
        mHandler = new ActivityStackSupervisorHandler(looper);
    }

    @VisibleForTesting
    void setService(ActivityTaskManagerService service) {
        mService = service;
    }

    @VisibleForTesting
    void setWindowContainerController(RootWindowContainerController controller) {
        mWindowContainerController = controller;
+4 −3
Original line number Diff line number Diff line
@@ -341,7 +341,8 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
    ActivityManagerInternal mAmInternal;
    UriGrantsManagerInternal mUgmInternal;
    private PackageManagerInternal mPmInternal;
    private ActivityTaskManagerInternal mInternal;
    @VisibleForTesting
    final ActivityTaskManagerInternal mInternal;
    PowerManagerInternal mPowerManagerInternal;
    private UsageStatsManagerInternal mUsageStatsInternal;

@@ -643,6 +644,7 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
        mSystemThread = ActivityThread.currentActivityThread();
        mUiContext = mSystemThread.getSystemUiContext();
        mLifecycleManager = new ClientLifecycleManager();
        mInternal = new LocalService();
        GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version", GL_ES_VERSION_UNDEFINED);
    }

@@ -893,7 +895,6 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub {
    }

    private void start() {
        mInternal = new LocalService();
        LocalServices.addService(ActivityTaskManagerInternal.class, mInternal);
    }

+76 −101
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.doNothing;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.server.wm.ActivityStack.REMOVE_TASK_MODE_DESTROYING;
import static com.android.server.wm.ActivityStackSupervisor.ON_TOP;

@@ -66,6 +67,8 @@ import com.android.server.AppOpsService;
import com.android.server.AttributeCache;
import com.android.server.ServiceThread;
import com.android.server.am.ActivityManagerService;
import com.android.server.am.PendingIntentController;
import com.android.server.firewall.IntentFirewall;
import com.android.server.uri.UriGrantsManagerInternal;

import org.junit.After;
@@ -115,27 +118,13 @@ class ActivityTestsBase {
    }

    ActivityTaskManagerService createActivityTaskManagerService() {
        final TestActivityTaskManagerService atm =
                spy(new TestActivityTaskManagerService(mContext));
        setupActivityManagerService(atm);
        return atm;
    }

    void setupActivityTaskManagerService() {
        mService = createActivityTaskManagerService();
        mService = new TestActivityTaskManagerService(mContext);
        mSupervisor = mService.mStackSupervisor;
        return mService;
    }

    ActivityManagerService createActivityManagerService() {
        final TestActivityTaskManagerService atm =
                spy(new TestActivityTaskManagerService(mContext));
        return setupActivityManagerService(atm);
    }

    ActivityManagerService setupActivityManagerService(TestActivityTaskManagerService atm) {
        final TestActivityManagerService am = spy(new TestActivityManagerService(mTestInjector));
        setupActivityManagerService(am, atm);
        return am;
    void setupActivityTaskManagerService() {
        createActivityTaskManagerService();
    }

    /** Creates a {@link TestActivityDisplay}. */
@@ -154,32 +143,6 @@ class ActivityTestsBase {
        return display;
    }

    void setupActivityManagerService(
            TestActivityManagerService am, TestActivityTaskManagerService atm) {
        atm.setActivityManagerService(am.mIntentFirewall, am.mPendingIntentController);
        atm.mAmInternal = am.getLocalService();
        am.mAtmInternal = atm.getLocalService();
        // Makes sure the supervisor is using with the spy object.
        atm.mStackSupervisor.setService(atm);
        doReturn(mock(IPackageManager.class)).when(am).getPackageManager();
        doReturn(mock(IPackageManager.class)).when(atm).getPackageManager();
        PackageManagerInternal mockPackageManager = mock(PackageManagerInternal.class);
        doReturn(mockPackageManager).when(am).getPackageManagerInternalLocked();
        doReturn(null).when(mockPackageManager).getDefaultHomeActivity(anyInt());
        doNothing().when(am).grantEphemeralAccessLocked(anyInt(), any(), anyInt(), anyInt());
        am.mActivityTaskManager = atm;
        am.mWindowManager = prepareMockWindowManager();
        atm.setWindowManager(am.mWindowManager);

        // Put a home stack on the default display, so that we'll always have something focusable.
        final TestActivityStackSupervisor supervisor =
                (TestActivityStackSupervisor) atm.mStackSupervisor;
        supervisor.mDisplay.createStack(WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
        final TaskRecord task = new TaskBuilder(atm.mStackSupervisor)
                .setStack(supervisor.getDefaultDisplay().getHomeStack()).build();
        new ActivityBuilder(atm).setTask(task).build();
    }

    /**
     * Builder for creating new activities.
     */
@@ -405,37 +368,53 @@ class ActivityTestsBase {
        }
    }

    protected static class TestActivityTaskManagerService extends ActivityTaskManagerService {
        private LockTaskController mLockTaskController;
        private ActivityTaskManagerInternal mInternal;
    protected class TestActivityTaskManagerService extends ActivityTaskManagerService {
        private PackageManagerInternal mPmInternal;

        // ActivityStackSupervisor may be created more than once while setting up AMS and ATMS.
        // We keep the reference in order to prevent creating it twice.
        private ActivityStackSupervisor mTestStackSupervisor;
        ActivityStackSupervisor mTestStackSupervisor;

        TestActivityTaskManagerService(Context context) {
            super(context);
            spyOn(this);

            mUgmInternal = mock(UriGrantsManagerInternal.class);

            mSupportsMultiWindow = true;
            mSupportsMultiDisplay = true;
            mSupportsSplitScreenMultiWindow = true;
            mSupportsFreeformWindowManagement = true;
            mSupportsPictureInPicture = true;
            mUgmInternal = mock(UriGrantsManagerInternal.class);
        }

        @Override
        int handleIncomingUser(int callingPid, int callingUid, int userId, String name) {
            return userId;
            final TestActivityManagerService am =
                    new TestActivityManagerService(mTestInjector, this);

            // Put a home stack on the default display, so that we'll always have something
            // focusable.
            final TestActivityStackSupervisor supervisor =
                    (TestActivityStackSupervisor) mStackSupervisor;
            supervisor.mDisplay.createStack(WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_HOME, ON_TOP);
            final TaskRecord task = new TaskBuilder(mStackSupervisor)
                    .setStack(supervisor.getDefaultDisplay().getHomeStack()).build();
            new ActivityBuilder(this).setTask(task).build();

            spyOn(getLifecycleManager());
            spyOn(getLockTaskController());
            doReturn(mock(IPackageManager.class)).when(this).getPackageManager();
        }

        @Override
        public LockTaskController getLockTaskController() {
            if (mLockTaskController == null) {
                mLockTaskController = spy(super.getLockTaskController());
        void setActivityManagerService(IntentFirewall intentFirewall,
                PendingIntentController intentController, ActivityManagerInternal amInternal,
                WindowManagerService wm) {
            mAmInternal = amInternal;
            setActivityManagerService(intentFirewall, intentController);
            setWindowManager(wm);
        }

            return mLockTaskController;
        @Override
        int handleIncomingUser(int callingPid, int callingUid, int userId, String name) {
            return userId;
        }

        @Override
@@ -443,41 +422,13 @@ class ActivityTestsBase {
        }

        @Override
        protected final ActivityStackSupervisor createStackSupervisor() {
        protected ActivityStackSupervisor createStackSupervisor() {
            if (mTestStackSupervisor == null) {
                final ActivityStackSupervisor supervisor = spy(createTestSupervisor());
                final KeyguardController keyguardController = mock(KeyguardController.class);

                // Invoked during {@link ActivityStack} creation.
                doNothing().when(supervisor).updateUIDsPresentOnDisplay();
                // Always keep things awake.
                doReturn(true).when(supervisor).hasAwakeDisplay();
                // Called when moving activity to pinned stack.
                doNothing().when(supervisor).ensureActivitiesVisibleLocked(any(), anyInt(),
                        anyBoolean());
                // Do not schedule idle timeouts
                doNothing().when(supervisor).scheduleIdleTimeoutLocked(any());
                // unit test version does not handle launch wake lock
                doNothing().when(supervisor).acquireLaunchWakelock();
                doReturn(keyguardController).when(supervisor).getKeyguardController();

                supervisor.initialize();
                mTestStackSupervisor = supervisor;
                mTestStackSupervisor = new TestActivityStackSupervisor(this, mH.getLooper());
            }
            return mTestStackSupervisor;
        }

        protected ActivityStackSupervisor createTestSupervisor() {
            return new TestActivityStackSupervisor(this, mH.getLooper());
        }

        ActivityTaskManagerInternal getLocalService() {
            if (mInternal == null) {
                mInternal = new ActivityTaskManagerService.LocalService();
            }
            return mInternal;
        }

        @Override
        PackageManagerInternal getPackageManagerInternalLocked() {
            if (mPmInternal == null) {
@@ -524,24 +475,31 @@ class ActivityTestsBase {
        }
    }

    // TODO: Replace this with a mock object since we are no longer in AMS package.
    /**
     * An {@link ActivityManagerService} subclass which provides a test
     * {@link ActivityStackSupervisor}.
     */
    static class TestActivityManagerService extends ActivityManagerService {

        private ActivityManagerInternal mInternal;
    class TestActivityManagerService extends ActivityManagerService {

        TestActivityManagerService(TestInjector testInjector) {
        TestActivityManagerService(TestInjector testInjector, TestActivityTaskManagerService atm) {
            super(testInjector, testInjector.mHandlerThread);
            spyOn(this);

            mWindowManager = prepareMockWindowManager();
            mUgmInternal = mock(UriGrantsManagerInternal.class);
        }

        ActivityManagerInternal getLocalService() {
            if (mInternal == null) {
                mInternal = new LocalService();
            }
            return mInternal;
            atm.setActivityManagerService(mIntentFirewall, mPendingIntentController,
                    new LocalService(), mWindowManager);

            mActivityTaskManager = atm;
            mAtmInternal = atm.mInternal;

            doReturn(mock(IPackageManager.class)).when(this).getPackageManager();
            PackageManagerInternal mockPackageManager = mock(PackageManagerInternal.class);
            doReturn(mockPackageManager).when(this).getPackageManagerInternalLocked();
            doReturn(null).when(mockPackageManager).getDefaultHomeActivity(anyInt());
            doNothing().when(this).grantEphemeralAccessLocked(anyInt(), any(), anyInt(), anyInt());
        }
    }

@@ -549,23 +507,40 @@ class ActivityTestsBase {
     * An {@link ActivityStackSupervisor} which stubs out certain methods that depend on
     * setup not available in the test environment. Also specifies an injector for
     */
    protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
    protected class TestActivityStackSupervisor extends ActivityStackSupervisor {
        private ActivityDisplay mDisplay;
        private KeyguardController mKeyguardController;

        public TestActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
        TestActivityStackSupervisor(ActivityTaskManagerService service, Looper looper) {
            super(service, looper);
            spyOn(this);
            mDisplayManager =
                    (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
            mWindowManager = prepareMockWindowManager();
            mKeyguardController = mock(KeyguardController.class);
            setWindowContainerController(mock(RootWindowContainerController.class));

            // Invoked during {@link ActivityStack} creation.
            doNothing().when(this).updateUIDsPresentOnDisplay();
            // Always keep things awake.
            doReturn(true).when(this).hasAwakeDisplay();
            // Called when moving activity to pinned stack.
            doNothing().when(this).ensureActivitiesVisibleLocked(any(), anyInt(),
                    anyBoolean());
            // Do not schedule idle timeouts
            doNothing().when(this).scheduleIdleTimeoutLocked(any());
            // unit test version does not handle launch wake lock
            doNothing().when(this).acquireLaunchWakelock();
            doReturn(mKeyguardController).when(this).getKeyguardController();

            initialize();
        }

        @Override
        public void initialize() {
            super.initialize();
            mDisplay = spy(TestActivityDisplay.create(this, DEFAULT_DISPLAY));
            mDisplay = TestActivityDisplay.create(this, DEFAULT_DISPLAY);
            spyOn(mDisplay);
            addChild(mDisplay, ActivityDisplay.POSITION_TOP);
        }

+6 −17
Original line number Diff line number Diff line
@@ -110,9 +110,7 @@ public class RecentTasksTest extends ActivityTestsBase {
    @Before
    public void setUp() throws Exception {
        mTaskPersister = new TestTaskPersister(mContext.getFilesDir());
        mTestService = spy(new MyTestActivityTaskManagerService(mContext));
        final TestActivityManagerService am = spy(new MyTestActivityManagerService());
        setupActivityManagerService(am, mTestService);
        mTestService = new MyTestActivityTaskManagerService(mContext);
        mRecentTasks = (TestRecentTasks) mTestService.getRecentTasks();
        mRecentTasks.loadParametersFromResources(mContext.getResources());
        mHomeStack = mTestService.mStackSupervisor.getDefaultDisplay().getOrCreateStack(
@@ -868,20 +866,11 @@ public class RecentTasksTest extends ActivityTestsBase {
        }

        @Override
        protected ActivityStackSupervisor createTestSupervisor() {
            return new MyTestActivityStackSupervisor(this, mH.getLooper());
        protected ActivityStackSupervisor createStackSupervisor() {
            if (mTestStackSupervisor == null) {
                mTestStackSupervisor = new MyTestActivityStackSupervisor(this, mH.getLooper());
            }

    }

    private class MyTestActivityManagerService extends TestActivityManagerService {
        MyTestActivityManagerService() {
            super(mTestInjector);
        }

        @Override
        public boolean isUserRunning(int userId, int flags) {
            return true;
            return mTestStackSupervisor;
        }
    }

+15 −25
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.eq;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
import static com.android.server.wm.RecentsAnimationController.REORDER_KEEP_IN_PLACE;
@@ -50,61 +51,50 @@ import org.junit.Test;
public class RecentsAnimationTest extends ActivityTestsBase {

    private Context mContext = InstrumentationRegistry.getContext();
    private TestActivityTaskManagerService mTestService;
    private ComponentName mRecentsComponent;

    @Before
    public void setUp() throws Exception {
        mRecentsComponent = new ComponentName(mContext.getPackageName(), "RecentsActivity");
        mTestService = spy(new MyTestActivityTaskManagerService(mContext));
        setupActivityManagerService(mTestService);
        mService = new TestActivityTaskManagerService(mContext);

        final RecentTasks recentTasks = mService.getRecentTasks();
        spyOn(recentTasks);
        mRecentsComponent = new ComponentName(mContext.getPackageName(), "RecentsActivity");
        doReturn(mRecentsComponent).when(recentTasks).getRecentsComponent();
    }

    @Test
    public void testCancelAnimationOnStackOrderChange() {
        ActivityStack fullscreenStack =
                mTestService.mStackSupervisor.getDefaultDisplay().createStack(
                mService.mStackSupervisor.getDefaultDisplay().createStack(
                        WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
        ActivityStack recentsStack = mTestService.mStackSupervisor.getDefaultDisplay().createStack(
        ActivityStack recentsStack = mService.mStackSupervisor.getDefaultDisplay().createStack(
                WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_RECENTS, true /* onTop */);
        ActivityRecord recentsActivity = new ActivityBuilder(mTestService)
        ActivityRecord recentsActivity = new ActivityBuilder(mService)
                .setComponent(mRecentsComponent)
                .setCreateTask(true)
                .setStack(recentsStack)
                .build();
        ActivityStack fullscreenStack2 =
                mTestService.mStackSupervisor.getDefaultDisplay().createStack(
                mService.mStackSupervisor.getDefaultDisplay().createStack(
                        WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD, true /* onTop */);
        ActivityRecord fsActivity = new ActivityBuilder(mTestService)
        ActivityRecord fsActivity = new ActivityBuilder(mService)
                .setComponent(new ComponentName(mContext.getPackageName(), "App1"))
                .setCreateTask(true)
                .setStack(fullscreenStack2)
                .build();
        doReturn(true).when(mTestService.mWindowManager).canStartRecentsAnimation();
        doReturn(true).when(mService.mWindowManager).canStartRecentsAnimation();

        // Start the recents animation
        Intent recentsIntent = new Intent();
        recentsIntent.setComponent(mRecentsComponent);
        mTestService.startRecentsActivity(recentsIntent, null, mock(IRecentsAnimationRunner.class));
        mService.startRecentsActivity(recentsIntent, null, mock(IRecentsAnimationRunner.class));

        fullscreenStack.moveToFront("Activity start");

        // Ensure that the recents animation was canceled
        verify(mTestService.mWindowManager, times(1)).cancelRecentsAnimationSynchronously(
        verify(mService.mWindowManager, times(1)).cancelRecentsAnimationSynchronously(
                eq(REORDER_KEEP_IN_PLACE), any());
    }

    private class MyTestActivityTaskManagerService extends TestActivityTaskManagerService {
        MyTestActivityTaskManagerService(Context context) {
            super(context);
        }

        @Override
        protected RecentTasks createRecentTasks() {
            RecentTasks recents = mock(RecentTasks.class);
            doReturn(mRecentsComponent).when(recents).getRecentsComponent();
            System.out.println(mRecentsComponent);
            return recents;
        }
    }
}