DO NOT MERGE - Fixed SessionLifecycleTest so it always finishes OutOfProcessLoginActivity.

Otherwise it could cause failures on other tests when the other test activity
finishes, the focus goes back to OutOfProcessLoginActivity, and the dataset
picker is shown.

Bug: 111622428
Bug: 37566627

Test: atest \
      CtsAutoFillServiceTestCases:SessionLifecycleTest#testDatasetVisibleWhileAutofilledAppIsLifecycled \
      CtsAutoFillServiceTestCases:AutofillValueTest#autofillZeroListValueToSpinner \
      CtsAutoFillServiceTestCases:SimpleSaveActivityTest#testDismissSave_byTappingBack

Change-Id: I59af3c84b370d9384d6f2f6c6d8ec050c4a77b02
Merged-In: I59af3c84b370d9384d6f2f6c6d8ec050c4a77b02
(cherry picked from commit ca0e72d757d26e60d58b2bdfe2dd3aa2af120316)
(cherry picked from commit d917c6c7e12c22d32eff45407b63bd88d00a776d)
diff --git a/tests/autofillservice/AndroidManifest.xml b/tests/autofillservice/AndroidManifest.xml
index e47e6df..5feaf8d 100644
--- a/tests/autofillservice/AndroidManifest.xml
+++ b/tests/autofillservice/AndroidManifest.xml
@@ -99,6 +99,9 @@
         <receiver android:name=".SelfDestructReceiver"
             android:exported="true"
             android:process="android.autofillservice.cts.outside"/>
+        <receiver android:name=".OutOfProcessLoginActivityFinisherReceiver"
+            android:exported="true"
+            android:process="android.autofillservice.cts.outside"/>
 
         <service
             android:name=".InstrumentedAutoFillService"
diff --git a/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java b/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java
index dadb3c9..a9974f1 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivity.java
@@ -19,9 +19,10 @@
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
+import android.util.Log;
+
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
-import android.util.Log;
 
 import java.io.File;
 import java.io.IOException;
@@ -30,46 +31,50 @@
  * Simple activity showing R.layout.login_activity. Started outside of the test process.
  */
 public class OutOfProcessLoginActivity extends Activity {
-    private static final String LOG_TAG = OutOfProcessLoginActivity.class.getSimpleName();
+    private static final String TAG = "OutOfProcessLoginActivity";
+
+    private static OutOfProcessLoginActivity sInstance;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
-        Log.i(LOG_TAG, "onCreate(" + savedInstanceState + ")");
+        Log.i(TAG, "onCreate(" + savedInstanceState + ")");
         super.onCreate(savedInstanceState);
 
         setContentView(R.layout.login_activity);
 
         findViewById(R.id.login).setOnClickListener((v) -> finish());
+
+        sInstance = this;
     }
 
     @Override
     protected void onStart() {
-        Log.i(LOG_TAG, "onStart()");
+        Log.i(TAG, "onStart()");
         super.onStart();
         try {
             if (!getStartedMarker(this).createNewFile()) {
-                Log.e(LOG_TAG, "cannot write started file");
+                Log.e(TAG, "cannot write started file");
             }
         } catch (IOException e) {
-            Log.e(LOG_TAG, "cannot write started file");
+            Log.e(TAG, "cannot write started file: " + e);
         }
     }
 
     @Override
     protected void onStop() {
-        Log.i(LOG_TAG, "onStop()");
+        Log.i(TAG, "onStop()");
         super.onStop();
 
         try {
             getStoppedMarker(this).createNewFile();
         } catch (IOException e) {
-            Log.e(LOG_TAG, "cannot write stopped filed");
+            Log.e(TAG, "cannot write stopped file: " + e);
         }
     }
 
     @Override
     protected void onDestroy() {
-        Log.i(LOG_TAG, "onDestroy()");
+        Log.i(TAG, "onDestroy()");
         super.onDestroy();
     }
 
@@ -92,4 +97,11 @@
     @NonNull public static File getStartedMarker(@NonNull Context context) {
         return new File(context.getFilesDir(), "started");
     }
+
+    public static void finishIt() {
+        Log.v(TAG, "Finishing " + sInstance);
+        if (sInstance != null) {
+            sInstance.finish();
+        }
+    }
 }
diff --git a/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivityFinisherReceiver.java b/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivityFinisherReceiver.java
new file mode 100644
index 0000000..b75785e
--- /dev/null
+++ b/tests/autofillservice/src/android/autofillservice/cts/OutOfProcessLoginActivityFinisherReceiver.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.autofillservice.cts;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+/**
+ * A {@link BroadcastReceiver} that finishes {@link OutOfProcessLoginActivity}.
+ */
+public class OutOfProcessLoginActivityFinisherReceiver extends BroadcastReceiver {
+
+    private static final String TAG = "OutOfProcessLoginActivityFinisherReceiver";
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        Log.i(TAG, "Goodbye, unfinished business!");
+        OutOfProcessLoginActivity.finishIt();
+    }
+}
diff --git a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
index 9b03d0a..01098e9 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
@@ -96,6 +96,13 @@
         Helper.allowAutoRotation();
     }
 
+    @After
+    public void finishLoginActivityOnAnotherProcess() throws Exception {
+        runShellCommand("am broadcast --receiver-foreground "
+                + "-n android.autofillservice.cts/.OutOfProcessLoginActivityFinisherReceiver");
+        mUiBot.assertGoneByRelativeId(ID_USERNAME, Timeouts.ACTIVITY_RESURRECTION);
+    }
+
     private void killOfProcessLoginActivityProcess() throws Exception {
         // Waiting for activity to stop (stop marker appears)
         eventually("getStoppedMarker()", () -> {