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

Commit 4f19db55 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add additional tests to functions in HeadsetStateMachine"

parents 7792e17e 134479cc
Loading
Loading
Loading
Loading
+26 −13
Original line number Diff line number Diff line
@@ -136,8 +136,10 @@ public class HeadsetStateMachine extends StateMachine {
    private final HeadsetSystemInterface mSystemInterface;

    // Runtime states
    private int mSpeakerVolume;
    private int mMicVolume;
    @VisibleForTesting
    int mSpeakerVolume;
    @VisibleForTesting
    int mMicVolume;
    private boolean mDeviceSilenced;
    private HeadsetAgIndicatorEnableState mAgIndicatorEnableState;
    // The timestamp when the device entered connecting/connected state
@@ -1646,7 +1648,8 @@ public class HeadsetStateMachine extends StateMachine {
        }
    }

    private void processVolumeEvent(int volumeType, int volume) {
    @VisibleForTesting
    void processVolumeEvent(int volumeType, int volume) {
        // Only current active device can change SCO volume
        if (!mDevice.equals(mHeadsetService.getActiveDevice())) {
            Log.w(TAG, "processVolumeEvent, ignored because " + mDevice + " is not active");
@@ -1705,7 +1708,8 @@ public class HeadsetStateMachine extends StateMachine {
    }

    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    private void processSubscriberNumberRequest(BluetoothDevice device) {
    @VisibleForTesting
    void processSubscriberNumberRequest(BluetoothDevice device) {
        String number = mSystemInterface.getSubscriberNumber();
        if (number != null) {
            mNativeInterface.atResponseString(device,
@@ -1740,7 +1744,8 @@ public class HeadsetStateMachine extends StateMachine {
    }

    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    private void processAtCops(BluetoothDevice device) {
    @VisibleForTesting
    void processAtCops(BluetoothDevice device) {
        // Get operator name suggested by Telephony
        String operatorName = null;
        ServiceState serviceState = mSystemInterface.getHeadsetPhoneState().getServiceState();
@@ -1783,7 +1788,8 @@ public class HeadsetStateMachine extends StateMachine {
        }
    }

    private void processAtCscs(String atString, int type, BluetoothDevice device) {
    @VisibleForTesting
    void processAtCscs(String atString, int type, BluetoothDevice device) {
        log("processAtCscs - atString = " + atString);
        if (mPhonebook != null) {
            mPhonebook.handleCscsCommand(atString, type, device);
@@ -1793,7 +1799,8 @@ public class HeadsetStateMachine extends StateMachine {
        }
    }

    private void processAtCpbs(String atString, int type, BluetoothDevice device) {
    @VisibleForTesting
    void processAtCpbs(String atString, int type, BluetoothDevice device) {
        log("processAtCpbs - atString = " + atString);
        if (mPhonebook != null) {
            mPhonebook.handleCpbsCommand(atString, type, device);
@@ -1803,7 +1810,8 @@ public class HeadsetStateMachine extends StateMachine {
        }
    }

    private void processAtCpbr(String atString, int type, BluetoothDevice device) {
    @VisibleForTesting
    void processAtCpbr(String atString, int type, BluetoothDevice device) {
        log("processAtCpbr - atString = " + atString);
        if (mPhonebook != null) {
            mPhonebook.handleCpbrCommand(atString, type, device);
@@ -1864,7 +1872,8 @@ public class HeadsetStateMachine extends StateMachine {
     * @param atString AT command after the "AT+" prefix
     * @param device Remote device that has sent this command
     */
    private void processVendorSpecificAt(String atString, BluetoothDevice device) {
    @VisibleForTesting
    void processVendorSpecificAt(String atString, BluetoothDevice device) {
        log("processVendorSpecificAt - atString = " + atString);

        // Currently we accept only SET type commands.
@@ -1905,7 +1914,8 @@ public class HeadsetStateMachine extends StateMachine {
     * @param args command arguments after the equal sign
     * @param device Remote device that has sent this command
     */
    private void processAtXapl(Object[] args, BluetoothDevice device) {
    @VisibleForTesting
    void processAtXapl(Object[] args, BluetoothDevice device) {
        if (args.length != 2) {
            Log.w(TAG, "processAtXapl() args length must be 2: " + String.valueOf(args.length));
            return;
@@ -1934,7 +1944,8 @@ public class HeadsetStateMachine extends StateMachine {
        mNativeInterface.atResponseString(device, "+XAPL=iPhone," + String.valueOf(2));
    }

    private void processUnknownAt(String atString, BluetoothDevice device) {
    @VisibleForTesting
    void processUnknownAt(String atString, BluetoothDevice device) {
        if (device == null) {
            Log.w(TAG, "processUnknownAt device is null");
            return;
@@ -2042,7 +2053,8 @@ public class HeadsetStateMachine extends StateMachine {
        sendIndicatorIntent(device, indId, indValue);
    }

    private void processSendClccResponse(HeadsetClccResponse clcc) {
    @VisibleForTesting
    void processSendClccResponse(HeadsetClccResponse clcc) {
        if (!hasMessages(CLCC_RSP_TIMEOUT)) {
            return;
        }
@@ -2053,7 +2065,8 @@ public class HeadsetStateMachine extends StateMachine {
                clcc.mMode, clcc.mMpty, clcc.mNumber, clcc.mType);
    }

    private void processSendVendorSpecificResultCode(HeadsetVendorSpecificResultCode resultCode) {
    @VisibleForTesting
    void processSendVendorSpecificResultCode(HeadsetVendorSpecificResultCode resultCode) {
        String stringToSend = resultCode.mCommand + ": ";
        if (resultCode.mArg != null) {
            stringToSend += resultCode.mArg;
+180 −0
Original line number Diff line number Diff line
@@ -38,7 +38,9 @@ import android.os.HandlerThread;
import android.os.UserHandle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
import android.telephony.PhoneNumberUtils;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;

@@ -59,6 +61,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;
@@ -1247,6 +1250,183 @@ public class HeadsetStateMachineTest {
        verify(mNativeInterface).clccResponse(mTestDevice, 0, 0, 0, 0, false, "", 0);
    }

    @Test
    public void testProcessAtCops() {
        ServiceState serviceState = mock(ServiceState.class);
        when(serviceState.getOperatorAlphaLong()).thenReturn("");
        when(serviceState.getOperatorAlphaShort()).thenReturn("");
        HeadsetPhoneState phoneState = mock(HeadsetPhoneState.class);
        when(phoneState.getServiceState()).thenReturn(serviceState);
        when(mSystemInterface.getHeadsetPhoneState()).thenReturn(phoneState);
        when(mSystemInterface.isInCall()).thenReturn(true);
        when(mSystemInterface.getNetworkOperator()).thenReturn(null);

        mHeadsetStateMachine.processAtCops(mTestDevice);

        verify(mNativeInterface).copsResponse(mTestDevice, "");
    }

    @Test
    public void testProcessAtCpbr() {
        String atString = "command=ERR";
        int type = AtPhonebook.TYPE_SET;

        mHeadsetStateMachine.processAtCpbr(atString, type, mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                BluetoothCmeError.TEXT_HAS_INVALID_CHARS);
    }

    @Test
    public void testProcessAtCpbs() {
        String atString = "command=ERR";
        int type = AtPhonebook.TYPE_SET;

        mHeadsetStateMachine.processAtCpbs(atString, type, mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                BluetoothCmeError.OPERATION_NOT_ALLOWED);
    }

    @Test
    public void testProcessAtCscs() {
        String atString = "command=GSM";
        int type = AtPhonebook.TYPE_SET;

        mHeadsetStateMachine.processAtCscs(atString, type, mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_OK,
                -1);
    }

    @Test
    public void testProcessAtXapl() {
        Object[] args = new Object[2];
        args[0] = "1-12-3";
        args[1] = 1;

        mHeadsetStateMachine.processAtXapl(args, mTestDevice);

        verify(mNativeInterface).atResponseString(mTestDevice, "+XAPL=iPhone," + String.valueOf(2));
    }

    @Test
    public void testProcessSendVendorSpecificResultCode() {
        HeadsetVendorSpecificResultCode resultCode = new HeadsetVendorSpecificResultCode(
                mTestDevice, "command", "arg");

        mHeadsetStateMachine.processSendVendorSpecificResultCode(resultCode);

        verify(mNativeInterface).atResponseString(mTestDevice, "command" + ": " + "arg");
    }

    @Test
    public void testProcessSubscriberNumberRequest_withSubscriberNumberNull() {
        when(mSystemInterface.getSubscriberNumber()).thenReturn(null);

        mHeadsetStateMachine.processSubscriberNumberRequest(mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_OK, 0);
    }

    @Test
    public void testProcessSubscriberNumberRequest_withSubscriberNumberNotNull() {
        String number = "1111";
        when(mSystemInterface.getSubscriberNumber()).thenReturn(number);

        mHeadsetStateMachine.processSubscriberNumberRequest(mTestDevice);

        verify(mNativeInterface).atResponseString(mTestDevice,
                "+CNUM: ,\"" + number + "\"," + PhoneNumberUtils.toaFromString(number) + ",,4");
        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_OK, 0);
    }

    @Test
    public void testProcessUnknownAt() {
        String atString = "+CSCS=invalid";
        mHeadsetStateMachine.processUnknownAt(atString, mTestDevice);
        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                BluetoothCmeError.OPERATION_NOT_SUPPORTED);
        Mockito.clearInvocations(mNativeInterface);

        atString = "+CPBS=";
        mHeadsetStateMachine.processUnknownAt(atString, mTestDevice);
        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                BluetoothCmeError.OPERATION_NOT_SUPPORTED);

        atString = "+CPBR=ERR";
        mHeadsetStateMachine.processUnknownAt(atString, mTestDevice);
        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                BluetoothCmeError.TEXT_HAS_INVALID_CHARS);

        atString = "inval=";
        mHeadsetStateMachine.processUnknownAt(atString, mTestDevice);
        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                0);
    }

    @Test
    public void testProcessVendorSpecificAt_withNoEqualSignCommand() {
        String atString = "invalid_command";

        mHeadsetStateMachine.processVendorSpecificAt(atString, mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                0);
    }

    @Test
    public void testProcessVendorSpecificAt_withUnsupportedCommand() {
        String atString = "invalid_command=";

        mHeadsetStateMachine.processVendorSpecificAt(atString, mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                0);
    }

    @Test
    public void testProcessVendorSpecificAt_withQuestionMarkArg() {
        String atString = BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_XEVENT + "=?arg";

        mHeadsetStateMachine.processVendorSpecificAt(atString, mTestDevice);

        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_ERROR,
                0);
    }

    @Test
    public void testProcessVendorSpecificAt_withValidCommandAndArg() {
        String atString = BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_XAPL + "=1-12-3,1";

        mHeadsetStateMachine.processVendorSpecificAt(atString, mTestDevice);

        verify(mNativeInterface).atResponseString(mTestDevice, "+XAPL=iPhone," + "2");
        verify(mNativeInterface).atResponseCode(mTestDevice, HeadsetHalConstants.AT_RESPONSE_OK, 0);
    }

    @Test
    public void testProcessVolumeEvent_withVolumeTypeMic() {
        when(mHeadsetService.getActiveDevice()).thenReturn(mTestDevice);

        mHeadsetStateMachine.processVolumeEvent(HeadsetHalConstants.VOLUME_TYPE_MIC, 1);

        Assert.assertEquals(mHeadsetStateMachine.mMicVolume, 1);
    }

    @Test
    public void testProcessVolumeEvent_withVolumeTypeSpk() {
        when(mHeadsetService.getActiveDevice()).thenReturn(mTestDevice);
        AudioManager mockAudioManager = mock(AudioManager.class);
        when(mockAudioManager.getStreamVolume(AudioManager.STREAM_BLUETOOTH_SCO)).thenReturn(1);
        when(mSystemInterface.getAudioManager()).thenReturn(mockAudioManager);

        mHeadsetStateMachine.processVolumeEvent(HeadsetHalConstants.VOLUME_TYPE_SPK, 2);

        Assert.assertEquals(mHeadsetStateMachine.mSpeakerVolume, 2);
        verify(mockAudioManager).setStreamVolume(AudioManager.STREAM_BLUETOOTH_SCO, 2, 0);
    }

    /**
     * Setup Connecting State
     * @return number of times mHeadsetService.sendBroadcastAsUser() has been invoked