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.media.AudioManager; 26 import android.os.Vibrator; 27 import android.telephony.TelephonyManager; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.mockito.Mock; 33 import org.mockito.MockitoAnnotations; 34 import org.robolectric.RobolectricTestRunner; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.annotation.Config; 37 38 @RunWith(RobolectricTestRunner.class) 39 public class NotificationVolumePreferenceControllerTest { 40 41 @Mock 42 private AudioHelper mHelper; 43 @Mock 44 private TelephonyManager mTelephonyManager; 45 @Mock 46 private AudioManager mAudioManager; 47 @Mock 48 private Vibrator mVibrator; 49 50 private Context mContext; 51 private NotificationVolumePreferenceController mController; 52 53 @Before setUp()54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 mContext = spy(RuntimeEnvironment.application); 57 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 58 when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager); 59 when(mContext.getSystemService(Context.VIBRATOR_SERVICE)).thenReturn(mVibrator); 60 mController = new NotificationVolumePreferenceController(mContext); 61 mController.setAudioHelper(mHelper); 62 } 63 64 @Test 65 @Config(qualifiers = "mcc999") isAvailable_whenNotVisible_shouldReturnFalse()66 public void isAvailable_whenNotVisible_shouldReturnFalse() { 67 assertThat(mController.isAvailable()).isFalse(); 68 } 69 70 @Test isAvailable_singleVolume_shouldReturnFalse()71 public void isAvailable_singleVolume_shouldReturnFalse() { 72 when(mHelper.isSingleVolume()).thenReturn(true); 73 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 74 75 assertThat(mController.isAvailable()).isFalse(); 76 } 77 78 @Test isAvailable_voiceCapable_shouldReturnFalse()79 public void isAvailable_voiceCapable_shouldReturnFalse() { 80 when(mHelper.isSingleVolume()).thenReturn(false); 81 when(mTelephonyManager.isVoiceCapable()).thenReturn(true); 82 83 assertThat(mController.isAvailable()).isFalse(); 84 } 85 86 @Test isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue()87 public void isAvailable_notSingleVolume_notVoiceCapable_shouldReturnTrue() { 88 when(mHelper.isSingleVolume()).thenReturn(false); 89 when(mTelephonyManager.isVoiceCapable()).thenReturn(false); 90 91 assertThat(mController.isAvailable()).isTrue(); 92 } 93 94 @Test getAudioStream_shouldReturnNotification()95 public void getAudioStream_shouldReturnNotification() { 96 assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_NOTIFICATION); 97 } 98 99 @Test isSliceableCorrectKey_returnsTrue()100 public void isSliceableCorrectKey_returnsTrue() { 101 final NotificationVolumePreferenceController controller = 102 new NotificationVolumePreferenceController(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