Merge "Set FLAG_SAVE_ON_ALL_VIEWS_INVISIBLE on compat mode test." into pi-dev
diff --git a/apps/CtsVerifier/res/values/strings.xml b/apps/CtsVerifier/res/values/strings.xml
index 59689fd..8a5b66f 100755
--- a/apps/CtsVerifier/res/values/strings.xml
+++ b/apps/CtsVerifier/res/values/strings.xml
@@ -235,6 +235,8 @@
         paired, unpair it first from system Settings.
         \n\nAfter the remote device completes the \"Send_report\" command, text \"bluetooth\" should
         appear in the EditText. Mark the test as passed.
+        \n\n If the device under test (DUT) does not have Bluetooth HID Host service or HID Device
+        service enabled, mark the test as passed.
     </string>
     <string name="bt_hid_host">Bluetooth HID Host</string>
     <string name="bt_hid_host_select_device">Select device</string>
@@ -249,6 +251,8 @@
         \n\nFinally, click the \"Test send_report\", \"Test reply_report\", \"Test report_error\".
         \n\nIf all the commands are successful, then click \"Unregister app\" and mark the test as
         passed.
+        \n\n If the device under test (DUT) does not have Bluetooth HID Host service or HID Device
+        service enabled, mark the test as passed.
     </string>
 
     <string name="bt_hid_device">Bluetooth HID Device</string>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidDeviceActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidDeviceActivity.java
index 534b030..465573c 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidDeviceActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidDeviceActivity.java
@@ -106,7 +106,6 @@
         setContentView(R.layout.bt_hid_device);
         setPassFailButtonClickListeners();
         setInfoResources(R.string.bt_hid_device_test_name, R.string.bt_hid_device_test_info, -1);
-        getPassButton().setEnabled(false);
 
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
         mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener,
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidHostActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidHostActivity.java
index c3bcbf6..2e70b45 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidHostActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/bluetooth/HidHostActivity.java
@@ -52,7 +52,6 @@
         setContentView(R.layout.bt_hid_host);
         setPassFailButtonClickListeners();
         setInfoResources(R.string.bt_hid_host_test_name, R.string.bt_hid_host_test_info, -1);
-        getPassButton().setEnabled(false);
         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 
         mEditText = (EditText) findViewById(R.id.bt_hid_host_edit_text);
diff --git a/tests/autofillservice/AndroidManifest.xml b/tests/autofillservice/AndroidManifest.xml
index 874d0c3..2af9b90 100644
--- a/tests/autofillservice/AndroidManifest.xml
+++ b/tests/autofillservice/AndroidManifest.xml
@@ -93,6 +93,10 @@
             android:taskAffinity="nobody.but.EmptyActivity"
             android:exported="true" />
 
+        <receiver android:name=".SelfDestructReceiver"
+            android:exported="true"
+            android:process="android.autofillservice.cts.outside"/>
+
         <service
             android:name=".InstrumentedAutoFillService"
             android:label="InstrumentedAutoFillService"
