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 android.app.Fragment; 20 import android.content.Context; 21 import android.support.v7.preference.Preference.OnPreferenceChangeListener; 22 import android.support.v7.preference.PreferenceScreen; 23 24 import com.android.settings.R; 25 import com.android.settings.SettingsActivity; 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.testutils.FakeFeatureFactory; 29 import com.android.settings.widget.MasterSwitchPreference; 30 import com.android.settingslib.bluetooth.BluetoothCallback; 31 import com.android.settingslib.bluetooth.LocalBluetoothManager; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Answers; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.annotation.Config; 41 42 import static com.google.common.truth.Truth.assertThat; 43 44 import static org.mockito.Matchers.any; 45 import static org.mockito.Matchers.eq; 46 import static org.mockito.Mockito.spy; 47 import static org.mockito.Mockito.verify; 48 import static org.mockito.Mockito.when; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 52 public class BluetoothMasterSwitchPreferenceControllerTest { 53 54 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 55 private LocalBluetoothManager mBluetoothManager; 56 @Mock 57 private PreferenceScreen mScreen; 58 @Mock 59 private MasterSwitchPreference mPreference; 60 @Mock 61 private RestrictionUtils mRestrictionUtils; 62 @Mock 63 private Fragment mFragment; 64 @Mock 65 private SettingsActivity mActivity; 66 67 private Context mContext; 68 private BluetoothMasterSwitchPreferenceController mController; 69 private FakeFeatureFactory mFeatureFactory; 70 71 @Before 72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 75 FakeFeatureFactory.setupForTest(mContext); 76 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 77 78 mController = new BluetoothMasterSwitchPreferenceController( 79 mContext, mBluetoothManager, mRestrictionUtils, mFragment, mActivity); 80 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 81 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 82 } 83 84 @Test 85 public void isAvailable_shouldAlwaysReturnTrue() { 86 assertThat(mController.isAvailable()).isTrue(); 87 } 88 89 @Test 90 public void onResume_shouldRegisterCallback() { 91 mController.onResume(); 92 93 verify(mBluetoothManager.getEventManager()).registerCallback(any(BluetoothCallback.class)); 94 } 95 96 @Test 97 public void onPause_shouldUnregisterCallback() { 98 mController.onPause(); 99 100 verify(mBluetoothManager.getEventManager()).unregisterCallback( 101 any(BluetoothCallback.class)); 102 } 103 104 @Test 105 public void onStart_shouldRegisterPreferenceChangeListener() { 106 mController.displayPreference(mScreen); 107 mController.onStart(); 108 109 verify(mPreference).setOnPreferenceChangeListener(any(OnPreferenceChangeListener.class)); 110 } 111 112 @Test 113 public void onStop_shouldRegisterPreferenceChangeListener() { 114 mController.displayPreference(mScreen); 115 mController.onStart(); 116 117 mController.onStop(); 118 119 verify(mPreference).setOnPreferenceChangeListener(null); 120 } 121 122 @Test 123 public void onSummaryUpdated_shouldUpdatePreferenceSummary() { 124 mController.displayPreference(mScreen); 125 126 mController.onSummaryChanged("test summary"); 127 128 verify(mPreference).setSummary("test summary"); 129 } 130 } 131