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

Commit dff59dd2 authored by Kyunglyul Hyun's avatar Kyunglyul Hyun
Browse files

BAS: Handle disconnect right after connect

In the "connecting" state, if a disconnect request is received,
the state changed to "disconnecting" instead of "disconnected".
The "disconnecting" state always timed out because we're not
connected to the remote device yet.
This CL fixes this issue by changing the state to "disconnected"
directly.
A new test is added to confirm the behavior.

Bug: 264829420
Tag: #feature
Test: atest BatteryStateMachineTest
Change-Id: Id422e207249258642bafd28754245ac6c98f5a6e
parent 7f6d6d19
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -361,10 +361,9 @@ public class BatteryStateMachine extends StateMachine {
                    log(TAG, "Connection canceled to " + mDevice);
                    if (mBluetoothGatt != null) {
                        mBluetoothGatt.disconnect();
                        transitionTo(mDisconnecting);
                    } else {
                        transitionTo(mDisconnected);
                    }
                    // As we're not yet connected we don't need to wait for callbacks.
                    transitionTo(mDisconnected);
                    break;
                case CONNECTION_STATE_CHANGED:
                    processConnectionEvent(message.arg1);
+20 −0
Original line number Diff line number Diff line
@@ -192,6 +192,26 @@ public class BatteryStateMachineTest {
                IsInstanceOf.instanceOf(BatteryStateMachine.Connected.class));
    }

    @Test
    public void testDisconnectBeforeConnected() {
        allowConnection(true);
        allowConnectGatt(true);

        mBatteryStateMachine.sendMessage(BatteryStateMachine.CONNECT);

        verify(mBatteryService, timeout(TIMEOUT_MS))
                .handleConnectionStateChanged(any(BatteryStateMachine.class),
                        eq(BluetoothProfile.STATE_DISCONNECTED),
                        eq(BluetoothProfile.STATE_CONNECTING));

        mBatteryStateMachine.sendMessage(BatteryStateMachine.DISCONNECT);

        verify(mBatteryService, timeout(TIMEOUT_MS))
                .handleConnectionStateChanged(any(BatteryStateMachine.class),
                        eq(BluetoothProfile.STATE_CONNECTING),
                        eq(BluetoothProfile.STATE_DISCONNECTED));
    }

    @Test
    public void testConnectedStateChanges() {
        allowConnection(true);