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