CtsWindowManagerBackgroundActivityTestCases improvements.

- Refactored assertTaskStack() usage into
  assertTaskStackHasComponents(...) and assertTaskStackIsEmpty(),
  which uses Truth assertions
- Added user id on activity lifecycle events
- Implemented toString() in some POJOs

These changes will make it easier to identify failures.

Test: atest CtsWindowManagerBackgroundActivityTestCases
Bug: 267503734

Change-Id: I59ef39fb63df9ff5b2d254ab0ac33ab836d292c1
diff --git a/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/BackgroundActivity.java b/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/BackgroundActivity.java
index f913bef..6e67c90 100644
--- a/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/BackgroundActivity.java
+++ b/tests/framework/base/windowmanager/backgroundactivity/AppA/src/android/server/wm/backgroundactivity/appa/BackgroundActivity.java
@@ -18,6 +18,7 @@
 
 import android.app.Activity;
 import android.os.Bundle;
+import android.os.Process;
 import android.util.Log;
 
 /**
@@ -31,18 +32,18 @@
     @Override
     protected void onCreate(Bundle bundle) {
         super.onCreate(bundle);
-        Log.i(TAG, "onCreate");
+        Log.i(TAG, "onCreate(" + Process.myUserHandle() + ")");
     }
 
     @Override
     protected void onResume() {
         super.onResume();
-        Log.i(TAG, "onResume");
+        Log.i(TAG, "onResume(" + Process.myUserHandle() + ")");
     }
 
     @Override
     protected void onPause() {
         super.onPause();
-        Log.i(TAG, "onPause");
+        Log.i(TAG, "onPause(" + Process.myUserHandle() + ")");
     }
 }
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 53b82f2..470270c 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
@@ -129,7 +129,7 @@
         mContext.sendBroadcast(intent);
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("Should not able to launch background activity", result);
-        assertTaskStack(null, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackIsEmpty(APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -223,19 +223,19 @@
         mContext.sendBroadcast(getLaunchActivitiesBroadcast(APP_A, APP_B.FOREGROUND_ACTIVITY));
         mWmState.waitForValidState(APP_B.FOREGROUND_ACTIVITY);
         mWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY);
-        assertTaskStack(new ComponentName[]{
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
                 APP_B.FOREGROUND_ACTIVITY,
-                APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+                APP_A.FOREGROUND_ACTIVITY);
 
         // Start AppA background activity fails as AppA not on top of stack
         intent = new Intent();
         intent.setComponent(APP_A.START_ACTIVITY_RECEIVER);
         mContext.sendBroadcast(intent);
         mWmState.waitForValidState(APP_A.BACKGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
                 APP_B.FOREGROUND_ACTIVITY,
-                APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
-        assertTaskStack(null, APP_A.BACKGROUND_ACTIVITY);
+                APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackIsEmpty(APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -245,16 +245,15 @@
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         mContext.startActivity(intent);
         mWmState.waitForValidState(APP_A.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{
-                APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
 
         // Start AppA background activity successfully in new task as there's a foreground activity
         intent = new Intent();
         intent.setComponent(APP_A.START_ACTIVITY_RECEIVER);
         mContext.sendBroadcast(intent);
         mWmState.waitForValidState(APP_A.BACKGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{APP_A.BACKGROUND_ACTIVITY}, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -266,20 +265,19 @@
         mWmState.waitForValidState(APP_A_33.FOREGROUND_ACTIVITY);
         mContext.sendBroadcast(getLaunchActivitiesBroadcast(APP_A_33, APP_B.FOREGROUND_ACTIVITY));
         mWmState.waitForValidState(APP_B.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{
-                APP_B.FOREGROUND_ACTIVITY,
-                APP_A_33.FOREGROUND_ACTIVITY}, APP_A_33.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A_33.FOREGROUND_ACTIVITY,
+                        APP_B.FOREGROUND_ACTIVITY,
+                        APP_A_33.FOREGROUND_ACTIVITY);
 
         // Start AppA background activity successfully as there's a foreground activity
         intent = new Intent();
         intent.setComponent(APP_A_33.START_ACTIVITY_RECEIVER);
         mContext.sendBroadcast(intent);
         mWmState.waitForValidState(APP_A_33.BACKGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{
+        assertTaskStackHasComponents(APP_A_33.FOREGROUND_ACTIVITY,
                 APP_B.FOREGROUND_ACTIVITY,
-                APP_A_33.FOREGROUND_ACTIVITY}, APP_A_33.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{APP_A_33.BACKGROUND_ACTIVITY},
-                APP_A_33.BACKGROUND_ACTIVITY);
+                APP_A_33.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A_33.BACKGROUND_ACTIVITY, APP_A_33.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -293,8 +291,9 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.BACKGROUND_ACTIVITY,
-                APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
+                APP_A.BACKGROUND_ACTIVITY,
+                APP_A.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -308,7 +307,7 @@
         boolean result = waitForActivityFocused(ACTIVITY_FOCUS_TIMEOUT_MS,
                 APP_A.FOREGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
         // We can't resume app switching after pressing home button, otherwise the grace period
         // will allow the starts.
         pressHomeAndWaitHomeResumed();
@@ -321,7 +320,8 @@
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("Previously background Activity should not be able to make it focused",
                 result);
-        assertTaskStack(new ComponentName[] {APP_A.BACKGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY},
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
+                APP_A.BACKGROUND_ACTIVITY,
                 APP_A.FOREGROUND_ACTIVITY);
     }
 
@@ -335,8 +335,9 @@
         mWmState.waitForValidState(APP_A.FOREGROUND_ACTIVITY);
         mContext.sendBroadcast(getLaunchActivitiesBroadcast(APP_A, APP_B.FOREGROUND_ACTIVITY));
         mWmState.waitForValidState(APP_B.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{APP_B.FOREGROUND_ACTIVITY,
-                APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
+                APP_B.FOREGROUND_ACTIVITY,
+                APP_A.FOREGROUND_ACTIVITY);
 
         // Refresh last-stop-app-switch-time by returning to home and then make the task foreground.
         pressHomeAndResumeAppSwitch();
@@ -351,9 +352,9 @@
                                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) }));
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("FG activity shouldn't be visible", result);
-        assertTaskStack(new ComponentName[]{APP_B.FOREGROUND_ACTIVITY,
-                APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
-
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
+                APP_B.FOREGROUND_ACTIVITY,
+                APP_A.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -367,8 +368,9 @@
         mWmState.waitForValidState(APP_A_33.FOREGROUND_ACTIVITY);
         mContext.sendBroadcast(getLaunchActivitiesBroadcast(APP_A_33, APP_B.FOREGROUND_ACTIVITY));
         mWmState.waitForValidState(APP_B.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{APP_B.FOREGROUND_ACTIVITY,
-                APP_A_33.FOREGROUND_ACTIVITY}, APP_A_33.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A_33.FOREGROUND_ACTIVITY,
+                APP_B.FOREGROUND_ACTIVITY,
+                APP_A_33.FOREGROUND_ACTIVITY);
 
         // Refresh last-stop-app-switch-time by returning to home and then make the task foreground.
         pressHomeAndResumeAppSwitch();
@@ -400,7 +402,7 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_A.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
 
         // The foreground activity will be paused but will attempt to restart itself in onPause()
         pressHomeAndResumeAppSwitch();
@@ -408,7 +410,7 @@
         result = waitForActivityFocused(APP_A.FOREGROUND_ACTIVITY);
         assertFalse("Previously foreground Activity should not be able to relaunch itself",
                 result);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -427,9 +429,10 @@
 
         waitAndAssertActivityState(APP_A.BACKGROUND_ACTIVITY, STATE_INITIALIZING,
                 "First activity should have been created");
-        assertTaskStack(
-                new ComponentName[]{APP_A.SECOND_BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY,
-                        APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
+                APP_A.SECOND_BACKGROUND_ACTIVITY,
+                APP_A.BACKGROUND_ACTIVITY,
+                APP_A.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -440,14 +443,15 @@
         mContext.startActivity(baseActivityIntent);
         boolean result = waitForActivityFocused(APP_A.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
         // We can't resume app switching after pressing home button, otherwise the grace period
         // will allow the starts.
         pressHomeAndWaitHomeResumed();
 
         // The activity, now in the background, will attempt to start 2 activities in quick
         // succession
-        mContext.sendBroadcast(getLaunchActivitiesBroadcast(APP_A, APP_A.BACKGROUND_ACTIVITY,
+        mContext.sendBroadcast(getLaunchActivitiesBroadcast(APP_A,
+                APP_A.BACKGROUND_ACTIVITY,
                 APP_A.SECOND_BACKGROUND_ACTIVITY));
 
         // There should be 2 activities in the background (not focused) INITIALIZING
@@ -455,9 +459,10 @@
         assertFalse("Activity should not have been launched in the foreground", result);
         result = waitForActivityFocused(APP_A.SECOND_BACKGROUND_ACTIVITY);
         assertFalse("Second activity should not have been launched in the foreground", result);
-        assertTaskStack(
-                new ComponentName[]{APP_A.SECOND_BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY,
-                        APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY,
+                APP_A.SECOND_BACKGROUND_ACTIVITY,
+                APP_A.BACKGROUND_ACTIVITY,
+                APP_A.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -466,7 +471,7 @@
         sendPendingIntentActivity(APP_A, APP_B);
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("Should not able to launch background activity", result);
-        assertTaskStack(null, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackIsEmpty(APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -479,7 +484,7 @@
         startPendingIntentSenderActivity(APP_A, APP_B, /* allowBalBySender */ true);
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.BACKGROUND_ACTIVITY}, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -505,15 +510,15 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_A.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A
         sendPendingIntentActivity(APP_A, APP_B);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.FOREGROUND_ACTIVITY}, APP_A.FOREGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[]{APP_A.BACKGROUND_ACTIVITY}, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.FOREGROUND_ACTIVITY, APP_A.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -525,14 +530,14 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_B.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[]{APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A
         sendPendingIntentActivity(APP_A, APP_B);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertWithMessage("Able to launch background activity").that(result).isFalse();
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -545,15 +550,15 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_B.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A
         sendPendingIntentActivity(APP_A, APP_B);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[] {APP_A.BACKGROUND_ACTIVITY}, APP_A.BACKGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
     }
 
     private void enableDefaultRescindBalPrivilegesFromPendingIntentSender(boolean enable) {
@@ -570,16 +575,15 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_B_33.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[]{APP_B_33.FOREGROUND_ACTIVITY}, APP_B_33.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B_33.FOREGROUND_ACTIVITY, APP_B_33.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A
         sendPendingIntentActivity(APP_A, APP_B_33);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[]{APP_A.BACKGROUND_ACTIVITY}, APP_A.BACKGROUND_ACTIVITY);
