Do not remove the default display during tests.

The default display is used by some tests that exercise features only
functional only on the default display (like policy rotation). This CL
makes this display not get removed with the created test displays and
ensures no collision in id space from a previous test run (done in the
test setup).

Change-Id: Ia14b9c023c779d263283fe8c7b512dca17ff312f
Fix: 36385757
Test: bit FrameworksServicesTests:com.android.server.wm.StackWindowControllerTests#testRemoveContainer_deferRemoval
diff --git a/services/tests/servicestests/src/com/android/server/wm/AppWindowTokenTests.java b/services/tests/servicestests/src/com/android/server/wm/AppWindowTokenTests.java
index 921e0e3..b5826f0 100644
--- a/services/tests/servicestests/src/com/android/server/wm/AppWindowTokenTests.java
+++ b/services/tests/servicestests/src/com/android/server/wm/AppWindowTokenTests.java
@@ -132,9 +132,10 @@
         sWm.mDisplayEnabled = true;
 
         // Create an app window with token on a display.
-        final TaskStack stack = createTaskStackOnDisplay(sDisplayContent);
+        final DisplayContent defaultDisplayContent = sWm.getDefaultDisplayContentLocked();
+        final TaskStack stack = createTaskStackOnDisplay(defaultDisplayContent);
         final Task task = createTaskInStack(stack, 0 /* userId */);
-        final TestAppWindowToken appWindowToken = new TestAppWindowToken(sDisplayContent);
+        final TestAppWindowToken appWindowToken = new TestAppWindowToken(defaultDisplayContent);
         task.addChild(appWindowToken, 0);
         final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(
                 TYPE_BASE_APPLICATION);
diff --git a/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java b/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java
index 65efd9c..ce632ae 100644
--- a/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java
+++ b/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java
@@ -73,8 +73,8 @@
     private final static Session sMockSession = mock(Session.class);
     // The default display is removed in {@link #setUp} and then we iterate over all displays to
     // make sure we don't collide with any existing display. If we run into no other display, the
-    // added display should be treated as default.
-    private static int sNextDisplayId = Display.DEFAULT_DISPLAY;
+    // added display should be treated as default. This cannot be the default display
+    private static int sNextDisplayId = Display.DEFAULT_DISPLAY + 1;
     static int sNextStackId = FIRST_DYNAMIC_STACK_ID;
     private static int sNextTaskId = 0;
 
@@ -105,17 +105,23 @@
         sWm = TestWindowManagerPolicy.getWindowManagerService(context);
         sPolicy = (TestWindowManagerPolicy) sWm.mPolicy;
         sLayersController = new WindowLayersController(sWm);
-        sDisplayContent = sWm.mRoot.getDisplayContent(context.getDisplay().getDisplayId());
-        if (sDisplayContent != null) {
-            sDisplayContent.removeImmediately();
-        }
+
         // Make sure that display ids don't overlap, so there won't be several displays with same
         // ids among RootWindowContainer children.
         for (DisplayContent dc : sWm.mRoot.mChildren) {
             if (dc.getDisplayId() >= sNextDisplayId) {
                 sNextDisplayId = dc.getDisplayId() + 1;
             }
+
+            // The default display must be preserved as some tests require it to function
+            // (such as policy rotation).
+            if (dc.getDisplayId() != Display.DEFAULT_DISPLAY) {
+                // It is safe to remove these displays as new displays will always be created with
+                // new ids.
+                dc.removeImmediately();
+            }
         }
+
         context.getDisplay().getDisplayInfo(sDisplayInfo);
         sDisplayContent = createNewDisplay();
         sWm.mDisplayEnabled = true;