GsmCdmaCallTrackerTest.java revision 655bae990bdd7e4584491def4d30692e6173779c
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 */
16package com.android.internal.telephony;
17
18import android.os.HandlerThread;
19import android.os.SystemProperties;
20import android.telephony.DisconnectCause;
21import android.telephony.PhoneNumberUtils;
22import android.telephony.ServiceState;
23import android.test.suitebuilder.annotation.SmallTest;
24
25import com.android.internal.telephony.test.SimulatedCommandsVerifier;
26import android.os.Message;
27import org.junit.After;
28import org.junit.Assert;
29import org.junit.Before;
30import org.junit.Test;
31import org.mockito.Mock;
32import org.mockito.ArgumentCaptor;
33import android.os.Handler;
34
35import static org.junit.Assert.assertFalse;
36import static org.junit.Assert.assertNotNull;
37import static org.junit.Assert.assertTrue;
38import static org.mockito.Mockito.doReturn;
39import static org.junit.Assert.assertEquals;
40import static org.mockito.Mockito.*;
41import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
42
43
44public class GsmCdmaCallTrackerTest extends TelephonyTest {
45    private static final int VOICE_CALL_STARTED_EVENT = 0;
46    private static final int VOICE_CALL_ENDED_EVENT = 1;
47    private String mDialString = PhoneNumberUtils.stripSeparators("+17005554141");
48    /* Handler class initiated at the HandlerThread */
49    private GsmCdmaCallTracker mCTUT;
50    @Mock
51    GsmCdmaCall mCall;
52    @Mock
53    private Handler mHandler;
54
55    private class GsmCdmaCTHandlerThread extends HandlerThread {
56
57        private GsmCdmaCTHandlerThread(String name) {
58            super(name);
59        }
60        @Override
61        public void onLooperPrepared() {
62            mCTUT = new GsmCdmaCallTracker(mPhone);
63            setReady(true);
64        }
65    }
66
67    @Before
68    public void setUp() throws Exception {
69        super.setUp(this.getClass().getSimpleName());
70        mSimulatedCommands.setRadioPower(true, null);
71        mPhone.mCi = this.mSimulatedCommands;
72        mContextFixture.putStringArrayResource(com.android.internal.R.array.dial_string_replace,
73                new String[]{});
74
75        new GsmCdmaCTHandlerThread(TAG).start();
76
77        waitUntilReady();
78        logd("GsmCdmaCallTracker initiated, waiting for Power on");
79        /* Make sure radio state is power on before dial.
80         * When radio state changed from off to on, CallTracker
81         * will poll result from RIL. Avoid dialing triggered at the same*/
82        waitForMs(100);
83    }
84
85    @After
86    public void tearDown() throws Exception {
87        mCTUT = null;
88        super.tearDown();
89    }
90
91    @Test
92    @SmallTest
93    public void testMOCallDial() {
94        doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
95        assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
96        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
97        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mBackgroundCall.getState());
98        assertEquals(0, mCTUT.mForegroundCall.getConnections().size());
99        /* debug */
100        assertTrue(mPhone.getServiceState().getState() != ServiceState.STATE_POWER_OFF);
101        assertFalse(mCTUT.mRingingCall.isRinging());
102        assertFalse(mCTUT.mForegroundCall.getState().isAlive());
103        assertFalse(mCTUT.mBackgroundCall.getState().isAlive());
104        assertFalse(SystemProperties.get(
105                TelephonyProperties.PROPERTY_DISABLE_CALL, "false").equals("true"));
106
107        try {
108            mCTUT.dial(mDialString);
109        } catch(Exception ex) {
110            ex.printStackTrace();
111            Assert.fail("unexpected exception thrown"+ex.getMessage()+ex.getStackTrace());
112        }
113
114        assertEquals(PhoneConstants.State.OFFHOOK, mCTUT.getState());
115        assertEquals(GsmCdmaCall.State.DIALING, mCTUT.mForegroundCall.getState());
116        assertEquals(1, mCTUT.mForegroundCall.getConnections().size());
117        /* verify the command is sent out to RIL */
118        verify(mSimulatedCommandsVerifier).dial(eq(PhoneNumberUtils.
119                        extractNetworkPortionAlt(mDialString)), anyInt(),
120                eq((UUSInfo) null),
121                isA(Message.class));
122    }
123
124    @Test
125    @SmallTest
126    public void testMOCallPickUp() {
127        testMOCallDial();
128        logd("Waiting for POLL CALL response from RIL");
129        TelephonyTestUtils.waitForMs(50);
130        logd("Pick Up MO call, expecting call state change event ");
131        mSimulatedCommands.progressConnectingToActive();
132        waitForMs(100);
133        assertEquals(GsmCdmaCall.State.ACTIVE, mCTUT.mForegroundCall.getState());
134        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mBackgroundCall.getState());
135    }
136
137    @Test
138    @SmallTest
139    public void testMOCallHangup() {
140        testMOCallDial();
141        logd("Waiting for POLL CALL response from RIL ");
142        TelephonyTestUtils.waitForMs(50);
143        assertEquals(GsmCdmaCall.State.DIALING, mCTUT.mForegroundCall.getState());
144        assertEquals(PhoneConstants.State.OFFHOOK, mCTUT.getState());
145        assertEquals(1, mCTUT.mForegroundCall.getConnections().size());
146        logd("Hang up MO call after MO call established ");
147        try {
148            mCTUT.hangup(mCTUT.mForegroundCall);
149        } catch(Exception ex) {
150            ex.printStackTrace();
151            Assert.fail("unexpected exception thrown" + ex.getMessage());
152        }
153        //assertEquals(GsmCdmaCall.State.DISCONNECTING, mCTUT.mForegroundCall.getState());
154        /* request send to RIL still in disconnecting state */
155        waitForMs(50);
156        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
157        assertEquals(0, mCTUT.mForegroundCall.getConnections().size());
158        assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
159    }
160
161    @Test
162    @SmallTest
163    public void testMOCallDialPickUpHangup() {
164        testMOCallPickUp();
165        assertEquals(GsmCdmaCall.State.ACTIVE, mCTUT.mForegroundCall.getState());
166        assertEquals(PhoneConstants.State.OFFHOOK, mCTUT.getState());
167        assertEquals(1, mCTUT.mForegroundCall.getConnections().size());
168         /* get the reference of the connection before reject */
169        Connection mConnection = mCTUT.mForegroundCall.getConnections().get(0);
170        assertEquals(DisconnectCause.NOT_DISCONNECTED, mConnection.getDisconnectCause());
171        logd("hang up MO call after pickup");
172        try {
173            mCTUT.hangup(mCTUT.mForegroundCall);
174        } catch(Exception ex) {
175            ex.printStackTrace();
176            Assert.fail("unexpected exception thrown" + ex.getMessage());
177        }
178        assertEquals(GsmCdmaCall.State.DISCONNECTING, mCTUT.mForegroundCall.getState());
179        /* request send to RIL still in disconnecting state */
180        waitForMs(50);
181        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
182        assertEquals(0, mCTUT.mForegroundCall.getConnections().size());
183        assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
184        assertEquals(DisconnectCause.LOCAL, mConnection.getDisconnectCause());
185
186    }
187
188    @Test
189    @SmallTest
190    public void testMOCallPendingHangUp() {
191        testMOCallDial();
192        logd("MO call hangup before established[ getting result from RIL ]");
193        /* poll call result from RIL, find that there is a pendingMO call,
194         * Didn't do anything for hangup, clear during handle poll result */
195        try {
196            mCTUT.hangup(mCTUT.mForegroundCall);
197        } catch(Exception ex) {
198            ex.printStackTrace();
199            Assert.fail("unexpected exception thrown" + ex.getMessage());
200        }
201        waitForMs(50);
202        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
203        assertEquals(0, mCTUT.mForegroundCall.getConnections().size());
204        assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
205    }
206
207    @Test
208    @SmallTest
209    public void testMOCallSwitch() {
210        testMOCallPickUp();
211        logd("MO call picked up, initiating a new MO call");
212        assertEquals(GsmCdmaCall.State.ACTIVE, mCTUT.mForegroundCall.getState());
213        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mBackgroundCall.getState());
214        assertEquals(1, mCTUT.mForegroundCall.getConnections().size());
215        assertEquals(0, mCTUT.mBackgroundCall.getConnections().size());
216
217        String mDialString = PhoneNumberUtils.stripSeparators("+17005554142");
218        try {
219            mCTUT.dial(mDialString);
220        } catch(Exception ex) {
221            ex.printStackTrace();
222            Assert.fail("unexpected exception thrown" + ex.getMessage());
223        }
224        waitForMs(100);
225        assertEquals(GsmCdmaCall.State.DIALING, mCTUT.mForegroundCall.getState());
226        assertEquals(GsmCdmaCall.State.HOLDING, mCTUT.mBackgroundCall.getState());
227        assertEquals(1, mCTUT.mForegroundCall.getConnections().size());
228        assertEquals(1, mCTUT.mBackgroundCall.getConnections().size());
229
230    }
231
232    @Test
233    @SmallTest
234    public void testMTCallRinging() {
235        /* Mock there is a MT call mRinging call and try to accept this MT call */
236        /* if we got a active state followed by another MT call-> move to background call */
237        assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
238        assertEquals(0, mCTUT.mRingingCall.getConnections().size());
239        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
240        String mDialString = PhoneNumberUtils.stripSeparators("+17005554141");
241        logd("MT call Ringing");
242        mSimulatedCommands.triggerRing(mDialString);
243        waitForMs(50);
244        assertEquals(PhoneConstants.State.RINGING, mCTUT.getState());
245        assertEquals(1, mCTUT.mRingingCall.getConnections().size());
246    }
247
248    @Test
249    @SmallTest
250    public void testMTCallAccept() {
251        testMTCallRinging();
252        assertEquals(mCTUT.mForegroundCall.getConnections().size(),0);
253        logd("accept the MT call");
254        try{
255            mCTUT.acceptCall();
256        } catch(Exception ex) {
257            ex.printStackTrace();
258            Assert.fail("unexpected exception thrown" + ex.getMessage());
259        }
260        verify(mSimulatedCommandsVerifier).acceptCall(isA(Message.class));
261        /* send to the RIL */
262        TelephonyTestUtils.waitForMs(50);
263        assertEquals(PhoneConstants.State.OFFHOOK, mCTUT.getState());
264        assertEquals(GsmCdmaCall.State.ACTIVE, mCTUT.mForegroundCall.getState());
265        assertEquals(1, mCTUT.mForegroundCall.getConnections().size());
266        assertEquals(0, mCTUT.mRingingCall.getConnections().size());
267    }
268
269    @Test
270    @SmallTest
271    public void testMTCallReject() {
272        testMTCallRinging();
273        logd("MT call ringing and rejected ");
274        /* get the reference of the connection before reject */
275        Connection mConnection = mCTUT.mRingingCall.getConnections().get(0);
276        assertNotNull(mConnection);
277        assertEquals(DisconnectCause.NOT_DISCONNECTED, mConnection.getDisconnectCause());
278        try {
279            mCTUT.rejectCall();
280        } catch(Exception ex) {
281            ex.printStackTrace();
282            Assert.fail("unexpected exception thrown" + ex.getMessage());
283        }
284        waitForMs(50);
285        assertEquals(PhoneConstants.State.IDLE, mCTUT.getState());
286        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
287        assertEquals(0, mCTUT.mForegroundCall.getConnections().size());
288        /* ? why rejectCall didnt -> hang up locally to set the cause to LOCAL? */
289        assertEquals(DisconnectCause.INCOMING_MISSED, mConnection.getDisconnectCause());
290
291    }
292
293    @Test
294    @SmallTest
295    public void testMOCallSwitchHangupForeGround() {
296        testMOCallSwitch();
297        logd("Hang up the foreground MO call while dialing ");
298        try {
299            mCTUT.hangup(mCTUT.mForegroundCall);
300        } catch(Exception ex) {
301            ex.printStackTrace();
302            Assert.fail("unexpected exception thrown" + ex.getMessage());
303        }
304        waitForMs(100);
305        logd(" Foreground Call is IDLE and BackGround Call is still HOLDING ");
306        /* if we want to hang up foreground call which is alerting state, hangup all */
307        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mForegroundCall.getState());
308        assertEquals(GsmCdmaCall.State.HOLDING, mCTUT.mBackgroundCall.getState());
309    }
310
311    @Test
312    @SmallTest
313    public void testMOCallPickUpHangUpResumeBackGround() {
314        testMOCallSwitch();
315        logd("Pick up the new MO Call");
316        try{
317            mSimulatedCommands.progressConnectingToActive();
318        } catch(Exception ex) {
319            ex.printStackTrace();
320            Assert.fail("unexpected exception thrown" + ex.getMessage());
321        }
322
323        waitForMs(100);
324        assertEquals(GsmCdmaCall.State.ACTIVE, mCTUT.mForegroundCall.getState());
325        assertEquals(GsmCdmaCall.State.HOLDING, mCTUT.mBackgroundCall.getState());
326
327        logd("Hang up the new MO Call");
328        try {
329            mCTUT.hangup(mCTUT.mForegroundCall);
330        } catch(Exception ex) {
331            ex.printStackTrace();
332            Assert.fail("unexpected exception thrown" + ex.getMessage());
333        }
334
335        waitForMs(100);
336        logd(" BackGround Call switch to ForeGround Call ");
337        assertEquals(GsmCdmaCall.State.ACTIVE, mCTUT.mForegroundCall.getState());
338        assertEquals(GsmCdmaCall.State.IDLE, mCTUT.mBackgroundCall.getState());
339    }
340
341    @Test @SmallTest
342    public void testVoiceCallStartListener(){
343        logd("register for voice call started event");
344        mCTUT.registerForVoiceCallStarted(mHandler, VOICE_CALL_STARTED_EVENT, null);
345        logd("voice call started");
346        testMOCallPickUp();
347        ArgumentCaptor<Message> mCaptorMessage = ArgumentCaptor.forClass(Message.class);
348        ArgumentCaptor<Long>    mCaptorLong = ArgumentCaptor.forClass(Long.class);
349        verify(mHandler,times(1)).sendMessageAtTime(mCaptorMessage.capture(), mCaptorLong.capture());
350        assertEquals(VOICE_CALL_STARTED_EVENT, mCaptorMessage.getValue().what);
351
352    }
353
354    @Test @SmallTest
355    public void testVoiceCallEndedListener(){
356        logd("register for voice call ended event");
357        mCTUT.registerForVoiceCallEnded(mHandler, VOICE_CALL_ENDED_EVENT, null);
358        ArgumentCaptor<Message> mCaptorMessage = ArgumentCaptor.forClass(Message.class);
359        ArgumentCaptor<Long> mCaptorLong = ArgumentCaptor.forClass(Long.class);
360        testMOCallHangup();
361        verify(mHandler,times(1)).sendMessageAtTime(mCaptorMessage.capture(), mCaptorLong.capture());
362        assertEquals(VOICE_CALL_ENDED_EVENT, mCaptorMessage.getValue().what);
363    }
364}
365
366