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 android.bluetooth.BluetoothProfile; 19 import android.bluetooth.BluetoothDevice; 20 import android.content.Context; 21 import android.media.AudioManager; 22 23 import com.android.settings.connecteddevice.DevicePreferenceCallback; 24 import com.android.settings.dashboard.DashboardFragment; 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 27 import com.android.settings.testutils.shadow.ShadowAudioManager; 28 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 29 import com.android.settingslib.bluetooth.HeadsetProfile; 30 import com.android.settingslib.bluetooth.LocalBluetoothManager; 31 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 32 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.annotation.Config; 41 42 import java.util.ArrayList; 43 import java.util.Collection; 44 45 import static org.mockito.Matchers.any; 46 import static org.mockito.Mockito.doNothing; 47 import static org.mockito.Mockito.doReturn; 48 import static org.mockito.Mockito.spy; 49 import static org.mockito.Mockito.verify; 50 import static org.mockito.Mockito.when; 51 52 @RunWith(SettingsRobolectricTestRunner.class) 53 @Config(shadows = {ShadowAudioManager.class}) 54 public class AvailableMediaBluetoothDeviceUpdaterTest { 55 @Mock 56 private DashboardFragment mDashboardFragment; 57 @Mock 58 private DevicePreferenceCallback mDevicePreferenceCallback; 59 @Mock 60 private CachedBluetoothDevice mCachedBluetoothDevice; 61 @Mock 62 private BluetoothDevice mBluetoothDevice; 63 @Mock 64 private LocalBluetoothManager mLocalManager; 65 @Mock 66 private LocalBluetoothProfileManager mLocalBluetoothProfileManager; 67 @Mock 68 private HeadsetProfile mHeadsetProfile; 69 @Mock 70 private CachedBluetoothDeviceManager mCachedDeviceManager; 71 72 private Context mContext; 73 private AvailableMediaBluetoothDeviceUpdater mBluetoothDeviceUpdater; 74 private Collection<CachedBluetoothDevice> cachedDevices; 75 private ShadowAudioManager mShadowAudioManager; 76 private BluetoothDevicePreference mPreference; 77 78 @Before 79 public void setUp() { 80 MockitoAnnotations.initMocks(this); 81 82 mShadowAudioManager = ShadowAudioManager.getShadow(); 83 mContext = RuntimeEnvironment.application; 84 doReturn(mContext).when(mDashboardFragment).getContext(); 85 cachedDevices = 86 new ArrayList<CachedBluetoothDevice>(new ArrayList<CachedBluetoothDevice>()); 87 88 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 89 when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager); 90 when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile); 91 when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); 92 when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); 93 94 mBluetoothDeviceUpdater = spy(new AvailableMediaBluetoothDeviceUpdater(mDashboardFragment, 95 mDevicePreferenceCallback, mLocalManager)); 96 mBluetoothDeviceUpdater.setPrefContext(mContext); 97 mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false); 98 doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); 99 doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); 100 } 101 102 @Test 103 public void onAudioModeChanged_hfpDeviceConnected_inCall_addPreference() { 104 mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL); 105 when(mBluetoothDeviceUpdater. 106 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 107 when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); 108 cachedDevices.add(mCachedBluetoothDevice); 109 110 mBluetoothDeviceUpdater.onAudioModeChanged(); 111 112 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 113 } 114 115 @Test 116 public void onAudioModeChanged_hfpDeviceConnected_notInCall_removePreference() { 117 mShadowAudioManager.setMode(AudioManager.MODE_NORMAL); 118 when(mBluetoothDeviceUpdater. 119 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 120 when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); 121 cachedDevices.add(mCachedBluetoothDevice); 122 123 mBluetoothDeviceUpdater.onAudioModeChanged(); 124 125 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 126 } 127 128 @Test 129 public void onAudioModeChanged_a2dpDeviceConnected_inCall_removePreference() { 130 mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL); 131 when(mBluetoothDeviceUpdater. 132 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 133 when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); 134 cachedDevices.add(mCachedBluetoothDevice); 135 136 mBluetoothDeviceUpdater.onAudioModeChanged(); 137 138 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 139 } 140 141 @Test 142 public void onAudioModeChanged_a2dpDeviceConnected_notInCall_addPreference() { 143 mShadowAudioManager.setMode(AudioManager.MODE_NORMAL); 144 when(mBluetoothDeviceUpdater. 145 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 146 when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); 147 cachedDevices.add(mCachedBluetoothDevice); 148 149 mBluetoothDeviceUpdater.onAudioModeChanged(); 150 151 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 152 } 153 154 @Test 155 public void onProfileConnectionStateChanged_a2dpDeviceConnected_notInCall_addPreference() { 156 mShadowAudioManager.setMode(AudioManager.MODE_NORMAL); 157 when(mBluetoothDeviceUpdater. 158 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 159 when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); 160 161 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 162 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 163 164 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 165 } 166 167 @Test 168 public void onProfileConnectionStateChanged_a2dpDeviceConnected_inCall_removePreference() { 169 mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL); 170 when(mBluetoothDeviceUpdater. 171 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 172 when(mCachedBluetoothDevice.isA2dpDevice()).thenReturn(true); 173 174 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 175 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 176 177 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 178 } 179 180 @Test 181 public void onProfileConnectionStateChanged_hfpDeviceConnected_notInCall_removePreference() { 182 mShadowAudioManager.setMode(AudioManager.MODE_NORMAL); 183 when(mBluetoothDeviceUpdater. 184 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 185 when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); 186 187 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 188 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 189 190 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 191 } 192 193 @Test 194 public void onProfileConnectionStateChanged_hfpDeviceConnected_inCall_addPreference() { 195 mShadowAudioManager.setMode(AudioManager.MODE_IN_CALL); 196 when(mBluetoothDeviceUpdater. 197 isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); 198 when(mCachedBluetoothDevice.isHfpDevice()).thenReturn(true); 199 200 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 201 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 202 203 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 204 } 205 206 @Test 207 public void onProfileConnectionStateChanged_deviceDisconnected_removePreference() { 208 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 209 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); 210 211 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 212 } 213 214 @Test 215 public void onClick_Preference_setActive() { 216 mBluetoothDeviceUpdater.onPreferenceClick(mPreference); 217 218 verify(mCachedBluetoothDevice).setActive(); 219 } 220 } 221 222