Home | History | Annotate | Download | only in tests
      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 
     19 import android.net.Uri;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import com.android.internal.telephony.CallerInfo;
     23 import com.android.server.telecom.Call;
     24 import com.android.server.telecom.callfiltering.CallFilterResultCallback;
     25 import com.android.server.telecom.CallerInfoLookupHelper;
     26 import com.android.server.telecom.callfiltering.CallFilteringResult;
     27 import com.android.server.telecom.callfiltering.DirectToVoicemailCallFilter;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.junit.runners.JUnit4;
     33 import org.mockito.ArgumentCaptor;
     34 import org.mockito.Mock;
     35 
     36 import static org.mockito.Matchers.eq;
     37 import static org.mockito.Mockito.verify;
     38 import static org.mockito.Mockito.when;
     39 
     40 @RunWith(JUnit4.class)
     41 public class DirectToVoicemailCallFilterTest extends TelecomTestCase {
     42     @Mock private CallerInfoLookupHelper mCallerInfoLookupHelper;
     43     @Mock private CallFilterResultCallback mCallback;
     44     @Mock private Call mCall;
     45 
     46     private static final Uri TEST_HANDLE = Uri.parse("tel:1235551234");
     47 
     48     @Override
     49     @Before
     50     public void setUp() throws Exception {
     51         super.setUp();
     52     }
     53 
     54     @SmallTest
     55     @Test
     56     public void testSendToVoicemail() {
     57         CallerInfoLookupHelper.OnQueryCompleteListener queryListener = verifyLookupStart();
     58 
     59         CallerInfo callerInfo = new CallerInfo();
     60         callerInfo.shouldSendToVoicemail = true;
     61 
     62         queryListener.onCallerInfoQueryComplete(TEST_HANDLE, callerInfo);
     63         verify(mCallback).onCallFilteringComplete(mCall,
     64                 new CallFilteringResult(
     65                         false, // shouldAllowCall
     66                         true, // shouldReject
     67                         true, // shouldAddToCallLog
     68                         true // shouldShowNotification
     69                 ));
     70     }
     71 
     72     @SmallTest
     73     @Test
     74     public void testDontSendToVoicemail() {
     75         CallerInfoLookupHelper.OnQueryCompleteListener queryListener = verifyLookupStart();
     76 
     77         CallerInfo callerInfo = new CallerInfo();
     78         callerInfo.shouldSendToVoicemail = false;
     79 
     80         queryListener.onCallerInfoQueryComplete(TEST_HANDLE, callerInfo);
     81         verify(mCallback).onCallFilteringComplete(mCall,
     82                 new CallFilteringResult(
     83                         true, // shouldAllowCall
     84                         false, // shouldReject
     85                         true, // shouldAddToCallLog
     86                         true // shouldShowNotification
     87                 ));
     88     }
     89 
     90     @SmallTest
     91     @Test
     92     public void testNullResponseFromLookupHelper() {
     93         CallerInfoLookupHelper.OnQueryCompleteListener queryListener = verifyLookupStart(null);
     94 
     95         queryListener.onCallerInfoQueryComplete(null, null);
     96         verify(mCallback).onCallFilteringComplete(mCall,
     97                 new CallFilteringResult(
     98                         true, // shouldAllowCall
     99                         false, // shouldReject
    100                         true, // shouldAddToCallLog
    101                         true // shouldShowNotification
    102                 ));
    103     }
    104 
    105     private CallerInfoLookupHelper.OnQueryCompleteListener verifyLookupStart() {
    106         return verifyLookupStart(TEST_HANDLE);
    107     }
    108 
    109     private CallerInfoLookupHelper.OnQueryCompleteListener verifyLookupStart(Uri handle) {
    110         when(mCall.getHandle()).thenReturn(handle);
    111         DirectToVoicemailCallFilter filter =
    112                 new DirectToVoicemailCallFilter(mCallerInfoLookupHelper);
    113         filter.startFilterLookup(mCall, mCallback);
    114         ArgumentCaptor<CallerInfoLookupHelper.OnQueryCompleteListener> captor =
    115                 ArgumentCaptor.forClass(CallerInfoLookupHelper.OnQueryCompleteListener.class);
    116         verify(mCallerInfoLookupHelper).startLookup(eq(handle), captor.capture());
    117         return captor.getValue();
    118     }
    119 }
    120