CallScreeningServiceFilterTest.java revision 6d4b66df3d918e3f17263ff40ca3ba0ec5a46719
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
17package com.android.server.telecom.tests;
18import android.Manifest;
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.content.pm.ServiceInfo;
26import android.os.IBinder;
27import android.os.RemoteException;
28import android.os.UserHandle;
29import android.telecom.CallScreeningService;
30import android.telecom.ParcelableCall;
31import android.test.suitebuilder.annotation.SmallTest;
32
33import com.android.internal.telecom.ICallScreeningAdapter;
34import com.android.internal.telecom.ICallScreeningService;
35import com.android.server.telecom.Call;
36import com.android.server.telecom.CallsManager;
37import com.android.server.telecom.ParcelableCallUtils;
38import com.android.server.telecom.PhoneAccountRegistrar;
39import com.android.server.telecom.TelecomServiceImpl;
40import com.android.server.telecom.callfiltering.CallFilterResultCallback;
41import com.android.server.telecom.callfiltering.CallFilteringResult;
42import com.android.server.telecom.callfiltering.CallScreeningServiceFilter;
43import com.android.server.telecom.TelecomSystem;
44
45import org.mockito.ArgumentCaptor;
46import org.mockito.Mock;
47
48import java.util.Collections;
49
50import static org.mockito.Matchers.any;
51import static org.mockito.Matchers.anyBoolean;
52import static org.mockito.Matchers.anyInt;
53import static org.mockito.Matchers.anyString;
54import static org.mockito.Matchers.eq;
55import static org.mockito.Mockito.doReturn;
56import static org.mockito.Mockito.doThrow;
57import static org.mockito.Mockito.mock;
58import static org.mockito.Mockito.verify;
59import static org.mockito.Mockito.when;
60
61public 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
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
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
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
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
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
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
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
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
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
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
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