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

Commit a9fe0c09 authored by Himanshu Rawat's avatar Himanshu Rawat
Browse files

Update A2dpServiceTest to expect state machine removal with unbond

aosp/3381766 ensures that A2DP state machine is removed when the bond is
removed. A2dpServiceTest has to be updated to match the new behaviour.

Test: atest A2dpServiceTest
Flag: com.android.bluetooth.flags.a2dp_cleanup_on_remove_device
Bug: 339763326
Bug: 381313622
Change-Id: I46bd3ff4feacf3ce03196b5a6d12945f0eff3b16
parent f471374f
Loading
Loading
Loading
Loading
+71 −1
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import android.media.BluetoothProfileConnectionInfo;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.test.TestLooper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.FlagsParameterization;
import android.platform.test.flag.junit.SetFlagsRule;
@@ -595,7 +596,8 @@ public class A2dpServiceTest {
     * Test that a state machine in DISCONNECTED state is removed only after the device is unbond.
     */
    @Test
    public void testDeleteStateMachineUnbondEvents() {
    @DisableFlags(Flags.FLAG_A2DP_CLEANUP_ON_REMOVE_DEVICE)
    public void testDeleteDisconnectedStateMachineUnbondEvents() {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(sTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
@@ -639,6 +641,74 @@ public class A2dpServiceTest {
        assertThat(mA2dpService.getDevices()).doesNotContain(sTestDevice);
    }

    /** Test that a state machine is removed after the device is unbond. */
    @Test
    @EnableFlags(Flags.FLAG_A2DP_CLEANUP_ON_REMOVE_DEVICE)
    public void testDeleteStateMachineUnbondEvents() {
        // Update the device priority so okToConnect() returns true
        when(mDatabaseManager.getProfileConnectionPolicy(sTestDevice, BluetoothProfile.A2DP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        doReturn(true).when(mMockNativeInterface).connectA2dp(any(BluetoothDevice.class));
        doReturn(true).when(mMockNativeInterface).disconnectA2dp(any(BluetoothDevice.class));

        // A2DP stack event: CONNECTION_STATE_CONNECTING - state machine should be created
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTING, STATE_DISCONNECTED);
        assertThat(mA2dpService.getConnectionState(sTestDevice)).isEqualTo(STATE_CONNECTING);
        assertThat(mA2dpService.getDevices()).contains(sTestDevice);
        // Device unbond - state machine is not removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_NONE);
        if (Flags.a2dpBroadcastConnectionStateWhenTurnedOff()) {
            // Verify that the intent CONNECTION_STATE_CHANGED is generated
            // for the existing connections.
            verifyConnectionStateIntent(sTestDevice, STATE_DISCONNECTED, STATE_CONNECTING);
        }
        assertThat(mA2dpService.getDevices()).doesNotContain(sTestDevice);

        // A2DP stack event: CONNECTION_STATE_CONNECTED - state machine is not removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_BONDED);
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTING, STATE_DISCONNECTED);
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTED, STATE_CONNECTING);
        assertThat(mA2dpService.getConnectionState(sTestDevice)).isEqualTo(STATE_CONNECTED);
        assertThat(mA2dpService.getDevices()).contains(sTestDevice);
        // Device unbond - state machine is not removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_NONE);
        if (Flags.a2dpBroadcastConnectionStateWhenTurnedOff()) {
            // Verify that the intent CONNECTION_STATE_CHANGED is generated
            // for the existing connections.
            verifyConnectionStateIntent(sTestDevice, STATE_DISCONNECTED, STATE_CONNECTED);
        }
        assertThat(mA2dpService.getDevices()).doesNotContain(sTestDevice);

        // A2DP stack event: CONNECTION_STATE_DISCONNECTING - state machine is not removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_BONDED);
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTING, STATE_DISCONNECTED);
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTED, STATE_CONNECTING);
        generateConnectionMessageFromNative(sTestDevice, STATE_DISCONNECTING, STATE_CONNECTED);
        assertThat(mA2dpService.getConnectionState(sTestDevice)).isEqualTo(STATE_DISCONNECTING);
        assertThat(mA2dpService.getDevices()).contains(sTestDevice);
        // Device unbond - state machine is not removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_NONE);
        if (Flags.a2dpBroadcastConnectionStateWhenTurnedOff()) {
            // Verify that the intent CONNECTION_STATE_CHANGED is generated
            // for the existing connections.
            verifyConnectionStateIntent(sTestDevice, STATE_DISCONNECTED, STATE_DISCONNECTING);
        }
        assertThat(mA2dpService.getDevices()).doesNotContain(sTestDevice);

        // A2DP stack event: CONNECTION_STATE_DISCONNECTED - state machine is not removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_BONDED);
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTING, STATE_DISCONNECTED);
        generateConnectionMessageFromNative(sTestDevice, STATE_CONNECTED, STATE_CONNECTING);
        generateConnectionMessageFromNative(sTestDevice, STATE_DISCONNECTING, STATE_CONNECTED);
        generateConnectionMessageFromNative(sTestDevice, STATE_DISCONNECTED, STATE_DISCONNECTING);
        assertThat(mA2dpService.getConnectionState(sTestDevice)).isEqualTo(STATE_DISCONNECTED);
        assertThat(mA2dpService.getDevices()).contains(sTestDevice);
        // Device unbond - state machine is removed
        mA2dpService.bondStateChanged(sTestDevice, BluetoothDevice.BOND_NONE);
        dispatchAtLeastOneMessage();
        assertThat(mA2dpService.getDevices()).doesNotContain(sTestDevice);
    }

    /**
     * Test that a CONNECTION_STATE_DISCONNECTED A2DP stack event will remove the state machine only
     * if the device is unbond.