1 /* 2 * Copyright 2018 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 org.mockito.ArgumentMatchers.any; 19 import static org.mockito.Mockito.doNothing; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.bluetooth.BluetoothAdapter; 26 import android.bluetooth.BluetoothDevice; 27 import android.bluetooth.BluetoothProfile; 28 import android.content.Context; 29 import android.graphics.drawable.Drawable; 30 import android.media.AudioManager; 31 import android.util.Pair; 32 33 import com.android.settings.connecteddevice.DevicePreferenceCallback; 34 import com.android.settings.dashboard.DashboardFragment; 35 import com.android.settings.testutils.shadow.ShadowAudioManager; 36 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 37 import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager; 38 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 import org.robolectric.shadow.api.Shadow; 49 50 import java.util.ArrayList; 51 import java.util.Collection; 52 53 @RunWith(RobolectricTestRunner.class) 54 @Config(shadows = {ShadowAudioManager.class, ShadowBluetoothAdapter.class, 55 ShadowCachedBluetoothDeviceManager.class}) 56 public class AvailableMediaBluetoothDeviceUpdaterTest { 57 private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; 58 59 @Mock 60 private DashboardFragment mDashboardFragment; 61 @Mock 62 private DevicePreferenceCallback mDevicePreferenceCallback; 63 @Mock 64 private CachedBluetoothDevice mCachedBluetoothDevice; 65 @Mock 66 private BluetoothDevice mBluetoothDevice; 67 @Mock 68 private Drawable mDrawable; 69 70 private Context mContext; 71 private AvailableMediaBluetoothDeviceUpdater mBluetoothDeviceUpdater; 72 private Collection<CachedBluetoothDevice> mCachedDevices; 73 private AudioManager mAudioManager; 74 private BluetoothDevicePreference mPreference; 75 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 76 private ShadowCachedBluetoothDeviceManager mShadowCachedBluetoothDeviceManager; 77 78 @Before setUp()79 public void setUp() { 80 MockitoAnnotations.initMocks(this); 81 82 mContext = RuntimeEnvironment.application; 83 mAudioManager = mContext.getSystemService(AudioManager.class); 84 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); 85 mShadowBluetoothAdapter.setEnabled(true); 86 mShadowCachedBluetoothDeviceManager = Shadow.extract( 87 Utils.getLocalBtManager(mContext).getCachedDeviceManager()); 88 mCachedDevices = new ArrayList<>(); 89 mShadowCachedBluetoothDeviceManager.setCachedDevicesCopy(mCachedDevices); 90 Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device"); 91 92 doReturn(mContext).when(mDashboardFragment).getContext(); 93 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 94 when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); 95 when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); 96 97 mBluetoothDeviceUpdater = spy(new AvailableMediaBluetoothDeviceUpdater(mContext, 98 mDashboardFragment, mDevicePreferenceCallback)); 99 mBluetoothDeviceUpdater.setPrefContext(mContext); 100 mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false, 101 BluetoothDevicePreference.SortType.TYPE_DEFAULT); 102 doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); 103 doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); 104 } 105 106 @Test onAudioModeChanged_hfpDeviceConnected_inCall_addPreference()107 public void onAudioModeChanged_hfpDeviceConnected_inCall_addPreference() { 108 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 109 when(mBluetoothDeviceUpdater. 110 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 111 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 112 mCachedDevices.add(mCachedBluetoothDevice); 113 114 mBluetoothDeviceUpdater.onAudioModeChanged(); 115 116 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 117 } 118 119 @Test onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference()120 public void onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference() { 121 mAudioManager.setMode(AudioManager.MODE_NORMAL); 122 when(mBluetoothDeviceUpdater. 123 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 124 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 125 mCachedDevices.add(mCachedBluetoothDevice); 126 127 mBluetoothDeviceUpdater.onAudioModeChanged(); 128 129 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 130 } 131 132 @Test onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference()133 public void onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference() { 134 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 135 when(mBluetoothDeviceUpdater. 136 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 137 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 138 mCachedDevices.add(mCachedBluetoothDevice); 139 140 mBluetoothDeviceUpdater.onAudioModeChanged(); 141 142 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 143 } 144 145 @Test onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference()146 public void onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference() { 147 mAudioManager.setMode(AudioManager.MODE_NORMAL); 148 when(mBluetoothDeviceUpdater. 149 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 150 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 151 mCachedDevices.add(mCachedBluetoothDevice); 152 153 mBluetoothDeviceUpdater.onAudioModeChanged(); 154 155 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 156 } 157 158 @Test onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference()159 public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference() { 160 mAudioManager.setMode(AudioManager.MODE_NORMAL); 161 when(mBluetoothDeviceUpdater. 162 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 163 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 164 165 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 166 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 167 168 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 169 } 170 171 @Test onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference()172 public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference() { 173 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 174 when(mBluetoothDeviceUpdater. 175 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 176 when(mCachedBluetoothDevice.isConnectedA2dpDevice()).thenReturn(true); 177 178 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 179 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 180 181 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 182 } 183 184 @Test onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference()185 public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference() { 186 mAudioManager.setMode(AudioManager.MODE_NORMAL); 187 when(mBluetoothDeviceUpdater. 188 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 189 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 190 191 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 192 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 193 194 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 195 } 196 197 @Test onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference()198 public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference() { 199 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 200 when(mBluetoothDeviceUpdater. 201 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 202 when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); 203 204 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 205 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 206 207 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 208 } 209 210 @Test onProfileConnectionStateChanged_hearingAidDeviceConnected_notInCall_addPreference()211 public void onProfileConnectionStateChanged_hearingAidDeviceConnected_notInCall_addPreference() 212 { 213 mAudioManager.setMode(AudioManager.MODE_NORMAL); 214 when(mBluetoothDeviceUpdater. 215 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 216 when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true); 217 218 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 219 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.HEARING_AID); 220 221 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 222 } 223 224 @Test onProfileConnectionStateChanged_hearingAidDeviceConnected_inCall_addPreference()225 public void onProfileConnectionStateChanged_hearingAidDeviceConnected_inCall_addPreference() { 226 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 227 when(mBluetoothDeviceUpdater. 228 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 229 when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true); 230 231 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 232 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.HEARING_AID); 233 234 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 235 } 236 237 @Test onProfileConnectionStateChanged_deviceDisconnected_removePreference()238 public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { 239 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 240 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); 241 242 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 243 } 244 245 @Test onClick_Preference_setActive()246 public void onClick_Preference_setActive() { 247 mBluetoothDeviceUpdater.onPreferenceClick(mPreference); 248 249 verify(mCachedBluetoothDevice).setActive(); 250 } 251 } 252 253