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