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 17 package com.android.settings.bluetooth; 18 19 import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP; 20 import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceInputRoutingController.KEY_HEARING_DEVICE_INPUT_ROUTING; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.bluetooth.BluetoothDevice; 30 import android.media.AudioDeviceInfo; 31 import android.media.AudioManager; 32 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceCategory; 35 import androidx.test.core.app.ApplicationProvider; 36 37 import com.android.settings.R; 38 import com.android.settings.bluetooth.HearingDeviceInputRoutingPreference.InputRoutingValue; 39 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 40 import com.android.settingslib.bluetooth.HapClientProfile; 41 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.Spy; 47 import org.mockito.junit.MockitoJUnit; 48 import org.mockito.junit.MockitoRule; 49 import org.robolectric.RobolectricTestRunner; 50 51 import java.util.Collections; 52 import java.util.List; 53 import java.util.Set; 54 55 /** Tests for {@link BluetoothDetailsHearingDeviceInputRoutingController}. */ 56 57 @RunWith(RobolectricTestRunner.class) 58 public class BluetoothDetailsHearingDeviceInputRoutingControllerTest extends 59 BluetoothDetailsControllerTestBase { 60 @Rule 61 public final MockitoRule mockito = MockitoJUnit.rule(); 62 63 private static final String TEST_ADDRESS = "55:66:77:88:99:AA"; 64 private static final String TEST_ADDRESS_2 = "55:66:77:88:99:BB"; 65 66 @Mock 67 private BluetoothDevice mBluetoothDevice; 68 @Mock 69 private HapClientProfile mHapClientProfile; 70 @Mock 71 private CachedBluetoothDevice mMemberCachedDevice; 72 @Spy 73 private AudioManager mAudioManager; 74 75 private BluetoothDetailsHearingDeviceInputRoutingController mController; 76 77 @Override setUp()78 public void setUp() { 79 super.setUp(); 80 81 mContext = spy(ApplicationProvider.getApplicationContext()); 82 mAudioManager = spy(mContext.getSystemService(AudioManager.class)); 83 when(mContext.getSystemService(AudioManager.class)).thenReturn(mAudioManager); 84 setupDevice(makeDefaultDeviceConfig()); 85 when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice); 86 PreferenceCategory deviceControls = new PreferenceCategory(mContext); 87 deviceControls.setKey(KEY_HEARING_DEVICE_GROUP); 88 mScreen.addPreference(deviceControls); 89 mController = new BluetoothDetailsHearingDeviceInputRoutingController(mContext, 90 mFragment, mCachedDevice, mLifecycle); 91 } 92 93 @Test init_getExpectedPreference()94 public void init_getExpectedPreference() { 95 mController.init(mScreen); 96 97 Preference pref = mScreen.findPreference(KEY_HEARING_DEVICE_INPUT_ROUTING); 98 assertThat(pref.getKey()).isEqualTo(KEY_HEARING_DEVICE_INPUT_ROUTING); 99 } 100 101 @Test init_setPreferredMicrophoneTrue_expectedSummary()102 public void init_setPreferredMicrophoneTrue_expectedSummary() { 103 when(mBluetoothDevice.isMicrophonePreferredForCalls()).thenReturn(true); 104 105 mController.init(mScreen); 106 107 Preference pref = mScreen.findPreference(KEY_HEARING_DEVICE_INPUT_ROUTING); 108 assertThat(pref.getSummary().toString()).isEqualTo(mContext.getString( 109 R.string.bluetooth_hearing_device_input_routing_hearing_device_option)); 110 } 111 112 @Test init_setPreferredMicrophoneFalse_expectedSummary()113 public void init_setPreferredMicrophoneFalse_expectedSummary() { 114 when(mBluetoothDevice.isMicrophonePreferredForCalls()).thenReturn(false); 115 mController.init(mScreen); 116 117 Preference pref = mScreen.findPreference(KEY_HEARING_DEVICE_INPUT_ROUTING); 118 assertThat(pref.getSummary().toString()).isEqualTo(mContext.getString( 119 R.string.bluetooth_hearing_device_input_routing_builtin_option)); 120 } 121 122 @Test onInputRoutingUpdated_hearingDevice_setMicrophonePreferredForCallsTrue()123 public void onInputRoutingUpdated_hearingDevice_setMicrophonePreferredForCallsTrue() { 124 mController.init(mScreen); 125 126 mController.onInputRoutingUpdated(InputRoutingValue.HEARING_DEVICE); 127 128 verify(mBluetoothDevice).setMicrophonePreferredForCalls(true); 129 } 130 131 @Test onInputRoutingUpdated_builtin_setMicrophonePreferredForCallsFalse()132 public void onInputRoutingUpdated_builtin_setMicrophonePreferredForCallsFalse() { 133 mController.init(mScreen); 134 135 mController.onInputRoutingUpdated(InputRoutingValue.BUILTIN_MIC); 136 137 verify(mBluetoothDevice).setMicrophonePreferredForCalls(false); 138 } 139 140 @Test isAvailable_validInput_supportHapProfile_returnTrue()141 public void isAvailable_validInput_supportHapProfile_returnTrue() { 142 when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS); 143 AudioDeviceInfo[] mockInfo = new AudioDeviceInfo[] {mockTestAddressInfo(TEST_ADDRESS)}; 144 when(mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).thenReturn(mockInfo); 145 when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile)); 146 147 assertThat(mController.isAvailable()).isTrue(); 148 } 149 150 @Test isAvailable_notSupportHapProfile_returnFalse()151 public void isAvailable_notSupportHapProfile_returnFalse() { 152 when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS); 153 AudioDeviceInfo[] mockInfo = new AudioDeviceInfo[] {mockTestAddressInfo(TEST_ADDRESS)}; 154 when(mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).thenReturn(mockInfo); 155 when(mCachedDevice.getProfiles()).thenReturn(Collections.emptyList()); 156 157 assertThat(mController.isAvailable()).isFalse(); 158 } 159 160 @Test isAvailable_notValidInputDevice_returnFalse()161 public void isAvailable_notValidInputDevice_returnFalse() { 162 when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS); 163 when(mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).thenReturn( 164 new AudioDeviceInfo[] {}); 165 when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile)); 166 167 assertThat(mController.isAvailable()).isFalse(); 168 } 169 170 @Test isAvailable_validInputMember_supportHapProfile_returnTrue()171 public void isAvailable_validInputMember_supportHapProfile_returnTrue() { 172 when(mCachedDevice.getMemberDevice()).thenReturn(Set.of(mMemberCachedDevice)); 173 when(mCachedDevice.getAddress()).thenReturn(TEST_ADDRESS); 174 when(mMemberCachedDevice.getAddress()).thenReturn(TEST_ADDRESS_2); 175 AudioDeviceInfo[] mockInfo = new AudioDeviceInfo[] {mockTestAddressInfo(TEST_ADDRESS_2)}; 176 when(mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).thenReturn(mockInfo); 177 when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile)); 178 179 assertThat(mController.isAvailable()).isTrue(); 180 } 181 mockTestAddressInfo(String address)182 private AudioDeviceInfo mockTestAddressInfo(String address) { 183 final AudioDeviceInfo info = mock(AudioDeviceInfo.class); 184 when(info.getType()).thenReturn(AudioDeviceInfo.TYPE_BLE_HEADSET); 185 when(info.getAddress()).thenReturn(address); 186 return info; 187 } 188 } 189