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
forceUpdate_findCachedBluetoothDeviceIsMatched_addPreference()148     public void forceUpdate_findCachedBluetoothDeviceIsMatched_addPreference() {
149         final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
150         bluetoothDevices.add(mBluetoothDevice);
151 
152         when(mBluetoothAdapter.isEnabled()).thenReturn(true);
153         when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices);
154         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
155         when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
156         when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
157         when(mBluetoothDevice.isConnected()).thenReturn(false);
158 
159         mBluetoothDeviceUpdater.forceUpdate();
160 
161         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice,
162                 BluetoothDevicePreference.SortType.TYPE_NO_SORT);
163     }
164 
165     @Test
forceUpdate_findCachedBluetoothDeviceNotMatched_removePreference()166     public void forceUpdate_findCachedBluetoothDeviceNotMatched_removePreference() {
167         final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
168         bluetoothDevices.add(mBluetoothDevice);
169 
170         when(mBluetoothAdapter.isEnabled()).thenReturn(true);
171         when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices);
172         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
173         when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
174         when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
175         when(mBluetoothDevice.isConnected()).thenReturn(true);
176 
177         mBluetoothDeviceUpdater.forceUpdate();
178 
179         verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice);
180     }
181 
182     @Test
forceUpdate_notFindCachedBluetoothDevice_doNothing()183     public void forceUpdate_notFindCachedBluetoothDevice_doNothing() {
184         final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
185         bluetoothDevices.add(mBluetoothDevice);
186 
187         when(mBluetoothAdapter.isEnabled()).thenReturn(true);
188         when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices);
189         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
190         when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(null);
191 
192         mBluetoothDeviceUpdater.forceUpdate();
193 
194         verify(mBluetoothDeviceUpdater, never()).removePreference(mCachedBluetoothDevice);
195         verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice,
196                 BluetoothDevicePreference.SortType.TYPE_NO_SORT);
197     }
198 
199     @Test
forceUpdate_bluetoothAdapterNotEnable_removeAllDevicesFromPreference()200     public void forceUpdate_bluetoothAdapterNotEnable_removeAllDevicesFromPreference() {
201         final Collection<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
202         cachedDevices.add(mCachedBluetoothDevice);
203 
204         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
205         when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
206         when(mBluetoothAdapter.isEnabled()).thenReturn(false);
207 
208         mBluetoothDeviceUpdater.forceUpdate();
209 
210         verify(mBluetoothDeviceUpdater).removeAllDevicesFromPreference();
211     }
212 
213     @Test
forceUpdate_deviceNotContain_removePreference()214     public void forceUpdate_deviceNotContain_removePreference() {
215         final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
216         bluetoothDevices.add(mBluetoothDevice);
217         final BluetoothDevice device2 = mock(BluetoothDevice.class);
218         final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class);
219 
220         mBluetoothDeviceUpdater.mPreferenceMap.put(device2, mPreference);
221 
222         when(cachedDevice2.getDevice()).thenReturn(device2);
223         when(cachedDevice2.getAddress()).thenReturn("04:52:C7:0B:D8:3S");
224         when(mDeviceManager.findDevice(device2)).thenReturn(cachedDevice2);
225         when(mBluetoothAdapter.isEnabled()).thenReturn(true);
226         when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices);
227         when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager);
228         when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
229         when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
230         when(mBluetoothDevice.isConnected()).thenReturn(false);
231 
232         mBluetoothDeviceUpdater.forceUpdate();
233 
234         verify(mBluetoothDeviceUpdater).removePreference(cachedDevice2);
235         verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice,
236                 BluetoothDevicePreference.SortType.TYPE_NO_SORT);
237     }
238 }
239