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

Commit 4b619a71 authored by Kihong Seong's avatar Kihong Seong
Browse files

Add additional tests to HeadsetStateMachineTest

Bug: 237467631
Test: atest HeadsetStateMachineTest
Change-Id: Id5518a1c35f8950e2bb6156a707b9c971757c209
parent 28657ddb
Loading
Loading
Loading
Loading
+20 −10
Original line number Diff line number Diff line
@@ -146,7 +146,8 @@ public class HeadsetStateMachine extends StateMachine {
    private boolean mHasNrecEnabled = false;
    private boolean mHasWbsEnabled = false;
    // AT Phone book keeps a group of states used by AT+CPBR commands
    private final AtPhonebook mPhonebook;
    @VisibleForTesting
    final AtPhonebook mPhonebook;
    // HSP specific
    private boolean mNeedDialingOutReply;
    // Audio disconnect timeout retry count
@@ -1516,7 +1517,8 @@ public class HeadsetStateMachine extends StateMachine {
    /*
     * Put the AT command, company ID, arguments, and device in an Intent and broadcast it.
     */
    private void broadcastVendorSpecificEventIntent(String command, int companyId, int commandType,
    @VisibleForTesting
    void broadcastVendorSpecificEventIntent(String command, int companyId, int commandType,
            Object[] arguments, BluetoothDevice device) {
        log("broadcastVendorSpecificEventIntent(" + command + ")");
        Intent intent = new Intent(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
@@ -1540,7 +1542,8 @@ public class HeadsetStateMachine extends StateMachine {
        am.setBluetoothHeadsetProperties(getCurrentDeviceName(), mHasNrecEnabled, mHasWbsEnabled);
    }

    private String parseUnknownAt(String atString) {
    @VisibleForTesting
    String parseUnknownAt(String atString) {
        StringBuilder atCommand = new StringBuilder(atString.length());

        for (int i = 0; i < atString.length(); i++) {
@@ -1561,7 +1564,8 @@ public class HeadsetStateMachine extends StateMachine {
        return atCommand.toString();
    }

    private int getAtCommandType(String atCommand) {
    @VisibleForTesting
    int getAtCommandType(String atCommand) {
        int commandType = AtPhonebook.TYPE_UNKNOWN;
        String atString = null;
        atCommand = atCommand.trim();
@@ -1691,7 +1695,8 @@ public class HeadsetStateMachine extends StateMachine {
    }

    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    private void processAtChld(int chld, BluetoothDevice device) {
    @VisibleForTesting
    void processAtChld(int chld, BluetoothDevice device) {
        if (mSystemInterface.processChld(chld)) {
            mNativeInterface.atResponseCode(device, HeadsetHalConstants.AT_RESPONSE_OK, 0);
        } else {
@@ -1756,7 +1761,8 @@ public class HeadsetStateMachine extends StateMachine {
    }

    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    private void processAtClcc(BluetoothDevice device) {
    @VisibleForTesting
    void processAtClcc(BluetoothDevice device) {
        if (mHeadsetService.isVirtualCallStarted()) {
            // In virtual call, send our phone number instead of remote phone number
            String phoneNumber = mSystemInterface.getSubscriberNumber();
@@ -1811,7 +1817,8 @@ public class HeadsetStateMachine extends StateMachine {
     * Find a character ch, ignoring quoted sections.
     * Return input.length() if not found.
     */
    private static int findChar(char ch, String input, int fromIndex) {
    @VisibleForTesting
    static int findChar(char ch, String input, int fromIndex) {
        for (int i = fromIndex; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '"') {
@@ -1831,7 +1838,8 @@ public class HeadsetStateMachine extends StateMachine {
     * Integer arguments are turned into Integer objects. Otherwise a String
     * object is used.
     */
    private static Object[] generateArgs(String input) {
    @VisibleForTesting
    static Object[] generateArgs(String input) {
        int i = 0;
        int j;
        ArrayList<Object> out = new ArrayList<Object>();
@@ -2028,7 +2036,8 @@ public class HeadsetStateMachine extends StateMachine {
        }
    }

    private void processAtBiev(int indId, int indValue, BluetoothDevice device) {
    @VisibleForTesting
    void processAtBiev(int indId, int indValue, BluetoothDevice device) {
        log("processAtBiev: ind_id=" + indId + ", ind_value=" + indValue);
        sendIndicatorIntent(device, indId, indValue);
    }
@@ -2105,7 +2114,8 @@ public class HeadsetStateMachine extends StateMachine {
        return builder.toString();
    }

    private void handleAccessPermissionResult(Intent intent) {
    @VisibleForTesting
    void handleAccessPermissionResult(Intent intent) {
        log("handleAccessPermissionResult");
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (!mPhonebook.getCheckingAccessPermission()) {
+152 −0
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.ArrayList;

/**
 * Tests for {@link HeadsetStateMachine}
 */
@@ -84,6 +86,7 @@ public class HeadsetStateMachineTest {
    @Mock private HeadsetSystemInterface mSystemInterface;
    @Mock private AudioManager mAudioManager;
    @Mock private HeadsetPhoneState mPhoneState;
    @Mock private Intent mIntent;
    private MockContentResolver mMockContentResolver;
    private HeadsetNativeInterface mNativeInterface;

@@ -1095,6 +1098,155 @@ public class HeadsetStateMachineTest {
                PhoneStateListener.LISTEN_NONE);
    }

    @Test
    public void testBroadcastVendorSpecificEventIntent() {
        mHeadsetStateMachine.broadcastVendorSpecificEventIntent(
                "command", 1, 1, null, mTestDevice);
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBroadcastAsUser(
                mIntentArgument.capture(), eq(UserHandle.ALL), eq(BLUETOOTH_CONNECT),
                any(Bundle.class));
    }

    @Test
    public void testFindChar_withCharFound() {
        char ch = 's';
        String input = "test";
        int fromIndex = 0;

        Assert.assertEquals(HeadsetStateMachine.findChar(ch, input, fromIndex), 2);
    }

    @Test
    public void testFindChar_withCharNotFound() {
        char ch = 'x';
        String input = "test";
        int fromIndex = 0;

        Assert.assertEquals(HeadsetStateMachine.findChar(ch, input, fromIndex), input.length());
    }

    @Test
    public void testFindChar_withQuotes() {
        char ch = 's';
        String input = "te\"st";
        int fromIndex = 0;

        Assert.assertEquals(HeadsetStateMachine.findChar(ch, input, fromIndex), input.length());
    }

    @Test
    public void testGenerateArgs() {
        String input = "11,notint";
        ArrayList<Object> expected = new ArrayList<Object>();
        expected.add(11);
        expected.add("notint");

        Assert.assertEquals(HeadsetStateMachine.generateArgs(input), expected.toArray());
    }

    @Test
    public void testGetAtCommandType() {
        String atCommand = "start?";
        Assert.assertEquals(mHeadsetStateMachine.getAtCommandType(atCommand),
                AtPhonebook.TYPE_READ);

        atCommand = "start=?";
        Assert.assertEquals(mHeadsetStateMachine.getAtCommandType(atCommand),
                AtPhonebook.TYPE_TEST);

        atCommand = "start=comm";
        Assert.assertEquals(mHeadsetStateMachine.getAtCommandType(atCommand), AtPhonebook.TYPE_SET);

        atCommand = "start!";
        Assert.assertEquals(mHeadsetStateMachine.getAtCommandType(atCommand),
                AtPhonebook.TYPE_UNKNOWN);
    }

    @Test
    public void testParseUnknownAt() {
        String atString = "\"command\"";

        Assert.assertEquals(mHeadsetStateMachine.parseUnknownAt(atString), "\"command\"");
    }

    @Test
    public void testParseUnknownAt_withUnmatchingQuotes() {
        String atString = "\"command";

        Assert.assertEquals(mHeadsetStateMachine.parseUnknownAt(atString), "\"command\"");
    }

    @Test
    public void testParseUnknownAt_withCharOutsideQuotes() {
        String atString = "a\"command\"";

        Assert.assertEquals(mHeadsetStateMachine.parseUnknownAt(atString), "A\"command\"");
    }

    @Test
    public void testHandleAccessPermissionResult_withNoChangeInAtCommandResult() {
        when(mIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)).thenReturn(null);
        when(mIntent.getAction()).thenReturn(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
        when(mIntent.getIntExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT,
                BluetoothDevice.CONNECTION_ACCESS_NO))
                .thenReturn(BluetoothDevice.CONNECTION_ACCESS_NO);
        when(mIntent.getBooleanExtra(BluetoothDevice.EXTRA_ALWAYS_ALLOWED, false)).thenReturn(true);
        mHeadsetStateMachine.mPhonebook.setCheckingAccessPermission(true);

        mHeadsetStateMachine.handleAccessPermissionResult(mIntent);

        verify(mNativeInterface).atResponseCode(null, 0, 0);
    }

    @Test
    public void testProcessAtBievCommand() {
        mHeadsetStateMachine.processAtBiev(1, 1, mTestDevice);

        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBroadcast(
                mIntentArgument.capture(), eq(BLUETOOTH_CONNECT), any(Bundle.class));
    }

    @Test
    public void testProcessAtChld_withProcessChldTrue() {
        int chld = 1;
        when(mSystemInterface.processChld(chld)).thenReturn(true);

        mHeadsetStateMachine.processAtChld(chld, mTestDevice);

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

    @Test
    public void testProcessAtChld_withProcessChldFalse() {
        int chld = 1;
        when(mSystemInterface.processChld(chld)).thenReturn(false);

        mHeadsetStateMachine.processAtChld(chld, mTestDevice);

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

    @Test
    public void testProcessAtClcc_withVirtualCallStarted() {
        when(mHeadsetService.isVirtualCallStarted()).thenReturn(true);
        when(mSystemInterface.getSubscriberNumber()).thenReturn(null);

        mHeadsetStateMachine.processAtClcc(mTestDevice);

        verify(mNativeInterface).clccResponse(mTestDevice, 0, 0, 0, 0, false, "", 0);
    }

    @Test
    public void testProcessAtClcc_withVirtualCallNotStarted() {
        when(mHeadsetService.isVirtualCallStarted()).thenReturn(false);
        when(mSystemInterface.listCurrentCalls()).thenReturn(false);

        mHeadsetStateMachine.processAtClcc(mTestDevice);

        verify(mNativeInterface).clccResponse(mTestDevice, 0, 0, 0, 0, false, "", 0);
    }

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