1 /* 2 * Copyright (C) 2024 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.settings.accessibility; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.when; 21 22 import android.bluetooth.BluetoothDevice; 23 import android.content.Context; 24 import android.platform.test.annotations.EnableFlags; 25 import android.platform.test.flag.junit.SetFlagsRule; 26 27 import androidx.test.core.app.ApplicationProvider; 28 29 import com.android.settings.bluetooth.Utils; 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 import com.android.settingslib.search.SearchIndexableRaw; 35 36 import com.google.common.collect.ImmutableList; 37 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 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.annotation.Config; 46 47 import java.util.ArrayList; 48 import java.util.List; 49 50 /** Tests for {@link AvailableHearingDevicePreferenceController} */ 51 @RunWith(RobolectricTestRunner.class) 52 @Config(shadows = {ShadowBluetoothUtils.class}) 53 public class AvailableHearingDevicePreferenceControllerTest { 54 @Rule 55 public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); 56 57 private static final String PREFERENCE_KEY = "preference_key"; 58 private static final String DEVICE_NAME = "device"; 59 60 private Context mContext; 61 private AvailableHearingDevicePreferenceController mAvailableHearingDevicePreferenceController; 62 @Mock 63 private AvailableHearingDeviceUpdater mAvailableHearingDeviceUpdater; 64 @Mock 65 private CachedBluetoothDeviceManager mCachedDeviceManager; 66 @Mock 67 private LocalBluetoothManager mLocalBluetoothManager; 68 @Mock 69 private CachedBluetoothDevice mCachedDevice; 70 @Mock 71 private BluetoothDevice mDevice; 72 73 74 @Before setUp()75 public void setUp() { 76 MockitoAnnotations.initMocks(this); 77 78 mContext = ApplicationProvider.getApplicationContext(); 79 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBluetoothManager; 80 mLocalBluetoothManager = Utils.getLocalBtManager(mContext); 81 when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); 82 when(mCachedDevice.getDevice()).thenReturn(mDevice); 83 when(mCachedDevice.getName()).thenReturn(DEVICE_NAME); 84 when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn( 85 ImmutableList.of(mCachedDevice)); 86 87 mAvailableHearingDevicePreferenceController = 88 new AvailableHearingDevicePreferenceController(mContext, PREFERENCE_KEY); 89 mAvailableHearingDevicePreferenceController.init(mAvailableHearingDeviceUpdater); 90 } 91 92 @Test 93 @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) updateDynamicRawDataToIndex_isNotHearingAidDevice_deviceIsNotSearchable()94 public void updateDynamicRawDataToIndex_isNotHearingAidDevice_deviceIsNotSearchable() { 95 when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 96 when(mDevice.isConnected()).thenReturn(true); 97 when(mCachedDevice.isHearingAidDevice()).thenReturn(false); 98 List<SearchIndexableRaw> searchData = new ArrayList<>(); 99 100 mAvailableHearingDevicePreferenceController.updateDynamicRawDataToIndex(searchData); 101 102 assertThat(searchData).isEmpty(); 103 } 104 105 @Test 106 @EnableFlags(Flags.FLAG_FIX_A11Y_SETTINGS_SEARCH) updateDynamicRawDataToIndex_isHearingAidDevice_deviceIsSearchable()107 public void updateDynamicRawDataToIndex_isHearingAidDevice_deviceIsSearchable() { 108 when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 109 when(mDevice.isConnected()).thenReturn(true); 110 when(mCachedDevice.isHearingAidDevice()).thenReturn(true); 111 List<SearchIndexableRaw> searchData = new ArrayList<>(); 112 113 mAvailableHearingDevicePreferenceController.updateDynamicRawDataToIndex(searchData); 114 115 assertThat(searchData).isNotEmpty(); 116 assertThat(searchData.get(0).key).contains(DEVICE_NAME); 117 } 118 } 119