-        assertTaskStack(
-                new ComponentName[]{APP_B_33.FOREGROUND_ACTIVITY}, APP_B_33.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B_33.FOREGROUND_ACTIVITY, APP_B_33.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -591,7 +595,7 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_B.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[]{APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A.
@@ -602,7 +606,7 @@
                 APP_A.SEND_PENDING_INTENT_RECEIVER_EXTRA.ALLOW_BAL_EXTRA_ON_PENDING_INTENT);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertWithMessage("Able to launch background activity").that(result).isFalse();
-        assertTaskStack(new ComponentName[]{APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -614,7 +618,7 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_B.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A
@@ -624,7 +628,7 @@
                 APP_B.START_PENDING_INTENT_ACTIVITY_EXTRA.USE_NULL_BUNDLE /* on send by app B */);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertWithMessage("Able to launch background activity").that(result).isFalse();
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -636,15 +640,15 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(APP_B.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
 
         // Send pendingIntent from AppA to AppB, and the AppB launch the pending intent to start
         // activity in App A
         sendPendingIntentActivity(APP_A, APP_B, APP_B.START_PENDING_INTENT_ACTIVITY_EXTRA.ALLOW_BAL);
         result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertTrue("Not able to launch background activity", result);
-        assertTaskStack(new ComponentName[] {APP_A.BACKGROUND_ACTIVITY}, APP_A.BACKGROUND_ACTIVITY);
-        assertTaskStack(new ComponentName[] {APP_B.FOREGROUND_ACTIVITY}, APP_B.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackHasComponents(APP_B.FOREGROUND_ACTIVITY, APP_B.FOREGROUND_ACTIVITY);
     }
 
     @Test
@@ -683,7 +687,7 @@
         receiver.waitForEventOrThrow(BROADCAST_DELIVERY_TIMEOUT_MS);
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("Should not able to launch background activity", result);
-        assertTaskStack(null, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackIsEmpty(APP_A.BACKGROUND_ACTIVITY);
     }
 
     /**
@@ -781,14 +785,12 @@
         } else {
             assertWithMessage("Launched bg activity (%s) for (full) system user",
                     APP_A.BACKGROUND_ACTIVITY).that(actualResult).isTrue();
-            assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY,
-                    APP_A.BACKGROUND_ACTIVITY);
+            assertTaskStackHasComponents(APP_A.BACKGROUND_ACTIVITY, APP_A.BACKGROUND_ACTIVITY);
         }
     }
 
     @Test
     public void testAppCannotStartBgActivityAfterHomeButton() throws Exception {
-
         Intent intent = new Intent();
         intent.setComponent(APP_A.RELAUNCHING_ACTIVITY);
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -862,7 +864,7 @@
         mBackgroundActivityTestService.getAndStartManageSpaceActivity();
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("Should not able to launch background activity", result);
-        assertTaskStack(null, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackIsEmpty(APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -878,7 +880,7 @@
 
         boolean result = waitForActivityFocused(APP_A.BACKGROUND_ACTIVITY);
         assertFalse("Should not able to launch background activity", result);
-        assertTaskStack(null, APP_A.BACKGROUND_ACTIVITY);
+        assertTaskStackIsEmpty(APP_A.BACKGROUND_ACTIVITY);
     }
 
     @Test
@@ -1045,7 +1047,7 @@
         mContext.startActivity(intent);
         boolean result = waitForActivityFocused(appB.FOREGROUND_ACTIVITY);
         assertTrue("Not able to start foreground Activity", result);
-        assertTaskStack(new ComponentName[]{appB.FOREGROUND_ACTIVITY}, appB.FOREGROUND_ACTIVITY);
+        assertTaskStackHasComponents(appB.FOREGROUND_ACTIVITY, appB.FOREGROUND_ACTIVITY);
         EventReceiver receiver = new EventReceiver(
                 Event.APP_A_START_BACKGROUND_ACTIVITY_BROADCAST_RECEIVED);
 
@@ -1059,10 +1061,9 @@
                 appA.BACKGROUND_ACTIVITY);
         assertEquals(expectedResult, result);
         if (expectedResult) {
-            assertTaskStack(new ComponentName[]{appA.BACKGROUND_ACTIVITY},
-                    appA.BACKGROUND_ACTIVITY);
+            assertTaskStackHasComponents(appA.BACKGROUND_ACTIVITY, appA.BACKGROUND_ACTIVITY);
         } else {
-            assertTaskStack(null, appA.BACKGROUND_ACTIVITY);
+            assertTaskStackIsEmpty(appA.BACKGROUND_ACTIVITY);
         }
     }
 
diff --git a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityTestBase.java b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityTestBase.java
index 1af9814..72dd654 100644
--- a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityTestBase.java
+++ b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityTestBase.java
@@ -155,34 +155,17 @@
         return waitForActivityFocused(ACTIVITY_FOCUS_TIMEOUT_MS, componentName);
     }
 
-    /**
-     * @deprecated use {@link #assertTaskStackHasComponents(ComponentName, ComponentName...)}
-     * instead
-     */
-    @Deprecated
-    void assertTaskStack(ComponentName[] expectedComponents, ComponentName sourceComponent) {
-        if (expectedComponents == null) {
-            assertNull(mWmState.getTaskByActivity(sourceComponent));
-            return;
-        }
-        List<WindowManagerState.Activity> actual = mWmState.getTaskByActivity(
-                sourceComponent).mActivities;
-        assertEquals(expectedComponents.length, actual.size());
-        int size = expectedComponents.length;
-        for (int i = 0; i < size; i++) {
-            assertEquals(expectedComponents[i].flattenToShortString(), actual.get(i).getName());
-        }
+    void assertTaskStackIsEmpty(ComponentName sourceComponent) {
+        Task task = mWmState.getTaskByActivity(sourceComponent);
+        assertWithMessage("task for %s", sourceComponent.flattenToShortString()).that(task)
+                .isNull();
     }
 
     void assertTaskStackHasComponents(ComponentName sourceComponent,
             ComponentName... expectedComponents) {
         Task task = mWmState.getTaskByActivity(sourceComponent);
-        Log.d(TAG, "Task for " + sourceComponent.flattenToShortString() + ": " + task);
-        if (expectedComponents == null) {
-            assertWithMessage("mWmState.getTaskByActivity(%s)", sourceComponent)
-                    .that(task).isNull();
-            return;
-        }
+        assertWithMessage("task for %s", sourceComponent.flattenToShortString()).that(task)
+                .isNotNull();
         Log.d(TAG, "Task for " + sourceComponent.flattenToShortString() + ": " + task
                 + " Activities: " + task.mActivities);
         List<String> actualNames = getActivityNames(task.mActivities);
@@ -348,8 +331,8 @@
         }
 
         ActivityStartVerifier thenAssertTaskStack(ComponentName... expectedComponents) {
-            BackgroundActivityTestBase.this.assertTaskStack(expectedComponents,
-                    expectedComponents[expectedComponents.length - 1]);
+            assertTaskStackHasComponents(expectedComponents[expectedComponents.length - 1],
+                    expectedComponents);
             return this;
         }
 
diff --git a/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerState.java b/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerState.java
index 291bb65..88f86c4 100644
--- a/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerState.java
+++ b/tests/framework/base/windowmanager/util/src/android/server/wm/WindowManagerState.java
@@ -1631,6 +1631,13 @@
         int getActivityType() {
             return mTaskType;
         }
+
+        @Override
+        public String toString() {
+            return "Task[id=" + mTaskId + ", display=" + mDisplayId
+                    + ", mOrigActivity=" + mOrigActivity + ", realActivity=" + mRealActivity
+                    + ", activities=" + mActivities + "]";
+        }
     }
 
     public static class TaskFragment extends ActivityContainer {
@@ -1806,6 +1813,11 @@
         public Rect getAppBounds() {
             return mFullConfiguration.windowConfiguration.getAppBounds();
         }
+
+        @Override
+        public String toString() {
+            return "Activity[name=" + name + ", state=" + state + ", visible=" + visible + "]";
+        }
     }
 
     static abstract class ActivityContainer extends WindowContainer {