Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit acf4aa18 authored by fionaxu's avatar fionaxu Committed by android-build-merger
Browse files

Merge "More Unit Tests on CallManager" into nyc-dev

am: bdad20f0

* commit 'bdad20f0':
  More Unit Tests on CallManager
parents 6e6f574d bdad20f0
Loading
Loading
Loading
Loading
+219 −128
Original line number Diff line number Diff line
@@ -17,12 +17,12 @@ package com.android.internal.telephony;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.telephony.PhoneNumberUtils;
import android.telephony.ServiceState;
import android.test.suitebuilder.annotation.SmallTest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@@ -34,6 +34,7 @@ import java.lang.reflect.Field;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
@@ -41,6 +42,10 @@ import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyChar;
import static org.mockito.Mockito.anyString;

public class CallManagerTest extends TelephonyTest {

@@ -50,6 +55,12 @@ public class CallManagerTest extends TelephonyTest {
    GsmCdmaCall mBgCall;
    @Mock
    GsmCdmaCall mRingingCall;
    @Mock
    Phone mSecondPhone;

    private CallManagerHandlerThread mCallManagerHandlerThread;
    private Handler mHandler;
    private static final int PHONE_REGISTER_EVENT = 0;

    private class CallManagerHandlerThread extends HandlerThread {
        private CallManagerHandlerThread(String name) {
@@ -58,8 +69,29 @@ public class CallManagerTest extends TelephonyTest {
        @Override
        public void onLooperPrepared() {
            /* CallManager is a static object with private constructor,no need call constructor */
            CallManager.getInstance().registerPhone(mPhone);
            registerForPhone(mPhone);

            // create a custom handler for the Handler Thread
            mHandler = new Handler(mCallManagerHandlerThread.getLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case PHONE_REGISTER_EVENT:
                            logd("Phone registered with CallManager");
                            registerForPhone((Phone) msg.obj);
                            setReady(true);
                            break;
                        default:
                            logd("Unknown Event " + msg.what);
                    }
                }
            };

            setReady(true);
        }

        private void registerForPhone(Phone mPhone) {
            CallManager.getInstance().registerPhone(mPhone);
        }
    }

@@ -69,7 +101,6 @@ public class CallManagerTest extends TelephonyTest {
        restoreInstance(CallManager.class, "INSTANCE", null);
        /* Mock Phone and Call, initially all calls are idle */
        doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
        doReturn(mServiceState).when(mPhone).getServiceState();
        doReturn(mBgCall).when(mPhone).getBackgroundCall();
        doReturn(mFgCall).when(mPhone).getForegroundCall();
        doReturn(mRingingCall).when(mPhone).getRingingCall();
@@ -83,7 +114,8 @@ public class CallManagerTest extends TelephonyTest {
        doReturn(true).when(mFgCall).isIdle();
        doReturn(true).when(mRingingCall).isIdle();

        new CallManagerHandlerThread(TAG).start();
        mCallManagerHandlerThread = new CallManagerHandlerThread(TAG);
        mCallManagerHandlerThread.start();
        waitUntilReady();
    }

@@ -106,8 +138,7 @@ public class CallManagerTest extends TelephonyTest {
    }

    @SmallTest @Test
    public void testBasicDial() {
        try {
    public void testBasicDial() throws Exception {
        //verify can dial and dial function of the phone is being triggered
        CallManager.getInstance().dial(mPhone,
                PhoneNumberUtils.stripSeparators("+17005554141"), 0);
@@ -117,41 +148,27 @@ public class CallManagerTest extends TelephonyTest {
        assertEquals(PhoneNumberUtils.stripSeparators("+17005554141"),
                mCaptorString.getValue());
        assertEquals(0, mCaptorInt.getValue().intValue());

        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
        }

    }

    @SmallTest @Test
    public void testBasicAcceptCall() {
        try {
    public void testBasicAcceptCall() throws Exception {
        CallManager.getInstance().acceptCall(mRingingCall);
        verify(mPhone, times(1)).acceptCall(anyInt());
        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
        }
    }

    @SmallTest @Test
    public void testBasicRejectCall() {
        try {
    public void testBasicRejectCall() throws Exception {
        //verify can dial and dial function of the phone is being triggered
        CallManager.getInstance().rejectCall(mRingingCall);
        verify(mPhone, times(1)).rejectCall();
        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
        }
    }

    @SmallTest @Test
    public void testSendDtmf() {
    public void testSendDtmf() throws Exception {
        CallManager.getInstance().sendDtmf('a');
        verify(mPhone, times(0)).sendDtmf(eq('a'));

        //has active fg Call
        doReturn(false).when(mFgCall).isIdle();
        assertEquals(mFgCall, CallManager.getInstance().getActiveFgCall());
        CallManager.getInstance().sendDtmf('a');
@@ -159,8 +176,59 @@ public class CallManagerTest extends TelephonyTest {
    }

    @SmallTest @Test
    public void testSwitchHoldingAndActive() {
        try {
    public void testStartDtmf() throws Exception {
        doReturn(true).when(mFgCall).isIdle();
        assertFalse(CallManager.getInstance().startDtmf('a'));
        verify(mPhone, times(0)).startDtmf(anyChar());

        //has active fg Call
        doReturn(false).when(mFgCall).isIdle();
        assertEquals(mFgCall, CallManager.getInstance().getActiveFgCall());
        assertTrue(CallManager.getInstance().startDtmf('a'));
        verify(mPhone, times(1)).startDtmf('a');
    }

    @SmallTest @Test
    public void testStopDtmf() throws Exception {
        doReturn(true).when(mFgCall).isIdle();
        CallManager.getInstance().stopDtmf();
        verify(mPhone, times(0)).stopDtmf();

        //has active fg Call
        doReturn(false).when(mFgCall).isIdle();
        assertEquals(mPhone, CallManager.getInstance().getFgPhone());
        CallManager.getInstance().stopDtmf();
        verify(mPhone, times(1)).stopDtmf();
    }

    @SmallTest @Test
    public void testSendBurstDtmf() throws Exception {
        doReturn(true).when(mFgCall).isIdle();
        assertFalse(CallManager.getInstance().sendBurstDtmf("12*#", 0, 0, null));
        verify(mPhone, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(), (Message) any());

        //has active fg Call
        doReturn(false).when(mFgCall).isIdle();
        assertTrue(CallManager.getInstance().sendBurstDtmf("12*#", 0, 0, null));
        verify(mPhone, times(1)).sendBurstDtmf("12*#", 0, 0, null);
    }

    @SmallTest @Test
    public void testSetGetMute() throws Exception {
        CallManager.getInstance().setMute(false);
        verify(mPhone, times(0)).setMute(anyBoolean());

        //has active fg Call
        doReturn(false).when(mFgCall).isIdle();
        CallManager.getInstance().setMute(false);
        verify(mPhone, times(1)).setMute(false);

        CallManager.getInstance().setMute(true);
        verify(mPhone, times(1)).setMute(true);
    }

    @SmallTest @Test
    public void testSwitchHoldingAndActive() throws Exception {
        /* case 1: only active call */
        doReturn(false).when(mFgCall).isIdle();
        CallManager.getInstance().switchHoldingAndActive(null);
@@ -179,15 +247,10 @@ public class CallManagerTest extends TelephonyTest {
        CallManager.getInstance().switchHoldingAndActive(mBgCall);
        verify(mPhone, times(4)).switchHoldingAndActive();
        verify(mPhoneHold, times(1)).switchHoldingAndActive();
        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
        }
    }

    @SmallTest @Test
    public void testHangupForegroundResumeBackground() {
        try {
    public void testHangupForegroundResumeBackground() throws Exception {
        CallManager.getInstance().hangupForegroundResumeBackground(mBgCall);
        /* no active fgCall */
        verify(mPhone, times(0)).switchHoldingAndActive();
@@ -207,15 +270,10 @@ public class CallManagerTest extends TelephonyTest {
        /* always hangup fgcall and both phone trigger swap */
        verify(mPhoneHold, times(1)).switchHoldingAndActive();
        verify(mPhone, times(1)).switchHoldingAndActive();

        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
        }
    }

    @SmallTest @Test
    public void testFgCallActiveDial() {
    public void testFgCallActiveDial() throws Exception {
        /* set Fg/Bg Call state to active, verify CallManager Logical */
        doReturn(false).when(mFgCall).isIdle();
        doReturn(false).when(mBgCall).isIdle();
@@ -224,7 +282,6 @@ public class CallManagerTest extends TelephonyTest {
        assertTrue(CallManager.getInstance().hasActiveFgCall(mPhone.getSubId()));
        assertFalse(CallManager.getInstance().hasDisconnectedFgCall());
        /* try dial with non-idle foreground call and background call */
        try {
        CallManager.getInstance().dial(mPhone,
                PhoneNumberUtils.stripSeparators("+17005554141"), 0);
        ArgumentCaptor<String> mCaptorString = ArgumentCaptor.forClass(String.class);
@@ -234,16 +291,10 @@ public class CallManagerTest extends TelephonyTest {
        assertEquals(PhoneNumberUtils.stripSeparators("+17005554141"),
                mCaptorString.getValue());
        assertEquals(0, mCaptorInt.getValue().intValue());

        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
        }
    }

    @Test @SmallTest
    public void testRegisterEvent() {
        try {
    public void testRegisterEvent() throws Exception {
        Field field = CallManager.class.getDeclaredField("EVENT_CALL_WAITING");
        field.setAccessible(true);
        int mEvent = (Integer) field.get(CallManager.getInstance());
@@ -261,9 +312,49 @@ public class CallManagerTest extends TelephonyTest {
        mEvent = (Integer) field.get(CallManager.getInstance());
        verify(mPhone, times(1)).registerForRingbackTone(isA(Handler.class),
                eq(mEvent), isA(Object.class));
        } catch (Exception ex) {
            ex.printStackTrace();
            Assert.fail(ex.toString());
    }

    @Test @SmallTest
    public void testGetServiceState() throws Exception {
        // register for another phone
        ServiceState mSecondServiceState = mock(ServiceState.class);
        doReturn(mSecondServiceState).when(mSecondPhone).getServiceState();

        Message mRegisterPhone = mHandler.obtainMessage(PHONE_REGISTER_EVENT,
                mSecondPhone);
        setReady(false);
        mRegisterPhone.sendToTarget();

        waitUntilReady();

        // mPhone: STATE_IN_SERVICE > mPhoneSecond: state STATE_OUT_OF_SERVICE
        doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mSecondServiceState).getState();
        assertEquals(ServiceState.STATE_IN_SERVICE, CallManager.getInstance().getServiceState());

        // mPhone: STATE_IN_SERVICE > mPhoneSecond: state STATE_EMERGENCY_ONLY
        doReturn(ServiceState.STATE_EMERGENCY_ONLY).when(mSecondServiceState).getState();
        assertEquals(ServiceState.STATE_IN_SERVICE, CallManager.getInstance().getServiceState());

        // mPhone: STATE_IN_SERVICE > mPhoneSecond: state STATE_POWER_OFF
        doReturn(ServiceState.STATE_POWER_OFF).when(mSecondServiceState).getState();
        assertEquals(ServiceState.STATE_IN_SERVICE, CallManager.getInstance().getServiceState());

        // mPhone: STATE_EMERGENCY_ONLY < mPhoneSecond: state STATE_OUT_OF_SERVICE
        doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mSecondServiceState).getState();
        doReturn(ServiceState.STATE_EMERGENCY_ONLY).when(mServiceState).getState();
        assertEquals(ServiceState.STATE_OUT_OF_SERVICE,
                CallManager.getInstance().getServiceState());

        // mPhone: STATE_POWER_OFF < mPhoneSecond: state STATE_OUT_OF_SERVICE
        doReturn(ServiceState.STATE_POWER_OFF).when(mServiceState).getState();
        assertEquals(ServiceState.STATE_OUT_OF_SERVICE,
                CallManager.getInstance().getServiceState());

        /* mPhone: STATE_POWER_OFF < mPhoneSecond: state STATE_EMERGENCY_ONLY
           but OUT_OF_SERVICE will replaces EMERGENCY_ONLY and POWER_OFF */
        doReturn(ServiceState.STATE_EMERGENCY_ONLY).when(mSecondServiceState).getState();
        assertEquals(ServiceState.STATE_OUT_OF_SERVICE,
                CallManager.getInstance().getServiceState());
        CallManager.getInstance().unregisterPhone(mSecondPhone);
    }
}