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.Matchers.any; 19 import static org.mockito.Mockito.doNothing; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.bluetooth.BluetoothProfile; 26 import android.bluetooth.BluetoothDevice; 27 import android.content.Context; 28 29 import com.android.settings.connecteddevice.DevicePreferenceCallback; 30 import com.android.settings.dashboard.DashboardFragment; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 33 import com.android.settingslib.bluetooth.LocalBluetoothManager; 34 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RuntimeEnvironment; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 public class SavedBluetoothDeviceUpdaterTest { 45 46 @Mock 47 private DashboardFragment mDashboardFragment; 48 @Mock 49 private DevicePreferenceCallback mDevicePreferenceCallback; 50 @Mock 51 private CachedBluetoothDevice mCachedBluetoothDevice; 52 @Mock 53 private BluetoothDevice mBluetoothDevice; 54 @Mock 55 private LocalBluetoothManager mLocalManager; 56 @Mock 57 private LocalBluetoothProfileManager mLocalBluetoothProfileManager; 58 59 private Context mContext; 60 private SavedBluetoothDeviceUpdater mBluetoothDeviceUpdater; 61 private BluetoothDevicePreference mPreference; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 67 mContext = RuntimeEnvironment.application; 68 doReturn(mContext).when(mDashboardFragment).getContext(); 69 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); 70 when(mLocalManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager); 71 72 mBluetoothDeviceUpdater = spy(new SavedBluetoothDeviceUpdater(mDashboardFragment, 73 mDevicePreferenceCallback, mLocalManager)); 74 mBluetoothDeviceUpdater.setPrefContext(mContext); 75 mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false); 76 doNothing().when(mBluetoothDeviceUpdater).addPreference(any()); 77 doNothing().when(mBluetoothDeviceUpdater).removePreference(any()); 78 } 79 80 @Test update_filterMatch_addPreference()81 public void update_filterMatch_addPreference() { 82 doReturn(BluetoothDevice.BOND_BONDED).when(mBluetoothDevice).getBondState(); 83 doReturn(false).when(mBluetoothDevice).isConnected(); 84 85 mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); 86 87 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 88 } 89 90 @Test update_filterNotMatch_removePreference()91 public void update_filterNotMatch_removePreference() { 92 doReturn(BluetoothDevice.BOND_NONE).when(mBluetoothDevice).getBondState(); 93 doReturn(true).when(mBluetoothDevice).isConnected(); 94 95 mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); 96 97 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 98 } 99 100 @Test onProfileConnectionStateChanged_deviceConnected_removePreference()101 public void onProfileConnectionStateChanged_deviceConnected_removePreference() { 102 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 103 BluetoothProfile.STATE_CONNECTED, BluetoothProfile.A2DP); 104 105 verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); 106 } 107 108 @Test onProfileConnectionStateChanged_deviceDisconnected_addPreference()109 public void onProfileConnectionStateChanged_deviceDisconnected_addPreference() { 110 mBluetoothDeviceUpdater.onProfileConnectionStateChanged(mCachedBluetoothDevice, 111 BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.A2DP); 112 113 verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); 114 } 115 116 @Test onClick_Preference_setConnect()117 public void onClick_Preference_setConnect() { 118 mBluetoothDeviceUpdater.onPreferenceClick(mPreference); 119 120 verify(mCachedBluetoothDevice).connect(true); 121 } 122 } 123