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.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.app.Fragment; 27 import android.app.FragmentTransaction; 28 import android.content.Context; 29 import android.support.v7.preference.Preference; 30 import android.support.v7.preference.PreferenceScreen; 31 32 import com.android.settings.TestConfig; 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 import org.robolectric.annotation.Config; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 47 public class BluetoothDeviceRenamePreferenceControllerTest { 48 49 private static final String DEVICE_NAME = "Nightshade"; 50 51 @Mock 52 private LocalBluetoothAdapter mLocalAdapter; 53 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 54 private Fragment mFragment; 55 @Mock 56 private FragmentTransaction mFragmentTransaction; 57 @Mock 58 private PreferenceScreen mScreen; 59 private Context mContext; 60 private Preference mPreference; 61 private BluetoothDeviceRenamePreferenceController mController; 62 63 @Before 64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 67 mContext = spy(RuntimeEnvironment.application); 68 mPreference = new Preference(mContext); 69 mPreference.setKey(BluetoothDeviceRenamePreferenceController.PREF_KEY); 70 71 mController = new BluetoothDeviceRenamePreferenceController( 72 mContext, mFragment, mLocalAdapter); 73 } 74 75 @Test 76 public void testUpdateDeviceName_showSummaryWithDeviceName() { 77 mController.updateDeviceName(mPreference, DEVICE_NAME); 78 79 final CharSequence summary = mPreference.getSummary(); 80 81 assertThat(summary.toString()).isEqualTo(DEVICE_NAME); 82 } 83 84 @Test 85 public void testHandlePreferenceTreeClick_startDialogFragment() { 86 when(mFragment.getFragmentManager().beginTransaction()).thenReturn(mFragmentTransaction); 87 88 mController.handlePreferenceTreeClick(mPreference); 89 90 verify(mFragmentTransaction).add(any(), anyString()); 91 verify(mFragmentTransaction).commit(); 92 } 93 94 @Test 95 public void displayPreference_shouldFindPreferenceWithMatchingPrefKey() { 96 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 97 98 mController.displayPreference(mScreen); 99 100 assertThat(mController.mPreference.getKey()).isEqualTo(mController.getPreferenceKey()); 101 } 102 } 103