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

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

Merge "Make sure connection requests are rejected if a device is PRIORITY_OFF" into qt-dev

parents 321587a2 a0fd735a
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -224,6 +224,10 @@ public class A2dpSinkService extends ProfileService {
            Log.d(TAG, " connect device: " + device
                    + ", InstanceMap start state: " + sb.toString());
        }
        if (getPriority(device) == BluetoothProfile.PRIORITY_OFF) {
            Log.w(TAG, "Connection not allowed: <" + device.getAddress() + "> is PRIORITY_OFF");
            return false;
        }
        A2dpSinkStateMachine stateMachine = getOrCreateStateMachine(device);
        if (stateMachine != null) {
            stateMachine.connect();
@@ -233,7 +237,6 @@ public class A2dpSinkService extends ProfileService {
            Log.e(TAG, "Maxed out on the number of allowed MAP connections. "
                    + "Connect request rejected on " + device);
            return false;

        }
    }

+4 −0
Original line number Diff line number Diff line
@@ -101,6 +101,10 @@ public class MapClientService extends ProfileService {
            Log.d(TAG, "MAP connect device: " + device
                    + ", InstanceMap start state: " + sb.toString());
        }
        if (getPriority(device) == BluetoothProfile.PRIORITY_OFF) {
            Log.w(TAG, "Connection not allowed: <" + device.getAddress() + "> is PRIORITY_OFF");
            return false;
        }
        MceStateMachine mapStateMachine = mMapInstanceMap.get(device);
        if (mapStateMachine == null) {
            // a map state machine instance doesn't exist yet, create a new one if we can.
+42 −0
Original line number Diff line number Diff line
@@ -15,7 +15,11 @@
 */
package com.android.bluetooth.a2dpsink;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;

import androidx.test.InstrumentationRegistry;
@@ -26,6 +30,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
import org.junit.Assert;
@@ -47,6 +52,7 @@ public class A2dpSinkServiceTest {
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Mock private AdapterService mAdapterService;
    @Mock private DatabaseManager mDatabaseManager;

    @Before
    public void setUp() throws Exception {
@@ -61,6 +67,7 @@ public class A2dpSinkServiceTest {
        // Try getting the Bluetooth adapter
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        Assert.assertNotNull(mAdapter);
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
    }

    @After
@@ -74,8 +81,43 @@ public class A2dpSinkServiceTest {
        TestUtils.clearAdapterService(mAdapterService);
    }

    private BluetoothDevice makeBluetoothDevice(String address) {
        return mAdapter.getRemoteDevice(address);
    }

    /**
     * Mock the priority of a bluetooth device
     *
     * @param device - The bluetooth device you wish to mock the priority of
     * @param priority - The priority value you want the device to have
     */
    private void mockDevicePriority(BluetoothDevice device, int priority) {
        when(mDatabaseManager.getProfilePriority(device, BluetoothProfile.A2DP_SINK))
                .thenReturn(priority);
    }

    @Test
    public void testInitialize() {
        Assert.assertNotNull(A2dpSinkService.getA2dpSinkService());
    }

    /**
     * Test that a PRIORITY_ON device is connected to
     */
    @Test
    public void testConnect() {
        BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11");
        mockDevicePriority(device, BluetoothProfile.PRIORITY_ON);
        Assert.assertTrue(mService.connect(device));
    }

    /**
     * Test that a PRIORITY_OFF device is not connected to
     */
    @Test
    public void testConnectPriorityOffDevice() {
        BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11");
        mockDevicePriority(device, BluetoothProfile.PRIORITY_OFF);
        Assert.assertFalse(mService.connect(device));
    }
}
+39 −1
Original line number Diff line number Diff line
@@ -16,8 +16,11 @@

package com.android.bluetooth.mapclient;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;

import androidx.test.InstrumentationRegistry;
@@ -28,6 +31,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
import org.junit.Assert;
@@ -53,6 +57,7 @@ public class MapClientTest {

    @Mock private AdapterService mAdapterService;
    @Mock private MnsService mMockMnsService;
    @Mock private DatabaseManager mDatabaseManager;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

@@ -69,6 +74,7 @@ public class MapClientTest {
        Assert.assertNotNull(mService);
        cleanUpInstanceMap();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
    }

    @After
@@ -92,6 +98,17 @@ public class MapClientTest {
        Assert.assertTrue(mService.getInstanceMap().isEmpty());
    }

    /**
     * Mock the priority of a bluetooth device
     *
     * @param device - The bluetooth device you wish to mock the priority of
     * @param priority - The priority value you want the device to have
     */
    private void mockDevicePriority(BluetoothDevice device, int priority) {
        when(mDatabaseManager.getProfilePriority(device, BluetoothProfile.MAP_CLIENT))
                .thenReturn(priority);
    }

    @Test
    public void testInitialize() {
        Assert.assertNotNull(MapClientService.getMapClientService());
@@ -107,6 +124,7 @@ public class MapClientTest {
        Assert.assertNull(mService.getInstanceMap().get(device));

        // connect a bluetooth device
        mockDevicePriority(device, BluetoothProfile.PRIORITY_ON);
        Assert.assertTrue(mService.connect(device));

        // is the statemachine created
@@ -115,6 +133,25 @@ public class MapClientTest {
        Assert.assertNotNull(map.get(device));
    }

    /**
     * Test that a PRIORITY_OFF device is not connected to
     */
    @Test
    public void testConnectPriorityOffDevice() {
        // make sure there is no statemachine already defined for this device
        BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11");
        Assert.assertNull(mService.getInstanceMap().get(device));

        // connect a bluetooth device
        mockDevicePriority(device, BluetoothProfile.PRIORITY_OFF);
        Assert.assertFalse(mService.connect(device));

        // is the statemachine created
        Map<BluetoothDevice, MceStateMachine> map = mService.getInstanceMap();
        Assert.assertEquals(0, map.size());
        Assert.assertNull(map.get(device));
    }

    /**
     * Test connecting MAXIMUM_CONNECTED_DEVICES devices.
     */
@@ -132,8 +169,9 @@ public class MapClientTest {
            Assert.assertNull(mService.getInstanceMap().get(d));
        }

        // run the test - connect all devices
        // run the test - connect all devices, set their priorities to on
        for (BluetoothDevice d : list) {
            mockDevicePriority(d, BluetoothProfile.PRIORITY_ON);
            Assert.assertTrue(mService.connect(d));
        }