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 android.app.Activity; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.support.v14.preference.SwitchPreference; 23 import android.support.v7.preference.PreferenceScreen; 24 import android.provider.Settings.System; 25 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.annotation.Config; 35 import org.robolectric.shadows.ShadowApplication; 36 37 import static com.google.common.truth.Truth.assertThat; 38 import static org.mockito.Mockito.doReturn; 39 import static org.mockito.Mockito.when; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 43 public class ScreenLockSoundPreferenceControllerTest { 44 45 @Mock 46 private PreferenceScreen mScreen; 47 @Mock 48 private Activity mActivity; 49 @Mock 50 private ContentResolver mContentResolver; 51 @Mock 52 private SoundSettings mSetting; 53 @Mock 54 private Context mContext; 55 56 private ScreenLockSoundPreferenceController mController; 57 private SwitchPreference mPreference; 58 59 @Before 60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 when(mSetting.getActivity()).thenReturn(mActivity); 63 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 64 mPreference = new SwitchPreference(ShadowApplication.getInstance().getApplicationContext()); 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 71 public void isAvailable_isAlwaysTrue() { 72 assertThat(mController.isAvailable()).isTrue(); 73 } 74 75 @Test 76 public void displayPreference_lockScreenSoundEnabled_shouldCheckedPreference() { 77 System.putInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1); 78 79 mController.displayPreference(mScreen); 80 81 assertThat(mPreference.isChecked()).isTrue(); 82 } 83 84 @Test 85 public void displayPreference_lockScreenSoundDisabled_shouldUncheckedPreference() { 86 System.putInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 0); 87 88 mController.displayPreference(mScreen); 89 90 assertThat(mPreference.isChecked()).isFalse(); 91 } 92 93 @Test 94 public void onPreferenceChanged_preferenceChecked_shouldEnabledLockScreenSound() { 95 mController.displayPreference(mScreen); 96 97 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true); 98 99 assertThat(System.getInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1)) 100 .isEqualTo(1); 101 } 102 103 @Test 104 public void onPreferenceChanged_preferenceUnchecked_shouldDisabledLockScreenSound() { 105 mController.displayPreference(mScreen); 106 107 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false); 108 109 assertThat(System.getInt(mContentResolver, System.LOCKSCREEN_SOUNDS_ENABLED, 1)) 110 .isEqualTo(0); 111 } 112 } 113