1 /* 2 * Copyright (C) 2022 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 import static org.robolectric.shadows.ShadowLooper.shadowMainLooper; 23 24 import android.bluetooth.BluetoothAdapter; 25 import android.bluetooth.BluetoothDevice; 26 import android.bluetooth.BluetoothProfile; 27 import android.content.Context; 28 29 import androidx.appcompat.app.AlertDialog; 30 import androidx.fragment.app.FragmentActivity; 31 import androidx.fragment.app.FragmentManager; 32 import androidx.test.core.app.ApplicationProvider; 33 34 import com.android.settings.bluetooth.Utils; 35 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 36 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 37 import com.android.settings.testutils.shadow.ShadowBluetoothUtils; 38 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 39 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 40 import com.android.settingslib.bluetooth.CsipSetCoordinatorProfile; 41 import com.android.settingslib.bluetooth.HearingAidInfo; 42 import com.android.settingslib.bluetooth.LocalBluetoothManager; 43 import com.android.settingslib.bluetooth.LocalBluetoothProfile; 44 45 import org.junit.Before; 46 import org.junit.Rule; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mock; 50 import org.mockito.junit.MockitoJUnit; 51 import org.mockito.junit.MockitoRule; 52 import org.robolectric.Robolectric; 53 import org.robolectric.RobolectricTestRunner; 54 import org.robolectric.annotation.Config; 55 import org.robolectric.shadow.api.Shadow; 56 57 import java.util.ArrayList; 58 import java.util.List; 59 60 /** Tests for {@link HearingAidUtils}. */ 61 @RunWith(RobolectricTestRunner.class) 62 @Config(shadows = { 63 com.android.settings.testutils.shadow.ShadowAlertDialogCompat.class, 64 com.android.settings.testutils.shadow.ShadowBluetoothAdapter.class, 65 com.android.settings.testutils.shadow.ShadowBluetoothUtils.class, 66 }) 67 public class HearingAidUtilsTest { 68 69 @Rule 70 public final MockitoRule mockito = MockitoJUnit.rule(); 71 private final Context mContext = ApplicationProvider.getApplicationContext(); 72 73 private static final String TEST_DEVICE_ADDRESS = "00:A1:A1:A1:A1:A1"; 74 private static final int TEST_LAUNCH_PAGE = 1; 75 76 @Mock 77 private CachedBluetoothDevice mCachedBluetoothDevice; 78 @Mock 79 private CachedBluetoothDevice mSubCachedBluetoothDevice; 80 @Mock 81 private LocalBluetoothManager mLocalBluetoothManager; 82 @Mock 83 private CachedBluetoothDeviceManager mCachedDeviceManager; 84 @Mock 85 private CsipSetCoordinatorProfile mCsipSetCoordinatorProfile; 86 private BluetoothDevice mBluetoothDevice; 87 private BluetoothAdapter mBluetoothAdapter; 88 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 89 private FragmentManager mFragmentManager; 90 91 @Before setUp()92 public void setUp() { 93 setupEnvironment(); 94 final FragmentActivity mActivity = Robolectric.setupActivity(FragmentActivity.class); 95 shadowMainLooper().idle(); 96 mFragmentManager = mActivity.getSupportFragmentManager(); 97 ShadowAlertDialogCompat.reset(); 98 when(mCachedBluetoothDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS); 99 } 100 101 @Test launchHearingAidPairingDialog_deviceIsNotConnectedAshaHearingAid_noDialog()102 public void launchHearingAidPairingDialog_deviceIsNotConnectedAshaHearingAid_noDialog() { 103 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(false); 104 105 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, mCachedBluetoothDevice, 106 TEST_LAUNCH_PAGE); 107 108 shadowMainLooper().idle(); 109 final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 110 assertThat(dialog).isNull(); 111 } 112 113 @Test launchHearingAidPairingDialog_deviceIsMonauralMode_noDialog()114 public void launchHearingAidPairingDialog_deviceIsMonauralMode_noDialog() { 115 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 116 when(mCachedBluetoothDevice.getDeviceMode()).thenReturn( 117 HearingAidInfo.DeviceMode.MODE_MONAURAL); 118 119 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, mCachedBluetoothDevice, 120 TEST_LAUNCH_PAGE); 121 122 shadowMainLooper().idle(); 123 final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 124 assertThat(dialog).isNull(); 125 } 126 127 @Test launchHearingAidPairingDialog_deviceHasSubDevice_noDialog()128 public void launchHearingAidPairingDialog_deviceHasSubDevice_noDialog() { 129 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 130 when(mCachedBluetoothDevice.getDeviceMode()).thenReturn( 131 HearingAidInfo.DeviceMode.MODE_BINAURAL); 132 when(mCachedBluetoothDevice.getSubDevice()).thenReturn(mSubCachedBluetoothDevice); 133 134 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, mCachedBluetoothDevice, 135 TEST_LAUNCH_PAGE); 136 137 shadowMainLooper().idle(); 138 final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 139 assertThat(dialog).isNull(); 140 } 141 142 @Test launchHearingAidPairingDialog_deviceIsInvalidSide_noDialog()143 public void launchHearingAidPairingDialog_deviceIsInvalidSide_noDialog() { 144 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 145 when(mCachedBluetoothDevice.getDeviceMode()).thenReturn( 146 HearingAidInfo.DeviceMode.MODE_BINAURAL); 147 when(mCachedBluetoothDevice.getDeviceSide()).thenReturn( 148 HearingAidInfo.DeviceSide.SIDE_INVALID); 149 150 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, mCachedBluetoothDevice, 151 TEST_LAUNCH_PAGE); 152 153 shadowMainLooper().idle(); 154 final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 155 assertThat(dialog).isNull(); 156 } 157 158 @Test launchHearingAidPairingDialog_deviceSupportsCsip_noDialog()159 public void launchHearingAidPairingDialog_deviceSupportsCsip_noDialog() { 160 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 161 when(mCachedBluetoothDevice.getDeviceMode()).thenReturn( 162 HearingAidInfo.DeviceMode.MODE_BINAURAL); 163 when(mCachedBluetoothDevice.getDeviceSide()).thenReturn( 164 HearingAidInfo.DeviceSide.SIDE_LEFT); 165 makeDeviceSupportCsip(); 166 167 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, mCachedBluetoothDevice, 168 TEST_LAUNCH_PAGE); 169 170 shadowMainLooper().idle(); 171 final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 172 assertThat(dialog).isNull(); 173 } 174 175 @Test launchHearingAidPairingDialog_dialogShown()176 public void launchHearingAidPairingDialog_dialogShown() { 177 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 178 when(mCachedBluetoothDevice.getDeviceMode()).thenReturn( 179 HearingAidInfo.DeviceMode.MODE_BINAURAL); 180 when(mCachedBluetoothDevice.getDeviceSide()).thenReturn( 181 HearingAidInfo.DeviceSide.SIDE_LEFT); 182 183 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, mCachedBluetoothDevice, 184 TEST_LAUNCH_PAGE); 185 186 shadowMainLooper().idle(); 187 final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog(); 188 assertThat(dialog.isShowing()).isTrue(); 189 } 190 makeDeviceSupportCsip()191 private void makeDeviceSupportCsip() { 192 List<LocalBluetoothProfile> uuids = new ArrayList<>(); 193 uuids.add(mCsipSetCoordinatorProfile); 194 when(mCachedBluetoothDevice.getProfiles()).thenReturn(uuids); 195 } 196 setupEnvironment()197 private void setupEnvironment() { 198 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager; 199 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 200 mLocalBluetoothManager = Utils.getLocalBtManager(mContext); 201 mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter); 202 mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS); 203 mShadowBluetoothAdapter.addSupportedProfiles(BluetoothProfile.HEARING_AID); 204 when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); 205 when(mCachedDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); 206 when(mCachedBluetoothDevice.getAddress()).thenReturn(TEST_DEVICE_ADDRESS); 207 } 208 } 209