1 /*
2  * Copyright (C) 2018 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.phone;
18 
19 import static junit.framework.TestCase.assertFalse;
20 
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.never;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.telephony.NumberVerificationCallback;
30 import android.telephony.PhoneNumberRange;
31 import android.telephony.ServiceState;
32 
33 import com.android.internal.telephony.Call;
34 import com.android.internal.telephony.INumberVerificationCallback;
35 import com.android.internal.telephony.Phone;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.JUnit4;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 
44 @RunWith(JUnit4.class)
45 public class NumberVerificationManagerTest {
46     private static final PhoneNumberRange SAMPLE_RANGE =
47             new PhoneNumberRange("1", "650555", "0000", "8999");
48     private static final long DEFAULT_VERIFICATION_TIMEOUT = 100;
49     @Mock private Phone mPhone1;
50     @Mock private Phone mPhone2;
51     @Mock private Call mRingingCall;
52     @Mock private Call mForegroundCall;
53     @Mock private Call mBackgroundCall;
54     @Mock private INumberVerificationCallback mCallback;
55 
56     @Before
setUp()57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59         ServiceState ss = mock(ServiceState.class);
60         when(ss.getState()).thenReturn(ServiceState.STATE_IN_SERVICE);
61         when(mPhone1.getServiceState()).thenReturn(ss);
62         when(mPhone1.getForegroundCall()).thenReturn(mForegroundCall);
63         when(mPhone1.getRingingCall()).thenReturn(mRingingCall);
64         when(mPhone1.getBackgroundCall()).thenReturn(mBackgroundCall);
65         when(mPhone2.getServiceState()).thenReturn(ss);
66         when(mPhone2.getForegroundCall()).thenReturn(mForegroundCall);
67         when(mPhone2.getRingingCall()).thenReturn(mRingingCall);
68         when(mPhone2.getBackgroundCall()).thenReturn(mBackgroundCall);
69 
70         when(mForegroundCall.getState()).thenReturn(Call.State.IDLE);
71         when(mRingingCall.getState()).thenReturn(Call.State.IDLE);
72         when(mBackgroundCall.getState()).thenReturn(Call.State.IDLE);
73     }
74 
75     @Test
testConcurrentRequestFailure()76     public void testConcurrentRequestFailure() throws Exception {
77         NumberVerificationManager manager =
78                 new NumberVerificationManager(() -> new Phone[]{mPhone1});
79         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
80         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
81         verify(mCallback, times(1)).onVerificationFailed(
82                 NumberVerificationCallback.REASON_CONCURRENT_REQUESTS);
83     }
84 
85     @Test
testEcbmFailure()86     public void testEcbmFailure() throws Exception {
87         NumberVerificationManager manager =
88                 new NumberVerificationManager(() -> new Phone[]{mPhone1});
89         when(mPhone1.isInEcm()).thenReturn(true);
90 
91         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
92         verify(mCallback, times(1)).onVerificationFailed(
93                 NumberVerificationCallback.REASON_IN_ECBM);
94     }
95 
96     @Test
testEmergencyCallFailure()97     public void testEmergencyCallFailure() throws Exception {
98         NumberVerificationManager manager =
99                 new NumberVerificationManager(() -> new Phone[]{mPhone1});
100         when(mPhone1.isInEmergencyCall()).thenReturn(true);
101 
102         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
103         verify(mCallback, times(1)).onVerificationFailed(
104                 NumberVerificationCallback.REASON_IN_EMERGENCY_CALL);
105     }
106 
107     @Test
testNoPhoneInServiceFailure()108     public void testNoPhoneInServiceFailure() throws Exception {
109         ServiceState ss = mock(ServiceState.class);
110         when(ss.getState()).thenReturn(ServiceState.STATE_POWER_OFF);
111         when(mPhone1.getServiceState()).thenReturn(ss);
112         when(mPhone2.getServiceState()).thenReturn(ss);
113         NumberVerificationManager manager =
114                 new NumberVerificationManager(() -> new Phone[]{mPhone1, mPhone2});
115 
116         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
117         verify(mCallback, times(1)).onVerificationFailed(
118                 NumberVerificationCallback.REASON_NETWORK_NOT_AVAILABLE);
119     }
120 
121     @Test
testAllLinesFullFailure()122     public void testAllLinesFullFailure() throws Exception {
123         NumberVerificationManager manager =
124                 new NumberVerificationManager(() -> new Phone[]{mPhone1, mPhone2});
125         when(mRingingCall.getState()).thenReturn(Call.State.ALERTING);
126 
127         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
128         verify(mCallback, times(1)).onVerificationFailed(
129                 NumberVerificationCallback.REASON_TOO_MANY_CALLS);
130     }
131 
verifyDefaultRangeMatching(NumberVerificationManager manager)132     private void verifyDefaultRangeMatching(NumberVerificationManager manager) throws Exception {
133         String testNumber = "6505550000";
134         assertTrue(manager.checkIncomingCall(testNumber));
135         verify(mCallback).onCallReceived(testNumber);
136     }
137 
138     @Test
testVerificationWorksWithOnePhoneInService()139     public void testVerificationWorksWithOnePhoneInService() throws Exception {
140         ServiceState ss = mock(ServiceState.class);
141         when(ss.getState()).thenReturn(ServiceState.STATE_POWER_OFF);
142         when(mPhone1.getServiceState()).thenReturn(ss);
143         NumberVerificationManager manager =
144                 new NumberVerificationManager(() -> new Phone[]{mPhone1, mPhone2});
145 
146         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
147         verify(mCallback, never()).onVerificationFailed(anyInt());
148         verifyDefaultRangeMatching(manager);
149     }
150 
151     @Test
testVerificationWorksWithOnePhoneFull()152     public void testVerificationWorksWithOnePhoneFull() throws Exception {
153         Call fakeCall = mock(Call.class);
154         when(fakeCall.getState()).thenReturn(Call.State.ACTIVE);
155         when(mPhone1.getForegroundCall()).thenReturn(fakeCall);
156         when(mPhone1.getRingingCall()).thenReturn(fakeCall);
157         when(mPhone1.getBackgroundCall()).thenReturn(fakeCall);
158         NumberVerificationManager manager =
159                 new NumberVerificationManager(() -> new Phone[]{mPhone1, mPhone2});
160 
161         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
162         verify(mCallback, never()).onVerificationFailed(anyInt());
163         verifyDefaultRangeMatching(manager);
164     }
165 
166     @Test
testDoubleVerificationFailure()167     public void testDoubleVerificationFailure() throws Exception {
168         NumberVerificationManager manager =
169                 new NumberVerificationManager(() -> new Phone[]{mPhone1, mPhone2});
170         manager.requestVerification(SAMPLE_RANGE, mCallback, DEFAULT_VERIFICATION_TIMEOUT);
171         verifyDefaultRangeMatching(manager);
172         assertFalse(manager.checkIncomingCall("this doesn't even matter"));
173     }
174 }
175