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