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