• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.media.session.ISessionController;
27 import android.media.session.MediaController;
28 import android.media.session.MediaSession;
29 import android.media.session.MediaSessionManager;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.BasePreferenceController;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Answers;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class RemoteVolumePreferenceControllerTest {
48     private static final int CURRENT_POS = 5;
49     private static final int MAX_POS = 10;
50 
51     @Mock
52     private MediaSessionManager mMediaSessionManager;
53     @Mock
54     private MediaController mMediaController;
55     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
56     private ISessionController mStub;
57     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
58     private ISessionController mStub2;
59     private MediaSession.Token mToken;
60     private MediaSession.Token mToken2;
61     private RemoteVolumePreferenceController mController;
62     private Context mContext;
63     private List<MediaController> mActiveSessions;
64     private MediaController.PlaybackInfo mPlaybackInfo;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69 
70         mContext = spy(RuntimeEnvironment.application);
71         when(mContext.getSystemService(MediaSessionManager.class)).thenReturn(mMediaSessionManager);
72         mActiveSessions = new ArrayList<>();
73         mActiveSessions.add(mMediaController);
74         when(mMediaSessionManager.getActiveSessions(null)).thenReturn(
75                 mActiveSessions);
76         mToken = new MediaSession.Token(mStub);
77         mToken2 = new MediaSession.Token(mStub2);
78 
79         mController = new RemoteVolumePreferenceController(mContext);
80         mPlaybackInfo = new MediaController.PlaybackInfo(
81                 MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE, 0, MAX_POS, CURRENT_POS, null);
82         when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
83         when(mMediaController.getSessionToken()).thenReturn(mToken);
84     }
85 
86     @Test
getActiveRemoteToken_containRemoteMedia_returnToken()87     public void getActiveRemoteToken_containRemoteMedia_returnToken() {
88         when(mMediaController.getPlaybackInfo()).thenReturn(
89                 new MediaController.PlaybackInfo(MediaController.PlaybackInfo.PLAYBACK_TYPE_REMOTE,
90                         0, 0, 0, null));
91         assertThat(mController.getActiveRemoteToken(mContext)).isEqualTo(mToken);
92     }
93 
94     @Test
getActiveRemoteToken_noRemoteMedia_returnNull()95     public void getActiveRemoteToken_noRemoteMedia_returnNull() {
96         when(mMediaController.getPlaybackInfo()).thenReturn(
97                 new MediaController.PlaybackInfo(MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
98                         0, 0, 0, null));
99         assertThat(mController.getActiveRemoteToken(mContext)).isNull();
100     }
101 
102     @Test
isAvailable_returnAvailableUnsearchable()103     public void isAvailable_returnAvailableUnsearchable() {
104         assertThat(mController.isAvailable()).isTrue();
105         assertThat(mController.getAvailabilityStatus()).isEqualTo(
106                 BasePreferenceController.AVAILABLE_UNSEARCHABLE);
107     }
108 
109     @Test
getMuteIcon_returnMuteIcon()110     public void getMuteIcon_returnMuteIcon() {
111         assertThat(mController.getMuteIcon()).isEqualTo(R.drawable.ic_volume_remote_mute);
112     }
113 
114     @Test
getAudioStream_returnRemoteVolume()115     public void getAudioStream_returnRemoteVolume() {
116         assertThat(mController.getAudioStream()).isEqualTo(
117                 RemoteVolumePreferenceController.REMOTE_VOLUME);
118     }
119 
120     @Test
getSliderPosition_controllerNull_returnZero()121     public void getSliderPosition_controllerNull_returnZero() {
122         mController.mMediaController = null;
123 
124         assertThat(mController.getSliderPosition()).isEqualTo(0);
125     }
126 
127     @Test
getSliderPosition_controllerExists_returnValue()128     public void getSliderPosition_controllerExists_returnValue() {
129         mController.mMediaController = mMediaController;
130 
131         assertThat(mController.getSliderPosition()).isEqualTo(CURRENT_POS);
132     }
133 
134     @Test
getMinValue_controllerNull_returnZero()135     public void getMinValue_controllerNull_returnZero() {
136         mController.mMediaController = null;
137 
138         assertThat(mController.getMin()).isEqualTo(0);
139     }
140 
141     @Test
getMinValue_controllerExists_returnValue()142     public void getMinValue_controllerExists_returnValue() {
143         mController.mMediaController = mMediaController;
144 
145         assertThat(mController.getMin()).isEqualTo(0);
146     }
147 
148     @Test
getMaxValue_controllerNull_returnZero()149     public void getMaxValue_controllerNull_returnZero() {
150         mController.mMediaController = null;
151 
152         assertThat(mController.getMax()).isEqualTo(0);
153     }
154 
155     @Test
getMaxValue_controllerExists_returnValue()156     public void getMaxValue_controllerExists_returnValue() {
157         mController.mMediaController = mMediaController;
158 
159         assertThat(mController.getMax()).isEqualTo(MAX_POS);
160     }
161 
162     @Test
setSliderPosition_controllerNull_returnFalse()163     public void setSliderPosition_controllerNull_returnFalse() {
164         mController.mMediaController = null;
165 
166         assertThat(mController.setSliderPosition(CURRENT_POS)).isFalse();
167     }
168 
169     @Test
setSliderPosition_controllerExists_returnTrue()170     public void setSliderPosition_controllerExists_returnTrue() {
171         mController.mMediaController = mMediaController;
172 
173         assertThat(mController.setSliderPosition(CURRENT_POS)).isTrue();
174         verify(mMediaController).setVolumeTo(CURRENT_POS, 0 /* flags */);
175     }
176 
177     @Test
onRemoteUpdate_firstToken_updateTokenAndPreference()178     public void onRemoteUpdate_firstToken_updateTokenAndPreference() {
179         mController.mPreference = new VolumeSeekBarPreference(mContext);
180         mController.mActiveToken = null;
181 
182         mController.mCallbacks.onRemoteUpdate(mToken, "token", mPlaybackInfo);
183 
184         assertThat(mController.mActiveToken).isEqualTo(mToken);
185         assertThat(mController.mPreference.isVisible()).isTrue();
186         assertThat(mController.mPreference.getMax()).isEqualTo(MAX_POS);
187         assertThat(mController.mPreference.getProgress()).isEqualTo(CURRENT_POS);
188     }
189 
190     @Test
onRemoteUpdate_differentToken_doNothing()191     public void onRemoteUpdate_differentToken_doNothing() {
192         mController.mActiveToken = mToken;
193 
194         mController.mCallbacks.onRemoteUpdate(mToken2, "token2", mPlaybackInfo);
195 
196         assertThat(mController.mActiveToken).isEqualTo(mToken);
197     }
198 
199     @Test
onRemoteRemoved_tokenRemoved_setInvisible()200     public void onRemoteRemoved_tokenRemoved_setInvisible() {
201         mController.mPreference = new VolumeSeekBarPreference(mContext);
202         mController.mActiveToken = mToken;
203 
204         mController.mCallbacks.onRemoteRemoved(mToken);
205 
206         assertThat(mController.mActiveToken).isNull();
207         assertThat(mController.mPreference.isVisible()).isFalse();
208     }
209 
210     @Test
onRemoteVolumeChanged_volumeChanged_updateIt()211     public void onRemoteVolumeChanged_volumeChanged_updateIt() {
212         mController.mPreference = new VolumeSeekBarPreference(mContext);
213         mController.mPreference.setMax(MAX_POS);
214         mController.mActiveToken = mToken;
215         mController.mMediaController = mMediaController;
216 
217         mController.mCallbacks.onRemoteVolumeChanged(mToken, 0 /* flags */);
218 
219         assertThat(mController.mPreference.getProgress()).isEqualTo(CURRENT_POS);
220     }
221 }
222