Prevents an NPE when content provider is slow to start
Also, extends the client-side timeout to match that
in ActivityManagerService.
Also, fixes an ANR that could occur if getType is called for
an unknown content provider
---------------
Revert "Revert "Prevents an NPE when content provider is slow to start""
This reverts commit 3a54effffdb361ccd60169dcce93cf4e87fc6dac.
Reason for revert: Roll forward of the original CL,
with a fix for the regression it caused
Original commit 140fc2e9c9382d5326b72ff28e8782e7bffc493d
Test: atest FrameworksCoreTests:android.content.ContentResolverTest
Fixes: 148987678
Change-Id: Ic60acceaabbf6b32c71d0b8bdf831a7d1f13d392
diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml
index 59335a5..718ca46 100644
--- a/core/tests/coretests/AndroidManifest.xml
+++ b/core/tests/coretests/AndroidManifest.xml
@@ -1330,6 +1330,12 @@
android:process=":FakeProvider">
</provider>
+ <provider
+ android:name="android.content.SlowProvider"
+ android:authorities="android.content.SlowProvider"
+ android:process=":SlowProvider">
+ </provider>
+
<!-- Application components used for os tests -->
<service android:name="android.os.MessengerService"
diff --git a/core/tests/coretests/src/android/content/ContentResolverTest.java b/core/tests/coretests/src/android/content/ContentResolverTest.java
index 9dcce1e..78c4420 100644
--- a/core/tests/coretests/src/android/content/ContentResolverTest.java
+++ b/core/tests/coretests/src/android/content/ContentResolverTest.java
@@ -16,6 +16,8 @@
package android.content;
+import static com.google.common.truth.Truth.assertThat;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -31,6 +33,7 @@
import android.net.Uri;
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
+import android.os.SystemClock;
import android.util.Size;
import androidx.test.InstrumentationRegistry;
@@ -209,4 +212,26 @@
String type = mResolver.getType(Uri.parse("content://android.content.FakeProviderRemote"));
assertEquals("fake/remote", type);
}
+
+ @Test
+ public void testGetType_slowProvider() {
+ // This provider is running in a different process and is intentionally slow to start.
+ // We are trying to confirm that it does not cause an ANR
+ long start = SystemClock.uptimeMillis();
+ String type = mResolver.getType(Uri.parse("content://android.content.SlowProvider"));
+ long end = SystemClock.uptimeMillis();
+ assertEquals("slow", type);
+ assertThat(end).isLessThan(start + 5000);
+ }
+
+ @Test
+ public void testGetType_unknownProvider() {
+ // This provider does not exist.
+ // We are trying to confirm that getType returns null and does not cause an ANR
+ long start = SystemClock.uptimeMillis();
+ String type = mResolver.getType(Uri.parse("content://android.content.NonexistentProvider"));
+ long end = SystemClock.uptimeMillis();
+ assertThat(type).isNull();
+ assertThat(end).isLessThan(start + 5000);
+ }
}
diff --git a/core/tests/coretests/src/android/content/SlowProvider.java b/core/tests/coretests/src/android/content/SlowProvider.java
new file mode 100644
index 0000000..aba32e8
--- /dev/null
+++ b/core/tests/coretests/src/android/content/SlowProvider.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2020 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.content;
+
+import android.database.Cursor;
+import android.net.Uri;
+
+/**
+ * A dummy content provider for tests. This provider runs in a different process from the test and
+ * is intentionally slow.
+ */
+public class SlowProvider extends ContentProvider {
+
+ private static final int ON_CREATE_LATENCY_MILLIS = 3000;
+
+ @Override
+ public boolean onCreate() {
+ try {
+ Thread.sleep(ON_CREATE_LATENCY_MILLIS);
+ } catch (InterruptedException e) {
+ // Ignore
+ }
+ return true;
+ }
+
+ @Override
+ public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
+ String sortOrder) {
+ return null;
+ }
+
+ @Override
+ public String getType(Uri uri) {
+ return "slow";
+ }
+
+ @Override
+ public Uri insert(Uri uri, ContentValues values) {
+ return null;
+ }
+
+ @Override
+ public int delete(Uri uri, String selection, String[] selectionArgs) {
+ return 0;
+ }
+
+ @Override
+ public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
+ return 0;
+ }
+}