1 /* 2 * Copyright (C) 2016 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.server.telecom.tests; 18 19 import android.content.Context; 20 import android.net.Uri; 21 import android.os.Bundle; 22 import android.os.PersistableBundle; 23 import android.telecom.TelecomManager; 24 import android.telephony.CarrierConfigManager; 25 import android.test.suitebuilder.annotation.SmallTest; 26 27 import com.android.server.telecom.Call; 28 import com.android.server.telecom.CallerInfoLookupHelper; 29 import com.android.server.telecom.callfiltering.AsyncBlockCheckFilter; 30 import com.android.server.telecom.callfiltering.BlockCheckerAdapter; 31 import com.android.server.telecom.callfiltering.CallFilterResultCallback; 32 import com.android.server.telecom.callfiltering.CallFilteringResult; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.junit.runners.JUnit4; 38 import org.mockito.ArgumentCaptor; 39 import org.mockito.Mock; 40 41 import java.util.concurrent.CountDownLatch; 42 43 import static org.mockito.Matchers.any; 44 import static org.mockito.Matchers.eq; 45 import static org.mockito.Mockito.doAnswer; 46 import static org.mockito.Mockito.timeout; 47 import static org.mockito.Mockito.verify; 48 import static org.mockito.Mockito.when; 49 50 @RunWith(JUnit4.class) 51 public class AsyncBlockCheckFilterTest extends TelecomTestCase { 52 @Mock private Context mContext; 53 @Mock private BlockCheckerAdapter mBlockCheckerAdapter; 54 @Mock private Call mCall; 55 @Mock private CallFilterResultCallback mCallback; 56 @Mock private CallerInfoLookupHelper mCallerInfoLookupHelper; 57 @Mock private CarrierConfigManager mCarrierConfigManager; 58 59 private AsyncBlockCheckFilter mFilter; 60 private static final CallFilteringResult BLOCK_RESULT = new CallFilteringResult( 61 false, // shouldAllowCall 62 true, //shouldReject 63 false, //shouldAddToCallLog 64 false // shouldShowNotification 65 ); 66 67 private static final CallFilteringResult PASS_RESULT = new CallFilteringResult( 68 true, // shouldAllowCall 69 false, // shouldReject 70 true, // shouldAddToCallLog 71 true // shouldShowNotification 72 ); 73 74 private static final Uri TEST_HANDLE = Uri.parse("tel:1235551234"); 75 private static final int TEST_TIMEOUT = 100; 76 77 @Override 78 @Before 79 public void setUp() throws Exception { 80 super.setUp(); 81 when(mCall.getHandle()).thenReturn(TEST_HANDLE); 82 mFilter = new AsyncBlockCheckFilter(mContext, mBlockCheckerAdapter, 83 mCallerInfoLookupHelper); 84 } 85 86 @SmallTest 87 @Test 88 public void testBlockNumber() { 89 final CountDownLatch latch = new CountDownLatch(1); 90 doAnswer(invocation -> { 91 latch.countDown(); 92 return true; 93 }).when(mBlockCheckerAdapter) 94 .isBlocked(any(Context.class), eq(TEST_HANDLE.getSchemeSpecificPart()), 95 any(Bundle.class)); 96 97 setEnhancedBlockingEnabled(false); 98 mFilter.startFilterLookup(mCall, mCallback); 99 100 waitOnLatch(latch); 101 verify(mCallback, timeout(TEST_TIMEOUT)) 102 .onCallFilteringComplete(eq(mCall), eq(BLOCK_RESULT)); 103 } 104 105 @SmallTest 106 @Test 107 public void testBlockNumber_enhancedBlockingEnabled() { 108 final CountDownLatch latch = new CountDownLatch(1); 109 doAnswer(invocation -> { 110 latch.countDown(); 111 return true; 112 }).when(mBlockCheckerAdapter) 113 .isBlocked(any(Context.class), eq(TEST_HANDLE.getSchemeSpecificPart()), 114 any(Bundle.class)); 115 116 setEnhancedBlockingEnabled(true); 117 CallerInfoLookupHelper.OnQueryCompleteListener queryListener = verifyEnhancedLookupStart(); 118 queryListener.onCallerInfoQueryComplete(TEST_HANDLE, null); 119 120 waitOnLatch(latch); 121 verify(mCallback, timeout(TEST_TIMEOUT)) 122 .onCallFilteringComplete(eq(mCall), eq(BLOCK_RESULT)); 123 } 124 125 @SmallTest 126 @Test 127 public void testDontBlockNumber() { 128 final CountDownLatch latch = new CountDownLatch(1); 129 doAnswer(invocation -> { 130 latch.countDown(); 131 return false; 132 }).when(mBlockCheckerAdapter) 133 .isBlocked(any(Context.class), eq(TEST_HANDLE.getSchemeSpecificPart()), 134 any(Bundle.class)); 135 136 setEnhancedBlockingEnabled(false); 137 mFilter.startFilterLookup(mCall, mCallback); 138 139 waitOnLatch(latch); 140 verify(mCallback, timeout(TEST_TIMEOUT)) 141 .onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); 142 } 143 144 @SmallTest 145 @Test 146 public void testDontBlockNumber_enhancedBlockingEnabled() { 147 final CountDownLatch latch = new CountDownLatch(1); 148 doAnswer(invocation -> { 149 latch.countDown(); 150 return false; 151 }).when(mBlockCheckerAdapter) 152 .isBlocked(any(Context.class), eq(TEST_HANDLE.getSchemeSpecificPart()), 153 any(Bundle.class)); 154 155 setEnhancedBlockingEnabled(true); 156 CallerInfoLookupHelper.OnQueryCompleteListener queryListener = verifyEnhancedLookupStart(); 157 queryListener.onCallerInfoQueryComplete(TEST_HANDLE, null); 158 159 waitOnLatch(latch); 160 verify(mCallback, timeout(TEST_TIMEOUT)) 161 .onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); 162 } 163 164 private void setEnhancedBlockingEnabled(Boolean value) { 165 PersistableBundle bundle = new PersistableBundle(); 166 bundle.putBoolean(CarrierConfigManager.KEY_SUPPORT_ENHANCED_CALL_BLOCKING_BOOL, 167 value); 168 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) 169 .thenReturn(mCarrierConfigManager); 170 when(mCarrierConfigManager.getConfig()).thenReturn(bundle); 171 } 172 173 private CallerInfoLookupHelper.OnQueryCompleteListener verifyEnhancedLookupStart() { 174 // The enhanced lookup will only be excuted when enhanced blocking enabled and the 175 // presentation is PRESENTATION_ALLOWED. 176 when(mCall.getHandlePresentation()).thenReturn(TelecomManager.PRESENTATION_ALLOWED); 177 mFilter.startFilterLookup(mCall, mCallback); 178 ArgumentCaptor<CallerInfoLookupHelper.OnQueryCompleteListener> captor = 179 ArgumentCaptor.forClass(CallerInfoLookupHelper.OnQueryCompleteListener.class); 180 verify(mCallerInfoLookupHelper).startLookup(eq(TEST_HANDLE), captor.capture()); 181 return captor.getValue(); 182 } 183 184 private void waitOnLatch(CountDownLatch latch) { 185 while (latch.getCount() > 0) { 186 try { 187 latch.await(); 188 } catch (InterruptedException e) { 189 // do nothing 190 } 191 } 192 } 193 } 194