1 /*
2  * Copyright (C) 2022 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.telephony.TelephonyManager;
31 
32 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 import org.robolectric.shadows.ShadowApplication;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = {ShadowDeviceConfig.class})
46 public class SeparateRingVolumePreferenceControllerTest {
47 
48     @Mock
49     private AudioHelper mHelper;
50     @Mock
51     private TelephonyManager mTelephonyManager;
52     @Mock
53     private AudioManager mAudioManager;
54     @Mock
55     private Vibrator mVibrator;
56     @Mock
57     private NotificationManager mNotificationManager;
58     @Mock
59     private ComponentName mSuppressor;
60     @Mock
61     private Resources mResources;
62 
63     private Context mContext;
64 
65     private SeparateRingVolumePreferenceController mController;
66 
67     @Before
setUp()68     public void setUp() {
69         MockitoAnnotations.initMocks(this);
70         ShadowApplication shadowContext = ShadowApplication.getInstance();
71         shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
72         shadowContext.setSystemService(Context.AUDIO_SERVICE, mAudioManager);
73         shadowContext.setSystemService(Context.VIBRATOR_SERVICE, mVibrator);
74         shadowContext.setSystemService(Context.NOTIFICATION_SERVICE, mNotificationManager);
75         mContext = spy(RuntimeEnvironment.application);
76         when(mNotificationManager.getEffectsSuppressor()).thenReturn(mSuppressor);
77         when(mContext.getResources()).thenReturn(mResources);
78         mController = new SeparateRingVolumePreferenceController(mContext);
79         mController.setAudioHelper(mHelper);
80     }
81 
82     /**
83      * Maintain that the device does not need to be voice capable to display this slider
84      */
85     @Test
isAvailable_whenNotVoiceCapable_shouldReturnTrue()86     public void isAvailable_whenNotVoiceCapable_shouldReturnTrue() {
87         when(mHelper.isSingleVolume()).thenReturn(false);
88         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
89 
90         assertThat(mController.isAvailable()).isTrue();
91     }
92 
93     @Test
getAudioStream_shouldReturnRing()94     public void getAudioStream_shouldReturnRing() {
95         assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING);
96     }
97 
98     @Test
isSliceableCorrectKey_returnsTrue()99     public void isSliceableCorrectKey_returnsTrue() {
100         final SeparateRingVolumePreferenceController controller =
101                 new SeparateRingVolumePreferenceController(mContext);
102         assertThat(controller.isSliceable()).isTrue();
103     }
104 
105     @Test
isPublicSlice_returnTrue()106     public void isPublicSlice_returnTrue() {
107         assertThat(mController.isPublicSlice()).isTrue();
108     }
109 
110 }
111