Wait until activity is destroyed before finishing SessionLifecycleTest.

Test: cts-tradefed run commandAndExit cts-dev -m CtsAutoFillServiceTestCases -t android.autofillservice.cts.SessionLifecycleTest

Bug: 111622428
Bug: 37566627

Change-Id: I41cf1ee36b200a46fe69f219bb578b8c1c6a03ca
Merged-In: I41cf1ee36b200a46fe69f219bb578b8c1c6a03ca
(cherry picked from commit b7e16e3984ca308467f2763913e7af080f59d95d)
diff --git a/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java b/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java
index c3887a7..2ec6655 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java
@@ -65,15 +65,28 @@
         super.onStop();
 
         try {
-            getStoppedMarker(this).createNewFile();
+            if (!getStoppedMarker(this).createNewFile()) {
+                Log.e(TAG, "could not write stopped marker");
+            } else {
+                Log.v(TAG, "wrote stopped marker");
+            }
         } catch (IOException e) {
-            Log.e(TAG, "cannot write stopped file: " + e);
+            Log.e(TAG, "could write stopped marker: " + e);
         }
     }
 
     @Override
     protected void onDestroy() {
         Log.i(TAG, "onDestroy()");
+        try {
+            if (!getDestroyedMarker(this).createNewFile()) {
+                Log.e(TAG, "could not write destroyed marker");
+            } else {
+                Log.v(TAG, "wrote destroyed marker");
+            }
+        } catch (IOException e) {
+            Log.e(TAG, "could write destroyed marker: " + e);
+        }
         super.onDestroy();
     }
 
@@ -97,6 +110,16 @@
         return new File(context.getFilesDir(), "started");
     }
 
+   /**
+     * Get the file that signals that the activity has entered {@link Activity#onDestroy()}.
+     *
+     * @param context Context of the app
+     * @return The marker file that is written onDestroy()
+     */
+    @NonNull public static File getDestroyedMarker(@NonNull Context context) {
+        return new File(context.getFilesDir(), "destroyed");
+    }
+
     public static void finishIt() {
         Log.v(TAG, "Finishing " + sInstance);
         if (sInstance != null) {
diff --git a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
index 400c12a..48b7e6e 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
@@ -25,6 +25,7 @@
 import static android.autofillservice.cts.Helper.findNodeByResourceId;
 import static android.autofillservice.cts.Helper.getContext;
 import static android.autofillservice.cts.Helper.runShellCommand;
+import static android.autofillservice.cts.OutOfProcessLoginActivity.getDestroyedMarker;
 import static android.autofillservice.cts.OutOfProcessLoginActivity.getStoppedMarker;
 import static android.autofillservice.cts.UiBot.LANDSCAPE;
 import static android.autofillservice.cts.UiBot.PORTRAIT;
@@ -81,6 +82,9 @@
         runShellCommand("am broadcast --receiver-foreground "
                 + "-n android.autofillservice.cts/.OutOfProcessLoginActivityFinisherReceiver");
         sUiBot.assertGoneByRelativeId(ID_USERNAME, Helper.ACTIVITY_RESURRECTION_MS);
+
+        // Waiting for activity to be destroyed (destroy marker appears)
+        eventually(() -> assertThat(getDestroyedMarker(getContext()).exists()).isTrue());
     }
 
     private void killOfProcessLoginActivityProcess() throws Exception {