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

Commit 76c45b08 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Ensure looper finishes each task before sending another for...

Merge "Ensure looper finishes each task before sending another for PhonePolicyTest#testDisconnectNoAutoConnect. This prevents a race condition causing the test to be flaky." into sc-dev
parents 24a3d72e e082ea9a
Loading
Loading
Loading
Loading
+29 −20
Original line number Original line Diff line number Diff line
@@ -16,6 +16,9 @@


package com.android.bluetooth.btservice;
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 static org.mockito.Mockito.*;


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


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


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


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


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


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


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


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


        // Only calls setConnection on device connectionOrder.get(1) with STATE_CONNECTED
        // Only calls setConnection on device connectionOrder.get(1) with STATE_CONNECTED
        verify(mDatabaseManager, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(1)).setConnection(
        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.putExtra(BluetoothProfile.EXTRA_STATE, BluetoothProfile.STATE_DISCONNECTED);
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);
        waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());


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


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


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


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


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


        // Verify no one changes the priority of the failed profile
        // Verify no one changes the priority of the failed profile
        verify(mA2dpService, never()).setConnectionPolicy(eq(connectionOrder.get(0)), anyInt());
        verify(mA2dpService, never()).setConnectionPolicy(eq(connectionOrder.get(0)), anyInt());
@@ -360,7 +369,7 @@ public class PhonePolicyTest {
        updateProfileConnectionStateHelper(connectionOrder.get(0), BluetoothProfile.HEADSET,
        updateProfileConnectionStateHelper(connectionOrder.get(0), BluetoothProfile.HEADSET,
                BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);
                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
        // Send a connection success event for one profile again to trigger re-connect
        hsConnectedDevices.add(connectionOrder.get(0));
        hsConnectedDevices.add(connectionOrder.get(0));
@@ -389,7 +398,7 @@ public class PhonePolicyTest {
        BluetoothDevice a2dpNotConnectedDevice2 = null;
        BluetoothDevice a2dpNotConnectedDevice2 = null;


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


            // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles
            // 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<>();
        ArrayList<BluetoothDevice> a2dpConnectedDevices = new ArrayList<>();


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


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


        // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles are
        // 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() {
    public void testNoReconnectOnNoConnect_MultiDevice() {
        // Return a list of bonded devices (just one)
        // Return a list of bonded devices (just one)
        BluetoothDevice[] bondedDevices = new BluetoothDevice[2];
        BluetoothDevice[] bondedDevices = new BluetoothDevice[2];
        bondedDevices[0] = TestUtils.getTestDevice(mAdapter, 0);
        bondedDevices[0] = getTestDevice(mAdapter, 0);
        bondedDevices[1] = TestUtils.getTestDevice(mAdapter, 1);
        bondedDevices[1] = getTestDevice(mAdapter, 1);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);


        // Return PRIORITY_AUTO_CONNECT over HFP and A2DP. This would imply that the profiles are
        // 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() {
    public void testReconnectOnPartialConnect_MultiDevice() {
        // Return a list of bonded devices (just one)
        // Return a list of bonded devices (just one)
        BluetoothDevice[] bondedDevices = new BluetoothDevice[2];
        BluetoothDevice[] bondedDevices = new BluetoothDevice[2];
        bondedDevices[0] = TestUtils.getTestDevice(mAdapter, 0);
        bondedDevices[0] = getTestDevice(mAdapter, 0);
        bondedDevices[1] = TestUtils.getTestDevice(mAdapter, 1);
        bondedDevices[1] = getTestDevice(mAdapter, 1);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);
        when(mAdapterService.getBondedDevices()).thenReturn(bondedDevices);


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