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.Matchers.any; 22 import static org.mockito.Matchers.anyInt; 23 import static org.mockito.Matchers.anyString; 24 import static org.mockito.Matchers.eq; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.FragmentManager; 29 import android.app.FragmentTransaction; 30 import android.content.Context; 31 import android.support.v14.preference.PreferenceFragment; 32 import android.support.v7.preference.Preference; 33 import android.support.v7.preference.PreferenceScreen; 34 35 import com.android.settings.SettingsActivity; 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 import com.android.settings.TestConfig; 38 import com.android.settings.R; 39 import com.android.settingslib.bluetooth.LocalBluetoothAdapter; 40 import com.android.settingslib.core.lifecycle.Lifecycle; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Answers; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 51 @RunWith(SettingsRobolectricTestRunner.class) 52 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 53 public class BluetoothPairingPreferenceControllerTest { 54 private static final int ORDER = 1; 55 private Context mContext; 56 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 57 private PreferenceFragment mFragment; 58 @Mock 59 private Lifecycle mLifecycle; 60 @Mock 61 private LocalBluetoothAdapter mLocalAdapter; 62 @Mock 63 private FragmentManager mFragmentManager; 64 @Mock 65 private FragmentTransaction mFragmentTransaction; 66 @Mock 67 private PreferenceScreen mPreferenceScreen; 68 @Mock 69 private SettingsActivity mSettingsActivity; 70 private Preference mPreference; 71 72 private BluetoothPairingPreferenceController mController; 73 74 @Before setUp()75 public void setUp() { 76 MockitoAnnotations.initMocks(this); 77 78 mContext = RuntimeEnvironment.application; 79 when(mFragment.getPreferenceScreen().getContext()).thenReturn(mContext); 80 81 mPreference = new Preference(mContext); 82 mPreference.setKey(BluetoothPairingPreferenceController.KEY_PAIRING); 83 84 mController = new BluetoothPairingPreferenceController(mContext, mFragment, 85 mSettingsActivity); 86 } 87 88 @Test testCreateBluetoothPairingPreference()89 public void testCreateBluetoothPairingPreference() { 90 Preference pref = mController.createBluetoothPairingPreference(ORDER); 91 92 assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING); 93 assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_add)); 94 assertThat(pref.getOrder()).isEqualTo(ORDER); 95 assertThat(pref.getTitle()).isEqualTo( 96 mContext.getString(R.string.bluetooth_pairing_pref_title)); 97 } 98 99 @Test testHandlePreferenceTreeClick_startFragment()100 public void testHandlePreferenceTreeClick_startFragment() { 101 mController.handlePreferenceTreeClick(mPreference); 102 103 verify(mSettingsActivity).startPreferencePanelAsUser(eq(mFragment), anyString(), any(), 104 anyInt(), any(), any()); 105 } 106 } 107