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 17 package com.android.settings.bluetooth; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.bluetooth.BluetoothAdapter; 29 import android.content.Context; 30 31 import androidx.fragment.app.Fragment; 32 import androidx.fragment.app.FragmentTransaction; 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceScreen; 35 36 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Answers; 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 import org.robolectric.shadow.api.Shadow; 48 49 @RunWith(RobolectricTestRunner.class) 50 @Config(shadows = {ShadowBluetoothAdapter.class}) 51 public class BluetoothDeviceRenamePreferenceControllerTest { 52 53 private static final String DEVICE_NAME = "Nightshade"; 54 private static final String PREF_KEY = "bt_rename_devices"; 55 56 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 57 private Fragment mFragment; 58 @Mock 59 private FragmentTransaction mFragmentTransaction; 60 @Mock 61 private PreferenceScreen mScreen; 62 private Context mContext; 63 private Preference mPreference; 64 private BluetoothDeviceRenamePreferenceController mController; 65 private BluetoothAdapter mBluetoothAdapter; 66 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 67 68 @Before setUp()69 public void setUp() { 70 MockitoAnnotations.initMocks(this); 71 72 mContext = spy(RuntimeEnvironment.application); 73 mPreference = new Preference(mContext); 74 mPreference.setKey(PREF_KEY); 75 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 76 mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter); 77 78 mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, PREF_KEY)); 79 mController.setFragment(mFragment); 80 doReturn(DEVICE_NAME).when(mController).getDeviceName(); 81 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 82 mController.displayPreference(mScreen); 83 } 84 85 @Test testUpdateDeviceName_showSummaryWithDeviceName()86 public void testUpdateDeviceName_showSummaryWithDeviceName() { 87 mController.updatePreferenceState(mPreference); 88 89 final CharSequence summary = mPreference.getSummary(); 90 91 assertThat(summary.toString()).isEqualTo(DEVICE_NAME); 92 } 93 94 @Test testHandlePreferenceTreeClick_startDialogFragment()95 public void testHandlePreferenceTreeClick_startDialogFragment() { 96 when(mFragment.getFragmentManager().beginTransaction()).thenReturn(mFragmentTransaction); 97 98 mController.handlePreferenceTreeClick(mPreference); 99 100 verify(mFragmentTransaction).add(any(), anyString()); 101 verify(mFragmentTransaction).commit(); 102 } 103 104 @Test displayPreference_shouldFindPreferenceWithMatchingPrefKey()105 public void displayPreference_shouldFindPreferenceWithMatchingPrefKey() { 106 assertThat(mController.mPreference.getKey()).isEqualTo(mController.getPreferenceKey()); 107 } 108 109 @Test updatePreferenceState_whenBTisOnPreferenceShouldBeVisible()110 public void updatePreferenceState_whenBTisOnPreferenceShouldBeVisible() { 111 mShadowBluetoothAdapter.setEnabled(true); 112 113 mController.updatePreferenceState(mPreference); 114 115 assertThat(mPreference.isVisible()).isTrue(); 116 } 117 118 @Test updatePreferenceState_whenBTisOffPreferenceShouldBeHide()119 public void updatePreferenceState_whenBTisOffPreferenceShouldBeHide() { 120 mShadowBluetoothAdapter.setEnabled(false); 121 122 mController.updatePreferenceState(mPreference); 123 124 assertThat(mPreference.isVisible()).isFalse(); 125 } 126 } 127