1 /*
2  * Copyright (C) 2016 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.spy;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 import android.content.res.Resources;
26 import android.media.AudioManager;
27 import android.os.Vibrator;
28 import android.service.notification.NotificationListenerService;
29 import android.telephony.TelephonyManager;
30 
31 import androidx.preference.PreferenceManager;
32 
33 import com.android.settings.core.BasePreferenceController;
34 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 
45 @RunWith(RobolectricTestRunner.class)
46 @Config(shadows = {ShadowDeviceConfig.class})
47 public class NotificationVolumePreferenceControllerTest {
48     @Mock
49     private AudioHelper mHelper;
50     @Mock
51     private TelephonyManager mTelephonyManager;
52     @Mock
53     private AudioManager mAudioManager;
54     @Mock
55     private Vibrator mVibrator;
56     @Mock
57     private Resources mResources;
58     @Mock
59     private PreferenceManager mPreferenceManager;
60 
61     private Context mContext;
62     private NotificationVolumePreferenceController mController;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67         mContext = spy(RuntimeEnvironment.application);
68         when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
69         when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager);
70         when(mContext.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator);
71         when(mContext.getResources()).thenReturn(mResources);
72 
73         mController = new NotificationVolumePreferenceController(mContext);
74         mController.setAudioHelper(mHelper);
75     }
76 
77     @Test
78     @Config(qualifiers = "mcc999")
isAvailable_whenNotVisible_shouldReturnFalse()79     public void isAvailable_whenNotVisible_shouldReturnFalse() {
80         assertThat(mController.isAvailable()).isFalse();
81     }
82 
83     @Test
isAvailable_singleVolume_shouldReturnFalse()84     public void isAvailable_singleVolume_shouldReturnFalse() {
85         when(mHelper.isSingleVolume()).thenReturn(true);
86         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
87 
88         assertThat(mController.isAvailable()).isFalse();
89     }
90 
91     /**
92      * With the introduction of ring-notification volume separation, voice-capable devices could now
93      * display the notification volume slider.
94      */
95     @Test
isAvailable_whenVoiceCapable_shouldReturnTrue()96     public void isAvailable_whenVoiceCapable_shouldReturnTrue() {
97         when(mResources.getBoolean(
98                 com.android.settings.R.bool.config_show_notification_volume)).thenReturn(true);
99 
100         NotificationVolumePreferenceController controller =
101                 new NotificationVolumePreferenceController(mContext);
102 
103         when(mHelper.isSingleVolume()).thenReturn(false);
104         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
105 
106         assertThat(controller.isAvailable()).isTrue();
107     }
108 
109     @Test
isAvailable_notShowNotificationVolume_shouldReturnFalse()110     public void isAvailable_notShowNotificationVolume_shouldReturnFalse() {
111         when(mResources.getBoolean(
112                 com.android.settings.R.bool.config_show_notification_volume)).thenReturn(false);
113 
114         assertThat(mController.isAvailable()).isFalse();
115     }
116 
117     @Test
isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue()118     public void isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue() {
119         when(mResources.getBoolean(
120                 com.android.settings.R.bool.config_show_notification_volume)).thenReturn(true);
121         when(mHelper.isSingleVolume()).thenReturn(false);
122         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
123 
124         assertThat(mController.isAvailable()).isTrue();
125     }
126 
127     @Test
getAudioStream_shouldReturnNotification()128     public void getAudioStream_shouldReturnNotification() {
129         assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_NOTIFICATION);
130     }
131 
132     @Test
isSliceableCorrectKey_returnsTrue()133     public void isSliceableCorrectKey_returnsTrue() {
134         final NotificationVolumePreferenceController controller =
135                 new NotificationVolumePreferenceController(mContext);
136         assertThat(controller.isSliceable()).isTrue();
137     }
138 
139     @Test
isPublicSlice_returnTrue()140     public void isPublicSlice_returnTrue() {
141         assertThat(mController.isPublicSlice()).isTrue();
142     }
143 
144     @Test
setHintsRing_DoesNotMatch()145     public void setHintsRing_DoesNotMatch() {
146         assertThat(mController.hintsMatch(
147                 NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS)).isFalse();
148     }
149 
150     @Test
setHintsAll_Matches()151     public void setHintsAll_Matches() {
152         assertThat(mController.hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_EFFECTS))
153                 .isTrue();
154     }
155 
156     @Test
setHintNotification_Matches()157     public void setHintNotification_Matches() {
158         assertThat(mController
159                 .hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS))
160                 .isTrue();
161     }
162 
163     @Test
ringerModeSilent_getAvailability_returnsDisabled()164     public void ringerModeSilent_getAvailability_returnsDisabled() {
165         when(mResources.getBoolean(
166                 com.android.settings.R.bool.config_show_notification_volume)).thenReturn(true);
167         when(mHelper.isSingleVolume()).thenReturn(false);
168 
169         when(mAudioManager.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_SILENT);
170 
171         assertThat(mController.getAvailabilityStatus())
172                 .isEqualTo(BasePreferenceController.DISABLED_DEPENDENT_SETTING);
173     }
174 
175 }
176