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

Commit 134479cc authored by Kihong Seong's avatar Kihong Seong
Browse files

Add additional tests to functions in HeadsetStateMachine

Bug: 237467631
Test: atest HeadsetStateMachineTest
Change-Id: I9ec7d110d75fb9c36f28e345511a1eb5ae795e52
parent 73ea8bd5
Loading
Loading
Loading
Loading
+26 −13
Original line number Original line Diff line number Diff line
@@ -136,8 +136,10 @@ public class HeadsetStateMachine extends StateMachine {
    private final HeadsetSystemInterface mSystemInterface;
    private final HeadsetSystemInterface mSystemInterface;


    // Runtime states
    // Runtime states
    private int mSpeakerVolume;
    @VisibleForTesting
    private int mMicVolume;
    int mSpeakerVolume;
    @VisibleForTesting
    int mMicVolume;
    private boolean mDeviceSilenced;
    private boolean mDeviceSilenced;
    private HeadsetAgIndicatorEnableState mAgIndicatorEnableState;
    private HeadsetAgIndicatorEnableState mAgIndicatorEnableState;
    // The timestamp when the device entered connecting/connected state
    // 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
        // Only current active device can change SCO volume
        if (!mDevice.equals(mHeadsetService.getActiveDevice())) {
        if (!mDevice.equals(mHeadsetService.getActiveDevice())) {
            Log.w(TAG, "processVolumeEvent, ignored because " + mDevice + " is not active");
            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)
    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    private void processSubscriberNumberRequest(BluetoothDevice device) {
    @VisibleForTesting
    void processSubscriberNumberRequest(BluetoothDevice device) {
        String number = mSystemInterface.getSubscriberNumber();
        String number = mSystemInterface.getSubscriberNumber();
        if (number != null) {
        if (number != null) {
            mNativeInterface.atResponseString(device,
            mNativeInterface.atResponseString(device,
@@ -1740,7 +1744,8 @@ public class HeadsetStateMachine extends StateMachine {
    }
    }


    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    private void processAtCops(BluetoothDevice device) {
    @VisibleForTesting
    void processAtCops(BluetoothDevice device) {
        // Get operator name suggested by Telephony
        // Get operator name suggested by Telephony
        String operatorName = null;
        String operatorName = null;
        ServiceState serviceState = mSystemInterface.getHeadsetPhoneState().getServiceState();
        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);
        log("processAtCscs - atString = " + atString);
        if (mPhonebook != null) {
        if (mPhonebook != null) {
            mPhonebook.handleCscsCommand(atString, type, device);
            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);
        log("processAtCpbs - atString = " + atString);
        if (mPhonebook != null) {
        if (mPhonebook != null) {
            mPhonebook.handleCpbsCommand(atString, type, device);
            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);
        log("processAtCpbr - atString = " + atString);
        if (mPhonebook != null) {
        if (mPhonebook != null) {
            mPhonebook.handleCpbrCommand(atString, type, device);
            mPhonebook.handleCpbrCommand(atString, type, device);
@@ -1864,7 +1872,8 @@ public class HeadsetStateMachine extends StateMachine {
     * @param atString AT command after the "AT+" prefix
     * @param atString AT command after the "AT+" prefix
     * @param device Remote device that has sent this command
     * @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);
        log("processVendorSpecificAt - atString = " + atString);


        // Currently we accept only SET type commands.
        // 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 args command arguments after the equal sign
     * @param device Remote device that has sent this command
     * @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) {
        if (args.length != 2) {
            Log.w(TAG, "processAtXapl() args length must be 2: " + String.valueOf(args.length));
            Log.w(TAG, "processAtXapl() args length must be 2: " + String.valueOf(args.length));
            return;
            return;
@@ -1934,7 +1944,8 @@ public class HeadsetStateMachine extends StateMachine {
        mNativeInterface.atResponseString(device, "+XAPL=iPhone," + String.valueOf(2));
        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) {
        if (device == null) {
            Log.w(TAG, "processUnknownAt device is null");
            Log.w(TAG, "processUnknownAt device is null");
            return;
            return;
@@ -2042,7 +2053,8 @@ public class HeadsetStateMachine extends StateMachine {
        sendIndicatorIntent(device, indId, indValue);
        sendIndicatorIntent(device, indId, indValue);
    }
    }


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


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


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


import java.util.ArrayList;
import java.util.ArrayList;
@@ -1247,6 +1250,183 @@ public class HeadsetStateMachineTest {
        verify(mNativeInterface).clccResponse(mTestDevice, 0, 0, 0, 0, false, "", 0);
        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
     * Setup Connecting State
     * @return number of times mHeadsetService.sendBroadcastAsUser() has been invoked
     * @return number of times mHeadsetService.sendBroadcastAsUser() has been invoked