diff --git a/tests/autofillservice/src/android/autofillservice/cts/Helper.java b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
index 1e2bd54..c3af0b1 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/Helper.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
@@ -708,28 +708,6 @@
     }
 
     /**
-     * Wait until a process starts and returns the process ID of the process.
-     *
-     * @return The pid of the process
-     */
-    public static int getOutOfProcessPid(@NonNull String processName, @NonNull Timeout timeout)
-            throws Exception {
-
-        return timeout.run("getOutOfProcessPid(" + processName + ")", () -> {
-            final String[] allProcessDescs = runShellCommand("ps -eo PID,ARGS=CMD").split("\n");
-
-            for (String processDesc : allProcessDescs) {
-                final String[] pidAndName = processDesc.trim().split(" ");
-
-                if (pidAndName[1].equals(processName)) {
-                    return Integer.parseInt(pidAndName[0]);
-                }
-            }
-            return null;
-        });
-    }
-
-    /**
      * Gets the maximum number of partitions per session.
      */
     public static int getMaxPartitions() {
diff --git a/tests/autofillservice/src/android/autofillservice/cts/SelfDestructReceiver.java b/tests/autofillservice/src/android/autofillservice/cts/SelfDestructReceiver.java
new file mode 100644
index 0000000..8dc8dd9
--- /dev/null
+++ b/tests/autofillservice/src/android/autofillservice/cts/SelfDestructReceiver.java
@@ -0,0 +1,36 @@
+/*
+ * 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.os.Process;
+import android.util.Log;
+
+/**
+ * A {@link BroadcastReceiver} that kills its process.
+ */
+public class SelfDestructReceiver extends BroadcastReceiver {
+
+    private static final String TAG = "SelfDestructReceiver";
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        Log.i(TAG, "Goodbye, cruel world!");
+        Process.killProcess(Process.myPid());
+    }
+}
diff --git a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
index d3613e8..55d2d03 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
@@ -22,7 +22,6 @@
 import static android.autofillservice.cts.Helper.assertTextAndValue;
 import static android.autofillservice.cts.Helper.findNodeByResourceId;
 import static android.autofillservice.cts.Helper.getContext;
-import static android.autofillservice.cts.Helper.getOutOfProcessPid;
 import static android.autofillservice.cts.OutOfProcessLoginActivity.getStartedMarker;
 import static android.autofillservice.cts.OutOfProcessLoginActivity.getStoppedMarker;
 import static android.autofillservice.cts.UiBot.LANDSCAPE;
@@ -104,9 +103,8 @@
         SystemClock.sleep(WAIT_ACTIVITY_MS);
 
         // Kill activity that is in the background
-        runShellCommand("kill -9 %d",
-                getOutOfProcessPid("android.autofillservice.cts.outside",
-                        SESSION_LIFECYCLE_TIMEOUT));
+        runShellCommand("am broadcast --receiver-foreground "
+                + "-n android.autofillservice.cts/.SelfDestructReceiver");
     }
 
     private void startAndWaitExternalActivity() throws Exception {
diff --git a/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleClientTestBase.java b/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleClientTestBase.java
index d4b9559..070fce1 100644
--- a/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleClientTestBase.java
+++ b/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleClientTestBase.java
@@ -9,6 +9,7 @@
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.Handler;
 import android.server.am.ActivityManagerTestBase;
 import android.server.am.lifecycle.LifecycleLog.ActivityCallback;
 import android.support.test.InstrumentationRegistry;
@@ -26,6 +27,8 @@
 public class ActivityLifecycleClientTestBase extends ActivityManagerTestBase {
 
     static final String EXTRA_RECREATE = "recreate";
+    static final String EXTRA_FINISH_IN_ON_RESUME = "finish_in_on_resume";
+    static final String EXTRA_FINISH_AFTER_RESUME = "finish_after_resume";
 
     final ActivityTestRule mFirstActivityTestRule = new ActivityTestRule(FirstActivity.class,
             true /* initialTouchMode */, false /* launchActivity */);
@@ -161,6 +164,7 @@
 
         private void startForResult() {
             final Intent intent = new Intent(this, ResultActivity.class);
+            intent.putExtras(getIntent());
             startActivityForResult(intent, 1 /* requestCode */);
         }
     }
@@ -171,7 +175,12 @@
         protected void onResume() {
             super.onResume();
             setResult(RESULT_OK);
-            finish();
+            final Intent intent = getIntent();
+            if (intent.getBooleanExtra(EXTRA_FINISH_IN_ON_RESUME, false)) {
+                finish();
+            } else if (intent.getBooleanExtra(EXTRA_FINISH_AFTER_RESUME, false)) {
+                new Handler().postDelayed(() -> finish(), 2000);
+            }
         }
     }
 
