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.connecteddevice; 17 18 import static com.android.settings.core.BasePreferenceController.AVAILABLE; 19 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.bluetooth.BluetoothAdapter; 29 import android.bluetooth.BluetoothDevice; 30 import android.content.Context; 31 import android.content.pm.PackageManager; 32 import android.graphics.drawable.Drawable; 33 import android.util.Pair; 34 35 import androidx.preference.Preference; 36 import androidx.preference.PreferenceCategory; 37 import androidx.preference.PreferenceGroup; 38 import androidx.preference.PreferenceManager; 39 40 import com.android.settings.R; 41 import com.android.settings.bluetooth.BluetoothDevicePreference; 42 import com.android.settings.bluetooth.BluetoothDeviceUpdater; 43 import com.android.settings.connecteddevice.dock.DockUpdater; 44 import com.android.settings.dashboard.DashboardFragment; 45 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 46 import com.android.settings.widget.SingleTargetGearPreference; 47 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 48 49 import org.junit.Before; 50 import org.junit.Test; 51 import org.junit.runner.RunWith; 52 import org.mockito.Mock; 53 import org.mockito.MockitoAnnotations; 54 import org.robolectric.RobolectricTestRunner; 55 import org.robolectric.RuntimeEnvironment; 56 import org.robolectric.annotation.Config; 57 import org.robolectric.shadow.api.Shadow; 58 59 import java.util.ArrayList; 60 import java.util.List; 61 62 @RunWith(RobolectricTestRunner.class) 63 @Config(shadows = ShadowBluetoothAdapter.class) 64 public class PreviouslyConnectedDevicePreferenceControllerTest { 65 66 private static final String KEY = "test_key"; 67 private static final String FAKE_ADDRESS_1 = "AA:AA:AA:AA:AA:01"; 68 private static final String FAKE_ADDRESS_2 = "AA:AA:AA:AA:AA:02"; 69 private static final String FAKE_ADDRESS_3 = "AA:AA:AA:AA:AA:03"; 70 private static final String FAKE_ADDRESS_4 = "AA:AA:AA:AA:AA:04"; 71 private static final String FAKE_ADDRESS_5 = "AA:AA:AA:AA:AA:05"; 72 73 @Mock 74 private DashboardFragment mDashboardFragment; 75 @Mock 76 private BluetoothDeviceUpdater mBluetoothDeviceUpdater; 77 @Mock 78 private DockUpdater mDockUpdater; 79 @Mock 80 private PackageManager mPackageManager; 81 @Mock 82 private PreferenceManager mPreferenceManager; 83 @Mock 84 private Preference mSeeAllPreference; 85 @Mock 86 private CachedBluetoothDevice mCachedDevice1; 87 @Mock 88 private CachedBluetoothDevice mCachedDevice2; 89 @Mock 90 private CachedBluetoothDevice mCachedDevice3; 91 @Mock 92 private CachedBluetoothDevice mCachedDevice4; 93 @Mock 94 private CachedBluetoothDevice mCachedDevice5; 95 @Mock 96 private BluetoothDevice mBluetoothDevice1; 97 @Mock 98 private BluetoothDevice mBluetoothDevice2; 99 @Mock 100 private BluetoothDevice mBluetoothDevice3; 101 @Mock 102 private BluetoothDevice mBluetoothDevice4; 103 @Mock 104 private BluetoothDevice mBluetoothDevice5; 105 @Mock 106 private Drawable mDrawable; 107 108 private Context mContext; 109 private PreviouslyConnectedDevicePreferenceController mPreConnectedDeviceController; 110 private PreferenceGroup mPreferenceGroup; 111 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 112 113 @Before setUp()114 public void setUp() { 115 MockitoAnnotations.initMocks(this); 116 Pair<Drawable, String> pairs = new Pair<>(mDrawable, "fake_device"); 117 mContext = spy(RuntimeEnvironment.application); 118 doReturn(mContext).when(mDashboardFragment).getContext(); 119 doReturn(mPackageManager).when(mContext).getPackageManager(); 120 mPreConnectedDeviceController = 121 new PreviouslyConnectedDevicePreferenceController(mContext, KEY); 122 mPreConnectedDeviceController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater); 123 mPreConnectedDeviceController.setSavedDockUpdater(mDockUpdater); 124 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); 125 126 when(mCachedDevice1.getDevice()).thenReturn(mBluetoothDevice1); 127 when(mCachedDevice1.getAddress()).thenReturn(FAKE_ADDRESS_1); 128 when(mCachedDevice1.getDrawableWithDescription()).thenReturn(pairs); 129 when(mCachedDevice2.getDevice()).thenReturn(mBluetoothDevice2); 130 when(mCachedDevice2.getAddress()).thenReturn(FAKE_ADDRESS_2); 131 when(mCachedDevice2.getDrawableWithDescription()).thenReturn(pairs); 132 when(mCachedDevice3.getDevice()).thenReturn(mBluetoothDevice3); 133 when(mCachedDevice3.getAddress()).thenReturn(FAKE_ADDRESS_3); 134 when(mCachedDevice3.getDrawableWithDescription()).thenReturn(pairs); 135 when(mCachedDevice4.getDevice()).thenReturn(mBluetoothDevice4); 136 when(mCachedDevice4.getAddress()).thenReturn(FAKE_ADDRESS_4); 137 when(mCachedDevice4.getDrawableWithDescription()).thenReturn(pairs); 138 when(mCachedDevice5.getDevice()).thenReturn(mBluetoothDevice5); 139 when(mCachedDevice5.getAddress()).thenReturn(FAKE_ADDRESS_5); 140 when(mCachedDevice5.getDrawableWithDescription()).thenReturn(pairs); 141 142 final List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>(); 143 mMostRecentlyConnectedDevices.add(mBluetoothDevice1); 144 mMostRecentlyConnectedDevices.add(mBluetoothDevice2); 145 mMostRecentlyConnectedDevices.add(mBluetoothDevice4); 146 mMostRecentlyConnectedDevices.add(mBluetoothDevice3); 147 mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(mMostRecentlyConnectedDevices); 148 149 mPreferenceGroup = spy(new PreferenceCategory(mContext)); 150 doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager(); 151 mPreferenceGroup.setVisible(false); 152 mPreConnectedDeviceController.setPreferenceGroup(mPreferenceGroup); 153 mPreConnectedDeviceController.mSeeAllPreference = mSeeAllPreference; 154 } 155 156 @Test callbackCanRegisterAndUnregister()157 public void callbackCanRegisterAndUnregister() { 158 // register the callback in onStart() 159 mPreConnectedDeviceController.onStart(); 160 161 verify(mBluetoothDeviceUpdater).registerCallback(); 162 verify(mDockUpdater).registerCallback(); 163 verify(mContext).registerReceiver(mPreConnectedDeviceController.mReceiver, 164 mPreConnectedDeviceController.mIntentFilter); 165 verify(mBluetoothDeviceUpdater).refreshPreference(); 166 167 // unregister the callback in onStop() 168 mPreConnectedDeviceController.onStop(); 169 170 verify(mBluetoothDeviceUpdater).unregisterCallback(); 171 verify(mDockUpdater).unregisterCallback(); 172 verify(mContext).unregisterReceiver(mPreConnectedDeviceController.mReceiver); 173 } 174 175 @Test getAvailabilityStatus_noBluetoothDockFeature_returnUnSupported()176 public void getAvailabilityStatus_noBluetoothDockFeature_returnUnSupported() { 177 doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 178 mPreConnectedDeviceController.setSavedDockUpdater(null); 179 180 assertThat(mPreConnectedDeviceController.getAvailabilityStatus()).isEqualTo( 181 CONDITIONALLY_UNAVAILABLE); 182 } 183 184 @Test getAvailabilityStatus_hasBluetoothFeature_returnSupported()185 public void getAvailabilityStatus_hasBluetoothFeature_returnSupported() { 186 doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 187 mPreConnectedDeviceController.setSavedDockUpdater(null); 188 189 assertThat(mPreConnectedDeviceController.getAvailabilityStatus()).isEqualTo( 190 AVAILABLE); 191 } 192 193 @Test getAvailabilityStatus_haveDockFeature_returnSupported()194 public void getAvailabilityStatus_haveDockFeature_returnSupported() { 195 doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); 196 197 assertThat(mPreConnectedDeviceController.getAvailabilityStatus()).isEqualTo( 198 AVAILABLE); 199 } 200 201 @Test onDeviceAdded_addDevicePreference_displayIt()202 public void onDeviceAdded_addDevicePreference_displayIt() { 203 final BluetoothDevicePreference preference1 = new BluetoothDevicePreference( 204 mContext, mCachedDevice1, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 205 206 mPreConnectedDeviceController.onDeviceAdded(preference1); 207 208 assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2); 209 } 210 211 @Test onDeviceAdded_addDockDevicePreference_displayIt()212 public void onDeviceAdded_addDockDevicePreference_displayIt() { 213 final SingleTargetGearPreference dockPreference = new SingleTargetGearPreference( 214 mContext, null /* AttributeSet */); 215 216 mPreConnectedDeviceController.onDeviceAdded(dockPreference); 217 218 assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2); 219 } 220 221 @Test onDeviceAdded_addFourDevicePreference_onlyDisplayThree()222 public void onDeviceAdded_addFourDevicePreference_onlyDisplayThree() { 223 final BluetoothDevicePreference preference1 = new BluetoothDevicePreference( 224 mContext, mCachedDevice1, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 225 final BluetoothDevicePreference preference2 = new BluetoothDevicePreference( 226 mContext, mCachedDevice2, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 227 final BluetoothDevicePreference preference3 = new BluetoothDevicePreference( 228 mContext, mCachedDevice3, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 229 final BluetoothDevicePreference preference4 = new BluetoothDevicePreference( 230 mContext, mCachedDevice4, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 231 final SingleTargetGearPreference dockPreference = new SingleTargetGearPreference( 232 mContext, null /* AttributeSet */); 233 234 mPreConnectedDeviceController.onDeviceAdded(preference1); 235 mPreConnectedDeviceController.onDeviceAdded(preference2); 236 mPreConnectedDeviceController.onDeviceAdded(preference3); 237 mPreConnectedDeviceController.onDeviceAdded(preference4); 238 mPreConnectedDeviceController.onDeviceAdded(dockPreference); 239 240 // 3 BluetoothDevicePreference and 1 see all preference 241 assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(4); 242 } 243 244 @Test onDeviceAdded_addPreferenceNotExistInRecentlyDevices_noCrash()245 public void onDeviceAdded_addPreferenceNotExistInRecentlyDevices_noCrash() { 246 final BluetoothDevicePreference preference = new BluetoothDevicePreference( 247 mContext, mCachedDevice5, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 248 249 mPreConnectedDeviceController.onDeviceAdded(preference); 250 251 // 1 BluetoothDevicePreference and 1 see all preference 252 assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2); 253 } 254 255 @Test onDeviceRemoved_removeLastDevice_showSeeAllPreference()256 public void onDeviceRemoved_removeLastDevice_showSeeAllPreference() { 257 final BluetoothDevicePreference preference1 = new BluetoothDevicePreference( 258 mContext, mCachedDevice1, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT); 259 final SingleTargetGearPreference dockPreference = new SingleTargetGearPreference( 260 mContext, null /* AttributeSet */); 261 mPreferenceGroup.addPreference(preference1); 262 mPreferenceGroup.addPreference(dockPreference); 263 264 mPreConnectedDeviceController.onDeviceRemoved(preference1); 265 mPreConnectedDeviceController.onDeviceRemoved(dockPreference); 266 267 assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1); 268 } 269 270 @Test updatePreferenceVisibility_bluetoothIsEnable_shouldShowCorrectText()271 public void updatePreferenceVisibility_bluetoothIsEnable_shouldShowCorrectText() { 272 mShadowBluetoothAdapter.setEnabled(true); 273 mPreConnectedDeviceController.updatePreferenceVisibility(); 274 275 verify(mSeeAllPreference).setSummary(""); 276 } 277 278 @Test updatePreferenceVisibility_bluetoothIsDisable_shouldShowCorrectText()279 public void updatePreferenceVisibility_bluetoothIsDisable_shouldShowCorrectText() { 280 mShadowBluetoothAdapter.setEnabled(false); 281 mPreConnectedDeviceController.updatePreferenceVisibility(); 282 283 verify(mSeeAllPreference).setSummary( 284 mContext.getString(R.string.connected_device_see_all_summary)); 285 } 286 } 287