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 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.app.Activity; 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.media.AudioManager; 28 import android.provider.Settings.System; 29 import android.support.v14.preference.SwitchPreference; 30 import android.support.v7.preference.PreferenceScreen; 31 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RuntimeEnvironment; 40 import org.robolectric.annotation.Config; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class TouchSoundPreferenceControllerTest { 44 45 @Mock 46 private AudioManager mAudioManager; 47 @Mock 48 private PreferenceScreen mScreen; 49 @Mock 50 private Activity mActivity; 51 @Mock 52 private ContentResolver mContentResolver; 53 @Mock 54 private SoundSettings mSetting; 55 56 private Context mContext; 57 private TouchSoundPreferenceController mController; 58 private SwitchPreference mPreference; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = spy(RuntimeEnvironment.application); 64 when(mActivity.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager); 65 when(mSetting.getActivity()).thenReturn(mActivity); 66 when(mActivity.getContentResolver()).thenReturn(mContentResolver); 67 mPreference = new SwitchPreference(RuntimeEnvironment.application); 68 mController = new TouchSoundPreferenceController(mContext, mSetting, null); 69 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 70 doReturn(mScreen).when(mSetting).getPreferenceScreen(); 71 } 72 73 @Test isAvailable_byDefault_isTrue()74 public void isAvailable_byDefault_isTrue() { 75 assertThat(mController.isAvailable()).isTrue(); 76 } 77 78 @Test 79 @Config(qualifiers = "mcc999") isAvailable_whenNotVisible_isFalse()80 public void isAvailable_whenNotVisible_isFalse() { 81 assertThat(mController.isAvailable()).isFalse(); 82 } 83 84 @Test displayPreference_soundEffectEnabled_shouldCheckedPreference()85 public void displayPreference_soundEffectEnabled_shouldCheckedPreference() { 86 System.putInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 1); 87 88 mController.displayPreference(mScreen); 89 90 assertThat(mPreference.isChecked()).isTrue(); 91 } 92 93 @Test displayPreference_soundEffectDisabled_shouldUncheckedPreference()94 public void displayPreference_soundEffectDisabled_shouldUncheckedPreference() { 95 System.putInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 0); 96 97 mController.displayPreference(mScreen); 98 99 assertThat(mPreference.isChecked()).isFalse(); 100 } 101 102 @Test onPreferenceChanged_preferenceChecked_shouldEnabledSoundEffect()103 public void onPreferenceChanged_preferenceChecked_shouldEnabledSoundEffect() { 104 mController.displayPreference(mScreen); 105 106 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, true); 107 108 assertThat(System.getInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 1)).isEqualTo(1); 109 } 110 111 @Test onPreferenceChanged_preferenceUnchecked_shouldDisabledSoundEffect()112 public void onPreferenceChanged_preferenceUnchecked_shouldDisabledSoundEffect() { 113 mController.displayPreference(mScreen); 114 115 mPreference.getOnPreferenceChangeListener().onPreferenceChange(mPreference, false); 116 117 assertThat(System.getInt(mContentResolver, System.SOUND_EFFECTS_ENABLED, 1)).isEqualTo(0); 118 } 119 } 120