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.Mockito.doNothing; 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.content.Context; 28 import android.content.Intent; 29 30 import androidx.preference.Preference; 31 32 import com.android.settings.R; 33 import com.android.settings.dashboard.DashboardFragment; 34 import com.android.settingslib.testutils.DrawableTestHelper; 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.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class BluetoothPairingPreferenceControllerTest { 47 48 private static final int ORDER = 1; 49 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 51 private DashboardFragment mFragment; 52 53 private Context mContext; 54 private Preference mPreference; 55 56 private BluetoothPairingPreferenceController mController; 57 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 62 mContext = spy(RuntimeEnvironment.application); 63 when(mFragment.getPreferenceScreen().getContext()).thenReturn(mContext); 64 65 mPreference = new Preference(mContext); 66 mPreference.setKey(BluetoothPairingPreferenceController.KEY_PAIRING); 67 68 mController = new BluetoothPairingPreferenceController(mContext, mFragment); 69 } 70 71 @Test testCreateBluetoothPairingPreference()72 public void testCreateBluetoothPairingPreference() { 73 Preference pref = mController.createBluetoothPairingPreference(ORDER); 74 75 assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING); 76 DrawableTestHelper.assertDrawableResId(pref.getIcon(), R.drawable.ic_add_24dp); 77 assertThat(pref.getOrder()).isEqualTo(ORDER); 78 assertThat(pref.getTitle()) 79 .isEqualTo(mContext.getString(R.string.bluetooth_pairing_pref_title)); 80 } 81 82 @Test testHandlePreferenceTreeClick_startFragment()83 public void testHandlePreferenceTreeClick_startFragment() { 84 doNothing().when(mContext).startActivity(any(Intent.class)); 85 86 mController.handlePreferenceTreeClick(mPreference); 87 88 verify(mContext).startActivity(any(Intent.class)); 89 } 90 } 91