1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.providers.blockednumber; 18 19 import static android.provider.BlockedNumberContract.SystemContract 20 .ENHANCED_SETTING_KEY_BLOCK_PAYPHONE; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.Mockito.when; 24 import static org.mockito.MockitoAnnotations.initMocks; 25 26 import android.content.ContentProvider; 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.provider.BlockedNumberContract; 32 import android.test.AndroidTestCase; 33 import android.test.mock.MockContentResolver; 34 import android.test.suitebuilder.annotation.SmallTest; 35 36 import junit.framework.TestCase; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.junit.runners.JUnit4; 42 import org.mockito.ArgumentMatchers; 43 import org.mockito.Mock; 44 import org.mockito.Mockito; 45 46 /** 47 * Tests for {@link android.provider.BlockedNumberContract}. 48 */ 49 @RunWith(JUnit4.class) 50 public class BlockedNumberContractTest extends AndroidTestCase { 51 private static final String TEST_NUMBER = "650-555-1212"; 52 53 @Mock 54 private Context mMockContext; 55 56 @Mock 57 private ContentProvider mMockProvider; 58 59 private MockContentResolver mMockContentResolver = new MockContentResolver(); 60 61 @Before 62 @Override 63 public void setUp() throws Exception { 64 super.setUp(); 65 initMocks(this); 66 mMockContentResolver.addProvider(BlockedNumberContract.AUTHORITY_URI.toString(), 67 mMockProvider); 68 when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver); 69 when(mMockProvider.call(any(), any(), any())) 70 .thenThrow(new NullPointerException()); 71 } 72 73 /** 74 * Ensures a content provider crash results in a "false" response from 75 * {@link BlockedNumberContract#isBlocked(Context, String)}. 76 */ 77 @SmallTest 78 @Test 79 public void testIsBlockedException() { 80 assertFalse(BlockedNumberContract.isBlocked(mMockContext, TEST_NUMBER)); 81 } 82 83 /** 84 * Ensures a content provider crash results in a "false" response from 85 * {@link BlockedNumberContract#canCurrentUserBlockNumbers(Context)}. 86 */ 87 @SmallTest 88 @Test 89 public void testCanUserBlockNumbersException() { 90 assertFalse(BlockedNumberContract.canCurrentUserBlockNumbers(mMockContext)); 91 } 92 93 /** 94 * Ensures a content provider crash results in a "false" response from 95 * {@link BlockedNumberContract.SystemContract#shouldSystemBlockNumber(Context, String, Bundle)} 96 */ 97 @SmallTest 98 @Test 99 public void testShouldSystemBlockNumberException() { 100 assertFalse(BlockedNumberContract.SystemContract.shouldSystemBlockNumber(mMockContext, 101 TEST_NUMBER, null)); 102 } 103 104 /** 105 * Ensures a content provider crash results in a "false" response from 106 * {@link BlockedNumberContract.SystemContract#shouldShowEmergencyCallNotification(Context)}. 107 */ 108 @SmallTest 109 @Test 110 public void testShouldShowEmergencyCallNotificationException() { 111 assertFalse(BlockedNumberContract.SystemContract.shouldShowEmergencyCallNotification( 112 mMockContext)); 113 } 114 115 /** 116 * Ensures a content provider crash results in a "false" response from 117 * {@link BlockedNumberContract.SystemContract#getEnhancedBlockSetting(Context, String)}. 118 */ 119 @SmallTest 120 @Test 121 public void testGetEnhancedBlockSettingException() { 122 assertFalse(BlockedNumberContract.SystemContract.getEnhancedBlockSetting( 123 mMockContext, ENHANCED_SETTING_KEY_BLOCK_PAYPHONE)); 124 } 125 } 126