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.Mockito.doReturn; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.provider.Settings.System; 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 import org.robolectric.annotation.Config; 41 42 @RunWith(RobolectricTestRunner.class) 43 public class ScreenLockSoundPreferenceControllerTest { 44 45 @Mock 46 private PreferenceScreen mScreen; 47 @Mock 48 private FragmentActivity mActivity; 49 @Mock 50 private ContentResolver mContentResolver; 51 @Mock 52 private SoundSettings mSetting; 53 54 private Context mContext; 55 private ScreenLockSoundPreferenceController mController; 56 private SwitchPreference mPreference; 57 58 @Before setUp()59 public void setUp() { 60 MockitoAnnotations.initMocks(this); 61 mContext = spy(RuntimeEnvironment.application); 62 when(mSetting.getActivity()).thenReturn(mActivity); 63 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 64 mPreference = new SwitchPreference(RuntimeEnvironment.application); 65 mController = new ScreenLockSoundPreferenceController(mContext, mSetting, null); 66 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 67 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 68 } 69 70 @Test isAvailable_byDefault_isTrue()71 public void isAvailable_byDefault_isTrue() { 72 assertThat(mController.isAvailable()).isTrue(); 73 } 74 75 @Test 76 @Config(qualifiers = "mcc999") isAvailable_whenNotVisible_isFalse()77 public void isAvailable_whenNotVisible_isFalse() { 78 assertThat(mController.isAvailable()).isFalse(); 79 } 80 81 @Test displayPreference_lockScreenSoundEnabled_shouldCheckedPreference()82 public void displayPreference_lockScreenSoundEnabled_shouldCheckedPreference() { 83 System.putInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1); 84 85 mController.displayPreference(mScreen); 86 87 assertThat(mPreference.isChecked()).isTrue(); 88 } 89 90 @Test displayPreference_lockScreenSoundDisabled_shouldUncheckedPreference()91 public void displayPreference_lockScreenSoundDisabled_shouldUncheckedPreference() { 92 System.putInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 0); 93 94 mController.displayPreference(mScreen); 95 96 assertThat(mPreference.isChecked()).isFalse(); 97 } 98 99 @Test onPreferenceChanged_preferenceChecked_shouldEnabledLockScreenSound()100 public void onPreferenceChanged_preferenceChecked_shouldEnabledLockScreenSound() { 101 mController.displayPreference(mScreen); 102 103 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true); 104 105 assertThat(System.getInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1)) 106 .isEqualTo(1); 107 } 108 109 @Test onPreferenceChanged_preferenceUnchecked_shouldDisabledLockScreenSound()110 public void onPreferenceChanged_preferenceUnchecked_shouldDisabledLockScreenSound() { 111 mController.displayPreference(mScreen); 112 113 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false); 114 115 assertThat(System.getInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1)) 116 .isEqualTo(0); 117 } 118 } 119