1 /* 2 * Copyright (C) 2017 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.bluetooth; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.Mockito.doNothing; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.bluetooth.BluetoothAdapter; 28 import android.bluetooth.BluetoothDevice; 29 import android.bluetooth.BluetoothProfile; 30 import android.content.Context; 31 import android.graphics.drawable.Drawable; 32 import android.media.AudioManager; 33 import android.util.Pair; 34 35 import com.android.settings.connecteddevice.DevicePreferenceCallback; 36 import com.android.settings.dashboard.DashboardFragment; 37 import com.android.settings.testutils.shadow.ShadowAudioManager; 38 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 39 import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager; 40 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 import org.robolectric.shadow.api.Shadow; 51 52 import java.util.ArrayList; 53 import java.util.Collection; 54 55 @RunWith(RobolectricTestRunner.class) 56 @Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class, 57 ShadowCachedBluetoothDeviceManager.class}) 58 public class ConnectedBluetoothDeviceUpdaterTest { 59 60 private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; 61 62 @Mock 63 private DashboardFragment mDashboardFragment; 64 @Mock 65 private DevicePreferenceCallback mDevicePreferenceCallback; 66 @Mock 67 private CachedBluetoothDevice mCachedBluetoothDevice; 68 @Mock 69 private BluetoothDevice mBluetoothDevice; 70 @Mock 71 private Drawable mDrawable; 72 73 private Context mContext; 74 private ConnectedBluetoothDeviceUpdater mBluetoothDeviceUpdater; 75 private Collection<CachedBluetoothDevice> mCachedDevices; 76 private AudioManager mAudioManager; 77 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 78 private ShadowCachedBluetoothDeviceManager mShadowCachedBluetoothDeviceManager; 79 80 @Before setUp()81 public void setUp() { 82 MockitoAnnotations.initMocks(this); 83 84 Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device"); 85 mContext = RuntimeEnvironment.application; 86 mAudioManager = mContext.getSystemService(AudioManager.class); 87 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); 88 mShadowBluetoothAdapter.setEnabled(true); 89 mShadowCachedBluetoothDeviceManager = Shadow.extract( 90 Utils.getLocalBtManager(mContext).getCachedDeviceManager()); 91 doReturn(mContext).when(mDashboardFragment).getContext(); 92 mCachedDevices = new ArrayList<>(); 93 mCachedDevices.add(mCachedBluetoothDevice); 94 95 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 96 when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); 97 when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); 98 mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices); 99 mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext, 100 mDevicePreferenceCallback, /* metricsCategory= */ 0)); 101 mBluetoothDeviceUpdater.setPrefContext(mContext); 102 doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); 103 doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); 104 } 105 106 @Test onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference()107 public void onAudioModeChanged_hfpDeviceConnected_notInCall_addPreference() { 108 mAudioManager.setMode(AudioManager.MODE_NORMAL); 109 when(mBluetoothDeviceUpdater. 110 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 111 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 112 113 mBluetoothDeviceUpdater.onAudioModeChanged(); 114 115 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 116 } 117 118 @Test onAudioModeChanged_hfpDeviceConnected_inCall_removePreference()119 public void onAudioModeChanged_hfpDeviceConnected_inCall_removePreference() { 120 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 121 when(mBluetoothDeviceUpdater. 122 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 123 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 124 125 mBluetoothDeviceUpdater.onAudioModeChanged(); 126 127 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 128 } 129 130 @Test onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference()131 public void onAudioModeChanged_a2dpDeviceConnected_notInCall_removePreference() { 132 mAudioManager.setMode(AudioManager.MODE_NORMAL); 133 when(mBluetoothDeviceUpdater. 134 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 135 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 136 137 mBluetoothDeviceUpdater.onAudioModeChanged(); 138 139 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 140 } 141 142 @Test onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference()143 public void onAudioModeChanged_a2dpDeviceConnected_inCall_addPreference() { 144 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 145 when(mBluetoothDeviceUpdater. 146 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 147 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 148 149 mBluetoothDeviceUpdater.onAudioModeChanged(); 150 151 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 152 } 153 154 @Test onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference()155 public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_addPreference() { 156 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 157 when(mBluetoothDeviceUpdater. 158 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 159 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 160 161 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 162 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 163 164 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 165 } 166 167 @Test onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference()168 public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovePreference() { 169 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 170 when(mBluetoothDeviceUpdater. 171 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 172 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 173 mCachedDevices.clear(); 174 175 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 176 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 177 178 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 179 } 180 181 @Test onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference()182 public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_removePreference() { 183 mAudioManager.setMode(AudioManager.MODE_NORMAL); 184 when(mBluetoothDeviceUpdater. 185 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 186 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 187 188 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 189 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 190 191 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 192 } 193 194 @Test onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference()195 public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_removePreference() { 196 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 197 when(mBluetoothDeviceUpdater. 198 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 199 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 200 201 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 202 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 203 204 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 205 } 206 207 @Test onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference()208 public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_addPreference() { 209 mAudioManager.setMode(AudioManager.MODE_NORMAL); 210 when(mBluetoothDeviceUpdater. 211 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 212 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 213 214 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 215 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 216 217 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 218 } 219 220 @Test onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_removePreference()221 public void onProfileConnectionStateChanged_ashaHearingAidConnected_inCall_removePreference() 222 { 223 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 224 when(mBluetoothDeviceUpdater. 225 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 226 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 227 228 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 229 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.HEARING_AID); 230 231 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 232 } 233 234 @Test onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_removePreference()235 public void onProfileConnectionStateChanged_ashaHearingAidConnected_notInCall_removePreference() 236 { 237 mAudioManager.setMode(AudioManager.MODE_NORMAL); 238 when(mBluetoothDeviceUpdater. 239 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 240 when(mCachedBluetoothDevice.isConnectedAshaHearingAidDevice()).thenReturn(true); 241 242 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 243 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.HEARING_AID); 244 245 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 246 } 247 248 @Test onProfileConnectionStateChanged_leAudioDeviceConnected_inCall_removesPreference()249 public void onProfileConnectionStateChanged_leAudioDeviceConnected_inCall_removesPreference() { 250 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 251 when(mBluetoothDeviceUpdater 252 .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 253 when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); 254 255 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 256 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); 257 258 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 259 } 260 261 @Test onProfileConnectionStateChanged_leAudioDeviceConnected_notInCall_removesPreference()262 public void onProfileConnectionStateChanged_leAudioDeviceConnected_notInCall_removesPreference() 263 { 264 mAudioManager.setMode(AudioManager.MODE_NORMAL); 265 when(mBluetoothDeviceUpdater 266 .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 267 when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); 268 269 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 270 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); 271 272 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 273 } 274 @Test onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovesPreference()275 public void onProfileConnectionStateChanged_deviceIsNotInList_inCall_invokesRemovesPreference() 276 { 277 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 278 when(mBluetoothDeviceUpdater 279 .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 280 when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); 281 mCachedDevices.clear(); 282 283 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 284 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); 285 286 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 287 } 288 289 @Test onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovesPreference()290 public void onProfileConnectionStateChanged_deviceIsNotInList_notInCall_invokesRemovesPreference 291 () { 292 mAudioManager.setMode(AudioManager.MODE_NORMAL); 293 when(mBluetoothDeviceUpdater 294 .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 295 when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true); 296 mCachedDevices.clear(); 297 298 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 299 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.LE_AUDIO); 300 301 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 302 } 303 304 @Test onProfileConnectionStateChanged_deviceDisconnected_removePreference()305 public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { 306 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 307 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); 308 309 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 310 } 311 312 @Test addPreference_addPreference_shouldHideSecondTarget()313 public void addPreference_addPreference_shouldHideSecondTarget() { 314 BluetoothDevicePreference btPreference = 315 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, 316 true, BluetoothDevicePreference.SortType.TYPE_DEFAULT); 317 mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, btPreference); 318 319 mBluetoothDeviceUpdater.addPreference(mCachedBluetoothDevice); 320 321 assertThat(btPreference.shouldHideSecondTarget()).isTrue(); 322 } 323 } 324