ActivityManagerProcessStateTest: wait for focus

ARC++ creates the windows in an asynchronous way. The window might not
be created, even though the activity is.

What this CL does, is to wait for activity to have focus and wait for possible
animations to finish after an activity is created.

This is needed to fix a flaky on ARC++ CTS tests.

Bug: 122971048
Test: atest -bit android.app.cts.ActivityManagerProcessStateTest
      Tested on Pixel Slate Chromebook
      Tested on Pixel One XL and Pixel Two phones
Change-Id: I30c175072782ea45f25fb007cd05611d052104fe
diff --git a/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java b/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
index ae58e43..23203e3 100644
--- a/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
+++ b/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
@@ -1130,9 +1130,13 @@
         WatchUidRunner uidWatcher = new WatchUidRunner(getInstrumentation(), appInfo.uid,
                 WAIT_TIME);
 
+        UiDevice device = UiDevice.getInstance(getInstrumentation());
+
         try {
             // Start the heavy-weight app, should launch like a normal app.
             mContext.startActivity(activityIntent);
+            waitForAppFocus(CANT_SAVE_STATE_1_PACKAGE_NAME, WAIT_TIME);
+            device.waitForIdle();
 
             // Wait for process state to reflect running activity.
             uidForegroundListener.waitForValue(
@@ -1181,6 +1185,7 @@
             uidWatcher.expect(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP);
 
             waitForAppFocus(CANT_SAVE_STATE_1_PACKAGE_NAME, WAIT_TIME);
+            device.waitForIdle();
 
             // Exit activity, check to see if we are now cached.
             getInstrumentation().getUiAutomation().performGlobalAction(
@@ -1259,6 +1264,8 @@
         try {
             // Start the first heavy-weight app, should launch like a normal app.
             mContext.startActivity(activity1Intent);
+            waitForAppFocus(CANT_SAVE_STATE_1_PACKAGE_NAME, WAIT_TIME);
+            device.waitForIdle();
 
             // Make sure the uid state reports are as expected.
             uid1Watcher.waitFor(WatchUidRunner.CMD_ACTIVE, null);
@@ -1277,6 +1284,7 @@
 
             // First, let's try returning to the original app.
             maybeClick(device, new UiSelector().resourceId("android:id/switch_old"));
+            waitForAppFocus(CANT_SAVE_STATE_1_PACKAGE_NAME, WAIT_TIME);
             device.waitForIdle();
 
             // App should now be back in foreground.
@@ -1293,6 +1301,7 @@
 
             // Now we'll switch to the new app.
             maybeClick(device, new UiSelector().resourceId("android:id/switch_new"));
+            waitForAppFocus(CANT_SAVE_STATE_2_PACKAGE_NAME, WAIT_TIME);
             device.waitForIdle();
 
             // The original app should now become cached.
@@ -1316,6 +1325,7 @@
             // Try starting the first heavy weight app, but return to the existing second.
             startActivityAndWaitForShow(activity1Intent);
             maybeClick(device, new UiSelector().resourceId("android:id/switch_old"));
+            waitForAppFocus(CANT_SAVE_STATE_2_PACKAGE_NAME, WAIT_TIME);
             device.waitForIdle();
             uid2Watcher.waitFor(WatchUidRunner.CMD_UNCACHED, null);
             uid2Watcher.expect(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP);
@@ -1328,6 +1338,7 @@
             // Again start the first heavy weight app, this time actually switching to it
             startActivityAndWaitForShow(activity1Intent);
             maybeClick(device, new UiSelector().resourceId("android:id/switch_new"));
+            waitForAppFocus(CANT_SAVE_STATE_1_PACKAGE_NAME, WAIT_TIME);
             device.waitForIdle();
 
             // The second app should now become cached.
@@ -1340,6 +1351,7 @@
 
             // Exit activity, check to see if we are now cached.
             waitForAppFocus(CANT_SAVE_STATE_1_PACKAGE_NAME, WAIT_TIME);
+            device.waitForIdle();
             getInstrumentation().getUiAutomation().performGlobalAction(
                     AccessibilityService.GLOBAL_ACTION_BACK);
             uid1Watcher.expect(WatchUidRunner.CMD_CACHED, null);