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

Commit 3c55bcdd authored by Charlie Boutier's avatar Charlie Boutier
Browse files

BumbleBluetoothTests: use l2cap async in test_receive

Wait for data to be received to conclude the test

Test: atest BumbleBluetoothTests
Bug: 354802634
Flag: EXEMPT TEST_ONLY

Change-Id: I6babf0c830a5466f50beefa90eae8da56a68b2eb
parent ae57e977
Loading
Loading
Loading
Loading
+17 −17
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ import org.junit.runner.RunWith
import pandora.HostProto.Connection
import pandora.l2cap.L2CAPProto.CreditBasedChannelRequest
import pandora.l2cap.L2CAPProto.ReceiveRequest
import pandora.l2cap.L2CAPProto.ReceiveResponse
import pandora.l2cap.L2CAPProto.WaitConnectionRequest
import pandora.l2cap.L2CAPProto.WaitConnectionResponse
import pandora.l2cap.L2CAPProto.WaitDisconnectionRequest
@@ -139,7 +140,8 @@ public class DckL2capClientTest() : Closeable {
                Utils.BUMBLE_RANDOM_ADDRESS,
                BluetoothDevice.ADDRESS_TYPE_RANDOM
            )
        // WaitConnection

        Log.d(TAG, "testReceive: Connect L2CAP")
        val bluetoothSocket = createSocket(dckSpsm, remoteDevice)
        runBlocking {
            val waitFlow = flow { emit(waitConnection(dckSpsm, remoteDevice)) }
@@ -150,31 +152,30 @@ public class DckL2capClientTest() : Closeable {
        assertThat(connectionResponse.hasChannel()).isTrue()

        val channel = connectionResponse.channel
        val sampleData: ByteArray = "testReceive Sample Test Data".toByteArray()
        val sampleData = "cafe-baguette".toByteArray()

        scope.launch {
            val deadline =
                Deadline.after(CHANNEL_READ_TIMEOUT.inWholeMilliseconds, TimeUnit.MILLISECONDS)
            val receiveResponse =
        val receiveObserver = StreamObserverSpliterator<ReceiveResponse>()
        mBumble
                    .l2capBlocking()
                    .withDeadline(deadline)
                    .receive(ReceiveRequest.newBuilder().setChannel(channel).build())
            assertThat(receiveResponse.next().data).isEqualTo(ByteString.copyFrom(sampleData))
        }
        Log.d(TAG, "testReceive: write started")
            .l2cap()
            .receive(ReceiveRequest.newBuilder().setChannel(channel).build(), receiveObserver)

        Log.d(TAG, "testReceive: Send data from Android to Bumble")
        val outputStream = bluetoothSocket.outputStream
        outputStream.write(sampleData)
        outputStream.flush()
        Log.d(TAG, "testReceive: write completed")

        // WaitDisconnection
        Log.d(TAG, "testReceive: waitReceive data on Bumble")
        val receiveData = receiveObserver.iterator().next()
        assertThat(receiveData.data.toByteArray()).isEqualTo(sampleData)

        bluetoothSocket.close()
        Log.d(TAG, "testReceive: waitDisconnection")
        val waitDisconnectionRequest =
            WaitDisconnectionRequest.newBuilder().setChannel(channel).build()
        val disconnectionResponse =
            mBumble.l2capBlocking().waitDisconnection(waitDisconnectionRequest)
        assertThat(disconnectionResponse.hasSuccess()).isTrue()
        Log.d(TAG, "testReceive: done")
    }

    private fun readDckSpsm(gatt: BluetoothGatt) = runBlocking {
@@ -227,7 +228,6 @@ public class DckL2capClientTest() : Closeable {
        remoteDevice: BluetoothDevice,
        isSecure: Boolean = false
    ): BluetoothSocket {
        Log.d(TAG, "createSocket")
        var socket: BluetoothSocket
        var expectedType: Int
        if (isSecure) {