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 android.bluetooth.BluetoothDevice; 19 import android.content.Context; 20 import android.os.UserManager; 21 22 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 23 import com.android.settings.R; 24 import com.android.settings.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 27 import com.android.settings.testutils.FakeFeatureFactory; 28 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.util.ReflectionHelpers; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Mockito.mock; 41 import static org.mockito.Mockito.spy; 42 import static org.mockito.Mockito.verify; 43 import static org.mockito.Mockito.when; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47 public class BluetoothDevicePreferenceTest { 48 49 private Context mContext; 50 @Mock 51 private CachedBluetoothDevice mCachedBluetoothDevice; 52 53 private FakeFeatureFactory mFakeFeatureFactory; 54 private MetricsFeatureProvider mMetricsFeatureProvider; 55 private BluetoothDevicePreference mPreference; 56 57 @Before 58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 61 FakeFeatureFactory.setupForTest(mContext); 62 mFakeFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 63 mMetricsFeatureProvider = mFakeFeatureFactory.getMetricsFeatureProvider(); 64 mPreference = new BluetoothDevicePreference(mContext, mCachedBluetoothDevice); 65 } 66 67 @Test 68 public void onClicked_deviceConnected_shouldLogBluetoothDisconnectEvent() { 69 when(mCachedBluetoothDevice.isConnected()).thenReturn(true); 70 71 mPreference.onClicked(); 72 73 verify(mMetricsFeatureProvider).action( 74 mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_DISCONNECT); 75 } 76 77 @Test 78 public void onClicked_deviceBonded_shouldLogBluetoothConnectEvent() { 79 when(mCachedBluetoothDevice.isConnected()).thenReturn(false); 80 when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 81 82 mPreference.onClicked(); 83 84 verify(mMetricsFeatureProvider).action( 85 mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_CONNECT); 86 } 87 88 @Test 89 public void onClicked_deviceNotBonded_shouldLogBluetoothPairEvent() { 90 when(mCachedBluetoothDevice.isConnected()).thenReturn(false); 91 when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); 92 when(mCachedBluetoothDevice.startPairing()).thenReturn(true); 93 94 mPreference.onClicked(); 95 96 verify(mMetricsFeatureProvider).action( 97 mContext, MetricsEvent.ACTION_SETTINGS_BLUETOOTH_PAIR); 98 } 99 100 @Test 101 public void getSecondTargetResource_shouldBeGearIconLayout() { 102 assertThat(mPreference.getSecondTargetResId()).isEqualTo(R.layout.preference_widget_gear); 103 } 104 105 @Test 106 public void shouldHideSecondTarget_noDevice_shouldReturnTrue() { 107 ReflectionHelpers.setField(mPreference, "mCachedDevice", null); 108 109 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 110 } 111 112 @Test 113 public void shouldHideSecondTarget_notBond_shouldReturnTrue() { 114 when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_NONE); 115 116 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 117 } 118 119 @Test 120 public void shouldHideSecondTarget_hasUserRestriction_shouldReturnTrue() { 121 final UserManager um = mock(UserManager.class); 122 ReflectionHelpers.setField(mPreference, "mUserManager", um); 123 when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH)) 124 .thenReturn(true); 125 126 assertThat(mPreference.shouldHideSecondTarget()).isTrue(); 127 } 128 129 @Test 130 public void shouldHideSecondTarget_hasBoundDeviceAndNoRestriction_shouldReturnFalse() { 131 when(mCachedBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); 132 final UserManager um = mock(UserManager.class); 133 ReflectionHelpers.setField(mPreference, "mUserManager", um); 134 when(um.hasUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH)) 135 .thenReturn(false); 136 137 assertThat(mPreference.shouldHideSecondTarget()).isFalse(); 138 } 139 } 140