Merge "Adding leaked stack check"
diff --git a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
index 94dd7fa..e0c756a 100644
--- a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
+++ b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
@@ -125,6 +125,7 @@
 
     @After
     public void tearDown() throws Exception {
+        super.tearDown();
         stopTestPackage(TEST_PACKAGE_APP_A);
         stopTestPackage(TEST_PACKAGE_APP_B);
         launchHomeActivity();
@@ -144,16 +145,6 @@
         boolean result = waitForActivityFocused(APP_A_BACKGROUND_ACTIVITY);
         assertFalse("Should not able to launch background activity", result);
         assertTaskStack(null, APP_A_BACKGROUND_ACTIVITY);
-
-        // Make sure aborting activity starts won't have any empty task/stack leaks.
-        List<ActivityManagerState.ActivityStack> stacks = mAmWmState.getAmState().getStacks();
-        for (ActivityManagerState.ActivityStack stack : stacks) {
-            assertThat(stack.getTopTask()).isNotNull();
-            List<ActivityManagerState.ActivityTask> tasks = stack.getTasks();
-            for (ActivityManagerState.ActivityTask task : tasks) {
-                assertThat(task.getActivities().size()).isGreaterThan(0);
-            }
-        }
     }
 
     @Test
diff --git a/tests/framework/base/windowmanager/src/android/server/wm/intent/IntentTestBase.java b/tests/framework/base/windowmanager/src/android/server/wm/intent/IntentTestBase.java
index a2eeac8..cdabec4 100644
--- a/tests/framework/base/windowmanager/src/android/server/wm/intent/IntentTestBase.java
+++ b/tests/framework/base/windowmanager/src/android/server/wm/intent/IntentTestBase.java
@@ -36,7 +36,6 @@
      * @param activitiesInUsedInTest activities that should be gone after tearDown
      */
     public void cleanUp(List<ComponentName> activitiesInUsedInTest) throws Exception {
-        super.tearDown();
         launchHomeActivityNoWait();
         removeStacksWithActivityTypes(ALL_ACTIVITY_TYPE_BUT_HOME);
 
@@ -48,6 +47,7 @@
     @After
     @Override
     public void tearDown() throws Exception {
+        super.tearDown();
         cleanUp(activitiesUsedInTest());
     }
 
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityAndWindowManagersState.java b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityAndWindowManagersState.java
index f11aa77..fe7a7ef 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityAndWindowManagersState.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityAndWindowManagersState.java
@@ -31,6 +31,8 @@
 import static android.util.DisplayMetrics.DENSITY_DEFAULT;
 import static android.view.Display.DEFAULT_DISPLAY;
 
+import static com.google.common.truth.Truth.assertThat;
+
 import static org.hamcrest.Matchers.greaterThan;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 import static org.hamcrest.Matchers.lessThan;
@@ -680,6 +682,18 @@
                 getAmState().getKeyguardControllerState().aodShowing);
     }
 
+    public void assertEmptyStackOrTask() {
+        getAmState().computeState();
+        final List<ActivityManagerState.ActivityStack> stacks = getAmState().getStacks();
+        for (ActivityManagerState.ActivityStack stack : stacks) {
+            assertThat(stack.getTopTask()).isNotNull();
+            final List<ActivityManagerState.ActivityTask> tasks = stack.getTasks();
+            for (ActivityManagerState.ActivityTask task : tasks) {
+                assertThat(task.getActivities().size()).isGreaterThan(0);
+            }
+        }
+    }
+
     public void assumePendingActivityContain(ComponentName activity) {
         assumeTrue(getAmState().pendingActivityContain(activity));
     }
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
index 63837c5..7cf31de 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/ActivityManagerTestBase.java
@@ -204,6 +204,7 @@
     private static Boolean sHasHomeScreen = null;
     private static Boolean sSupportsSystemDecorsOnSecondaryDisplays = null;
     private static Boolean sSupportsInsecureLockScreen = null;
+    private static boolean sStackTaskLeakFound;
 
     protected static final int INVALID_DEVICE_ROTATION = -1;
 
@@ -436,6 +437,17 @@
 
     @After
     public void tearDown() throws Exception {
+        try {
+            // Skip empty stack/task check if a leakage was already found in previous test, or
+            // all tests afterward would also fail (since the leakage is always there) and fire
+            // unnecessary false alarms.
+            if (!sStackTaskLeakFound) {
+                mAmWmState.assertEmptyStackOrTask();
+            }
+        } catch (Throwable t) {
+            sStackTaskLeakFound = true;
+            throw t;
+        }
         // Synchronous execution of removeStacksWithActivityTypes() ensures that all activities but
         // home are cleaned up from the stack at the end of each test. Am force stop shell commands
         // might be asynchronous and could interrupt the stack cleanup process if executed first.