• 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 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.bluetooth.BluetoothClass;
27 import android.bluetooth.BluetoothDevice;
28 import android.content.Context;
29 import android.os.UserManager;
30 
31 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
32 import com.android.settings.R;
33 import com.android.settings.testutils.FakeFeatureFactory;
34 import com.android.settings.testutils.SettingsRobolectricTestRunner;
35 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
36 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.util.ReflectionHelpers;
45 
46 @RunWith(SettingsRobolectricTestRunner.class)
47 public class BluetoothDevicePreferenceTest {
48     private static final boolean SHOW_DEVICES_WITHOUT_NAMES = true;
49 
50     private Context mContext;
51     @Mock
52     private CachedBluetoothDevice mCachedBluetoothDevice;
53 
54     private FakeFeatureFactory mFakeFeatureFactory;
55     private MetricsFeatureProvider mMetricsFeatureProvider;
56     private BluetoothDevicePreference mPreference;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         mContext = spy(RuntimeEnvironment.application.getApplicationContext());
62         mFakeFeatureFactory = FakeFeatureFactory.setupForTest();
63         mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider();
64         mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
65                 SHOW_DEVICES_WITHOUT_NAMES);
66     }
67 
68     @Test
onClicked_deviceConnected_shouldLogBluetoothDisconnectEvent()69     public void onClicked_deviceConnected_shouldLogBluetoothDisconnectEvent() {
70         when(mCachedBluetoothDevice.isConnected()).thenReturn(true);
71 
72         mPreference.onClicked();
73 
74         verify(mMetricsFeatureProvider)
75             .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT);
76     }
77 
78     @Test
onClicked_deviceBonded_shouldLogBluetoothConnectEvent()79     public void onClicked_deviceBonded_shouldLogBluetoothConnectEvent() {
80         when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
81         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
82 
83         mPreference.onClicked();
84 
85         verify(mMetricsFeatureProvider)
86             .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT);
87     }
88 
89     @Test
onClicked_deviceNotBonded_shouldLogBluetoothPairEvent()90     public void onClicked_deviceNotBonded_shouldLogBluetoothPairEvent() {
91         when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
92         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
93         when(mCachedBluetoothDevice.startPairing()).thenReturn(true);
94         when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(true);
95 
96         mPreference.onClicked();
97 
98         verify(mMetricsFeatureProvider)
99             .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
100         verify(mMetricsFeatureProvider, never())
101             .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
102     }
103 
104     @Test
onClicked_deviceNotBonded_shouldLogBluetoothPairEventAndPairWithoutNameEvent()105     public void onClicked_deviceNotBonded_shouldLogBluetoothPairEventAndPairWithoutNameEvent() {
106         when(mCachedBluetoothDevice.isConnected()).thenReturn(false);
107         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
108         when(mCachedBluetoothDevice.startPairing()).thenReturn(true);
109         when(mCachedBluetoothDevice.hasHumanReadableName()).thenReturn(false);
110 
111         mPreference.onClicked();
112 
113         verify(mMetricsFeatureProvider)
114             .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR);
115         verify(mMetricsFeatureProvider)
116             .action(mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR_DEVICES_WITHOUT_NAMES);
117     }
118 
119     @Test
getSecondTargetResource_shouldBeGearIconLayout()120     public void getSecondTargetResource_shouldBeGearIconLayout() {
121         assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear);
122     }
123 
124     @Test
shouldHideSecondTarget_noDevice_shouldReturnTrue()125     public void shouldHideSecondTarget_noDevice_shouldReturnTrue() {
126         ReflectionHelpers.setField(mPreference, "mCachedDevice", null);
127 
128         assertThat(mPreference.shouldHideSecondTarget()).isTrue();
129     }
130 
131     @Test
shouldHideSecondTarget_notBond_shouldReturnTrue()132     public void shouldHideSecondTarget_notBond_shouldReturnTrue() {
133         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE);
134 
135         assertThat(mPreference.shouldHideSecondTarget()).isTrue();
136     }
137 
138     @Test
shouldHideSecondTarget_hasUserRestriction_shouldReturnTrue()139     public void shouldHideSecondTarget_hasUserRestriction_shouldReturnTrue() {
140         final UserManager um = mock(UserManager.class);
141         ReflectionHelpers.setField(mPreference, "mUserManager", um);
142         when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
143 
144         assertThat(mPreference.shouldHideSecondTarget()).isTrue();
145     }
146 
147     @Test
shouldHideSecondTarget_hasBoundDeviceAndNoRestriction_shouldReturnFalse()148     public void shouldHideSecondTarget_hasBoundDeviceAndNoRestriction_shouldReturnFalse() {
149         when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
150         final UserManager um = mock(UserManager.class);
151         ReflectionHelpers.setField(mPreference, "mUserManager", um);
152         when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH)).thenReturn(false);
153 
154         assertThat(mPreference.shouldHideSecondTarget()).isFalse();
155     }
156 
157     @Test
imagingDeviceIcon_isICSettingsPrint()158     public void imagingDeviceIcon_isICSettingsPrint() {
159         when(mCachedBluetoothDevice.getBatteryLevel())
160             .thenReturn(BluetoothDevice.BATTERY_LEVEL_UNKNOWN);
161         when(mCachedBluetoothDevice.getBtClass())
162             .thenReturn(new BluetoothClass(BluetoothClass.Device.Major.IMAGING));
163 
164         mPreference.onDeviceAttributesChanged();
165         assertThat(mPreference.getIcon()).isEqualTo(
166                 mContext.getDrawable(R.drawable.ic_settings_print));
167     }
168 
169     @Test
testVisible_showDeviceWithoutNames_visible()170     public void testVisible_showDeviceWithoutNames_visible() {
171         doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName();
172         BluetoothDevicePreference preference =
173                 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice,
174                         SHOW_DEVICES_WITHOUT_NAMES);
175 
176         assertThat(preference.isVisible()).isTrue();
177     }
178 
179     @Test
testVisible_hideDeviceWithoutNames_invisible()180     public void testVisible_hideDeviceWithoutNames_invisible() {
181         doReturn(false).when(mCachedBluetoothDevice).hasHumanReadableName();
182         BluetoothDevicePreference preference =
183                 new BluetoothDevicePreference(mContext, mCachedBluetoothDevice, false);
184 
185         assertThat(preference.isVisible()).isFalse();
186     }
187 }
188