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.app.NotificationManager;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.res.Resources;
28 import android.media.AudioManager;
29 import android.os.Vibrator;
30 import android.service.notification.NotificationListenerService;
31 import android.telephony.TelephonyManager;
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 import org.robolectric.shadows.ShadowApplication;
45 
46 @RunWith(RobolectricTestRunner.class)
47 @Config(shadows = {ShadowDeviceConfig.class})
48 public class RingVolumePreferenceControllerTest {
49 
50     @Mock
51     private AudioHelper mHelper;
52     @Mock
53     private TelephonyManager mTelephonyManager;
54     @Mock
55     private AudioManager mAudioManager;
56     @Mock
57     private Vibrator mVibrator;
58     @Mock
59     private NotificationManager mNotificationManager;
60     @Mock
61     private ComponentName mSuppressor;
62     @Mock
63     private Resources mResources;
64     @Mock
65     private VolumeSeekBarPreference mPreference;
66 
67     private Context mContext;
68 
69     private RingVolumePreferenceController mController;
70 
71     @Before
setUp()72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74         ShadowApplication shadowContext = ShadowApplication.getInstance();
75         shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
76         shadowContext.setSystemService(Context.AUDIO_SERVICE, mAudioManager);
77         shadowContext.setSystemService(Context.VIBRATOR_SERVICE, mVibrator);
78         shadowContext.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
79         mContext = spy(RuntimeEnvironment.application);
80         when(mNotificationManager.getEffectsSuppressor()).thenReturn(mSuppressor);
81         when(mContext.getResources()).thenReturn(mResources);
82         mController = new RingVolumePreferenceController(mContext);
83         mController.setAudioHelper(mHelper);
84     }
85 
86     @Test
isAvailable_singleVolume_shouldReturnFalse()87     public void isAvailable_singleVolume_shouldReturnFalse() {
88         when(mHelper.isSingleVolume()).thenReturn(true);
89         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
90 
91         assertThat(mController.isAvailable()).isFalse();
92     }
93 
94     @Test
getAudioStream_shouldReturnRing()95     public void getAudioStream_shouldReturnRing() {
96         assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING);
97     }
98 
99     @Test
isSliceableCorrectKey_returnsTrue()100     public void isSliceableCorrectKey_returnsTrue() {
101         final RingVolumePreferenceController controller =
102                 new RingVolumePreferenceController(mContext);
103         assertThat(controller.isSliceable()).isTrue();
104     }
105 
106     @Test
isPublicSlice_returnTrue()107     public void isPublicSlice_returnTrue() {
108         assertThat(mController.isPublicSlice()).isTrue();
109     }
110 
111     /**
112      * Only when the two streams are merged would this controller appear
113      */
114     @Test
ringNotificationStreamsSeparate_controllerIsNotAvailable()115     public void ringNotificationStreamsSeparate_controllerIsNotAvailable() {
116         final RingVolumePreferenceController controller =
117                 new RingVolumePreferenceController(mContext);
118 
119         int controllerAvailability = controller.getAvailabilityStatus();
120 
121         assertThat(controllerAvailability)
122                 .isNotEqualTo(BasePreferenceController.AVAILABLE);
123     }
124 
125     @Test
setHintsRing_Matches()126     public void setHintsRing_Matches() {
127         assertThat(mController.hintsMatch(
128                 NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS)).isTrue();
129     }
130 
131     @Test
setHintsRingNotification_Matches()132     public void setHintsRingNotification_Matches() {
133         assertThat(mController.hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_EFFECTS))
134                 .isTrue();
135     }
136 
137     @Test
setHintNotification_doesNotMatch()138     public void setHintNotification_doesNotMatch() {
139         assertThat(mController
140                 .hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS))
141                 .isFalse();
142     }
143 
144     @Test
setRingerModeToVibrate_butNoVibratorAvailable_iconIsSilent()145     public void setRingerModeToVibrate_butNoVibratorAvailable_iconIsSilent() {
146         when(mHelper.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
147 
148         mController.setPreference(mPreference);
149         mController.setVibrator(null);
150         mController.updateRingerMode();
151 
152         assertThat(mController.getMuteIcon()).isEqualTo(mController.mSilentIconId);
153     }
154 
155     @Test
setRingerModeToVibrate_VibratorAvailable_iconIsVibrate()156     public void setRingerModeToVibrate_VibratorAvailable_iconIsVibrate() {
157         when(mHelper.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE);
158         when(mVibrator.hasVibrator()).thenReturn(true);
159 
160         mController.setPreference(mPreference);
161         mController.setVibrator(mVibrator);
162         mController.updateRingerMode();
163 
164         assertThat(mController.getMuteIcon()).isEqualTo(mController.mVibrateIconId);
165     }
166 
167 }
168