blob: a319fad697bb93b4cbd6cbc9bddde3aa613dad42 [file] [log] [blame]
Hall Liu6d4b66d2016-04-01 16:31:13 -07001/*
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
17package com.android.server.telecom.tests;
Hall Liuc8a396b2017-12-27 18:23:28 -080018
Hall Liu6d4b66d2016-04-01 16:31:13 -070019import android.Manifest;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.ServiceConnection;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
26import android.content.pm.ServiceInfo;
27import android.os.IBinder;
28import android.os.RemoteException;
29import android.os.UserHandle;
30import android.telecom.CallScreeningService;
31import android.telecom.ParcelableCall;
32import android.test.suitebuilder.annotation.SmallTest;
33
34import com.android.internal.telecom.ICallScreeningAdapter;
35import com.android.internal.telecom.ICallScreeningService;
36import com.android.server.telecom.Call;
37import com.android.server.telecom.CallsManager;
Hall Liu7c928322016-12-06 18:15:39 -080038import com.android.server.telecom.DefaultDialerCache;
Hall Liu6d4b66d2016-04-01 16:31:13 -070039import com.android.server.telecom.ParcelableCallUtils;
40import com.android.server.telecom.PhoneAccountRegistrar;
Hall Liu6d4b66d2016-04-01 16:31:13 -070041import com.android.server.telecom.callfiltering.CallFilterResultCallback;
42import com.android.server.telecom.callfiltering.CallFilteringResult;
43import com.android.server.telecom.callfiltering.CallScreeningServiceFilter;
44import com.android.server.telecom.TelecomSystem;
45
Hall Liuc8a396b2017-12-27 18:23:28 -080046import org.junit.Before;
47import org.junit.Test;
48import org.junit.runner.RunWith;
49import org.junit.runners.JUnit4;
Hall Liu6d4b66d2016-04-01 16:31:13 -070050import org.mockito.ArgumentCaptor;
51import org.mockito.Mock;
52
53import java.util.Collections;
54
Hall Liuc8a396b2017-12-27 18:23:28 -080055import static org.junit.Assert.assertEquals;
Brad Ebingerd0fe76e2017-03-20 13:17:15 -070056import static org.mockito.ArgumentMatchers.nullable;
Hall Liu6d4b66d2016-04-01 16:31:13 -070057import static org.mockito.Matchers.anyBoolean;
58import static org.mockito.Matchers.anyInt;
59import static org.mockito.Matchers.anyString;
60import static org.mockito.Matchers.eq;
61import static org.mockito.Mockito.doReturn;
62import static org.mockito.Mockito.doThrow;
Hall Liu6d4b66d2016-04-01 16:31:13 -070063import static org.mockito.Mockito.verify;
64import static org.mockito.Mockito.when;
65
Hall Liuc8a396b2017-12-27 18:23:28 -080066@RunWith(JUnit4.class)
Hall Liu6d4b66d2016-04-01 16:31:13 -070067public class CallScreeningServiceFilterTest extends TelecomTestCase {
68 @Mock Context mContext;
69 @Mock CallsManager mCallsManager;
70 @Mock PhoneAccountRegistrar mPhoneAccountRegistrar;
Hall Liu7c928322016-12-06 18:15:39 -080071 @Mock DefaultDialerCache mDefaultDialerCache;
72 @Mock ParcelableCallUtils.Converter mParcelableCallUtilsConverter;
Hall Liu6d4b66d2016-04-01 16:31:13 -070073 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 Liuc8a396b2017-12-27 18:23:28 -080097
Hall Liu6d4b66d2016-04-01 16:31:13 -070098 @Override
Hall Liuc8a396b2017-12-27 18:23:28 -080099 @Before
Hall Liu6d4b66d2016-04-01 16:31:13 -0700100 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 Liu6d4b66d2016-04-01 16:31:13 -0700105 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 Liu7c928322016-12-06 18:15:39 -0800115 mDefaultDialerCache, mParcelableCallUtilsConverter, mLock);
Hall Liu6d4b66d2016-04-01 16:31:13 -0700116
Hall Liu7c928322016-12-06 18:15:39 -0800117 when(mDefaultDialerCache.getDefaultDialerApplication(eq(UserHandle.USER_CURRENT)))
118 .thenReturn(PKG_NAME);
Brad Ebingerd0fe76e2017-03-20 13:17:15 -0700119 when(mPackageManager.queryIntentServicesAsUser(nullable(Intent.class), anyInt(), anyInt()))
Hall Liu6d4b66d2016-04-01 16:31:13 -0700120 .thenReturn(Collections.singletonList(mResolveInfo));
121 when(mParcelableCallUtilsConverter.toParcelableCall(
122 eq(mCall), anyBoolean(), eq(mPhoneAccountRegistrar))).thenReturn(null);
Brad Ebingerd0fe76e2017-03-20 13:17:15 -0700123 when(mContext.bindServiceAsUser(nullable(Intent.class), nullable(ServiceConnection.class),
Hall Liu6d4b66d2016-04-01 16:31:13 -0700124 anyInt(), eq(UserHandle.CURRENT))).thenReturn(true);
125 }
126
127 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800128 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700129 public void testNoDefaultDialer() {
Hall Liu7c928322016-12-06 18:15:39 -0800130 when(mDefaultDialerCache.getDefaultDialerApplication(eq(UserHandle.USER_CURRENT)))
131 .thenReturn(null);
Hall Liu6d4b66d2016-04-01 16:31:13 -0700132 mFilter.startFilterLookup(mCall, mCallback);
133 verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT));
134 }
135
136 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800137 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700138 public void testNoResolveEntries() {
Brad Ebingerd0fe76e2017-03-20 13:17:15 -0700139 when(mPackageManager.queryIntentServicesAsUser(nullable(Intent.class), anyInt(), anyInt()))
Hall Liu6d4b66d2016-04-01 16:31:13 -0700140 .thenReturn(Collections.emptyList());
141 mFilter.startFilterLookup(mCall, mCallback);
142 verify(mCallback).onCallFilteringComplete(eq(mCall), eq(PASS_RESULT));
143 }
144
145 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800146 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700147 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 Liuc8a396b2017-12-27 18:23:28 -0800154 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700155 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 Liuc8a396b2017-12-27 18:23:28 -0800162 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700163 public void testContextFailToBind() {
Brad Ebingerd0fe76e2017-03-20 13:17:15 -0700164 when(mContext.bindServiceAsUser(nullable(Intent.class), nullable(ServiceConnection.class),
Hall Liu6d4b66d2016-04-01 16:31:13 -0700165 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 Liuc8a396b2017-12-27 18:23:28 -0800171 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700172 public void testExceptionInScreeningService() throws Exception {
173 doThrow(new RemoteException()).when(mCallScreeningService).screenCall(
Brad Ebingerd0fe76e2017-03-20 13:17:15 -0700174 nullable(ICallScreeningAdapter.class), nullable(ParcelableCall.class));
Hall Liu6d4b66d2016-04-01 16:31:13 -0700175 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 Liuc8a396b2017-12-27 18:23:28 -0800182 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700183 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 Liuc8a396b2017-12-27 18:23:28 -0800193 @Test
Hall Liu6d4b66d2016-04-01 16:31:13 -0700194 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 Ebingerd0fe76e2017-03-20 13:17:15 -0700231 verify(mCallScreeningService).screenCall(captor.capture(), nullable(ParcelableCall.class));
Hall Liu6d4b66d2016-04-01 16:31:13 -0700232 return captor.getValue();
233 }
234}