1 /* 2 * Copyright (C) 2023 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 17 package com.android.settings.accessibility; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.when; 22 23 import android.bluetooth.BluetoothAdapter; 24 import android.bluetooth.BluetoothDevice; 25 import android.bluetooth.BluetoothProfile; 26 import android.content.Context; 27 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.settings.bluetooth.Utils; 31 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 32 import com.android.settings.testutils.shadow.ShadowBluetoothUtils; 33 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 34 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 35 import com.android.settingslib.bluetooth.HapClientProfile; 36 import com.android.settingslib.bluetooth.HearingAidProfile; 37 import com.android.settingslib.bluetooth.LocalBluetoothManager; 38 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 39 40 import org.junit.Before; 41 import org.junit.Rule; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.junit.MockitoJUnit; 46 import org.mockito.junit.MockitoRule; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.annotation.Config; 49 import org.robolectric.shadow.api.Shadow; 50 51 import java.util.ArrayList; 52 import java.util.Collections; 53 54 /** Tests for {@link HearingAidHelper}. */ 55 @RunWith(RobolectricTestRunner.class) 56 @Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothUtils.class}) 57 public class HearingAidHelperTest { 58 @Rule 59 public final MockitoRule mockito = MockitoJUnit.rule(); 60 private final Context mContext = ApplicationProvider.getApplicationContext(); 61 private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1"; 62 63 @Mock 64 private LocalBluetoothManager mLocalBluetoothManager; 65 @Mock 66 private LocalBluetoothProfileManager mLocalBluetoothProfileManager; 67 @Mock 68 private CachedBluetoothDeviceManager mCachedDeviceManager; 69 @Mock 70 private CachedBluetoothDevice mCachedBluetoothDevice; 71 @Mock 72 private HearingAidProfile mHearingAidProfile; 73 @Mock 74 private HapClientProfile mHapClientProfile; 75 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 76 private BluetoothAdapter mBluetoothAdapter; 77 private BluetoothDevice mBluetoothDevice; 78 private HearingAidHelper mHelper; 79 80 @Before setUp()81 public void setUp() { 82 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager; 83 mLocalBluetoothManager = Utils.getLocalBtManager(mContext); 84 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 85 mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter); 86 mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS); 87 when(mCachedBluetoothDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS); 88 when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); 89 when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); 90 when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager); 91 when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile); 92 when(mLocalBluetoothProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile); 93 94 mHelper = new HearingAidHelper(mContext); 95 } 96 97 @Test isHearingAidSupported_ashaSupported_returnTrue()98 public void isHearingAidSupported_ashaSupported_returnTrue() { 99 mShadowBluetoothAdapter.clearSupportedProfiles(); 100 mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID); 101 102 assertThat(mHelper.isHearingAidSupported()).isTrue(); 103 } 104 105 @Test isHearingAidSupported_hapSupported_returnTrue()106 public void isHearingAidSupported_hapSupported_returnTrue() { 107 mShadowBluetoothAdapter.clearSupportedProfiles(); 108 mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HAP_CLIENT); 109 110 assertThat(mHelper.isHearingAidSupported()).isTrue(); 111 } 112 113 @Test isHearingAidSupported_unsupported_returnFalse()114 public void isHearingAidSupported_unsupported_returnFalse() { 115 mShadowBluetoothAdapter.clearSupportedProfiles(); 116 117 assertThat(mHelper.isHearingAidSupported()).isFalse(); 118 } 119 120 @Test isAllHearingAidRelatedProfilesReady_allReady_returnTrue()121 public void isAllHearingAidRelatedProfilesReady_allReady_returnTrue() { 122 when(mHearingAidProfile.isProfileReady()).thenReturn(true); 123 when(mHapClientProfile.isProfileReady()).thenReturn(true); 124 125 assertThat(mHelper.isAllHearingAidRelatedProfilesReady()).isTrue(); 126 } 127 128 @Test isAllHearingAidRelatedProfilesReady_notFullReady_returnFalse()129 public void isAllHearingAidRelatedProfilesReady_notFullReady_returnFalse() { 130 when(mHearingAidProfile.isProfileReady()).thenReturn(false); 131 when(mHapClientProfile.isProfileReady()).thenReturn(true); 132 133 assertThat(mHelper.isAllHearingAidRelatedProfilesReady()).isFalse(); 134 } 135 136 @Test getConnectedHearingAidDeviceList_oneDeviceAdded_getOneDevice()137 public void getConnectedHearingAidDeviceList_oneDeviceAdded_getOneDevice() { 138 mBluetoothAdapter.enable(); 139 mShadowBluetoothAdapter.clearSupportedProfiles(); 140 mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID); 141 when(mHearingAidProfile.getConnectedDevices()).thenReturn(new ArrayList<>( 142 Collections.singletonList(mBluetoothDevice))); 143 144 assertThat(mHelper.getConnectedHearingAidDeviceList().size()).isEqualTo(1); 145 } 146 147 @Test getConnectedHearingAidDeviceList_oneSubDeviceAdded_getZeroDevice()148 public void getConnectedHearingAidDeviceList_oneSubDeviceAdded_getZeroDevice() { 149 mBluetoothAdapter.enable(); 150 mShadowBluetoothAdapter.clearSupportedProfiles(); 151 mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID); 152 when(mHearingAidProfile.getConnectedDevices()).thenReturn(new ArrayList<>( 153 Collections.singletonList(mBluetoothDevice))); 154 when(mLocalBluetoothManager.getCachedDeviceManager().isSubDevice( 155 mBluetoothDevice)).thenReturn(true); 156 157 assertThat(mHelper.getConnectedHearingAidDeviceList().size()).isEqualTo(0); 158 } 159 160 @Test getConnectedHearingAidDevice_getExpectedCachedBluetoothDevice()161 public void getConnectedHearingAidDevice_getExpectedCachedBluetoothDevice() { 162 mBluetoothAdapter.enable(); 163 mShadowBluetoothAdapter.clearSupportedProfiles(); 164 mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID); 165 when(mHearingAidProfile.getConnectedDevices()).thenReturn(new ArrayList<>( 166 Collections.singletonList(mBluetoothDevice))); 167 168 assertThat(mHelper.getConnectedHearingAidDevice()).isEqualTo(mCachedBluetoothDevice); 169 assertThat(mCachedBluetoothDevice.getAddress()).isEqualTo(mBluetoothDevice.getAddress()); 170 } 171 } 172