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.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.provider.Settings.Global; 28 29 import androidx.fragment.app.FragmentActivity; 30 import androidx.preference.PreferenceScreen; 31 import androidx.preference.SwitchPreference; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.RuntimeEnvironment; 40 41 @RunWith(RobolectricTestRunner.class) 42 public class DockingSoundPreferenceControllerTest { 43 44 @Mock 45 private PreferenceScreen mScreen; 46 @Mock(answer = RETURNS_DEEP_STUBS) 47 private FragmentActivity mActivity; 48 @Mock 49 private ContentResolver mContentResolver; 50 @Mock 51 private SoundSettings mSetting; 52 @Mock(answer = RETURNS_DEEP_STUBS) 53 private Context mContext; 54 55 private DockingSoundPreferenceController mController; 56 private SwitchPreference mPreference; 57 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 when(mSetting.getActivity()).thenReturn(mActivity); 62 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 63 mPreference = new SwitchPreference(RuntimeEnvironment.application); 64 when(mActivity.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) 65 .thenReturn(true); 66 mController = new DockingSoundPreferenceController(mContext, mSetting, null); 67 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 68 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 69 } 70 71 @Test isAvailable_hasDockSettings_shouldReturnTrue()72 public void isAvailable_hasDockSettings_shouldReturnTrue() { 73 when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) 74 .thenReturn(true); 75 76 assertThat(mController.isAvailable()).isTrue(); 77 } 78 79 @Test isAvailable_noDockSettings_shouldReturnFalse()80 public void isAvailable_noDockSettings_shouldReturnFalse() { 81 when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_dock_settings)) 82 .thenReturn(false); 83 84 assertThat(mController.isAvailable()).isFalse(); 85 } 86 87 @Test displayPreference_dockingSoundEnabled_shouldCheckedPreference()88 public void displayPreference_dockingSoundEnabled_shouldCheckedPreference() { 89 Global.putInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1); 90 91 mController.displayPreference(mScreen); 92 93 assertThat(mPreference.isChecked()).isTrue(); 94 } 95 96 @Test displayPreference_dockingSoundDisabled_shouldUncheckedPreference()97 public void displayPreference_dockingSoundDisabled_shouldUncheckedPreference() { 98 Global.putInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 0); 99 100 mController.displayPreference(mScreen); 101 102 assertThat(mPreference.isChecked()).isFalse(); 103 } 104 105 @Test onPreferenceChanged_preferenceChecked_shouldEnabledDockingSound()106 public void onPreferenceChanged_preferenceChecked_shouldEnabledDockingSound() { 107 mController.displayPreference(mScreen); 108 109 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true); 110 111 assertThat(Global.getInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1)).isEqualTo(1); 112 } 113 114 @Test onPreferenceChanged_preferenceUnchecked_shouldDisabledDockingSound()115 public void onPreferenceChanged_preferenceUnchecked_shouldDisabledDockingSound() { 116 mController.displayPreference(mScreen); 117 118 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false); 119 120 assertThat(Global.getInt(mContentResolver, Global.DOCK_SOUNDS_ENABLED, 1)).isEqualTo(0); 121 } 122 } 123