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

Commit e082ea9a authored by Rahul Sabnis's avatar Rahul Sabnis
Browse files

Ensure looper finishes each task before sending another for

PhonePolicyTest#testDisconnectNoAutoConnect. This prevents a race
condition causing the test to be flaky.

Tag: #feature
Bug: 191486788
Test: atest PhonePolicyTest#testDisconnectNoAutoConnect
Merged-In: I2baa86b67ded9f9051f4d3e1f4e2075b09419ffa
Change-Id: I2baa86b67ded9f9051f4d3e1f4e2075b09419ffa
parent 0d1dc0fe
Loading
Loading
Loading
Loading
+29 −20
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.bluetooth.btservice;

import static com.android.bluetooth.TestUtils.getTestDevice;
import static com.android.bluetooth.TestUtils.waitForLooperToFinishScheduledTask;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothA2dp;
@@ -84,7 +87,7 @@ public class PhonePolicyTest {
        mHandlerThread = new HandlerThread("PhonePolicyTestHandlerThread");
        mHandlerThread.start();
        // Mock the looper
        doReturn(mHandlerThread.getLooper()).when(mAdapterService).getMainLooper();
        when(mAdapterService.getMainLooper()).thenReturn(mHandlerThread.getLooper());
        // Tell the AdapterService that it is a mock (see isMock documentation)
        doReturn(true).when(mAdapterService).isMock();
        // Must be called to initialize services
@@ -95,7 +98,9 @@ public class PhonePolicyTest {

    @After
    public void tearDown() throws Exception {
        mHandlerThread.quit();
        if (mHandlerThread != null) {
            mHandlerThread.quitSafely();
        }
        TestUtils.clearAdapterService(mAdapterService);
    }

@@ -106,7 +111,7 @@ public class PhonePolicyTest {
     */
    @Test
    public void testProcessInitProfilePriorities() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        BluetoothDevice device = getTestDevice(mAdapter, 0);
        // Mock the HeadsetService to return unknown connection policy
        when(mHeadsetService.getConnectionPolicy(device))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);
@@ -148,7 +153,7 @@ public class PhonePolicyTest {
        when(mAdapterService.isQuietModeEnabled()).thenReturn(false);

        // Return a list of connection order
        BluetoothDevice bondedDevice = TestUtils.getTestDevice(mAdapter, 0);
        BluetoothDevice bondedDevice = getTestDevice(mAdapter, 0);
        when(mDatabaseManager.getMostRecentlyConnectedA2dpDevice()).thenReturn(bondedDevice);
        when(mAdapterService.getBondState(bondedDevice)).thenReturn(BluetoothDevice.BOND_BONDED);

@@ -179,10 +184,10 @@ public class PhonePolicyTest {

        // Return a list of connection order
        List<BluetoothDevice> connectionOrder = new ArrayList<>();
        connectionOrder.add(TestUtils.getTestDevice(mAdapter, 0));
        connectionOrder.add(TestUtils.getTestDevice(mAdapter, 1));
        connectionOrder.add(TestUtils.getTestDevice(mAdapter, 2));
        connectionOrder.add(TestUtils.getTestDevice(mAdapter, 3));
        connectionOrder.add(getTestDevice(mAdapter, 0));
        connectionOrder.add(getTestDevice(mAdapter, 1));
        connectionOrder.add(getTestDevice(mAdapter, 2));
        connectionOrder.add(getTestDevice(mAdapter, 3));

        when(mDatabaseManager.getMostRecentlyConnectedA2dpDevice()).thenReturn(
                connectionOrder.get(0));
@@ -202,6 +207,7 @@ public class PhonePolicyTest {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, connectionOrder.get(0));
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Only calls setConnection on device connectionOrder.get(0) with STATE_CONNECTED
        verify(mDatabaseManager, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).setConnection(
@@ -217,6 +223,7 @@ public class PhonePolicyTest {
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, connectionOrder.get(1));
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Only calls setConnection on device connectionOrder.get(1) with STATE_CONNECTED
        verify(mDatabaseManager, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(1)).setConnection(
@@ -235,6 +242,7 @@ public class PhonePolicyTest {
        intent.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify that we do not call setConnection, but instead setDisconnection on disconnect
        verify(mDatabaseManager, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(1)).setConnection(
@@ -247,6 +255,7 @@ public class PhonePolicyTest {
                BluetoothProfile.STATE_DISCONNECTED);
        updateProfileConnectionStateHelper(connectionOrder.get(1), BluetoothProfile.HEADSET,
                BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTING);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify we don't call deleteConnection as that only happens when we disconnect a2dp
        verify(mDatabaseManager, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(1)).setDisconnection(
@@ -265,7 +274,7 @@ public class PhonePolicyTest {
    public void testReconnectOnPartialConnect() {
        // Return a list of bonded devices (just one)
        BluetoothDevice[] bondedDevices = new BluetoothDevice[1];
        bondedDevices[0] = TestUtils.getTestDevice(mAdapter, 0);
        bondedDevices[0] = getTestDevice(mAdapter, 0);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);

        // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles are
@@ -305,7 +314,7 @@ public class PhonePolicyTest {
    @Test
    public void testReconnectOnPartialConnect_PreviousPartialFail() {
        List<BluetoothDevice> connectionOrder = new ArrayList<>();
        connectionOrder.add(TestUtils.getTestDevice(mAdapter, 0));
        connectionOrder.add(getTestDevice(mAdapter, 0));
        when(mDatabaseManager.getMostRecentlyConnectedA2dpDevice()).thenReturn(
                connectionOrder.get(0));

@@ -342,7 +351,7 @@ public class PhonePolicyTest {
        updateProfileConnectionStateHelper(connectionOrder.get(0), BluetoothProfile.A2DP,
                BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTING);

        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Verify no one changes the priority of the failed profile
        verify(mA2dpService, never()).setConnectionPolicy(eq(connectionOrder.get(0)), anyInt());
@@ -360,7 +369,7 @@ public class PhonePolicyTest {
        updateProfileConnectionStateHelper(connectionOrder.get(0), BluetoothProfile.HEADSET,
                BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);

        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());

        // Send a connection success event for one profile again to trigger re-connect
        hsConnectedDevices.add(connectionOrder.get(0));
@@ -389,7 +398,7 @@ public class PhonePolicyTest {
        BluetoothDevice a2dpNotConnectedDevice2 = null;

        for (int i = 0; i < kMaxTestDevices; i++) {
            BluetoothDevice testDevice = TestUtils.getTestDevice(mAdapter, i);
            BluetoothDevice testDevice = getTestDevice(mAdapter, i);
            testDevices[i] = testDevice;

            // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles
@@ -457,7 +466,7 @@ public class PhonePolicyTest {
        ArrayList<BluetoothDevice> a2dpConnectedDevices = new ArrayList<>();

        for (int i = 0; i < kMaxTestDevices; i++) {
            BluetoothDevice testDevice = TestUtils.getTestDevice(mAdapter, i);
            BluetoothDevice testDevice = getTestDevice(mAdapter, i);
            testDevices[i] = testDevice;

            // Connect HFP and A2DP for each device as appropriate.
@@ -595,7 +604,7 @@ public class PhonePolicyTest {
    public void testNoReconnectOnNoConnect() {
        // Return a list of bonded devices (just one)
        BluetoothDevice[] bondedDevices = new BluetoothDevice[1];
        bondedDevices[0] = TestUtils.getTestDevice(mAdapter, 0);
        bondedDevices[0] = getTestDevice(mAdapter, 0);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);

        // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles are
@@ -640,8 +649,8 @@ public class PhonePolicyTest {
    public void testNoReconnectOnNoConnect_MultiDevice() {
        // Return a list of bonded devices (just one)
        BluetoothDevice[] bondedDevices = new BluetoothDevice[2];
        bondedDevices[0] = TestUtils.getTestDevice(mAdapter, 0);
        bondedDevices[1] = TestUtils.getTestDevice(mAdapter, 1);
        bondedDevices[0] = getTestDevice(mAdapter, 0);
        bondedDevices[1] = getTestDevice(mAdapter, 1);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);

        // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles are
@@ -695,8 +704,8 @@ public class PhonePolicyTest {
    public void testReconnectOnPartialConnect_MultiDevice() {
        // Return a list of bonded devices (just one)
        BluetoothDevice[] bondedDevices = new BluetoothDevice[2];
        bondedDevices[0] = TestUtils.getTestDevice(mAdapter, 0);
        bondedDevices[1] = TestUtils.getTestDevice(mAdapter, 1);
        bondedDevices[0] = getTestDevice(mAdapter, 0);
        bondedDevices[1] = getTestDevice(mAdapter, 1);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);

        // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles are
@@ -742,7 +751,7 @@ public class PhonePolicyTest {
    @Test
    public void testNoSupportedUuids() {
        // Mock the HeadsetService to return undefined priority
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        BluetoothDevice device = getTestDevice(mAdapter, 0);
        when(mHeadsetService.getConnectionPolicy(device))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);