Home | History | Annotate | Download | only in notification
      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 import static org.mockito.Mockito.when;
     21 
     22 import android.app.NotificationManager;
     23 import android.content.ComponentName;
     24 import android.content.Context;
     25 import android.media.AudioManager;
     26 import android.os.Vibrator;
     27 import android.telephony.TelephonyManager;
     28 
     29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.Mock;
     35 import org.mockito.MockitoAnnotations;
     36 import org.robolectric.RuntimeEnvironment;
     37 import org.robolectric.shadows.ShadowApplication;
     38 
     39 @RunWith(SettingsRobolectricTestRunner.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
     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
     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
     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
     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
     97     public void getAudioStream_shouldReturnRing() {
     98         assertThat(mController.getAudioStream()).isEqualTo(AudioManager.STREAM_RING);
     99     }
    100 
    101     @Test
    102     public void isSliceableCorrectKey_returnsTrue() {
    103         final RingVolumePreferenceController controller =
    104                 new RingVolumePreferenceController(mContext);
    105         assertThat(controller.isSliceable()).isTrue();
    106     }
    107 }
    108