Remove window container from empty task when destroying.

Previously, the window container was being removed whenever we were
removing the task in a destroy mode. However, this caused issues
where an activity may still be present in the task record, leading to
a subequent change to limit this to tasks with overlays. This led to
the situation where the window container would not be destroyed when
it was supposed to, such as moving an activity to recents, but
otherwise destroying.

This changelist addresses the issue by always removing the window
container from tasks when removed from their parent stack in a
destroy mode and empty. In the recents flow, this will fire and
cleanup when the activity is destroyed.

Change-Id: I87548d6ff8e4d77b6294504d7cc78370cd5d31fa
Fixes: 37301159
Test: bit FrameworksServicesTests:com.android.server.am.ActivityStackTests
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 5240586..3bf0e5f 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
@@ -18,6 +18,7 @@
 
 import static org.mockito.Mockito.mock;
 
+import android.app.ActivityManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -62,14 +63,16 @@
     }
 
     protected ActivityManagerService createActivityManagerService() {
-        return new TestActivityManagerService(mContext);
+        final ActivityManagerService service = new TestActivityManagerService(mContext);
+        service.mWindowManager = WindowTestUtils.getWindowManagerService(mContext);
+        return service;
     }
 
     protected static TestActivityStack createActivityStack(ActivityManagerService service,
             int stackId, int displayId, boolean onTop) {
         if (service.mStackSupervisor instanceof TestActivityStackSupervisor) {
             final TestActivityStack stack = ((TestActivityStackSupervisor) service.mStackSupervisor)
-                    .createTestStack(stackId, onTop);
+                    .createTestStack(service, stackId, onTop);
             return stack;
         }
 
@@ -109,7 +112,7 @@
         intent.setComponent(component);
 
         final TaskRecord task = new TaskRecord(service, 0, aInfo, intent /*intent*/,
-                null /*_taskDescription*/, null /*thumbnailInfo*/);
+                null /*_taskDescription*/, new ActivityManager.TaskThumbnailInfo());
         stack.addTask(task, true, "creating test task");
         task.setStack(stack);
         task.createWindowContainer(true, true);
@@ -146,25 +149,39 @@
         void updateUIDsPresentOnDisplay() {
         }
 
-        public TestActivityStack createTestStack(int stackId, boolean onTop) {
+        public TestActivityStack createTestStack(ActivityManagerService service, int stackId,
+                boolean onTop) {
             final ActivityDisplay display = new ActivityDisplay();
             final TestActivityContainer container =
-                    new TestActivityContainer(stackId, display, onTop);
+                    new TestActivityContainer(service, stackId, display, onTop);
             return container.getStack();
         }
 
         private class TestActivityContainer extends ActivityContainer {
+            private ActivityManagerService mService;
             private TestActivityStack mStack;
-            TestActivityContainer(int stackId, ActivityDisplay activityDisplay, boolean onTop) {
+            private boolean mOnTop;
+
+            TestActivityContainer(ActivityManagerService service, int stackId,
+                    ActivityDisplay activityDisplay, boolean onTop) {
                 super(stackId, activityDisplay, onTop);
+                mService = service;
             }
 
             @Override
             protected void createStack(int stackId, boolean onTop) {
-                mStack = new TestActivityStack(this, null /*recentTasks*/, onTop);
+                // normally stack creation is done here. However we need to do it on demand since
+                // we cannot set {@link mService} by the time the super constructor calling this
+                // method is invoked.
+                mOnTop = onTop;
             }
 
             public TestActivityStack getStack() {
+                if (mStack == null) {
+                    mStack = new TestActivityStack(this,
+                            new RecentTasks(mService, mService.mStackSupervisor), mOnTop);
+                }
+
                 return mStack;
             }
         }