Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 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; |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 18 | |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| 26 | import android.content.pm.ServiceInfo; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.UserHandle; |
| 30 | import android.telecom.CallScreeningService; |
| 31 | import android.telecom.ParcelableCall; |
| 32 | import android.test.suitebuilder.annotation.SmallTest; |
| 33 | |
| 34 | import com.android.internal.telecom.ICallScreeningAdapter; |
| 35 | import com.android.internal.telecom.ICallScreeningService; |
| 36 | import com.android.server.telecom.Call; |
| 37 | import com.android.server.telecom.CallsManager; |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 38 | import com.android.server.telecom.DefaultDialerCache; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 39 | import com.android.server.telecom.ParcelableCallUtils; |
| 40 | import com.android.server.telecom.PhoneAccountRegistrar; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 41 | import com.android.server.telecom.callfiltering.CallFilterResultCallback; |
| 42 | import com.android.server.telecom.callfiltering.CallFilteringResult; |
| 43 | import com.android.server.telecom.callfiltering.CallScreeningServiceFilter; |
| 44 | import com.android.server.telecom.TelecomSystem; |
| 45 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 46 | import org.junit.Before; |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | import org.junit.runners.JUnit4; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 50 | import org.mockito.ArgumentCaptor; |
| 51 | import org.mockito.Mock; |
| 52 | |
| 53 | import java.util.Collections; |
| 54 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 55 | import static org.junit.Assert.assertEquals; |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 56 | import static org.mockito.ArgumentMatchers.nullable; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 57 | import static org.mockito.Matchers.anyBoolean; |
| 58 | import static org.mockito.Matchers.anyInt; |
| 59 | import static org.mockito.Matchers.anyString; |
| 60 | import static org.mockito.Matchers.eq; |
| 61 | import static org.mockito.Mockito.doReturn; |
| 62 | import static org.mockito.Mockito.doThrow; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 63 | import static org.mockito.Mockito.verify; |
| 64 | import static org.mockito.Mockito.when; |
| 65 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 66 | @RunWith(JUnit4.class) |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 67 | public class CallScreeningServiceFilterTest extends TelecomTestCase { |
| 68 | @Mock Context mContext; |
| 69 | @Mock CallsManager mCallsManager; |
| 70 | @Mock PhoneAccountRegistrar mPhoneAccountRegistrar; |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 71 | @Mock DefaultDialerCache mDefaultDialerCache; |
| 72 | @Mock ParcelableCallUtils.Converter mParcelableCallUtilsConverter; |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 73 | private TelecomSystem.SyncRoot mLock = new TelecomSystem.SyncRoot() { }; |
| 74 | |
| 75 | @Mock Call mCall; |
| 76 | @Mock CallFilterResultCallback mCallback; |
| 77 | |
| 78 | @Mock PackageManager mPackageManager; |
| 79 | @Mock IBinder mBinder; |
| 80 | @Mock ICallScreeningService mCallScreeningService; |
| 81 | |
| 82 | private static final String PKG_NAME = "com.android.services.telecom.tests"; |
| 83 | private static final String CLS_NAME = "CallScreeningService"; |
| 84 | private static final ComponentName COMPONENT_NAME = new ComponentName(PKG_NAME, CLS_NAME); |
| 85 | private static final String CALL_ID = "u89prgt9ps78y5"; |
| 86 | |
| 87 | private ResolveInfo mResolveInfo; |
| 88 | |
| 89 | private static final CallFilteringResult PASS_RESULT = new CallFilteringResult( |
| 90 | true, // shouldAllowCall |
| 91 | false, // shouldReject |
| 92 | true, // shouldAddToCallLog |
| 93 | true // shouldShowNotification |
| 94 | ); |
| 95 | |
| 96 | private CallScreeningServiceFilter mFilter; |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 97 | |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 98 | @Override |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 99 | @Before |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 100 | public void setUp() throws Exception { |
| 101 | super.setUp(); |
| 102 | when(mCallsManager.getCurrentUserHandle()).thenReturn(UserHandle.CURRENT); |
| 103 | when(mContext.getPackageManager()).thenReturn(mPackageManager); |
| 104 | when(mCall.getId()).thenReturn(CALL_ID); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 105 | doReturn(mCallScreeningService).when(mBinder).queryLocalInterface(anyString()); |
| 106 | |
| 107 | mResolveInfo = new ResolveInfo() {{ |
| 108 | serviceInfo = new ServiceInfo(); |
| 109 | serviceInfo.packageName = PKG_NAME; |
| 110 | serviceInfo.name = CLS_NAME; |
| 111 | serviceInfo.permission = Manifest.permission.BIND_SCREENING_SERVICE; |
| 112 | }}; |
| 113 | |
| 114 | mFilter = new CallScreeningServiceFilter(mContext, mCallsManager, mPhoneAccountRegistrar, |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 115 | mDefaultDialerCache, mParcelableCallUtilsConverter, mLock); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 116 | |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 117 | when(mDefaultDialerCache.getDefaultDialerApplication(eq(UserHandle.USER_CURRENT))) |
| 118 | .thenReturn(PKG_NAME); |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 119 | when(mPackageManager.queryIntentServicesAsUser(nullable(Intent.class), anyInt(), anyInt())) |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 120 | .thenReturn(Collections.singletonList(mResolveInfo)); |
| 121 | when(mParcelableCallUtilsConverter.toParcelableCall( |
| 122 | eq(mCall), anyBoolean(), eq(mPhoneAccountRegistrar))).thenReturn(null); |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 123 | when(mContext.bindServiceAsUser(nullable(Intent.class), nullable(ServiceConnection.class), |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 124 | anyInt(), eq(UserHandle.CURRENT))).thenReturn(true); |
| 125 | } |
| 126 | |
| 127 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 128 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 129 | public void testNoDefaultDialer() { |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 130 | when(mDefaultDialerCache.getDefaultDialerApplication(eq(UserHandle.USER_CURRENT))) |
| 131 | .thenReturn(null); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 132 | mFilter.startFilterLookup(mCall, mCallback); |
| 133 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 134 | } |
| 135 | |
| 136 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 137 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 138 | public void testNoResolveEntries() { |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 139 | when(mPackageManager.queryIntentServicesAsUser(nullable(Intent.class), anyInt(), anyInt())) |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 140 | .thenReturn(Collections.emptyList()); |
| 141 | mFilter.startFilterLookup(mCall, mCallback); |
| 142 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 143 | } |
| 144 | |
| 145 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 146 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 147 | public void testBadResolveEntry() { |
| 148 | mResolveInfo.serviceInfo = null; |
| 149 | mFilter.startFilterLookup(mCall, mCallback); |
| 150 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 151 | } |
| 152 | |
| 153 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 154 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 155 | public void testPermissionlessFilterService() { |
| 156 | mResolveInfo.serviceInfo.permission = null; |
| 157 | mFilter.startFilterLookup(mCall, mCallback); |
| 158 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 159 | } |
| 160 | |
| 161 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 162 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 163 | public void testContextFailToBind() { |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 164 | when(mContext.bindServiceAsUser(nullable(Intent.class), nullable(ServiceConnection.class), |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 165 | anyInt(), eq(UserHandle.CURRENT))).thenReturn(false); |
| 166 | mFilter.startFilterLookup(mCall, mCallback); |
| 167 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 168 | } |
| 169 | |
| 170 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 171 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 172 | public void testExceptionInScreeningService() throws Exception { |
| 173 | doThrow(new RemoteException()).when(mCallScreeningService).screenCall( |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 174 | nullable(ICallScreeningAdapter.class), nullable(ParcelableCall.class)); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 175 | mFilter.startFilterLookup(mCall, mCallback); |
| 176 | ServiceConnection serviceConnection = verifyBindingIntent(); |
| 177 | serviceConnection.onServiceConnected(COMPONENT_NAME, mBinder); |
| 178 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 179 | } |
| 180 | |
| 181 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 182 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 183 | public void testAllowCall() throws Exception { |
| 184 | mFilter.startFilterLookup(mCall, mCallback); |
| 185 | ServiceConnection serviceConnection = verifyBindingIntent(); |
| 186 | serviceConnection.onServiceConnected(COMPONENT_NAME, mBinder); |
| 187 | ICallScreeningAdapter csAdapter = getCallScreeningAdapter(); |
| 188 | csAdapter.allowCall(CALL_ID); |
| 189 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT)); |
| 190 | } |
| 191 | |
| 192 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 193 | @Test |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 194 | public void testDisallowCall() throws Exception { |
| 195 | mFilter.startFilterLookup(mCall, mCallback); |
| 196 | ServiceConnection serviceConnection = verifyBindingIntent(); |
| 197 | serviceConnection.onServiceConnected(COMPONENT_NAME, mBinder); |
| 198 | ICallScreeningAdapter csAdapter = getCallScreeningAdapter(); |
| 199 | csAdapter.disallowCall(CALL_ID, |
| 200 | true, // shouldReject |
| 201 | false, // shouldAddToCallLog |
| 202 | true // shouldShowNotification |
| 203 | ); |
| 204 | verify(mCallback).onCallFilteringComplete(eq(mCall), eq(new CallFilteringResult( |
| 205 | false, // shouldAllowCall |
| 206 | true, // shouldReject |
| 207 | false, // shouldAddToCallLog |
| 208 | true // shouldShowNotification |
| 209 | ))); |
| 210 | } |
| 211 | |
| 212 | private ServiceConnection verifyBindingIntent() { |
| 213 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 214 | ArgumentCaptor<ServiceConnection> serviceCaptor = |
| 215 | ArgumentCaptor.forClass(ServiceConnection.class); |
| 216 | verify(mContext).bindServiceAsUser(intentCaptor.capture(), serviceCaptor.capture(), |
| 217 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 218 | eq(UserHandle.CURRENT)); |
| 219 | |
| 220 | Intent capturedIntent = intentCaptor.getValue(); |
| 221 | assertEquals(CallScreeningService.SERVICE_INTERFACE, capturedIntent.getAction()); |
| 222 | assertEquals(PKG_NAME, capturedIntent.getPackage()); |
| 223 | assertEquals(COMPONENT_NAME, capturedIntent.getComponent()); |
| 224 | |
| 225 | return serviceCaptor.getValue(); |
| 226 | } |
| 227 | |
| 228 | private ICallScreeningAdapter getCallScreeningAdapter() throws Exception { |
| 229 | ArgumentCaptor<ICallScreeningAdapter> captor = |
| 230 | ArgumentCaptor.forClass(ICallScreeningAdapter.class); |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 231 | verify(mCallScreeningService).screenCall(captor.capture(), nullable(ParcelableCall.class)); |
Hall Liu | 6d4b66d | 2016-04-01 16:31:13 -0700 | [diff] [blame] | 232 | return captor.getValue(); |
| 233 | } |
| 234 | } |