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