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

Commit 5d4d49d4 authored by Kihong Seong's avatar Kihong Seong Committed by Gerrit Code Review
Browse files

Merge "Add additional tests to HeadsetStateMachineTest"

parents b8197244 4b619a71
Loading
Loading
Loading
Loading
+20 −10
Original line number Original line Diff line number Diff line
@@ -146,7 +146,8 @@ public class HeadsetStateMachine extends StateMachine {
    private boolean mHasNrecEnabled = false;
    private boolean mHasNrecEnabled = false;
    private boolean mHasWbsEnabled = false;
    private boolean mHasWbsEnabled = false;
    // AT Phone book keeps a group of states used by AT+CPBR commands
    // AT Phone book keeps a group of states used by AT+CPBR commands
    private final AtPhonebook mPhonebook;
    @VisibleForTesting
    final AtPhonebook mPhonebook;
    // HSP specific
    // HSP specific
    private boolean mNeedDialingOutReply;
    private boolean mNeedDialingOutReply;
    // Audio disconnect timeout retry count
    // 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.
     * 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) {
            Object[] arguments, BluetoothDevice device) {
        log("broadcastVendorSpecificEventIntent(" + command + ")");
        log("broadcastVendorSpecificEventIntent(" + command + ")");
        Intent intent = new Intent(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
        Intent intent = new Intent(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
@@ -1540,7 +1542,8 @@ public class HeadsetStateMachine extends StateMachine {
        am.setBluetoothHeadsetProperties(getCurrentDeviceName(), mHasNrecEnabled, mHasWbsEnabled);
        am.setBluetoothHeadsetProperties(getCurrentDeviceName(), mHasNrecEnabled, mHasWbsEnabled);
    }
    }


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


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


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


    @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
    @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)) {
        if (mSystemInterface.processChld(chld)) {
            mNativeInterface.atResponseCode(device, HeadsetHalConstants.AT_RESPONSE_OK, 0);
            mNativeInterface.atResponseCode(device, HeadsetHalConstants.AT_RESPONSE_OK, 0);
        } else {
        } else {
@@ -1756,7 +1761,8 @@ public class HeadsetStateMachine extends StateMachine {
    }
    }


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


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


import java.util.ArrayList;

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


@@ -1095,6 +1098,155 @@ public class HeadsetStateMachineTest {
                PhoneStateListener.LISTEN_NONE);
                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
     * Setup Connecting State
     * @return number of times mHeadsetService.sendBroadcastAsUser() has been invoked
     * @return number of times mHeadsetService.sendBroadcastAsUser() has been invoked