Loading android/app/src/com/android/bluetooth/a2dpsink/A2dpSinkService.java +4 −1 Original line number Diff line number Diff line Loading @@ -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(); Loading @@ -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; } } Loading android/app/src/com/android/bluetooth/mapclient/MapClientService.java +4 −0 Original line number Diff line number Diff line Loading @@ -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. Loading android/app/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkServiceTest.java +42 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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 { Loading @@ -61,6 +67,7 @@ public class A2dpSinkServiceTest { // Try getting the Bluetooth adapter mAdapter = BluetoothAdapter.getDefaultAdapter(); Assert.assertNotNull(mAdapter); when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager); } @After Loading @@ -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)); } } android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientTest.java +39 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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(); Loading @@ -69,6 +74,7 @@ public class MapClientTest { Assert.assertNotNull(mService); cleanUpInstanceMap(); mAdapter = BluetoothAdapter.getDefaultAdapter(); when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager); } @After Loading @@ -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()); Loading @@ -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 Loading @@ -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. */ Loading @@ -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)); } Loading Loading
android/app/src/com/android/bluetooth/a2dpsink/A2dpSinkService.java +4 −1 Original line number Diff line number Diff line Loading @@ -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(); Loading @@ -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; } } Loading
android/app/src/com/android/bluetooth/mapclient/MapClientService.java +4 −0 Original line number Diff line number Diff line Loading @@ -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. Loading
android/app/tests/unit/src/com/android/bluetooth/a2dpsink/A2dpSinkServiceTest.java +42 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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 { Loading @@ -61,6 +67,7 @@ public class A2dpSinkServiceTest { // Try getting the Bluetooth adapter mAdapter = BluetoothAdapter.getDefaultAdapter(); Assert.assertNotNull(mAdapter); when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager); } @After Loading @@ -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)); } }
android/app/tests/unit/src/com/android/bluetooth/mapclient/MapClientTest.java +39 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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; Loading @@ -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(); Loading @@ -69,6 +74,7 @@ public class MapClientTest { Assert.assertNotNull(mService); cleanUpInstanceMap(); mAdapter = BluetoothAdapter.getDefaultAdapter(); when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager); } @After Loading @@ -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()); Loading @@ -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 Loading @@ -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. */ Loading @@ -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)); } Loading