Make ActivityStackSupervisor instrumentable for unit tests.

Currently, the ActivityStackSupervisor passes an instance of itself
during construction to a number of its member variables. Passing
a self reference at this time leads to an incomplete object being
passed. In the case of testing, it's possible for the instance to be
copied after construction for instrumentation, leading to the
original instance being retained.

This changelist addresses this by moving the creation of these
variables to a separate initialize methods. In addition, the affected
variables accessed outside the class are now behind accessors to
prevent their modification.

Bug: 64750076
Test: bit FrameworksServicesTests:com.android.server.am.ActivityStarterTests
Test: bit FrameworksServicesTests:com.android.server.am.ActivityStackSupervisorTests
Test: bit FrameworksServicesTests:com.android.server.am.ActivityStackTests
Test: bit FrameworksServicesTests:com.android.server.am.LaunchingBoundsControllerTests
Test: bit FrameworksServicesTests:com.android.server.am.LaunchingActivityPositionerTests
Test: bit FrameworksServicesTests:com.android.server.am.LaunchingTaskPositionerTests
Test: runtest --path frameworks/base/services/tests/servicestests/src/com/android/server/am/RecentTasksTest.java
Change-Id: I19647b24925b82db0ab0955585d7588a28e00f94
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
index 198cc6d..f04b6d8 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
@@ -21,8 +21,11 @@
 import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
 import static android.view.Display.DEFAULT_DISPLAY;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyBoolean;
+import static org.mockito.Mockito.anyInt;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.spy;
 
@@ -255,7 +258,27 @@
         }
 
         @Override
-        protected ActivityStackSupervisor createStackSupervisor() {
+        final protected ActivityStackSupervisor createStackSupervisor() {
+            final ActivityStackSupervisor supervisor = spy(createTestSupervisor());
+
+            // No home stack is set.
+            doNothing().when(supervisor).moveHomeStackToFront(any());
+            doReturn(true).when(supervisor).moveHomeStackTaskToTop(any());
+            // 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());
+
+            supervisor.initialize();
+
+            return supervisor;
+        }
+
+        protected ActivityStackSupervisor createTestSupervisor() {
             return new TestActivityStackSupervisor(this, mHandlerThread.getLooper());
         }
 
@@ -269,14 +292,18 @@
      * setup not available in the test environment. Also specifies an injector for
      */
     protected static class TestActivityStackSupervisor extends ActivityStackSupervisor {
-        private final ActivityDisplay mDisplay;
-        private boolean mLastResizeable;
+        private ActivityDisplay mDisplay;
 
         public TestActivityStackSupervisor(ActivityManagerService service, Looper looper) {
             super(service, looper);
             mDisplayManager =
                     (DisplayManager) mService.mContext.getSystemService(Context.DISPLAY_SERVICE);
             mWindowManager = prepareMockWindowManager();
+        }
+
+        @Override
+        public void initialize() {
+            super.initialize();
             mDisplay = new TestActivityDisplay(this, DEFAULT_DISPLAY);
             attachDisplay(mDisplay);
         }
@@ -286,54 +313,11 @@
             return mDisplay;
         }
 
-        // TODO: Use Mockito spy instead. Currently not possible due to TestActivityStackSupervisor
-        // access to ActivityDisplay
-        @Override
-        boolean canPlaceEntityOnDisplay(int displayId, boolean resizeable, int callingPid,
-                int callingUid, ActivityInfo activityInfo) {
-            mLastResizeable = resizeable;
-            return super.canPlaceEntityOnDisplay(displayId, resizeable, callingPid, callingUid,
-                    activityInfo);
-        }
-
-        // TODO: remove and use Mockito verify once {@link #canPlaceEntityOnDisplay} override is
-        // removed.
-        public boolean getLastResizeableFromCanPlaceEntityOnDisplay() {
-            return mLastResizeable;
-        }
-
-        // No home stack is set.
-        @Override
-        void moveHomeStackToFront(String reason) {
-        }
-
-        @Override
-        boolean moveHomeStackTaskToTop(String reason) {
-            return true;
-        }
-
-        // Invoked during {@link ActivityStack} creation.
-        @Override
-        void updateUIDsPresentOnDisplay() {
-        }
-
-        // Just return the current front task.
+        // Just return the current front task. This is called internally so we cannot use spy to mock this out.
         @Override
         ActivityStack getNextFocusableStackLocked(ActivityStack currentFocus) {
             return mFocusedStack;
         }
-
-        // Called when moving activity to pinned stack.
-        @Override
-        void ensureActivitiesVisibleLocked(ActivityRecord starting, int configChanges,
-                boolean preserveWindows) {
-        }
-
-        // Always keep things awake
-        @Override
-        boolean hasAwakeDisplay() {
-            return true;
-        }
     }
 
     private static class TestActivityDisplay extends ActivityDisplay {