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

Commit ab793a1e authored by Pomai Ahlo's avatar Pomai Ahlo
Browse files

RfcommTest: Disable profiles

Disable HFP, A2DP, and HID profiles in the setup of the tests.

Bug: 331415222
Test: atest RfcommTest
Flag: TEST_ONLY
Change-Id: I1a01475921df3d5bebcafb83a80c6d374e847bff
parent e4e7b469
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.bluetooth

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
@@ -40,6 +41,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

@SuppressLint("MissingPermission")
@kotlinx.coroutines.ExperimentalCoroutinesApi
public class Host(context: Context) : Closeable {
    private val TAG = "PandoraHost"
@@ -54,6 +56,7 @@ public class Host(context: Context) : Closeable {
        val intentFilter = IntentFilter()
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
        intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST)
        intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)

        flow = intentFlow(context, intentFilter, scope).shareIn(scope, SharingStarted.Eagerly)
    }
@@ -113,11 +116,32 @@ public class Host(context: Context) : Closeable {
                        it.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothAdapter.ERROR) ==
                            BluetoothDevice.BOND_NONE
                    }
                    .first()
                Log.d(TAG, "removeBondAndVerify: done")
            }
        }
    }

    fun disconnectAndVerify(remoteDevice: BluetoothDevice) {
        Log.d(TAG, "disconnectAndVerify: $remoteDevice")
        runBlocking(scope.coroutineContext) {
            withTimeout(TIMEOUT) {
                assertThat(remoteDevice.disconnect()).isEqualTo(BluetoothStatusCodes.SUCCESS)
                flow
                    .filter { it.getAction() == BluetoothDevice.ACTION_ACL_DISCONNECTED }
                    .filter {
                        it.getIntExtra(
                            BluetoothDevice.EXTRA_TRANSPORT,
                            BluetoothDevice.TRANSPORT_AUTO,
                        ) == BluetoothDevice.TRANSPORT_BREDR
                    }
                    .filter { it.getBluetoothDeviceExtra() == remoteDevice }
                    .first()
                Log.d(TAG, "disconnectAndVerify: done")
            }
        }
    }

    fun Intent.getBluetoothDeviceExtra(): BluetoothDevice =
        this.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE, BluetoothDevice::class.java)!!

+37 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package android.bluetooth

import android.Manifest
import android.annotation.SuppressLint
import android.bluetooth.test_utils.EnableBluetoothRule
import android.content.Context
import androidx.test.core.app.ApplicationProvider
@@ -34,10 +35,16 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.timeout
import org.mockito.kotlin.verify
import pandora.RfcommProto
import pandora.RfcommProto.ServerId
import pandora.RfcommProto.StartServerRequest

@SuppressLint("MissingPermission")
@RunWith(AndroidJUnit4::class)
@ExperimentalCoroutinesApi
class RfcommTest {
@@ -52,7 +59,8 @@ class RfcommTest {
        AdoptShellPermissionsRule(
            InstrumentationRegistry.getInstrumentation().getUiAutomation(),
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_PRIVILEGED
            Manifest.permission.BLUETOOTH_PRIVILEGED,
            Manifest.permission.MODIFY_PHONE_STATE,
        )

    // Set up a Bumble Pandora device for the duration of the test.
@@ -63,12 +71,32 @@ class RfcommTest {
    private lateinit var mRemoteDevice: BluetoothDevice
    private lateinit var host: Host
    private var mConnectionCounter = 1
    private var mProfileServiceListener = mock<BluetoothProfile.ServiceListener>()

    @Before
    fun setUp() {
        mRemoteDevice = mBumble.remoteDevice
        host = Host(mContext)
        val bluetoothA2dp = getProfileProxy(mContext, BluetoothProfile.A2DP) as BluetoothA2dp
        bluetoothA2dp.setConnectionPolicy(
            mRemoteDevice,
            BluetoothProfile.CONNECTION_POLICY_FORBIDDEN,
        )
        val bluetoothHfp = getProfileProxy(mContext, BluetoothProfile.HEADSET) as BluetoothHeadset
        bluetoothHfp.setConnectionPolicy(
            mRemoteDevice,
            BluetoothProfile.CONNECTION_POLICY_FORBIDDEN,
        )
        val bluetoothHidHost =
            getProfileProxy(mContext, BluetoothProfile.HID_HOST) as BluetoothHidHost
        bluetoothHidHost.setConnectionPolicy(
            mRemoteDevice,
            BluetoothProfile.CONNECTION_POLICY_FORBIDDEN,
        )
        host.createBondAndVerify(mRemoteDevice)
        if (mRemoteDevice.isConnected) {
            host.disconnectAndVerify(mRemoteDevice)
        }
    }

    @After
@@ -313,6 +341,14 @@ class RfcommTest {
        }
    }

    private fun getProfileProxy(context: Context, profile: Int): BluetoothProfile {
        mAdapter.getProfileProxy(context, mProfileServiceListener, profile)
        val proxyCaptor = argumentCaptor<BluetoothProfile>()
        verify(mProfileServiceListener, timeout(GRPC_TIMEOUT.toMillis()))
            .onServiceConnected(eq(profile), proxyCaptor.capture())
        return proxyCaptor.lastValue
    }

    companion object {
        private val TAG = RfcommTest::class.java.getSimpleName()
        private val GRPC_TIMEOUT = Duration.ofSeconds(10)