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.when;
22 
23 import android.app.NotificationManager;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.media.AudioManager;
27 import android.os.Vibrator;
28 import android.telephony.TelephonyManager;
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.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.shadows.ShadowApplication;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class RingVolumePreferenceControllerTest {
41 
42     @Mock
43     private AudioHelper mHelper;
44     @Mock
45     private TelephonyManager mTelephonyManager;
46     @Mock
47     private AudioManager mAudioManager;
48     @Mock
49     private Vibrator mVibrator;
50     @Mock
51     private NotificationManager mNotificationManager;
52     @Mock
53     private ComponentName mSuppressor;
54 
55     private Context mContext;
56     private RingVolumePreferenceController mController;
57 
58     @Before
setUp()59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61         ShadowApplication shadowContext = ShadowApplication.getInstance();
62         shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
63         shadowContext.setSystemService(Context.AUDIO_SERVICE, mAudioManager);
64         shadowContext.setSystemService(Context.VIBRATOR_SERVICE, mVibrator);
65         shadowContext.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
66         mContext = RuntimeEnvironment.application;
67         when(mNotificationManager.getEffectsSuppressor()).thenReturn(mSuppressor);
68         mController = new RingVolumePreferenceController(mContext);
69         mController.setAudioHelper(mHelper);
70     }
71 
72     @Test
isAvailable_singleVolume_shouldReturnFalse()73     public void isAvailable_singleVolume_shouldReturnFalse() {
74         when(mHelper.isSingleVolume()).thenReturn(true);
75         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
76 
77         assertThat(mController.isAvailable()).isFalse();
78     }
79 
80     @Test
isAvailable_notVoiceCapable_shouldReturnFalse()81     public void isAvailable_notVoiceCapable_shouldReturnFalse() {
82         when(mHelper.isSingleVolume()).thenReturn(false);
83         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
84 
85         assertThat(mController.isAvailable()).isFalse();
86     }
87 
88     @Test
isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue()89     public void isAvailable_notSingleVolume_VoiceCapable_shouldReturnTrue() {
90         when(mHelper.isSingleVolume()).thenReturn(false);
91         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
92 
93         assertThat(mController.isAvailable()).isTrue();
94     }
95 
96     @Test
getAudioStream_shouldReturnRing()97     public void getAudioStream_shouldReturnRing() {
98         assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING);
99     }
100 
101     @Test
isSliceableCorrectKey_returnsTrue()102     public void isSliceableCorrectKey_returnsTrue() {
103         final RingVolumePreferenceController controller =
104                 new RingVolumePreferenceController(mContext);
105         assertThat(controller.isSliceable()).isTrue();
106     }
107 
108     @Test
isPublicSlice_returnTrue()109     public void isPublicSlice_returnTrue() {
110         assertThat(mController.isPublicSlice()).isTrue();
111     }
112 }
113