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

Commit 8240e49d authored by William Escande's avatar William Escande
Browse files

Migration Assert to Truth: Assert.assertThat

Bug: 311772251
Test: atest BluetoothInstrumentationTests
Flag: TEST_ONLY
Change-Id: Idf44cf41327be7f3c84c4e10cfa1f5acbd59f2be
parent 1e8d5ac3
Loading
Loading
Loading
Loading
+10 −17
Original line number Diff line number Diff line
@@ -50,9 +50,7 @@ import com.android.bluetooth.a2dpsink.A2dpSinkService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.flags.Flags;

import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -203,18 +201,16 @@ public class AvrcpControllerStateMachineTest {
     */
    private int setUpConnectedState(boolean control, boolean browsing) {

        Assert.assertThat(
                mAvrcpStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(AvrcpControllerStateMachine.Disconnected.class));
        assertThat(mAvrcpStateMachine.getCurrentState())
                .isInstanceOf(AvrcpControllerStateMachine.Disconnected.class);

        mAvrcpStateMachine.connect(StackEvent.connectionStateChanged(control, browsing));

        TestUtils.waitForLooperToFinishScheduledTask(mAvrcpStateMachine.getHandler().getLooper());
        verify(mAvrcpControllerService, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(2))
                .sendBroadcast(mIntentArgument.capture(), eq(BLUETOOTH_CONNECT), any(Bundle.class));
        Assert.assertThat(
                mAvrcpStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(AvrcpControllerStateMachine.Connected.class));
        assertThat(mAvrcpStateMachine.getCurrentState())
                .isInstanceOf(AvrcpControllerStateMachine.Connected.class);
        assertThat(mAvrcpStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_CONNECTED);

        return BluetoothProfile.STATE_CONNECTED;
@@ -369,9 +365,8 @@ public class AvrcpControllerStateMachineTest {
                .isEqualTo(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED);
        assertThat(mIntentArgument.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertThat(
                mAvrcpStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(AvrcpControllerStateMachine.Disconnected.class));
        assertThat(mAvrcpStateMachine.getCurrentState())
                .isInstanceOf(AvrcpControllerStateMachine.Disconnected.class);
        assertThat(mAvrcpStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        verify(mAvrcpControllerService).removeStateMachine(eq(mAvrcpStateMachine));
    }
@@ -399,9 +394,8 @@ public class AvrcpControllerStateMachineTest {
                .isEqualTo(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED);
        assertThat(mIntentArgument.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertThat(
                mAvrcpStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(AvrcpControllerStateMachine.Disconnected.class));
        assertThat(mAvrcpStateMachine.getCurrentState())
                .isInstanceOf(AvrcpControllerStateMachine.Disconnected.class);
        assertThat(mAvrcpStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        verify(mAvrcpControllerService).removeStateMachine(eq(mAvrcpStateMachine));
    }
@@ -429,9 +423,8 @@ public class AvrcpControllerStateMachineTest {
                .isEqualTo(BluetoothAvrcpController.ACTION_CONNECTION_STATE_CHANGED);
        assertThat(mIntentArgument.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertThat(
                mAvrcpStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(AvrcpControllerStateMachine.Disconnected.class));
        assertThat(mAvrcpStateMachine.getCurrentState())
                .isInstanceOf(AvrcpControllerStateMachine.Disconnected.class);
        assertThat(mAvrcpStateMachine.getState()).isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        verify(mAvrcpControllerService).removeStateMachine(eq(mAvrcpStateMachine));
    }
+13 −20
Original line number Diff line number Diff line
@@ -110,7 +110,6 @@ import com.google.common.primitives.Bytes;

import org.hamcrest.Matcher;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -262,9 +261,8 @@ public class BassClientStateMachineTest {
                .sendBroadcast(any(Intent.class), anyString());

        // Check that we are in Disconnected state
        Assert.assertThat(
                mBassClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(BassClientStateMachine.Disconnected.class));
        assertThat(mBassClientStateMachine.getCurrentState())
                .isInstanceOf(BassClientStateMachine.Disconnected.class);
    }

    @Test
@@ -280,9 +278,8 @@ public class BassClientStateMachineTest {
                .sendBroadcast(any(Intent.class), anyString());

        // Check that we are in Disconnected state
        Assert.assertThat(
                mBassClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(BassClientStateMachine.Disconnected.class));
        assertThat(mBassClientStateMachine.getCurrentState())
                .isInstanceOf(BassClientStateMachine.Disconnected.class);
        assertThat(mBassClientStateMachine.mBluetoothGatt).isNull();
    }

@@ -304,9 +301,8 @@ public class BassClientStateMachineTest {
        assertThat(intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(BluetoothProfile.STATE_CONNECTING);

        Assert.assertThat(
                mBassClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(BassClientStateMachine.Connecting.class));
        assertThat(mBassClientStateMachine.getCurrentState())
                .isInstanceOf(BassClientStateMachine.Connecting.class);

        assertThat(mBassClientStateMachine.mGattCallback).isNotNull();
        mBassClientStateMachine.notifyConnectionStateChanged(
@@ -321,9 +317,8 @@ public class BassClientStateMachineTest {
                        any(String[].class),
                        any(BroadcastOptions.class));

        Assert.assertThat(
                mBassClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(BassClientStateMachine.Connected.class));
        assertThat(mBassClientStateMachine.getCurrentState())
                .isInstanceOf(BassClientStateMachine.Connected.class);
    }

    @Test
@@ -344,9 +339,8 @@ public class BassClientStateMachineTest {
        assertThat(intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(BluetoothProfile.STATE_CONNECTING);

        Assert.assertThat(
                mBassClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(BassClientStateMachine.Connecting.class));
        assertThat(mBassClientStateMachine.getCurrentState())
                .isInstanceOf(BassClientStateMachine.Connecting.class);

        // Verify that one connection state broadcast is executed
        ArgumentCaptor<Intent> intentArgument2 = ArgumentCaptor.forClass(Intent.class);
@@ -358,9 +352,8 @@ public class BassClientStateMachineTest {
        assertThat(intentArgument2.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTED);

        Assert.assertThat(
                mBassClientStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(BassClientStateMachine.Disconnected.class));
        assertThat(mBassClientStateMachine.getCurrentState())
                .isInstanceOf(BassClientStateMachine.Disconnected.class);
    }

    @Test
@@ -3335,7 +3328,7 @@ public class BassClientStateMachineTest {
                            Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                                    | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND));
        }
        Assert.assertThat(mBassClientStateMachine.getCurrentState(), IsInstanceOf.instanceOf(type));
        assertThat(mBassClientStateMachine.getCurrentState()).isInstanceOf(type);
    }

    private BluetoothLeBroadcastMetadata createBroadcastMetadata() {
+16 −29
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;

import org.hamcrest.core.IsInstanceOf;
import org.junit.*;
import org.junit.Rule;
import org.junit.runner.RunWith;
@@ -140,9 +139,8 @@ public class CsipSetCoordinatorStateMachineTest {
        // Verify that no connection state broadcast is executed
        verify(mService, after(TIMEOUT_MS).never()).sendBroadcast(any(Intent.class), anyString());
        // Check that we are in Disconnected state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Disconnected.class));
        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Disconnected.class);
    }

    /** Test that an incoming connection with policy allowing connection is accepted */
@@ -165,10 +163,8 @@ public class CsipSetCoordinatorStateMachineTest {
        assertThat(intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(STATE_CONNECTING);

        // Check that we are in Connecting state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Connecting.class));
        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Connecting.class);

        // Send a message to trigger connection completed
        CsipSetCoordinatorStackEvent connCompletedEvent =
@@ -184,10 +180,9 @@ public class CsipSetCoordinatorStateMachineTest {
        ArgumentCaptor<Intent> intentArgument2 = ArgumentCaptor.forClass(Intent.class);
        verify(mService, timeout(TIMEOUT_MS).times(2))
                .sendBroadcast(intentArgument2.capture(), anyString());
        // Check that we are in Connected state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Connected.class));

        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Connected.class);
    }

    /** Test that an outgoing connection times out */
@@ -207,10 +202,8 @@ public class CsipSetCoordinatorStateMachineTest {
        assertThat(intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(STATE_CONNECTING);

        // Check that we are in Connecting state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Connecting.class));
        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Connecting.class);

        // Verify that one connection state broadcast is executed
        ArgumentCaptor<Intent> intentArgument2 = ArgumentCaptor.forClass(Intent.class);
@@ -219,10 +212,8 @@ public class CsipSetCoordinatorStateMachineTest {
        assertThat(intentArgument2.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(STATE_DISCONNECTED);

        // Check that we are in Disconnected state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Disconnected.class));
        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Disconnected.class);
        verify(mNativeInterface).disconnect(eq(mTestDevice));
    }

@@ -248,10 +239,8 @@ public class CsipSetCoordinatorStateMachineTest {
        assertThat(intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(STATE_CONNECTING);

        // Check that we are in Connecting state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Connecting.class));
        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Connecting.class);

        // Verify that one connection state broadcast is executed
        ArgumentCaptor<Intent> intentArgument2 = ArgumentCaptor.forClass(Intent.class);
@@ -260,10 +249,8 @@ public class CsipSetCoordinatorStateMachineTest {
        assertThat(intentArgument2.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1))
                .isEqualTo(STATE_DISCONNECTED);

        // Check that we are in Disconnected state
        Assert.assertThat(
                mStateMachine.getCurrentState(),
                IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Disconnected.class));
        assertThat(mStateMachine.getCurrentState())
                .isInstanceOf(CsipSetCoordinatorStateMachine.Disconnected.class);
        verify(mNativeInterface).disconnect(eq(mTestDevice));
    }

@@ -701,7 +688,7 @@ public class CsipSetCoordinatorStateMachineTest {
        mStateMachine.sendMessage(msg);
        // Verify that one connection state broadcast is executed
        verify(mService, timeout(TIMEOUT_MS)).sendBroadcast(any(Intent.class), anyString());
        Assert.assertThat(mStateMachine.getCurrentState(), IsInstanceOf.instanceOf(type));
        assertThat(mStateMachine.getCurrentState()).isInstanceOf(type);
    }

    public static class CsipSetCoordinatorStateMachineWrapper
+26 −40
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ import com.android.bluetooth.btservice.SilenceDeviceManager;
import com.android.bluetooth.btservice.storage.DatabaseManager;
import com.android.bluetooth.flags.Flags;

import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -442,9 +441,8 @@ public class HeadsetServiceTest {
            // Put device to connecting
            when(mStateMachines.get(mCurrentDevice).getConnectionState())
                    .thenReturn(BluetoothProfile.STATE_CONNECTING);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            // Put device to connected
            connectedDevices.add(mCurrentDevice);
            when(mStateMachines.get(mCurrentDevice).getDevice()).thenReturn(mCurrentDevice);
@@ -452,9 +450,8 @@ public class HeadsetServiceTest {
                    .thenReturn(BluetoothProfile.STATE_CONNECTED);
            assertThat(mHeadsetService.getConnectionState(mCurrentDevice))
                    .isEqualTo(BluetoothProfile.STATE_CONNECTED);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
        }
        // Connect the next device will fail
        mCurrentDevice = TestUtils.getTestDevice(mAdapter, MAX_HEADSET_CONNECTIONS);
@@ -470,9 +467,8 @@ public class HeadsetServiceTest {
                        eq(mSystemInterface));
        assertThat(mHeadsetService.getConnectionState(mCurrentDevice))
                .isEqualTo(BluetoothProfile.STATE_DISCONNECTED);
        Assert.assertThat(
                mHeadsetService.getConnectedDevices(),
                Matchers.containsInAnyOrder(connectedDevices.toArray()));
        assertThat(mHeadsetService.getConnectedDevices())
                .containsExactlyElementsIn(connectedDevices);
    }

    /**
@@ -577,9 +573,8 @@ public class HeadsetServiceTest {
                    mCurrentDevice,
                    BluetoothProfile.STATE_DISCONNECTED,
                    BluetoothProfile.STATE_CONNECTING);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            // Put device to connected
            connectedDevices.add(mCurrentDevice);
            when(mStateMachines.get(mCurrentDevice).getDevice()).thenReturn(mCurrentDevice);
@@ -593,9 +588,8 @@ public class HeadsetServiceTest {
                    mCurrentDevice,
                    BluetoothProfile.STATE_CONNECTING,
                    BluetoothProfile.STATE_CONNECTED);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            // Try to connect audio
            // Should fail
            assertThat(mHeadsetService.connectAudio(mCurrentDevice))
@@ -675,9 +669,8 @@ public class HeadsetServiceTest {
                    mCurrentDevice,
                    BluetoothProfile.STATE_DISCONNECTED,
                    BluetoothProfile.STATE_CONNECTING);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            // Put device to connected
            connectedDevices.add(mCurrentDevice);
            when(mStateMachines.get(mCurrentDevice).getDevice()).thenReturn(mCurrentDevice);
@@ -691,9 +684,8 @@ public class HeadsetServiceTest {
                    BluetoothProfile.STATE_CONNECTED);
            assertThat(mHeadsetService.getConnectionState(mCurrentDevice))
                    .isEqualTo(BluetoothProfile.STATE_CONNECTED);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
        }
        if (MAX_HEADSET_CONNECTIONS >= 2) {
            // Try to connect audio
@@ -777,9 +769,8 @@ public class HeadsetServiceTest {
                    mCurrentDevice,
                    BluetoothProfile.STATE_DISCONNECTED,
                    BluetoothProfile.STATE_CONNECTING);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            // Put device to connected
            connectedDevices.add(mCurrentDevice);
            when(mStateMachines.get(mCurrentDevice).getDevice()).thenReturn(mCurrentDevice);
@@ -789,9 +780,8 @@ public class HeadsetServiceTest {
                    .thenReturn(SystemClock.uptimeMillis());
            assertThat(mHeadsetService.getConnectionState(mCurrentDevice))
                    .isEqualTo(BluetoothProfile.STATE_CONNECTED);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            mHeadsetService.onConnectionStateChangedFromStateMachine(
                    mCurrentDevice,
                    BluetoothProfile.STATE_CONNECTING,
@@ -920,9 +910,8 @@ public class HeadsetServiceTest {
                mCurrentDevice,
                BluetoothProfile.STATE_DISCONNECTED,
                BluetoothProfile.STATE_CONNECTING);
        Assert.assertThat(
                mHeadsetService.getConnectedDevices(),
                Matchers.containsInAnyOrder(connectedDevices.toArray()));
        assertThat(mHeadsetService.getConnectedDevices())
                .containsExactlyElementsIn(connectedDevices);
        // Put device to connected
        connectedDevices.add(mCurrentDevice);
        when(mStateMachines.get(mCurrentDevice).getDevice()).thenReturn(mCurrentDevice);
@@ -932,9 +921,8 @@ public class HeadsetServiceTest {
                .thenReturn(SystemClock.uptimeMillis());
        assertThat(mHeadsetService.getConnectionState(mCurrentDevice))
                .isEqualTo(BluetoothProfile.STATE_CONNECTED);
        Assert.assertThat(
                mHeadsetService.getConnectedDevices(),
                Matchers.containsInAnyOrder(connectedDevices.toArray()));
        assertThat(mHeadsetService.getConnectedDevices())
                .containsExactlyElementsIn(connectedDevices);
        mHeadsetService.onConnectionStateChangedFromStateMachine(
                mCurrentDevice,
                BluetoothProfile.STATE_CONNECTING,
@@ -1031,9 +1019,8 @@ public class HeadsetServiceTest {
                    mCurrentDevice,
                    BluetoothProfile.STATE_DISCONNECTED,
                    BluetoothProfile.STATE_CONNECTING);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            // Put device to connected
            connectedDevices.add(mCurrentDevice);
            when(mStateMachines.get(mCurrentDevice).getDevice()).thenReturn(mCurrentDevice);
@@ -1043,9 +1030,8 @@ public class HeadsetServiceTest {
                    .thenReturn(SystemClock.uptimeMillis());
            assertThat(mHeadsetService.getConnectionState(mCurrentDevice))
                    .isEqualTo(BluetoothProfile.STATE_CONNECTED);
            Assert.assertThat(
                    mHeadsetService.getConnectedDevices(),
                    Matchers.containsInAnyOrder(connectedDevices.toArray()));
            assertThat(mHeadsetService.getConnectedDevices())
                    .containsExactlyElementsIn(connectedDevices);
            mHeadsetService.onConnectionStateChangedFromStateMachine(
                    mCurrentDevice,
                    BluetoothProfile.STATE_CONNECTING,
Loading