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.services.telephony;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.os.Bundle;
22 import android.os.PersistableBundle;
23 import android.telecom.PhoneAccountHandle;
24 
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.when;
31 
32 import com.android.internal.telephony.Call;
33 import com.android.internal.telephony.CallStateException;
34 import com.android.internal.telephony.Connection;
35 import com.android.internal.telephony.Phone;
36 import com.android.internal.telephony.PhoneConstants;
37 import com.android.internal.telephony.emergency.EmergencyNumberTracker;
38 
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 /**
46  * Mock Telephony Connection used in TelephonyConferenceController.java for testing purpose
47  */
48 
49 public class TestTelephonyConnection extends TelephonyConnection {
50 
51     @Mock
52     com.android.internal.telephony.Connection mMockRadioConnection;
53 
54     @Mock
55     Call mMockCall;
56 
57     @Mock
58     Context mMockContext;
59 
60     @Mock
61     Resources mMockResources;
62 
63     @Mock
64     EmergencyNumberTracker mEmergencyNumberTracker;
65 
66     private Phone mMockPhone;
67     private int mNotifyPhoneAccountChangedCount = 0;
68     private List<String> mLastConnectionEvents = new ArrayList<>();
69     private List<Bundle> mLastConnectionEventExtras = new ArrayList<>();
70 
71     @Override
getOriginalConnection()72     public com.android.internal.telephony.Connection getOriginalConnection() {
73         return mMockRadioConnection;
74     }
75 
76     @Override
getCall()77     protected Call getCall() {
78         return mMockCall;
79     }
80 
TestTelephonyConnection()81     public TestTelephonyConnection() {
82         super(null, null, android.telecom.Call.Details.DIRECTION_INCOMING);
83         MockitoAnnotations.initMocks(this);
84 
85         mMockPhone = mock(Phone.class);
86         mMockContext = mock(Context.class);
87         mOriginalConnection = mMockRadioConnection;
88         // Set up mMockRadioConnection and mMockPhone to contain an active call
89         when(mMockRadioConnection.getState()).thenReturn(Call.State.ACTIVE);
90         when(mOriginalConnection.getState()).thenReturn(Call.State.ACTIVE);
91         when(mMockRadioConnection.getAudioCodec()).thenReturn(
92                 android.telecom.Connection.AUDIO_CODEC_AMR);
93         when(mMockRadioConnection.getCall()).thenReturn(mMockCall);
94         when(mMockRadioConnection.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_IMS);
95         doNothing().when(mMockRadioConnection).addListener(any(Connection.Listener.class));
96         doNothing().when(mMockRadioConnection).addPostDialListener(
97                 any(Connection.PostDialListener.class));
98         when(mEmergencyNumberTracker.getEmergencyNumber(anyString())).thenReturn(null);
99         when(mMockPhone.getEmergencyNumberTracker()).thenReturn(mEmergencyNumberTracker);
100         when(mMockPhone.getRingingCall()).thenReturn(mMockCall);
101         when(mMockPhone.getContext()).thenReturn(mMockContext);
102         when(mMockPhone.getCurrentSubscriberUris()).thenReturn(null);
103         when(mMockContext.getResources()).thenReturn(mMockResources);
104         when(mMockResources.getBoolean(anyInt())).thenReturn(false);
105         when(mMockPhone.getDefaultPhone()).thenReturn(mMockPhone);
106         when(mMockPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_IMS);
107         when(mMockCall.getState()).thenReturn(Call.State.ACTIVE);
108         when(mMockCall.getPhone()).thenReturn(mMockPhone);
109         try {
110             doNothing().when(mMockCall).hangup();
111         } catch (CallStateException e) {
112             e.printStackTrace();
113         }
114     }
115 
116     @Override
isConferenceSupported()117     public boolean isConferenceSupported() {
118         return true;
119     }
120 
setMockPhone(Phone newPhone)121     public void setMockPhone(Phone newPhone) {
122         mMockPhone = newPhone;
123     }
124 
125     @Override
getPhone()126     public Phone getPhone() {
127         return mMockPhone;
128     }
129 
cloneConnection()130     public TelephonyConnection cloneConnection() {
131         return this;
132     }
133 
134     @Override
notifyPhoneAccountChanged(PhoneAccountHandle pHandle)135     public void notifyPhoneAccountChanged(PhoneAccountHandle pHandle) {
136         mNotifyPhoneAccountChangedCount++;
137     }
138 
139     @Override
sendConnectionEvent(String event, Bundle extras)140     public void sendConnectionEvent(String event, Bundle extras) {
141         mLastConnectionEvents.add(event);
142         mLastConnectionEventExtras.add(extras);
143     }
144 
145     @Override
clearOriginalConnection()146     void clearOriginalConnection() {
147         // Do nothing since the original connection is mock object
148     }
149 
150     @Override
getCarrierConfig()151     public PersistableBundle getCarrierConfig() {
152         // Depends on PhoneGlobals for context in TelephonyConnection, do not implement during
153         // testing.
154         return new PersistableBundle();
155     }
156 
157     @Override
getResourceText(int messageId)158     public CharSequence getResourceText(int messageId) {
159         return "TEST";
160     }
161 
162     @Override
getResourceString(int id)163     public String getResourceString(int id) {
164         return "TEST";
165     }
166 
167     @Override
refreshConferenceSupported()168     void refreshConferenceSupported() {
169         // Requires ImsManager dependencies, do not implement during testing.
170     }
171 
getNotifyPhoneAccountChangedCount()172     public int getNotifyPhoneAccountChangedCount() {
173         return mNotifyPhoneAccountChangedCount;
174     }
175 
getLastConnectionEvents()176     public List<String> getLastConnectionEvents() {
177         return mLastConnectionEvents;
178     }
179 
getLastConnectionEventExtras()180     public List<Bundle> getLastConnectionEventExtras() {
181         return mLastConnectionEventExtras;
182     }
183 }
184