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.BluetoothDevice; 24 import android.content.Context; 25 26 import androidx.test.core.app.ApplicationProvider; 27 28 import com.android.settings.bluetooth.Utils; 29 import com.android.settings.connecteddevice.DevicePreferenceCallback; 30 import com.android.settings.testutils.shadow.ShadowBluetoothUtils; 31 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 32 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 33 import com.android.settingslib.bluetooth.LocalBluetoothManager; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.junit.MockitoJUnit; 41 import org.mockito.junit.MockitoRule; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.annotation.Config; 44 45 import java.util.ArrayList; 46 import java.util.List; 47 48 /** Tests for {@link AvailableHearingDeviceUpdater}. */ 49 @RunWith(RobolectricTestRunner.class) 50 @Config(shadows = {ShadowBluetoothUtils.class}) 51 public class AvailableHearingDeviceUpdaterTest { 52 @Rule 53 public MockitoRule mMockitoRule = MockitoJUnit.rule(); 54 55 private final Context mContext = ApplicationProvider.getApplicationContext(); 56 57 @Mock 58 private DevicePreferenceCallback mDevicePreferenceCallback; 59 @Mock 60 private CachedBluetoothDeviceManager mCachedDeviceManager; 61 @Mock 62 private LocalBluetoothManager mLocalBluetoothManager; 63 @Mock 64 private CachedBluetoothDevice mCachedBluetoothDevice; 65 @Mock 66 private BluetoothDevice mBluetoothDevice; 67 private AvailableHearingDeviceUpdater mUpdater; 68 69 @Before setUp()70 public void setUp() { 71 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager; 72 mLocalBluetoothManager = Utils.getLocalBtManager(mContext); 73 when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); 74 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 75 mUpdater = new AvailableHearingDeviceUpdater(mContext, 76 mDevicePreferenceCallback, /* metricsCategory= */ 0); 77 } 78 79 @Test isFilterMatch_connectedHearingDevice_returnTrue()80 public void isFilterMatch_connectedHearingDevice_returnTrue() { 81 CachedBluetoothDevice connectedHearingDevice = mCachedBluetoothDevice; 82 when(connectedHearingDevice.isHearingAidDevice()).thenReturn(true); 83 when(mBluetoothDevice.isConnected()).thenReturn(true); 84 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 85 when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn( 86 new ArrayList<>(List.of(connectedHearingDevice))); 87 88 assertThat(mUpdater.isFilterMatched(connectedHearingDevice)).isEqualTo(true); 89 } 90 91 @Test isFilterMatch_nonConnectedHearingDevice_returnFalse()92 public void isFilterMatch_nonConnectedHearingDevice_returnFalse() { 93 CachedBluetoothDevice nonConnectedHearingDevice = mCachedBluetoothDevice; 94 when(nonConnectedHearingDevice.isHearingAidDevice()).thenReturn(true); 95 when(mBluetoothDevice.isConnected()).thenReturn(false); 96 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 97 when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn( 98 new ArrayList<>(List.of(nonConnectedHearingDevice))); 99 100 assertThat(mUpdater.isFilterMatched(nonConnectedHearingDevice)).isEqualTo(false); 101 } 102 103 @Test isFilterMatch_connectedBondingHearingDevice_returnFalse()104 public void isFilterMatch_connectedBondingHearingDevice_returnFalse() { 105 CachedBluetoothDevice connectedBondingHearingDevice = mCachedBluetoothDevice; 106 when(connectedBondingHearingDevice.isHearingAidDevice()).thenReturn(true); 107 when(mBluetoothDevice.isConnected()).thenReturn(true); 108 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING); 109 when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn( 110 new ArrayList<>(List.of(connectedBondingHearingDevice))); 111 112 assertThat(mUpdater.isFilterMatched(connectedBondingHearingDevice)).isEqualTo(false); 113 } 114 115 @Test isFilterMatch_hearingDeviceNotInCachedDevicesList_returnFalse()116 public void isFilterMatch_hearingDeviceNotInCachedDevicesList_returnFalse() { 117 CachedBluetoothDevice notInCachedDevicesListDevice = mCachedBluetoothDevice; 118 when(notInCachedDevicesListDevice.isHearingAidDevice()).thenReturn(true); 119 when(mBluetoothDevice.isConnected()).thenReturn(true); 120 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 121 when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(new ArrayList<>()); 122 123 assertThat(mUpdater.isFilterMatched(notInCachedDevicesListDevice)).isEqualTo(false); 124 } 125 } 126