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