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 android.app.NotificationManager; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.media.AudioManager; 24 import android.os.Vibrator; 25 import android.telephony.TelephonyManager; 26 27 import com.android.settings.SettingsRobolectricTestRunner; 28 import com.android.settings.TestConfig; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.annotation.Config; 36 import org.robolectric.shadows.ShadowApplication; 37 38 import static com.google.common.truth.Truth.assertThat; 39 import static org.mockito.Mockito.when; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 43 public class RingVolumePreferenceControllerTest { 44 45 @Mock 46 private AudioHelper mHelper; 47 @Mock 48 private TelephonyManager mTelephonyManager; 49 @Mock 50 private AudioManager mAudioManager; 51 @Mock 52 private Vibrator mVibrator; 53 @Mock 54 private NotificationManager mNotificationManager; 55 @Mock 56 private ComponentName mSuppressor; 57 58 private Context mContext; 59 private RingVolumePreferenceController mController; 60 61 @Before setUp()62 public void setUp() { 63 MockitoAnnotations.initMocks(this); 64 ShadowApplication shadowContext = ShadowApplication.getInstance(); 65 shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager); 66 shadowContext.setSystemService(Context.AUDIO_SERVICE, mAudioManager); 67 shadowContext.setSystemService(Context.VIBRATOR_SERVICE, mVibrator); 68 shadowContext.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager); 69 mContext = shadowContext.getApplicationContext(); 70 when(mNotificationManager.getEffectsSuppressor()).thenReturn(mSuppressor); 71 mController = new RingVolumePreferenceController(mContext, null, null, mHelper); 72 } 73 74 @Test isAvailable_singleVolume_shouldReturnFalse()75 public void isAvailable_singleVolume_shouldReturnFalse() { 76 when(mHelper.isSingleVolume()).thenReturn(true); 77 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 78 79 assertThat(mController.isAvailable()).isFalse(); 80 } 81 82 @Test isAvailable_notVoiceCapable_shouldReturnFalse()83 public void isAvailable_notVoiceCapable_shouldReturnFalse() { 84 when(mHelper.isSingleVolume()).thenReturn(false); 85 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 86 87 assertThat(mController.isAvailable()).isFalse(); 88 } 89 90 @Test isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue()91 public void isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue() { 92 when(mHelper.isSingleVolume()).thenReturn(false); 93 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 94 95 assertThat(mController.isAvailable()).isTrue(); 96 } 97 98 @Test getAudioStream_shouldReturnRing()99 public void getAudioStream_shouldReturnRing() { 100 assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING); 101 } 102 103 } 104