AsyncBlockCheckFilterTest.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;
18
19import android.content.Context;
20import android.net.Uri;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import com.android.server.telecom.Call;
24import com.android.server.telecom.callfiltering.AsyncBlockCheckFilter;
25import com.android.server.telecom.callfiltering.BlockCheckerAdapter;
26import com.android.server.telecom.callfiltering.CallFilterResultCallback;
27import com.android.server.telecom.callfiltering.CallFilteringResult;
28
29import org.mockito.Mock;
30
31import java.util.concurrent.CountDownLatch;
32
33import static org.mockito.Matchers.any;
34import static org.mockito.Matchers.eq;
35import static org.mockito.Mockito.doAnswer;
36import static org.mockito.Mockito.timeout;
37import static org.mockito.Mockito.verify;
38import static org.mockito.Mockito.when;
39
40public class AsyncBlockCheckFilterTest extends TelecomTestCase {
41    @Mock private Context mContext;
42    @Mock private BlockCheckerAdapter mBlockCheckerAdapter;
43    @Mock private Call mCall;
44    @Mock private CallFilterResultCallback mCallback;
45
46    private AsyncBlockCheckFilter mFilter;
47    private static final CallFilteringResult BLOCK_RESULT = new CallFilteringResult(
48            false, // shouldAllowCall
49            true, //shouldReject
50            false, //shouldAddToCallLog
51            false // shouldShowNotification
52    );
53
54    private static final CallFilteringResult PASS_RESULT = new CallFilteringResult(
55            true, // shouldAllowCall
56            false, // shouldReject
57            true, // shouldAddToCallLog
58            true // shouldShowNotification
59    );
60
61    private static final Uri TEST_HANDLE = Uri.parse("tel:1235551234");
62    private static final int TEST_TIMEOUT = 100;
63
64    @Override
65    public void setUp() throws Exception {
66        super.setUp();
67        when(mCall.getHandle()).thenReturn(TEST_HANDLE);
68        mFilter = new AsyncBlockCheckFilter(mContext, mBlockCheckerAdapter);
69    }
70
71    @SmallTest
72    public void testBlockNumber() {
73        final CountDownLatch latch = new CountDownLatch(1);
74        doAnswer(invocation -> {
75            latch.countDown();
76            return true;
77        }).when(mBlockCheckerAdapter)
78                .isBlocked(any(Context.class), eq(TEST_HANDLE.getSchemeSpecificPart()));
79        mFilter.startFilterLookup(mCall, mCallback);
80        waitOnLatch(latch);
81        verify(mCallback, timeout(TEST_TIMEOUT))
82                .onCallFilteringComplete(eq(mCall), eq(BLOCK_RESULT));
83    }
84
85    @SmallTest
86    public void testDontBlockNumber() {
87        final CountDownLatch latch = new CountDownLatch(1);
88        doAnswer(invocation -> {
89            latch.countDown();
90            return false;
91        }).when(mBlockCheckerAdapter)
92                .isBlocked(any(Context.class), eq(TEST_HANDLE.getSchemeSpecificPart()));
93        mFilter.startFilterLookup(mCall, mCallback);
94        waitOnLatch(latch);
95        verify(mCallback, timeout(TEST_TIMEOUT))
96                .onCallFilteringComplete(eq(mCall), eq(PASS_RESULT));
97    }
98
99    private void waitOnLatch(CountDownLatch latch) {
100        while (latch.getCount() > 0) {
101            try {
102                latch.await();
103            } catch (InterruptedException e) {
104                // do nothing
105            }
106        }
107    }
108}
109