1 /* 2 * Copyright (C) 2017 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 org.mockito.ArgumentMatchers.anyInt; 20 import static org.mockito.ArgumentMatchers.eq; 21 import static org.mockito.Mockito.doCallRealMethod; 22 import static org.mockito.Mockito.never; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.res.Configuration; 28 import android.content.res.Resources; 29 import android.media.AudioManager; 30 import android.os.LocaleList; 31 import android.preference.SeekBarVolumizer; 32 import android.widget.SeekBar; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.ArgumentCaptor; 38 import org.mockito.Captor; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 43 import java.util.Locale; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class VolumeSeekBarPreferenceTest { 47 48 private static final CharSequence CONTENT_DESCRIPTION = "TEST"; 49 private static final int STREAM = 5; 50 @Mock 51 private AudioManager mAudioManager; 52 @Mock 53 private VolumeSeekBarPreference mPreference; 54 @Mock 55 private Context mContext; 56 57 @Mock 58 private Resources mRes; 59 @Mock 60 private Configuration mConfig; 61 @Mock 62 private SeekBar mSeekBar; 63 @Captor 64 private ArgumentCaptor<SeekBarVolumizer.Callback> mSbvc; 65 @Mock 66 private SeekBarVolumizer mVolumizer; 67 @Mock 68 private SeekBarVolumizerFactory mSeekBarVolumizerFactory; 69 private VolumeSeekBarPreference.Listener mListener; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 when(mContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mAudioManager); 75 when(mSeekBarVolumizerFactory.create(eq(STREAM), eq(null), mSbvc.capture())) 76 .thenReturn(mVolumizer); 77 doCallRealMethod().when(mPreference).setStream(anyInt()); 78 doCallRealMethod().when(mPreference).updateContentDescription(CONTENT_DESCRIPTION); 79 mPreference.mSeekBar = mSeekBar; 80 mPreference.mAudioManager = mAudioManager; 81 mPreference.mSeekBarVolumizerFactory = mSeekBarVolumizerFactory; 82 mListener = () -> mPreference.updateContentDescription(CONTENT_DESCRIPTION); 83 } 84 85 @Test setStream_shouldSetMinMaxAndProgress()86 public void setStream_shouldSetMinMaxAndProgress() { 87 final int max = 17; 88 final int min = 1; 89 final int progress = 4; 90 when(mAudioManager.getStreamMaxVolume(STREAM)).thenReturn(max); 91 when(mAudioManager.getStreamMinVolumeInt(STREAM)).thenReturn(min); 92 when(mAudioManager.getStreamVolume(STREAM)).thenReturn(progress); 93 94 mPreference.setStream(STREAM); 95 96 verify(mPreference).setMax(max); 97 verify(mPreference).setMin(min); 98 verify(mPreference).setProgress(progress); 99 } 100 101 @Test init_listenerIsCalled()102 public void init_listenerIsCalled() { 103 doCallRealMethod().when(mPreference).setListener(mListener); 104 doCallRealMethod().when(mPreference).init(); 105 106 mPreference.setStream(STREAM); 107 mPreference.setListener(mListener); 108 mPreference.init(); 109 110 verify(mPreference).updateContentDescription(CONTENT_DESCRIPTION); 111 } 112 113 @Test init_listenerNotSet_noException()114 public void init_listenerNotSet_noException() { 115 doCallRealMethod().when(mPreference).init(); 116 117 mPreference.setStream(STREAM); 118 mPreference.init(); 119 120 verify(mPreference, never()).updateContentDescription(CONTENT_DESCRIPTION); 121 } 122 123 @Test init_changeProgress_overrideStateDescriptionCalled()124 public void init_changeProgress_overrideStateDescriptionCalled() { 125 final int progress = 4; 126 when(mPreference.formatStateDescription(progress)).thenReturn(CONTENT_DESCRIPTION); 127 doCallRealMethod().when(mPreference).init(); 128 129 mPreference.setStream(STREAM); 130 mPreference.init(); 131 132 verify(mSeekBarVolumizerFactory).create(eq(STREAM), eq(null), mSbvc.capture()); 133 134 mSbvc.getValue().onProgressChanged(mSeekBar, 4, true); 135 136 verify(mPreference).overrideSeekBarStateDescription(CONTENT_DESCRIPTION); 137 } 138 139 @Test init_changeProgress_stateDescriptionValueUpdated()140 public void init_changeProgress_stateDescriptionValueUpdated() { 141 final int max = 17; 142 final int min = 1; 143 int progress = 4; 144 when(mAudioManager.getStreamMaxVolume(STREAM)).thenReturn(max); 145 when(mAudioManager.getStreamMinVolumeInt(STREAM)).thenReturn(min); 146 when(mAudioManager.getStreamVolume(STREAM)).thenReturn(progress); 147 when(mPreference.getMin()).thenReturn(min); 148 when(mPreference.getMax()).thenReturn(max); 149 when(mPreference.getContext()).thenReturn(mContext); 150 when(mContext.getResources()).thenReturn(mRes); 151 when(mRes.getConfiguration()).thenReturn(mConfig); 152 when(mConfig.getLocales()).thenReturn(new LocaleList(Locale.US)); 153 doCallRealMethod().when(mPreference).init(); 154 155 mPreference.setStream(STREAM); 156 mPreference.init(); 157 158 // On progress change, Round down the percent to match it with what the talkback says. 159 // (b/285458191) 160 // when progress is 4, the percent is 0.187. The state description should be set to 18%. 161 testFormatStateDescription(progress, "18%"); 162 163 progress = 6; 164 165 // when progress is 6, the percent is 0.3125. The state description should be set to 31%. 166 testFormatStateDescription(progress, "31%"); 167 168 progress = 7; 169 170 // when progress is 7, the percent is 0.375. The state description should be set to 37%. 171 testFormatStateDescription(progress, "37%"); 172 } 173 testFormatStateDescription(int progress, String expected)174 private void testFormatStateDescription(int progress, String expected) { 175 doCallRealMethod().when(mPreference).formatStateDescription(progress); 176 doCallRealMethod().when(mPreference).getPercent(progress); 177 178 mSbvc.getValue().onProgressChanged(mSeekBar, progress, true); 179 180 verify(mPreference).overrideSeekBarStateDescription(expected); 181 } 182 } 183