Add unit tests to verify default behavior when provider crashes.

Added unit tests to verify the default behavior of the
BlockedNumberContract static methods when the provider crashes or is not
available.

Test: Added unit tests to verify default behavior when provider crashes.
Bug: 74965829
Change-Id: I21d238d8561f40930f1972ea7fdd3c89b95ece06
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 0405820..4c00887 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -18,8 +18,15 @@
         package="com.android.providers.blockednumber.tests"
         android:sharedUserId="android.uid.shared">
 
-    <application>
-        <uses-library android:name="android.test.runner" />
+    <application android:name="com.android.providers.blockednumber.TestApplication">
+      <uses-library android:name="android.test.runner" />
+      <activity android:label="BlockedNumberProviderTest"
+                android:name="BlockedNumberProviderTest">
+        <intent-filter>
+          <action android:name="android.intent.action.MAIN" />
+          <category android:name="android.intent.category.LAUNCHER"/>
+        </intent-filter>
+      </activity>
     </application>
 
     <instrumentation
diff --git a/tests/AndroidTest.xml b/tests/AndroidTest.xml
new file mode 100644
index 0000000..5b507b2
--- /dev/null
+++ b/tests/AndroidTest.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<configuration description="Runs BlockedNumberProvider Test Cases.">
+    <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">
+        <option name="test-file-name" value="BlockedNumberProviderTest.apk" />
+    </target_preparer>
+
+    <option name="test-suite-tag" value="apct" />
+    <option name="test-tag" value="BlockedNumberProviderTest" />
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+        <option name="package" value="com.android.providers.blockednumber.tests" />
+        <option name="runner" value="android.support.test.runner.AndroidJUnitRunner" />
+    </test>
+</configuration>
diff --git a/tests/src/com/android/providers/blockednumber/BlockedNumberContractTest.java b/tests/src/com/android/providers/blockednumber/BlockedNumberContractTest.java
new file mode 100644
index 0000000..89cca78
--- /dev/null
+++ b/tests/src/com/android/providers/blockednumber/BlockedNumberContractTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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 com.android.providers.blockednumber;
+
+import static android.provider.BlockedNumberContract.SystemContract
+        .ENHANCED_SETTING_KEY_BLOCK_PAYPHONE;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
+import android.content.ContentProvider;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.BlockedNumberContract;
+import android.test.AndroidTestCase;
+import android.test.mock.MockContentResolver;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.TestCase;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+import org.mockito.ArgumentMatchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+/**
+ * Tests for {@link android.provider.BlockedNumberContract}.
+ */
+@RunWith(JUnit4.class)
+public class BlockedNumberContractTest extends AndroidTestCase {
+    private static final String TEST_NUMBER = "650-555-1212";
+
+    @Mock
+    private Context mMockContext;
+
+    @Mock
+    private ContentProvider mMockProvider;
+
+    private MockContentResolver mMockContentResolver = new MockContentResolver();
+
+    @Before
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        initMocks(this);
+        mMockContentResolver.addProvider(BlockedNumberContract.AUTHORITY_URI.toString(),
+                mMockProvider);
+        when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver);
+        when(mMockProvider.call(any(), any(), any()))
+                .thenThrow(new NullPointerException());
+    }
+
+    /**
+     * Ensures a content provider crash results in a "false" response from
+     * {@link BlockedNumberContract#isBlocked(Context, String)}.
+     */
+    @SmallTest
+    @Test
+    public void testIsBlockedException() {
+        assertFalse(BlockedNumberContract.isBlocked(mMockContext, TEST_NUMBER));
+    }
+
+    /**
+     * Ensures a content provider crash results in a "false" response from
+     * {@link BlockedNumberContract#canCurrentUserBlockNumbers(Context)}.
+     */
+    @SmallTest
+    @Test
+    public void testCanUserBlockNumbersException() {
+        assertFalse(BlockedNumberContract.canCurrentUserBlockNumbers(mMockContext));
+    }
+
+    /**
+     * Ensures a content provider crash results in a "false" response from
+     * {@link BlockedNumberContract.SystemContract#shouldSystemBlockNumber(Context, String, Bundle)}
+     */
+    @SmallTest
+    @Test
+    public void testShouldSystemBlockNumberException() {
+        assertFalse(BlockedNumberContract.SystemContract.shouldSystemBlockNumber(mMockContext,
+                TEST_NUMBER, null));
+    }
+
+    /**
+     * Ensures a content provider crash results in a "false" response from
+     * {@link BlockedNumberContract.SystemContract#shouldShowEmergencyCallNotification(Context)}.
+     */
+    @SmallTest
+    @Test
+    public void testShouldShowEmergencyCallNotificationException() {
+        assertFalse(BlockedNumberContract.SystemContract.shouldShowEmergencyCallNotification(
+                mMockContext));
+    }
+
+    /**
+     * Ensures a content provider crash results in a "false" response from
+     * {@link BlockedNumberContract.SystemContract#getEnhancedBlockSetting(Context, String)}.
+     */
+    @SmallTest
+    @Test
+    public void testGetEnhancedBlockSettingException() {
+        assertFalse(BlockedNumberContract.SystemContract.getEnhancedBlockSetting(
+                mMockContext, ENHANCED_SETTING_KEY_BLOCK_PAYPHONE));
+    }
+}