Add test case for the maximum attempts of launching bad provider

Bug: 141020060
Test: atest CtsAppTestCases:BadProviderTest
Change-Id: If943b247707330f40ebb72f9ebafc2e52db80a4c
diff --git a/tests/app/AndroidTest.xml b/tests/app/AndroidTest.xml
index 76e6d54..5c2e4cf 100644
--- a/tests/app/AndroidTest.xml
+++ b/tests/app/AndroidTest.xml
@@ -29,6 +29,7 @@
         <option name="test-file-name" value="CtsAppTestStubsApp3.apk" />
         <option name="test-file-name" value="CtsAppTestStubsApp2.apk" />
         <option name="test-file-name" value="CtsAppTestCases.apk" />
+        <option name="test-file-name" value="CtsBadProviderStubs.apk" />
         <option name="test-file-name" value="CtsCantSaveState1.apk" />
         <option name="test-file-name" value="CtsCantSaveState2.apk" />
         <option name="test-file-name" value="NotificationDelegator.apk" />
diff --git a/tests/app/BadProviderStubs/Android.bp b/tests/app/BadProviderStubs/Android.bp
new file mode 100644
index 0000000..3180ef5
--- /dev/null
+++ b/tests/app/BadProviderStubs/Android.bp
@@ -0,0 +1,34 @@
+//
+// Copyright (C) 2019 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.
+//
+
+android_test_helper_app {
+    name: "CtsBadProviderStubs",
+    defaults: ["cts_support_defaults"],
+    static_libs: ["android-support-annotations"],
+    srcs: ["src/**/*.java"],
+    sdk_version: "current",
+    test_suites: [
+        "cts",
+        "vts",
+        "general-tests",
+    ],
+    optimize: {
+        enabled: false,
+    },
+    dex_preopt: {
+        enabled: false,
+    },
+}
diff --git a/tests/app/BadProviderStubs/AndroidManifest.xml b/tests/app/BadProviderStubs/AndroidManifest.xml
new file mode 100644
index 0000000..55f2228
--- /dev/null
+++ b/tests/app/BadProviderStubs/AndroidManifest.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+        package="com.android.cts.stubbad">
+
+    <application>
+        <provider
+            android:name=".BadProviderStub"
+            android:authorities="com.android.cts.stubbad.badprovider"
+            android:exported="true" />
+    </application>
+
+</manifest>
diff --git a/tests/app/BadProviderStubs/src/com/android/cts/stubbad/BadProviderStub.java b/tests/app/BadProviderStubs/src/com/android/cts/stubbad/BadProviderStub.java
new file mode 100644
index 0000000..01033918
--- /dev/null
+++ b/tests/app/BadProviderStubs/src/com/android/cts/stubbad/BadProviderStub.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2019 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 com.android.cts.stubbad;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+
+public class BadProviderStub extends ContentProvider {
+    @Override
+    public boolean onCreate() {
+        System.exit(0);
+        return false;
+    }
+
+    @Override
+    public Cursor query(@NonNull Uri uri, @Nullable String[] projection,
+            @Nullable String selection, @Nullable String[] selectionArgs,
+            @Nullable String sortOrder) {
+        return null;
+    }
+
+    @Override
+    public String getType(@NonNull Uri uri) {
+        return null;
+    }
+
+    @Override
+    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
+        return null;
+    }
+
+    @Override
+    public int delete(@NonNull Uri uri, @Nullable String selection,
+            @Nullable String[] selectionArgs) {
+        return 0;
+    }
+
+    @Override
+    public int update(@NonNull Uri uri, @Nullable ContentValues values,
+            @Nullable String selection, @Nullable String[] selectionArgs) {
+        return 0;
+    }
+}
diff --git a/tests/app/src/android/app/cts/BadProviderTest.java b/tests/app/src/android/app/cts/BadProviderTest.java
new file mode 100644
index 0000000..3c16fd3
--- /dev/null
+++ b/tests/app/src/android/app/cts/BadProviderTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2019 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.app.cts;
+
+import android.app.ActivityManager;
+import android.app.cts.android.app.cts.tools.WatchUidRunner;
+import android.content.ContentResolver;
+import android.content.pm.ApplicationInfo;
+import android.net.Uri;
+import android.os.HandlerThread;
+import android.os.Handler;
+import android.os.SystemClock;
+import android.test.AndroidTestCase;
+
+import androidx.test.InstrumentationRegistry;
+
+/**
+ * Test system behavior of a bad provider.
+ */
+public class BadProviderTest extends AndroidTestCase {
+    private static final String AUTHORITY = "com.android.cts.stubbad.badprovider";
+    private static final String TEST_PACKAGE_NAME = "com.android.cts.stubbad";
+    private static final int WAIT_TIME = 2000;
+
+    public void testExitOnCreate() {
+        WatchUidRunner uidWatcher = null;
+        ContentResolver res = mContext.getContentResolver();
+        HandlerThread worker = new HandlerThread("work");
+        worker.start();
+        Handler handler = new Handler(worker.getLooper());
+        try {
+            ApplicationInfo appInfo = mContext.getPackageManager().getApplicationInfo(
+                    TEST_PACKAGE_NAME, 0);
+            uidWatcher = new WatchUidRunner(InstrumentationRegistry.getInstrumentation(),
+                    appInfo.uid, WAIT_TIME);
+            long startTs = SystemClock.uptimeMillis();
+            handler.post(()->
+                res.query(Uri.parse("content://" + AUTHORITY), null, null, null, null)
+            );
+            // Ensure the system will try at least 3 times for a bad content provider.
+            uidWatcher.waitFor(WatchUidRunner.CMD_GONE, null);
+            uidWatcher.waitFor(WatchUidRunner.CMD_GONE, null);
+            uidWatcher.waitFor(WatchUidRunner.CMD_GONE, null);
+            // Finish the watcher
+            uidWatcher.finish();
+            // Sleep for 10 seconds and initialize the watcher again
+            // (content provider publish timeout is 10 seconds)
+            Thread.sleep(Math.max(0, 10000 - (SystemClock.uptimeMillis() - startTs)));
+            uidWatcher = new WatchUidRunner(InstrumentationRegistry.getInstrumentation(),
+                    appInfo.uid, WAIT_TIME);
+            // By now we shouldn't see it's retrying again.
+            try {
+                uidWatcher.waitFor(WatchUidRunner.CMD_GONE, null);
+                fail("Excessive attempts to bring up a provider");
+            } catch (IllegalStateException e) {
+            }
+        } catch (Exception e) {
+            fail("Unexpected exception while query provider: " + e.getMessage());
+        } finally {
+            if (uidWatcher != null) {
+                uidWatcher.finish();
+            }
+            worker.quitSafely();
+        }
+    }
+}