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