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 = { 51 ShadowBluetoothAdapter.class, 52 com.android.settings.testutils.shadow.ShadowFragment.class, 53 }) 54 public class BluetoothDeviceRenamePreferenceControllerTest { 55 56 private static final String DEVICE_NAME = "Nightshade"; 57 private static final String PREF_KEY = "bt_rename_devices"; 58 59 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 60 private Fragment mFragment; 61 @Mock 62 private FragmentTransaction mFragmentTransaction; 63 @Mock 64 private PreferenceScreen mScreen; 65 private Context mContext; 66 private Preference mPreference; 67 private BluetoothDeviceRenamePreferenceController mController; 68 private BluetoothAdapter mBluetoothAdapter; 69 private ShadowBluetoothAdapter mShadowBluetoothAdapter; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 75 mContext = spy(RuntimeEnvironment.application); 76 mPreference = new Preference(mContext); 77 mPreference.setKey(PREF_KEY); 78 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 79 mShadowBluetoothAdapter = Shadow.extract(mBluetoothAdapter); 80 81 mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, PREF_KEY)); 82 mController.setFragment(mFragment); 83 doReturn(DEVICE_NAME).when(mController).getDeviceName(); 84 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 85 mController.displayPreference(mScreen); 86 } 87 88 @Test testUpdateDeviceName_showSummaryWithDeviceName()89 public void testUpdateDeviceName_showSummaryWithDeviceName() { 90 mController.updatePreferenceState(mPreference); 91 92 final CharSequence summary = mPreference.getSummary(); 93 94 assertThat(summary.toString()).isEqualTo(DEVICE_NAME); 95 } 96 97 @Test testHandlePreferenceTreeClick_startDialogFragment()98 public void testHandlePreferenceTreeClick_startDialogFragment() { 99 when(mFragment.getFragmentManager().beginTransaction()).thenReturn(mFragmentTransaction); 100 101 mController.handlePreferenceTreeClick(mPreference); 102 103 verify(mFragmentTransaction).add(any(), anyString()); 104 verify(mFragmentTransaction).commit(); 105 } 106 107 @Test displayPreference_shouldFindPreferenceWithMatchingPrefKey()108 public void displayPreference_shouldFindPreferenceWithMatchingPrefKey() { 109 assertThat(mController.mPreference.getKey()).isEqualTo(mController.getPreferenceKey()); 110 } 111 112 @Test updatePreferenceState_whenBTisOnPreferenceShouldBeVisible()113 public void updatePreferenceState_whenBTisOnPreferenceShouldBeVisible() { 114 mShadowBluetoothAdapter.setEnabled(true); 115 116 mController.updatePreferenceState(mPreference); 117 118 assertThat(mPreference.isVisible()).isTrue(); 119 } 120 121 @Test updatePreferenceState_whenBTisOffPreferenceShouldBeHide()122 public void updatePreferenceState_whenBTisOffPreferenceShouldBeHide() { 123 mShadowBluetoothAdapter.setEnabled(false); 124 125 mController.updatePreferenceState(mPreference); 126 127 assertThat(mPreference.isVisible()).isFalse(); 128 } 129 } 130