1 /* 2 * Copyright 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.bluetooth.a2dpsink; 17 18 import static org.mockito.Mockito.*; 19 20 import android.bluetooth.BluetoothAdapter; 21 import android.bluetooth.BluetoothDevice; 22 import android.bluetooth.BluetoothProfile; 23 import android.content.Context; 24 25 import androidx.test.InstrumentationRegistry; 26 import androidx.test.filters.MediumTest; 27 import androidx.test.rule.ServiceTestRule; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import com.android.bluetooth.R; 31 import com.android.bluetooth.TestUtils; 32 import com.android.bluetooth.btservice.AdapterService; 33 import com.android.bluetooth.btservice.storage.DatabaseManager; 34 35 import org.junit.After; 36 import org.junit.Assert; 37 import org.junit.Assume; 38 import org.junit.Before; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 45 @MediumTest 46 @RunWith(AndroidJUnit4.class) 47 public class A2dpSinkServiceTest { 48 private A2dpSinkService mService = null; 49 private BluetoothAdapter mAdapter = null; 50 private Context mTargetContext; 51 52 @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); 53 54 @Mock private AdapterService mAdapterService; 55 @Mock private DatabaseManager mDatabaseManager; 56 57 @Before setUp()58 public void setUp() throws Exception { 59 mTargetContext = InstrumentationRegistry.getTargetContext(); 60 Assume.assumeTrue("Ignore test when A2dpSinkService is not enabled", 61 mTargetContext.getResources().getBoolean(R.bool.profile_supported_a2dp_sink)); 62 MockitoAnnotations.initMocks(this); 63 TestUtils.setAdapterService(mAdapterService); 64 doReturn(mDatabaseManager).when(mAdapterService).getDatabase(); 65 doReturn(true, false).when(mAdapterService).isStartedProfile(anyString()); 66 setMaxConnectedAudioDevices(1); 67 TestUtils.startService(mServiceRule, A2dpSinkService.class); 68 mService = A2dpSinkService.getA2dpSinkService(); 69 Assert.assertNotNull(mService); 70 // Try getting the Bluetooth adapter 71 mAdapter = BluetoothAdapter.getDefaultAdapter(); 72 Assert.assertNotNull(mAdapter); 73 } 74 75 @After tearDown()76 public void tearDown() throws Exception { 77 if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_a2dp_sink)) { 78 return; 79 } 80 TestUtils.stopService(mServiceRule, A2dpSinkService.class); 81 mService = A2dpSinkService.getA2dpSinkService(); 82 Assert.assertNull(mService); 83 TestUtils.clearAdapterService(mAdapterService); 84 } 85 makeBluetoothDevice(String address)86 private BluetoothDevice makeBluetoothDevice(String address) { 87 return mAdapter.getRemoteDevice(address); 88 } 89 90 /** 91 * Set the upper connected device limit 92 */ setMaxConnectedAudioDevices(int maxConnectedAudioDevices)93 private void setMaxConnectedAudioDevices(int maxConnectedAudioDevices) { 94 when(mAdapterService.getMaxConnectedAudioDevices()).thenReturn(maxConnectedAudioDevices); 95 } 96 97 /** 98 * Mock the priority of a bluetooth device 99 * 100 * @param device - The bluetooth device you wish to mock the priority of 101 * @param priority - The priority value you want the device to have 102 */ mockDevicePriority(BluetoothDevice device, int priority)103 private void mockDevicePriority(BluetoothDevice device, int priority) { 104 when(mDatabaseManager.getProfileConnectionPolicy(device, BluetoothProfile.A2DP_SINK)) 105 .thenReturn(priority); 106 } 107 108 @Test testInitialize()109 public void testInitialize() { 110 Assert.assertNotNull(A2dpSinkService.getA2dpSinkService()); 111 } 112 113 /** 114 * Test that a PRIORITY_ON device is connected to 115 */ 116 @Test testConnect()117 public void testConnect() { 118 BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11"); 119 mockDevicePriority(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED); 120 Assert.assertTrue(mService.connect(device)); 121 } 122 123 /** 124 * Test that a PRIORITY_OFF device is not connected to 125 */ 126 @Test testConnectPriorityOffDevice()127 public void testConnectPriorityOffDevice() { 128 BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11"); 129 mockDevicePriority(device, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN); 130 Assert.assertFalse(mService.connect(device)); 131 } 132 133 /** 134 * Test that we can connect multiple devices 135 */ 136 @Test testConnectMultipleDevices()137 public void testConnectMultipleDevices() { 138 setMaxConnectedAudioDevices(5); 139 140 BluetoothDevice device1 = makeBluetoothDevice("11:11:11:11:11:11"); 141 BluetoothDevice device2 = makeBluetoothDevice("22:22:22:22:22:22"); 142 BluetoothDevice device3 = makeBluetoothDevice("33:33:33:33:33:33"); 143 BluetoothDevice device4 = makeBluetoothDevice("44:44:44:44:44:44"); 144 BluetoothDevice device5 = makeBluetoothDevice("55:55:55:55:55:55"); 145 146 mockDevicePriority(device1, BluetoothProfile.CONNECTION_POLICY_ALLOWED); 147 mockDevicePriority(device2, BluetoothProfile.CONNECTION_POLICY_ALLOWED); 148 mockDevicePriority(device3, BluetoothProfile.CONNECTION_POLICY_ALLOWED); 149 mockDevicePriority(device4, BluetoothProfile.CONNECTION_POLICY_ALLOWED); 150 mockDevicePriority(device5, BluetoothProfile.CONNECTION_POLICY_ALLOWED); 151 152 Assert.assertTrue(mService.connect(device1)); 153 Assert.assertTrue(mService.connect(device2)); 154 Assert.assertTrue(mService.connect(device3)); 155 Assert.assertTrue(mService.connect(device4)); 156 Assert.assertTrue(mService.connect(device5)); 157 } 158 } 159