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 TestUtils.startService(mServiceRule, A2dpSinkService.class); 65 mService = A2dpSinkService.getA2dpSinkService(); 66 Assert.assertNotNull(mService); 67 // Try getting the Bluetooth adapter 68 mAdapter = BluetoothAdapter.getDefaultAdapter(); 69 Assert.assertNotNull(mAdapter); 70 when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager); 71 } 72 73 @After tearDown()74 public void tearDown() throws Exception { 75 if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_a2dp_sink)) { 76 return; 77 } 78 TestUtils.stopService(mServiceRule, A2dpSinkService.class); 79 mService = A2dpSinkService.getA2dpSinkService(); 80 Assert.assertNull(mService); 81 TestUtils.clearAdapterService(mAdapterService); 82 } 83 makeBluetoothDevice(String address)84 private BluetoothDevice makeBluetoothDevice(String address) { 85 return mAdapter.getRemoteDevice(address); 86 } 87 88 /** 89 * Mock the priority of a bluetooth device 90 * 91 * @param device - The bluetooth device you wish to mock the priority of 92 * @param priority - The priority value you want the device to have 93 */ mockDevicePriority(BluetoothDevice device, int priority)94 private void mockDevicePriority(BluetoothDevice device, int priority) { 95 when(mDatabaseManager.getProfilePriority(device, BluetoothProfile.A2DP_SINK)) 96 .thenReturn(priority); 97 } 98 99 @Test testInitialize()100 public void testInitialize() { 101 Assert.assertNotNull(A2dpSinkService.getA2dpSinkService()); 102 } 103 104 /** 105 * Test that a PRIORITY_ON device is connected to 106 */ 107 @Test testConnect()108 public void testConnect() { 109 BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11"); 110 mockDevicePriority(device, BluetoothProfile.PRIORITY_ON); 111 Assert.assertTrue(mService.connect(device)); 112 } 113 114 /** 115 * Test that a PRIORITY_OFF device is not connected to 116 */ 117 @Test testConnectPriorityOffDevice()118 public void testConnectPriorityOffDevice() { 119 BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11"); 120 mockDevicePriority(device, BluetoothProfile.PRIORITY_OFF); 121 Assert.assertFalse(mService.connect(device)); 122 } 123 } 124