diff --git a/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleTests.java b/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleTests.java
index af623a3..da47687 100644
--- a/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleTests.java
+++ b/tests/framework/base/activitymanager/src/android/server/am/lifecycle/ActivityLifecycleTests.java
@@ -167,8 +167,16 @@
             LifecycleVerifier.assertSequence(FirstActivity.class, getLifecycleLog(),
                     Arrays.asList(ON_DESTROY, PRE_ON_CREATE, ON_CREATE, ON_START, ON_RESUME,
                             ON_PAUSE), "becomingVisiblePaused");
-            LifecycleVerifier.assertSequence(TranslucentActivity.class, getLifecycleLog(),
-                    Arrays.asList(ON_DESTROY, PRE_ON_CREATE, ON_CREATE, ON_START, ON_RESUME),
+            // TODO(b/78021523): Local relaunch sequence should always be the same.
+            // It is possible to get another pause-resume now.
+            final List<LifecycleLog.ActivityCallback> expectedSequence =
+                    Arrays.asList(ON_DESTROY, PRE_ON_CREATE, ON_CREATE, ON_START, ON_RESUME);
+            final List<LifecycleLog.ActivityCallback> extraCycleSequence =
+                    Arrays.asList(ON_DESTROY, PRE_ON_CREATE, ON_CREATE, ON_START, ON_RESUME,
+                            ON_PAUSE, ON_RESUME);
+            LifecycleVerifier.assertSequenceMatchesOneOf(TranslucentActivity.class,
+                    getLifecycleLog(),
+                    Arrays.asList(expectedSequence, extraCycleSequence),
                     "becomingVisibleResumed");
         }
     }
@@ -299,7 +307,9 @@
 
     @Test
     public void testOnActivityResult() throws Exception {
-        mLaunchForResultActivityTestRule.launchActivity(new Intent());
+        final Intent intent = new Intent();
+        intent.putExtra(EXTRA_FINISH_IN_ON_RESUME, true);
+        mLaunchForResultActivityTestRule.launchActivity(intent);
 
         final List<LifecycleLog.ActivityCallback> expectedSequence =
                 Arrays.asList(PRE_ON_CREATE, ON_CREATE, ON_START, ON_POST_CREATE, ON_RESUME,
@@ -311,6 +321,21 @@
     }
 
     @Test
+    public void testOnActivityResultAfterStop() throws Exception {
+        final Intent intent = new Intent();
+        intent.putExtra(EXTRA_FINISH_AFTER_RESUME, true);
+        mLaunchForResultActivityTestRule.launchActivity(intent);
+
+        final List<LifecycleLog.ActivityCallback> expectedSequence =
+                Arrays.asList(PRE_ON_CREATE, ON_CREATE, ON_START, ON_POST_CREATE, ON_RESUME,
+                        ON_PAUSE, ON_STOP, ON_ACTIVITY_RESULT, ON_RESTART, ON_START, ON_RESUME);
+        waitForActivityTransitions(LaunchForResultActivity.class, expectedSequence);
+
+        LifecycleVerifier.assertSequence(LaunchForResultActivity.class,
+                getLifecycleLog(), expectedSequence, "activityResult");
+    }
+
+    @Test
     public void testOnPostCreateAfterCreate() throws Exception {
         final Activity callbackTrackingActivity =
                 mCallbackTrackingActivityTestRule.launchActivity(new Intent());
diff --git a/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java b/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java
index 6c366a7..eecf520 100644
--- a/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java
+++ b/tests/tests/app.usage/src/android/app/usage/cts/NetworkUsageStatsTest.java
@@ -216,6 +216,7 @@
         super.setUp();
         mNsm = (NetworkStatsManager) getInstrumentation().getContext()
                 .getSystemService(Context.NETWORK_STATS_SERVICE);
+        mNsm.setPollForce(true);
 
         mCm = (ConnectivityManager) getInstrumentation().getContext()
                 .getSystemService(Context.CONNECTIVITY_SERVICE);