• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.bluetooth.BluetoothDevice;
30 import android.content.Context;
31 import android.os.UserManager;
32 import android.view.ContextThemeWrapper;
33 
34 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
35 import com.android.settings.R;
36 import com.android.settings.testutils.FakeFeatureFactory;
37 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
38 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
39 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.util.ReflectionHelpers;
50 
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.Comparator;
54 import java.util.List;
55 
56 @RunWith(RobolectricTestRunner.class)
57 @Config(shadows = {ShadowAlertDialogCompat.class})
58 public class BluetoothDevicePreferenceTest {
59     private static final boolean SHOW_DEVICES_WITHOUT_NAMES = true;
60     private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
61     private static final String MAC_ADDRESS_2 = "05:52:C7:0B:D8:3C";
62     private static final String MAC_ADDRESS_3 = "06:52:C7:0B:D8:3C";
63     private static final String MAC_ADDRESS_4 = "07:52:C7:0B:D8:3C";
64     private static final Comparator<BluetoothDevicePreference> COMPARATOR =
65             Comparator.naturalOrder();
66 
67     private Context mContext;
68     @Mock
69     private CachedBluetoothDevice mCachedBluetoothDevice;
70     @Mock
71     private CachedBluetoothDevice mCachedDevice1;
72     @Mock
73     private CachedBluetoothDevice mCachedDevice2;
74     @Mock
75     private CachedBluetoothDevice mCachedDevice3;
76 
77     private FakeFeatureFactory mFakeFeatureFactory;
78     private MetricsFeatureProvider mMetricsFeatureProvider;
79     private BluetoothDevicePreference mPreference;
80     private List<BluetoothDevicePreference> mPreferenceList = new ArrayList<>();
81 
82     @Before
setUp()83     public void setUp() {
84         MockitoAnnotations.initMocks(this);
85         Context context = spy(RuntimeEnvironment.application.getApplicationContext());
86         mContext = new ContextThemeWrapper(context, R.style.Theme_Settings);
87         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
88         mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
89         when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
90         when(mCachedDevice1.getAddress()).thenReturn(MAC_ADDRESS_2);
91         when(mCachedDevice2.getAddress()).thenReturn(MAC_ADDRESS_3);
92         when(mCachedDevice3.getAddress()).thenReturn(MAC_ADDRESS_4);
93         mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
94                 SHOW_DEVICES_WITHOUT_NAMES, BluetoothDevicePreference.SortType.TYPE_DEFAULT);
95     }
96 
97     @Test
onClicked_deviceConnected_shouldLogBluetoothDisconnectEvent()98     public void onClicked_deviceConnected_shouldLogBluetoothDisconnectEvent() {
99         when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
100 
101         mPreference.onClicked();
102 
103         verify(mMetricsFeatureProvider)
104                 .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
105     }
106 
107     @Test
onClicked_deviceBonded_shouldLogBluetoothConnectEvent()108     public void onClicked_deviceBonded_shouldLogBluetoothConnectEvent() {
109         when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
110         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
111 
112         mPreference.onClicked();
113 
114         verify(mMetricsFeatureProvider)
115                 .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
116     }
117 
118     @Test
onClicked_deviceNotBonded_shouldLogBluetoothPairEvent()119     public void onClicked_deviceNotBonded_shouldLogBluetoothPairEvent() {
120         when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
121         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
122         when(mCachedBluetoothDevice.startPairing()).thenReturn(true);
123         when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true);
124 
125         mPreference.onClicked();
126 
127         verify(mMetricsFeatureProvider)
128                 .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
129         verify(mMetricsFeatureProvider, never())
130                 .action(mContext,
131                         MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
132     }
133 
134     @Test
onClicked_deviceNotBonded_shouldLogBluetoothPairEventAndPairWithoutNameEvent()135     public void onClicked_deviceNotBonded_shouldLogBluetoothPairEventAndPairWithoutNameEvent() {
136         when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
137         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
138         when(mCachedBluetoothDevice.startPairing()).thenReturn(true);
139         when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(false);
140 
141         mPreference.onClicked();
142 
143         verify(mMetricsFeatureProvider)
144                 .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
145         verify(mMetricsFeatureProvider)
146                 .action(mContext,
147                         MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
148     }
149 
150     @Test
getSecondTargetResource_shouldBeGearIconLayout()151     public void getSecondTargetResource_shouldBeGearIconLayout() {
152         assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear);
153     }
154 
155     @Test
shouldHideSecondTarget_noDevice_shouldReturnTrue()156     public void shouldHideSecondTarget_noDevice_shouldReturnTrue() {
157         ReflectionHelpers.setField(mPreference, "mCachedDevice", null);
158 
159         assertThat(mPreference.shouldHideSecondTarget()).isTrue();
160     }
161 
162     @Test
shouldHideSecondTarget_notBond_shouldReturnTrue()163     public void shouldHideSecondTarget_notBond_shouldReturnTrue() {
164         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
165 
166         assertThat(mPreference.shouldHideSecondTarget()).isTrue();
167     }
168 
169     @Test
shouldHideSecondTarget_hasUserRestriction_shouldReturnTrue()170     public void shouldHideSecondTarget_hasUserRestriction_shouldReturnTrue() {
171         final UserManager um = mock(UserManager.class);
172         ReflectionHelpers.setField(mPreference, "mUserManager", um);
173         when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
174 
175         assertThat(mPreference.shouldHideSecondTarget()).isTrue();
176     }
177 
178     @Test
shouldHideSecondTarget_hasBoundDeviceAndNoRestriction_shouldReturnFalse()179     public void shouldHideSecondTarget_hasBoundDeviceAndNoRestriction_shouldReturnFalse() {
180         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
181         final UserManager um = mock(UserManager.class);
182         ReflectionHelpers.setField(mPreference, "mUserManager", um);
183         when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH)).thenReturn(false);
184 
185         assertThat(mPreference.shouldHideSecondTarget()).isFalse();
186     }
187 
188     @Test
isVisible_showDeviceWithoutNames_visible()189     public void isVisible_showDeviceWithoutNames_visible() {
190         doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName();
191         BluetoothDevicePreference preference =
192                 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
193                         SHOW_DEVICES_WITHOUT_NAMES,
194                         BluetoothDevicePreference.SortType.TYPE_DEFAULT);
195 
196         assertThat(preference.isVisible()).isTrue();
197     }
198 
199     @Test
isVisible_hideDeviceWithoutNames_invisible()200     public void isVisible_hideDeviceWithoutNames_invisible() {
201         doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName();
202         BluetoothDevicePreference preference =
203                 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
204                         false, BluetoothDevicePreference.SortType.TYPE_DEFAULT);
205 
206         assertThat(preference.isVisible()).isFalse();
207     }
208 
209     @Test
setNeedNotifyHierarchyChanged_updateValue()210     public void setNeedNotifyHierarchyChanged_updateValue() {
211         mPreference.setNeedNotifyHierarchyChanged(true);
212 
213         assertThat(mPreference.mNeedNotifyHierarchyChanged).isTrue();
214     }
215 
216     @Test
compareTo_sortTypeFIFO()217     public void compareTo_sortTypeFIFO() {
218         final BluetoothDevicePreference preference3 = new BluetoothDevicePreference(mContext,
219                 mCachedDevice3, SHOW_DEVICES_WITHOUT_NAMES,
220                 BluetoothDevicePreference.SortType.TYPE_FIFO);
221         final BluetoothDevicePreference preference2 = new BluetoothDevicePreference(mContext,
222                 mCachedDevice2, SHOW_DEVICES_WITHOUT_NAMES,
223                 BluetoothDevicePreference.SortType.TYPE_FIFO);
224         final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(mContext,
225                 mCachedDevice1, SHOW_DEVICES_WITHOUT_NAMES,
226                 BluetoothDevicePreference.SortType.TYPE_FIFO);
227 
228         mPreferenceList.add(preference1);
229         mPreferenceList.add(preference2);
230         mPreferenceList.add(preference3);
231         Collections.sort(mPreferenceList, COMPARATOR);
232 
233         assertThat(mPreferenceList.get(0).getCachedDevice().getAddress())
234                 .isEqualTo(preference3.getCachedDevice().getAddress());
235         assertThat(mPreferenceList.get(1).getCachedDevice().getAddress())
236                 .isEqualTo(preference2.getCachedDevice().getAddress());
237         assertThat(mPreferenceList.get(2).getCachedDevice().getAddress())
238                 .isEqualTo(preference1.getCachedDevice().getAddress());
239     }
240 
241     @Test
compareTo_sortTypeDefault()242     public void compareTo_sortTypeDefault() {
243         final BluetoothDevicePreference preference3 = new BluetoothDevicePreference(mContext,
244                 mCachedDevice3, SHOW_DEVICES_WITHOUT_NAMES,
245                 BluetoothDevicePreference.SortType.TYPE_DEFAULT);
246         final BluetoothDevicePreference preference2 = new BluetoothDevicePreference(mContext,
247                 mCachedDevice2, SHOW_DEVICES_WITHOUT_NAMES,
248                 BluetoothDevicePreference.SortType.TYPE_DEFAULT);
249         final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(mContext,
250                 mCachedDevice1, SHOW_DEVICES_WITHOUT_NAMES,
251                 BluetoothDevicePreference.SortType.TYPE_DEFAULT);
252 
253         mPreferenceList.add(preference1);
254         mPreferenceList.add(preference2);
255         mPreferenceList.add(preference3);
256         Collections.sort(mPreferenceList, COMPARATOR);
257 
258         assertThat(mPreferenceList.get(0).getCachedDevice().getAddress())
259                 .isEqualTo(preference1.getCachedDevice().getAddress());
260         assertThat(mPreferenceList.get(1).getCachedDevice().getAddress())
261                 .isEqualTo(preference2.getCachedDevice().getAddress());
262         assertThat(mPreferenceList.get(2).getCachedDevice().getAddress())
263                 .isEqualTo(preference3.getCachedDevice().getAddress());
264     }
265 
266     @Test
onAttached_callbackNotRemoved_doNotRegisterCallback()267     public void onAttached_callbackNotRemoved_doNotRegisterCallback() {
268         mPreference.onAttached();
269 
270         verify(mCachedBluetoothDevice, never()).unregisterCallback(any());
271     }
272 
273     @Test
onAttached_callbackRemoved_registerCallback()274     public void onAttached_callbackRemoved_registerCallback() {
275         mPreference.onPrepareForRemoval();
276         mPreference.onAttached();
277 
278         verify(mCachedBluetoothDevice, times(2)).registerCallback(any());
279     }
280 }
281