• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         when(mPreference.isEnabled()).thenReturn(true);
104         doCallRealMethod().when(mPreference).setListener(mListener);
105         doCallRealMethod().when(mPreference).init();
106 
107         mPreference.setStream(STREAM);
108         mPreference.setListener(mListener);
109         mPreference.init();
110 
111         verify(mPreference).updateContentDescription(CONTENT_DESCRIPTION);
112     }
113 
114     @Test
init_listenerNotSet_noException()115     public void init_listenerNotSet_noException() {
116         when(mPreference.isEnabled()).thenReturn(true);
117         doCallRealMethod().when(mPreference).init();
118 
119         mPreference.setStream(STREAM);
120         mPreference.init();
121 
122         verify(mPreference, never()).updateContentDescription(CONTENT_DESCRIPTION);
123     }
124 
125     @Test
init_preferenceIsDisabled_shouldNotInvokeListener()126     public void init_preferenceIsDisabled_shouldNotInvokeListener() {
127         when(mPreference.isEnabled()).thenReturn(false);
128         doCallRealMethod().when(mPreference).setListener(mListener);
129         doCallRealMethod().when(mPreference).init();
130 
131         mPreference.setStream(STREAM);
132         mPreference.init();
133 
134         verify(mPreference, never()).updateContentDescription(CONTENT_DESCRIPTION);
135     }
136 
137     @Test
init_changeProgress_overrideStateDescriptionCalled()138     public void init_changeProgress_overrideStateDescriptionCalled() {
139         final int progress = 4;
140         when(mPreference.isEnabled()).thenReturn(true);
141         when(mPreference.formatStateDescription(progress)).thenReturn(CONTENT_DESCRIPTION);
142         doCallRealMethod().when(mPreference).init();
143 
144         mPreference.setStream(STREAM);
145         mPreference.init();
146 
147         verify(mSeekBarVolumizerFactory).create(eq(STREAM), eq(null), mSbvc.capture());
148 
149         mSbvc.getValue().onProgressChanged(mSeekBar, 4, true);
150 
151         verify(mPreference).overrideSeekBarStateDescription(CONTENT_DESCRIPTION);
152     }
153 
154     @Test
init_changeProgress_stateDescriptionValueUpdated()155     public void init_changeProgress_stateDescriptionValueUpdated() {
156         final int max = 17;
157         final int min = 1;
158         int progress = 4;
159         when(mAudioManager.getStreamMaxVolume(STREAM)).thenReturn(max);
160         when(mAudioManager.getStreamMinVolumeInt(STREAM)).thenReturn(min);
161         when(mAudioManager.getStreamVolume(STREAM)).thenReturn(progress);
162         when(mPreference.isEnabled()).thenReturn(true);
163         when(mPreference.getMin()).thenReturn(min);
164         when(mPreference.getMax()).thenReturn(max);
165         when(mPreference.getContext()).thenReturn(mContext);
166         when(mContext.getResources()).thenReturn(mRes);
167         when(mRes.getConfiguration()).thenReturn(mConfig);
168         when(mConfig.getLocales()).thenReturn(new LocaleList(Locale.US));
169         doCallRealMethod().when(mPreference).init();
170 
171         mPreference.setStream(STREAM);
172         mPreference.init();
173 
174         verify(mSeekBarVolumizerFactory).create(eq(STREAM), eq(null), mSbvc.capture());
175 
176         // On progress change, Round down the percent to match it with what the talkback says.
177         // (b/285458191)
178         // when progress is 4, the percent is 0.187. The state description should be set to 18%.
179         testFormatStateDescription(progress, "18%");
180 
181         progress = 6;
182 
183         // when progress is 6, the percent is 0.3125. The state description should be set to 31%.
184         testFormatStateDescription(progress, "31%");
185 
186         progress = 7;
187 
188         // when progress is 7, the percent is 0.375. The state description should be set to 37%.
189         testFormatStateDescription(progress, "37%");
190     }
191 
testFormatStateDescription(int progress, String expected)192     private void testFormatStateDescription(int progress, String expected) {
193         doCallRealMethod().when(mPreference).formatStateDescription(progress);
194         doCallRealMethod().when(mPreference).getPercent(progress);
195 
196         mSbvc.getValue().onProgressChanged(mSeekBar, progress, true);
197 
198         verify(mPreference).overrideSeekBarStateDescription(expected);
199     }
200 }
201