• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ArgumentMatchers.nullable;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Matchers.anyInt;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceScreen;
30 
31 import com.android.settings.testutils.SettingsRobolectricTestRunner;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 @RunWith(SettingsRobolectricTestRunner.class)
40 public class VolumeSeekBarPreferenceControllerTest {
41 
42     @Mock
43     private Context mContext;
44     @Mock
45     private PreferenceScreen mScreen;
46     @Mock
47     private VolumeSeekBarPreference mPreference;
48     @Mock
49     private VolumeSeekBarPreference.Callback mCallback;
50     @Mock
51     private AudioHelper mHelper;
52 
53     private VolumeSeekBarPreferenceControllerTestable mController;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         when(mScreen.findPreference(nullable(String.class))).thenReturn(mPreference);
59         when(mPreference.getKey()).thenReturn("key");
60         mController = new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback,
61                 mPreference.getKey());
62         mController.setAudioHelper(mHelper);
63     }
64 
65     @Test
displayPreference_available_shouldUpdatePreference()66     public void displayPreference_available_shouldUpdatePreference() {
67         mController.displayPreference(mScreen);
68 
69         verify(mPreference).setCallback(mCallback);
70         verify(mPreference).setStream(VolumeSeekBarPreferenceControllerTestable.AUDIO_STREAM);
71         verify(mPreference).setMuteIcon(VolumeSeekBarPreferenceControllerTestable.MUTE_ICON);
72     }
73 
74     @Test
displayPreference_notAvailable_shouldNotUpdatePreference()75     public void displayPreference_notAvailable_shouldNotUpdatePreference() {
76         mController = new VolumeSeekBarPreferenceControllerTestable(mContext, mCallback, false,
77                 mPreference.getKey());
78 
79         mController.displayPreference(mScreen);
80 
81         verify(mPreference, never()).setCallback(any(VolumeSeekBarPreference.Callback.class));
82         verify(mPreference, never()).setStream(anyInt());
83         verify(mPreference, never()).setMuteIcon(anyInt());
84     }
85 
86     @Test
onResume_shouldResumePreference()87     public void onResume_shouldResumePreference() {
88         mController.displayPreference(mScreen);
89 
90         mController.onResume();
91 
92         verify(mPreference).onActivityResume();
93     }
94 
95     @Test
onPause_shouldPausePreference()96     public void onPause_shouldPausePreference() {
97         mController.displayPreference(mScreen);
98 
99         mController.onPause();
100 
101         verify(mPreference).onActivityPause();
102     }
103 
104     @Test
sliderMethods_handleNullPreference()105     public void sliderMethods_handleNullPreference() {
106         when(mHelper.getStreamVolume(mController.getAudioStream())).thenReturn(4);
107         when(mHelper.getMaxVolume(mController.getAudioStream())).thenReturn(10);
108 
109         assertThat(mController.getMaxSteps()).isEqualTo(10);
110         assertThat(mController.getSliderPosition()).isEqualTo(4);
111 
112         mController.setSliderPosition(9);
113         verify(mHelper).setStreamVolume(mController.getAudioStream(), 9);
114     }
115 
116     @Test
setSliderPosition_passesAlongValue()117     public void setSliderPosition_passesAlongValue() {
118         mController.displayPreference(mScreen);
119 
120         mController.setSliderPosition(2);
121         verify(mPreference).setProgress(2);
122     }
123 
124     @Test
getMaxSteps_passesAlongValue()125     public void getMaxSteps_passesAlongValue() {
126         when(mPreference.getMax()).thenReturn(6);
127         mController.displayPreference(mScreen);
128 
129         assertThat(mController.getMaxSteps()).isEqualTo(6);
130     }
131 
132     @Test
getSliderPosition_passesAlongValue()133     public void getSliderPosition_passesAlongValue() {
134         when(mPreference.getProgress()).thenReturn(7);
135         mController.displayPreference(mScreen);
136 
137         assertThat(mController.getSliderPosition()).isEqualTo(7);
138     }
139 
140     private class VolumeSeekBarPreferenceControllerTestable
141         extends VolumeSeekBarPreferenceController {
142 
143         private final static int AUDIO_STREAM = 1;
144         private final static int MUTE_ICON = 2;
145 
146         private boolean mAvailable;
147 
VolumeSeekBarPreferenceControllerTestable(Context context, VolumeSeekBarPreference.Callback callback, String key)148         VolumeSeekBarPreferenceControllerTestable(Context context,
149             VolumeSeekBarPreference.Callback callback, String key) {
150             this(context, callback, true, key);
151         }
152 
VolumeSeekBarPreferenceControllerTestable(Context context, VolumeSeekBarPreference.Callback callback, boolean available, String key)153         VolumeSeekBarPreferenceControllerTestable(Context context,
154             VolumeSeekBarPreference.Callback callback, boolean available, String key) {
155             super(context, key);
156             setCallback(callback);
157             mAvailable = available;
158         }
159 
160         @Override
getPreferenceKey()161         public String getPreferenceKey() {
162             return "key";
163         }
164 
165         @Override
handlePreferenceTreeClick(Preference preference)166         public boolean handlePreferenceTreeClick(Preference preference) {
167             return false;
168         }
169 
170         @Override
getAvailabilityStatus()171         public int getAvailabilityStatus() {
172             return mAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
173         }
174 
175         @Override
getAudioStream()176         public int getAudioStream() {
177             return AUDIO_STREAM;
178         }
179 
180         @Override
getMuteIcon()181         public int getMuteIcon() {
182             return MUTE_ICON;
183         }
184     }
185 }
186