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