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.provider.DeviceConfig; 31 import android.service.notification.NotificationListenerService; 32 import android.telephony.TelephonyManager; 33 34 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags; 35 import com.android.settings.core.BasePreferenceController; 36 import com.android.settings.testutils.shadow.ShadowDeviceConfig; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.MockitoAnnotations; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.annotation.Config; 46 import org.robolectric.shadows.ShadowApplication; 47 48 @RunWith(RobolectricTestRunner.class) 49 @Config(shadows = {ShadowDeviceConfig.class}) 50 public class RingVolumePreferenceControllerTest { 51 52 @Mock 53 private AudioHelper mHelper; 54 @Mock 55 private TelephonyManager mTelephonyManager; 56 @Mock 57 private AudioManager mAudioManager; 58 @Mock 59 private Vibrator mVibrator; 60 @Mock 61 private NotificationManager mNotificationManager; 62 @Mock 63 private ComponentName mSuppressor; 64 @Mock 65 private Resources mResources; 66 @Mock 67 private VolumeSeekBarPreference mPreference; 68 69 private Context mContext; 70 71 private RingVolumePreferenceController mController; 72 73 @Before setUp()74 public void setUp() { 75 MockitoAnnotations.initMocks(this); 76 ShadowApplication shadowContext = ShadowApplication.getInstance(); 77 shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager); 78 shadowContext.setSystemService(Context.AUDIO_SERVICE, mAudioManager); 79 shadowContext.setSystemService(Context.VIBRATOR_SERVICE, mVibrator); 80 shadowContext.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 81 mContext = spy(RuntimeEnvironment.application); 82 when(mNotificationManager.getEffectsSuppressor()).thenReturn(mSuppressor); 83 when(mContext.getResources()).thenReturn(mResources); 84 mController = new RingVolumePreferenceController(mContext); 85 mController.setAudioHelper(mHelper); 86 87 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 88 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "false", false); 89 } 90 91 @Test isAvailable_singleVolume_shouldReturnFalse()92 public void isAvailable_singleVolume_shouldReturnFalse() { 93 when(mHelper.isSingleVolume()).thenReturn(true); 94 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 95 96 assertThat(mController.isAvailable()).isFalse(); 97 } 98 99 /** 100 * Devices that are not voice capable should still show Ring volume, because it is used by apps 101 * that make calls outside the cell network. 102 */ 103 @Test isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue()104 public void isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue() { 105 when(mHelper.isSingleVolume()).thenReturn(false); 106 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 107 108 assertThat(mController.isAvailable()).isTrue(); 109 } 110 111 @Test isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue()112 public void isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue() { 113 114 when(mHelper.isSingleVolume()).thenReturn(false); 115 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 116 117 assertThat(mController.isAvailable()).isTrue(); 118 } 119 120 @Test getAudioStream_shouldReturnRing()121 public void getAudioStream_shouldReturnRing() { 122 assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING); 123 } 124 125 @Test isSliceableCorrectKey_returnsTrue()126 public void isSliceableCorrectKey_returnsTrue() { 127 final RingVolumePreferenceController controller = 128 new RingVolumePreferenceController(mContext); 129 assertThat(controller.isSliceable()).isTrue(); 130 } 131 132 @Test isPublicSlice_returnTrue()133 public void isPublicSlice_returnTrue() { 134 assertThat(mController.isPublicSlice()).isTrue(); 135 } 136 137 /** 138 * Only when the two streams are merged would this controller appear 139 */ 140 @Test ringNotificationStreamsSeparate_controllerIsNotAvailable()141 public void ringNotificationStreamsSeparate_controllerIsNotAvailable() { 142 143 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 144 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "true", false); 145 146 final RingVolumePreferenceController controller = 147 new RingVolumePreferenceController(mContext); 148 149 int controllerAvailability = controller.getAvailabilityStatus(); 150 151 assertThat(controllerAvailability) 152 .isNotEqualTo(BasePreferenceController.AVAILABLE); 153 } 154 155 @Test setHintsRing_aliased_Matches()156 public void setHintsRing_aliased_Matches() { 157 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 158 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "false", false); 159 160 161 assertThat(mController.hintsMatch( 162 NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS)).isTrue(); 163 } 164 165 @Test setHintsRingNotification_aliased_Matches()166 public void setHintsRingNotification_aliased_Matches() { 167 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 168 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "false", false); 169 170 assertThat(mController.hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_EFFECTS)) 171 .isTrue(); 172 } 173 174 @Test setHintNotification_aliased_Matches()175 public void setHintNotification_aliased_Matches() { 176 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 177 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "false", false); 178 179 180 assertThat(mController 181 .hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS)) 182 .isTrue(); 183 } 184 185 @Test setHintsRing_unaliased_Matches()186 public void setHintsRing_unaliased_Matches() { 187 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 188 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "true", false); 189 190 assertThat(mController.hintsMatch( 191 NotificationListenerService.HINT_HOST_DISABLE_CALL_EFFECTS)).isTrue(); 192 } 193 194 @Test setHintsRingNotification_unaliased_Matches()195 public void setHintsRingNotification_unaliased_Matches() { 196 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 197 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "true", false); 198 199 assertThat(mController.hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_EFFECTS)) 200 .isTrue(); 201 } 202 203 @Test setHintNotification_unaliased_doesNotMatch()204 public void setHintNotification_unaliased_doesNotMatch() { 205 DeviceConfig.setProperty(DeviceConfig.NAMESPACE_SYSTEMUI, 206 SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, "true", false); 207 208 assertThat(mController 209 .hintsMatch(NotificationListenerService.HINT_HOST_DISABLE_NOTIFICATION_EFFECTS)) 210 .isFalse(); 211 } 212 213 @Test setRingerModeToVibrate_butNoVibratorAvailable_iconIsSilent()214 public void setRingerModeToVibrate_butNoVibratorAvailable_iconIsSilent() { 215 when(mHelper.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); 216 217 mController.setPreference(mPreference); 218 mController.setVibrator(null); 219 mController.updateRingerMode(); 220 221 assertThat(mController.getMuteIcon()).isEqualTo(mController.mSilentIconId); 222 } 223 224 @Test setRingerModeToVibrate_VibratorAvailable_iconIsVibrate()225 public void setRingerModeToVibrate_VibratorAvailable_iconIsVibrate() { 226 when(mHelper.getRingerModeInternal()).thenReturn(AudioManager.RINGER_MODE_VIBRATE); 227 when(mVibrator.hasVibrator()).thenReturn(true); 228 229 mController.setPreference(mPreference); 230 mController.setVibrator(mVibrator); 231 mController.updateRingerMode(); 232 233 assertThat(mController.getMuteIcon()).isEqualTo(mController.mVibrateIconId); 234 } 235 236 } 237