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 org.mockito.ArgumentMatchers.any; 19 import static org.mockito.Mockito.doNothing; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.never; 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.util.Pair; 33 34 import com.android.settings.connecteddevice.DevicePreferenceCallback; 35 import com.android.settings.dashboard.DashboardFragment; 36 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 37 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 38 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 39 import com.android.settingslib.bluetooth.LocalBluetoothManager; 40 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.annotation.Config; 49 50 import java.util.ArrayList; 51 import java.util.Collection; 52 import java.util.List; 53 54 @RunWith(RobolectricTestRunner.class) 55 @Config(shadows = {ShadowBluetoothAdapter.class}) 56 public class SavedBluetoothDeviceUpdaterTest { 57 58 private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; 59 60 @Mock 61 private DashboardFragment mDashboardFragment; 62 @Mock 63 private DevicePreferenceCallback mDevicePreferenceCallback; 64 @Mock 65 private CachedBluetoothDevice mCachedBluetoothDevice; 66 @Mock 67 private BluetoothDevice mBluetoothDevice; 68 @Mock 69 private BluetoothAdapter mBluetoothAdapter; 70 @Mock 71 private CachedBluetoothDeviceManager mDeviceManager; 72 @Mock 73 private LocalBluetoothManager mBluetoothManager; 74 @Mock 75 private Drawable mDrawable; 76 77 private Context mContext; 78 private SavedBluetoothDeviceUpdater mBluetoothDeviceUpdater; 79 private BluetoothDevicePreference mPreference; 80 81 @Before setUp()82 public void setUp() { 83 MockitoAnnotations.initMocks(this); 84 85 Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device"); 86 mContext = RuntimeEnvironment.application; 87 doReturn(mContext).when(mDashboardFragment).getContext(); 88 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 89 when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); 90 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 91 when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs); 92 93 mBluetoothDeviceUpdater = spy(new SavedBluetoothDeviceUpdater(mContext, mDashboardFragment, 94 mDevicePreferenceCallback)); 95 mBluetoothDeviceUpdater.setPrefContext(mContext); 96 mBluetoothDeviceUpdater.mBluetoothAdapter = mBluetoothAdapter; 97 mBluetoothDeviceUpdater.mLocalManager = mBluetoothManager; 98 mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, 99 false, BluetoothDevicePreference.SortType.TYPE_DEFAULT); 100 doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); 101 doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); 102 } 103 104 @Test update_filterMatch_addPreference()105 public void update_filterMatch_addPreference() { 106 doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState(); 107 doReturn(false).when(mBluetoothDevice).isConnected(); 108 109 mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); 110 111 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, 112 BluetoothDevicePreference.SortType.TYPE_NO_SORT); 113 } 114 115 @Test update_filterNotMatch_removePreference()116 public void update_filterNotMatch_removePreference() { 117 doReturn(BluetoothDevice.BOND_NONE).when(mBluetoothDevice).getBondState(); 118 doReturn(true).when(mBluetoothDevice).isConnected(); 119 120 mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); 121 122 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 123 } 124 125 @Test onProfileConnectionStateChanged_deviceConnected_removePreference()126 public void onProfileConnectionStateChanged_deviceConnected_removePreference() { 127 when(mBluetoothDevice.isConnected()).thenReturn(true); 128 129 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 130 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 131 132 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 133 } 134 135 @Test onProfileConnectionStateChanged_deviceDisconnected_addPreference()136 public void onProfileConnectionStateChanged_deviceDisconnected_addPreference() { 137 when(mBluetoothDevice.isConnected()).thenReturn(false); 138 139 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 140 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); 141 142 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, 143 BluetoothDevicePreference.SortType.TYPE_NO_SORT); 144 } 145 146 @Test onClick_Preference_setConnect()147 public void onClick_Preference_setConnect() { 148 mBluetoothDeviceUpdater.onPreferenceClick(mPreference); 149 150 verify(mCachedBluetoothDevice).connect(); 151 } 152 153 @Test onClick_Preference_connected_setActive()154 public void onClick_Preference_connected_setActive() { 155 when(mCachedBluetoothDevice.isConnected()).thenReturn(true); 156 157 mBluetoothDeviceUpdater.onPreferenceClick(mPreference); 158 159 verify(mCachedBluetoothDevice).setActive(); 160 } 161 162 @Test forceUpdate_findCachedBluetoothDeviceIsMatched_addPreference()163 public void forceUpdate_findCachedBluetoothDeviceIsMatched_addPreference() { 164 final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); 165 bluetoothDevices.add(mBluetoothDevice); 166 167 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 168 when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices); 169 when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); 170 when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); 171 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 172 when(mBluetoothDevice.isConnected()).thenReturn(false); 173 174 mBluetoothDeviceUpdater.forceUpdate(); 175 176 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, 177 BluetoothDevicePreference.SortType.TYPE_NO_SORT); 178 } 179 180 @Test forceUpdate_findCachedBluetoothDeviceNotMatched_removePreference()181 public void forceUpdate_findCachedBluetoothDeviceNotMatched_removePreference() { 182 final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); 183 bluetoothDevices.add(mBluetoothDevice); 184 185 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 186 when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices); 187 when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); 188 when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); 189 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 190 when(mBluetoothDevice.isConnected()).thenReturn(true); 191 192 mBluetoothDeviceUpdater.forceUpdate(); 193 194 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 195 } 196 197 @Test forceUpdate_notFindCachedBluetoothDevice_doNothing()198 public void forceUpdate_notFindCachedBluetoothDevice_doNothing() { 199 final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); 200 bluetoothDevices.add(mBluetoothDevice); 201 202 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 203 when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices); 204 when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); 205 when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(null); 206 207 mBluetoothDeviceUpdater.forceUpdate(); 208 209 verify(mBluetoothDeviceUpdater, never()).removePreference(mCachedBluetoothDevice); 210 verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice, 211 BluetoothDevicePreference.SortType.TYPE_NO_SORT); 212 } 213 214 @Test forceUpdate_bluetoothAdapterNotEnable_removeAllDevicesFromPreference()215 public void forceUpdate_bluetoothAdapterNotEnable_removeAllDevicesFromPreference() { 216 final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>(); 217 cachedDevices.add(mCachedBluetoothDevice); 218 219 when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); 220 when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); 221 when(mBluetoothAdapter.isEnabled()).thenReturn(false); 222 223 mBluetoothDeviceUpdater.forceUpdate(); 224 225 verify(mBluetoothDeviceUpdater).removeAllDevicesFromPreference(); 226 } 227 228 @Test forceUpdate_deviceNotContain_removePreference()229 public void forceUpdate_deviceNotContain_removePreference() { 230 final List<BluetoothDevice> bluetoothDevices = new ArrayList<>(); 231 bluetoothDevices.add(mBluetoothDevice); 232 final BluetoothDevice device2 = mock(BluetoothDevice.class); 233 final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class); 234 235 mBluetoothDeviceUpdater.mPreferenceMap.put(device2, mPreference); 236 237 when(cachedDevice2.getDevice()).thenReturn(device2); 238 when(cachedDevice2.getAddress()).thenReturn("04:52:C7:0B:D8:3S"); 239 when(mDeviceManager.findDevice(device2)).thenReturn(cachedDevice2); 240 when(mBluetoothAdapter.isEnabled()).thenReturn(true); 241 when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices); 242 when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); 243 when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); 244 when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 245 when(mBluetoothDevice.isConnected()).thenReturn(false); 246 247 mBluetoothDeviceUpdater.forceUpdate(); 248 249 verify(mBluetoothDeviceUpdater).removePreference(cachedDevice2); 250 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, 251 BluetoothDevicePreference.SortType.TYPE_NO_SORT); 252 } 253 } 254