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 com.google.common.truth.Truth.assertThat; 19 import static org.mockito.Matchers.any; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.never; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.bluetooth.BluetoothDevice; 26 import android.bluetooth.BluetoothProfile; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.support.v7.preference.Preference; 30 31 import com.android.settings.SettingsActivity; 32 import com.android.settings.connecteddevice.DevicePreferenceCallback; 33 import com.android.settings.dashboard.DashboardFragment; 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 36 import com.android.settingslib.bluetooth.HeadsetProfile; 37 import com.android.settingslib.bluetooth.A2dpProfile; 38 39 import com.android.settingslib.bluetooth.LocalBluetoothManager; 40 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.mockito.ArgumentCaptor; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RuntimeEnvironment; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 public class BluetoothDeviceUpdaterTest { 51 52 @Mock 53 private DashboardFragment mDashboardFragment; 54 @Mock 55 private DevicePreferenceCallback mDevicePreferenceCallback; 56 @Mock 57 private CachedBluetoothDevice mCachedBluetoothDevice; 58 @Mock 59 private BluetoothDevice mBluetoothDevice; 60 @Mock 61 private SettingsActivity mSettingsActivity; 62 @Mock 63 private LocalBluetoothManager mLocalManager; 64 @Mock 65 private LocalBluetoothProfileManager mLocalBluetoothProfileManager; 66 @Mock 67 private HeadsetProfile mHeadsetProfile; 68 @Mock 69 private A2dpProfile mA2dpProfile; 70 71 private Context mContext; 72 private BluetoothDeviceUpdater mBluetoothDeviceUpdater; 73 private BluetoothDevicePreference mPreference; 74 75 @Before setUp()76 public void setUp() { 77 MockitoAnnotations.initMocks(this); 78 79 mContext = RuntimeEnvironment.application; 80 doReturn(mContext).when(mDashboardFragment).getContext(); 81 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 82 when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager); 83 when(mLocalBluetoothProfileManager.getHeadsetProfile()).thenReturn(mHeadsetProfile); 84 when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile); 85 86 mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false); 87 mBluetoothDeviceUpdater = 88 new BluetoothDeviceUpdater(mDashboardFragment, mDevicePreferenceCallback, 89 mLocalManager) { 90 @Override 91 public boolean isFilterMatched(CachedBluetoothDevice cachedBluetoothDevice) { 92 return true; 93 } 94 }; 95 mBluetoothDeviceUpdater.setPrefContext(mContext); 96 } 97 98 @Test testAddPreference_deviceExist_doNothing()99 public void testAddPreference_deviceExist_doNothing() { 100 mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference); 101 102 mBluetoothDeviceUpdater.addPreference(mCachedBluetoothDevice); 103 104 verify(mDevicePreferenceCallback, never()).onDeviceAdded(any(Preference.class)); 105 } 106 107 @Test testAddPreference_deviceNotExist_addPreference()108 public void testAddPreference_deviceNotExist_addPreference() { 109 mBluetoothDeviceUpdater.addPreference(mCachedBluetoothDevice); 110 111 final Preference preference = mBluetoothDeviceUpdater.mPreferenceMap.get(mBluetoothDevice); 112 assertThat(preference).isNotNull(); 113 verify(mDevicePreferenceCallback).onDeviceAdded(preference); 114 } 115 116 @Test testRemovePreference_deviceExist_removePreference()117 public void testRemovePreference_deviceExist_removePreference() { 118 mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference); 119 120 mBluetoothDeviceUpdater.removePreference(mCachedBluetoothDevice); 121 122 verify(mDevicePreferenceCallback).onDeviceRemoved(mPreference); 123 assertThat(mBluetoothDeviceUpdater.mPreferenceMap.containsKey(mBluetoothDevice)).isFalse(); 124 } 125 126 @Test testOnDeviceDeleted_deviceExists_removePreference()127 public void testOnDeviceDeleted_deviceExists_removePreference() { 128 mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference); 129 130 mBluetoothDeviceUpdater.onDeviceDeleted(mCachedBluetoothDevice); 131 132 verify(mDevicePreferenceCallback).onDeviceRemoved(mPreference); 133 assertThat(mBluetoothDeviceUpdater.mPreferenceMap.containsKey(mBluetoothDevice)).isFalse(); 134 } 135 136 @Test testRemovePreference_deviceNotExist_doNothing()137 public void testRemovePreference_deviceNotExist_doNothing() { 138 mBluetoothDeviceUpdater.removePreference(mCachedBluetoothDevice); 139 140 verify(mDevicePreferenceCallback, never()).onDeviceRemoved(any(Preference.class)); 141 } 142 143 @Test testDeviceProfilesListener_click_startBluetoothDeviceDetailPage()144 public void testDeviceProfilesListener_click_startBluetoothDeviceDetailPage() { 145 doReturn(mSettingsActivity).when(mDashboardFragment).getContext(); 146 147 final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 148 mBluetoothDeviceUpdater.mDeviceProfilesListener.onGearClick(mPreference); 149 150 verify(mSettingsActivity).startActivity(intentCaptor.capture()); 151 assertThat(intentCaptor.getValue().getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) 152 .isEqualTo(BluetoothDeviceDetailsFragment.class.getName()); 153 } 154 155 @Test isDeviceConnected_deviceConnected()156 public void isDeviceConnected_deviceConnected() { 157 doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState(); 158 doReturn(true).when(mBluetoothDevice).isConnected(); 159 160 assertThat(mBluetoothDeviceUpdater.isDeviceConnected(mCachedBluetoothDevice)).isTrue(); 161 } 162 163 @Test isDeviceConnected_deviceNotConnected()164 public void isDeviceConnected_deviceNotConnected() { 165 doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState(); 166 doReturn(false).when(mBluetoothDevice).isConnected(); 167 168 assertThat(mBluetoothDeviceUpdater.isDeviceConnected(mCachedBluetoothDevice)).isFalse(); 169 } 170 } 171