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

Commit 29e85fc1 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add tests for class BluetoothInCallService" into tm-qpr-dev

parents 6ca787e3 a3bf7dfe
Loading
Loading
Loading
Loading
+21 −7
Original line number Diff line number Diff line
@@ -103,11 +103,17 @@ public class BluetoothInCallService extends InCallService {
    private BluetoothCall mOldHeldCall = null;
    private boolean mHeadsetUpdatedRecently = false;
    private boolean mIsDisconnectedTonePlaying = false;
    private boolean mIsTerminatedByClient = false;

    @VisibleForTesting
    boolean mIsTerminatedByClient = false;

    private static final Object LOCK = new Object();
    private BluetoothHeadsetProxy mBluetoothHeadset;
    private BluetoothLeCallControlProxy mBluetoothLeCallControl;

    @VisibleForTesting
    BluetoothHeadsetProxy mBluetoothHeadset;

    @VisibleForTesting
    BluetoothLeCallControlProxy mBluetoothLeCallControl;
    private ExecutorService mExecutor;

    @VisibleForTesting
@@ -640,8 +646,8 @@ public class BluetoothInCallService extends InCallService {
        super.onCreate();
        BluetoothAdapter.getDefaultAdapter()
                .getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
        BluetoothAdapter.getDefaultAdapter().
                getProfileProxy(this, mProfileListener, BluetoothProfile.LE_CALL_CONTROL);
        BluetoothAdapter.getDefaultAdapter()
                .getProfileProxy(this, mProfileListener, BluetoothProfile.LE_CALL_CONTROL);
        mBluetoothAdapterReceiver = new BluetoothAdapterReceiver();
        IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothAdapterReceiver, intentFilter);
@@ -656,7 +662,14 @@ public class BluetoothInCallService extends InCallService {
        super.onDestroy();
    }

    private void clear() {
    @Override
    @VisibleForTesting
    public void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @VisibleForTesting
    void clear() {
        Log.d(TAG, "clear");
        if (mBluetoothAdapterReceiver != null) {
            unregisterReceiver(mBluetoothAdapterReceiver);
@@ -1339,7 +1352,8 @@ public class BluetoothInCallService extends InCallService {
        return null;
    }

    private int getTbsTerminationReason(BluetoothCall call) {
    @VisibleForTesting
    int getTbsTerminationReason(BluetoothCall call) {
        DisconnectCause cause = call.getDisconnectCause();
        if (cause == null) {
            Log.w(TAG, " termination cause is null");
+164 −8
Original line number Diff line number Diff line
@@ -20,16 +20,21 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothLeCallControl;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.telecom.BluetoothCallQualityReport;
import android.telecom.Call;
import android.telecom.Connection;
import android.telecom.DisconnectCause;
import android.telecom.GatewayInfo;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
@@ -92,13 +97,20 @@ public class BluetoothInCallServiceTest {
    private static final int CHLD_TYPE_ADDHELDTOCONF = 3;

    private TestableBluetoothInCallService mBluetoothInCallService;
    @Rule public final ServiceTestRule mServiceRule
    @Rule
    public final ServiceTestRule mServiceRule
            = ServiceTestRule.withTimeout(1, TimeUnit.SECONDS);

    @Mock private BluetoothHeadsetProxy mMockBluetoothHeadset;
    @Mock private BluetoothLeCallControlProxy mMockBluetoothLeCallControl;
    @Mock private BluetoothInCallService.CallInfo mMockCallInfo;
    @Mock private TelephonyManager mMockTelephonyManager;
    @Mock
    private BluetoothHeadsetProxy mMockBluetoothHeadset;
    @Mock
    private BluetoothLeCallControlProxy mMockBluetoothLeCallControl;
    @Mock
    private BluetoothInCallService.CallInfo mMockCallInfo;
    @Mock
    private TelephonyManager mMockTelephonyManager;
    @Mock
    private Context mContext = ApplicationProvider.getApplicationContext();

    public class TestableBluetoothInCallService extends BluetoothInCallService {
        @Override
@@ -110,8 +122,10 @@ public class BluetoothInCallServiceTest {
            mTelecomManager = getSystemService(TelecomManager.class);
            return binder;
        }

        @Override
        protected void enforceModifyPermission() {}
        protected void enforceModifyPermission() {
        }

        protected void setOnCreateCalled(boolean called) {
            mOnCreateCalled = called;
@@ -1196,6 +1210,148 @@ public class BluetoothInCallServiceTest {
                eq("5550000"), eq(PhoneNumberUtils.TOA_Unknown), nullable(String.class));
    }

    @Test
    public void testClear() {
        doNothing().when(mContext).unregisterReceiver(any(
                BluetoothInCallService.BluetoothAdapterReceiver.class));
        mBluetoothInCallService.attachBaseContext(mContext);
        mBluetoothInCallService.mBluetoothAdapterReceiver
                = mBluetoothInCallService.new BluetoothAdapterReceiver();
        Assert.assertNotNull(mBluetoothInCallService.mBluetoothAdapterReceiver);
        Assert.assertNotNull(mBluetoothInCallService.mBluetoothHeadset);

        mBluetoothInCallService.clear();

        Assert.assertNull(mBluetoothInCallService.mBluetoothAdapterReceiver);
        Assert.assertNull(mBluetoothInCallService.mBluetoothHeadset);
    }

    @Test
    public void testGetBearerTechnology() {
        mBluetoothInCallService.mTelephonyManager = mMockTelephonyManager;

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_GSM);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_GSM);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_GPRS);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_2G);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_EVDO_B);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_3G);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_TD_SCDMA);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_WCDMA);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_LTE);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_LTE);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_1xRTT);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_CDMA);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_HSPAP);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_4G);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_IWLAN);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_WIFI);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_NR);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_5G);

        when(mMockTelephonyManager.getDataNetworkType()).thenReturn(
                TelephonyManager.NETWORK_TYPE_LTE_CA);
        Assert.assertEquals(mBluetoothInCallService.getBearerTechnology(),
                BluetoothLeCallControlProxy.BEARER_TECHNOLOGY_GSM);
    }

    @Test
    public void testGetTbsTerminationReason() {
        BluetoothCall call = getMockCall();

        when(call.getDisconnectCause()).thenReturn(null);
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_FAIL);

        DisconnectCause cause = new DisconnectCause(DisconnectCause.BUSY, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_LINE_BUSY);

        cause = new DisconnectCause(DisconnectCause.REJECTED, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_REMOTE_HANGUP);

        cause = new DisconnectCause(DisconnectCause.LOCAL, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        mBluetoothInCallService.mIsTerminatedByClient = false;
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_SERVER_HANGUP);

        cause = new DisconnectCause(DisconnectCause.LOCAL, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        mBluetoothInCallService.mIsTerminatedByClient = true;
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_CLIENT_HANGUP);

        cause = new DisconnectCause(DisconnectCause.ERROR, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_NETWORK_CONGESTION);

        cause = new DisconnectCause(
                DisconnectCause.CONNECTION_MANAGER_NOT_SUPPORTED, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_INVALID_URI);

        cause = new DisconnectCause(DisconnectCause.ERROR, null, null, null, 1);
        when(call.getDisconnectCause()).thenReturn(cause);
        Assert.assertEquals(mBluetoothInCallService.getTbsTerminationReason(call),
                BluetoothLeCallControl.TERMINATION_REASON_NETWORK_CONGESTION);
    }

    @Test
    public void testOnCreate() {
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.targetSdkVersion = Build.VERSION_CODES.S;
        when(mContext.getApplicationInfo()).thenReturn(applicationInfo);
        mBluetoothInCallService.attachBaseContext(mContext);
        mBluetoothInCallService.setOnCreateCalled(false);
        Assert.assertNull(mBluetoothInCallService.mBluetoothAdapterReceiver);

        mBluetoothInCallService.onCreate();

        Assert.assertNotNull(mBluetoothInCallService.mBluetoothAdapterReceiver);
        Assert.assertTrue(mBluetoothInCallService.mOnCreateCalled);
    }

    @Test
    public void testOnDestroy() {
        Assert.assertTrue(mBluetoothInCallService.mOnCreateCalled);

        mBluetoothInCallService.onDestroy();

        Assert.assertFalse(mBluetoothInCallService.mOnCreateCalled);
    }

    private void addCallCapability(BluetoothCall call, int capability) {
        when(call.can(capability)).thenReturn(true);
    }