blob: 49b1a19b330d690e1f53a5f1d27b506d11b55156 [file] [log] [blame]
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001/*
2 * Copyright 2018 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
17package androidx.media;
18
19import android.util.ArrayMap;
20
21import androidx.annotation.NonNull;
22
23import java.util.List;
24import java.util.concurrent.CountDownLatch;
25import java.util.concurrent.Executor;
26
27/**
28 * A mock implementation of {@link MediaPlayerBase} for testing.
29 */
30public class MockPlayer extends MediaPlayerBase {
31 public final CountDownLatch mCountDownLatch;
32
33 public boolean mPlayCalled;
34 public boolean mPauseCalled;
35 public boolean mResetCalled;
36 public boolean mPrepareCalled;
37 public boolean mSeekToCalled;
38 public boolean mSetPlaybackSpeedCalled;
39 public long mSeekPosition;
40 public long mCurrentPosition;
41 public long mBufferedPosition;
42 public float mPlaybackSpeed = 1.0f;
43 public @PlayerState int mLastPlayerState;
44 public @BuffState int mLastBufferingState;
45
46 public ArrayMap<PlayerEventCallback, Executor> mCallbacks = new ArrayMap<>();
47
48 private AudioAttributesCompat mAudioAttributes;
49
50 public MockPlayer(int count) {
51 mCountDownLatch = (count > 0) ? new CountDownLatch(count) : null;
52 }
53
54 @Override
55 public void close() {
56 // no-op
57 }
58
59 @Override
60 public void reset() {
61 mResetCalled = true;
62 if (mCountDownLatch != null) {
63 mCountDownLatch.countDown();
64 }
65 }
66
67 @Override
68 public void play() {
69 mPlayCalled = true;
70 if (mCountDownLatch != null) {
71 mCountDownLatch.countDown();
72 }
73 }
74
75 @Override
76 public void pause() {
77 mPauseCalled = true;
78 if (mCountDownLatch != null) {
79 mCountDownLatch.countDown();
80 }
81 }
82
83 @Override
84 public void prepare() {
85 mPrepareCalled = true;
86 if (mCountDownLatch != null) {
87 mCountDownLatch.countDown();
88 }
89 }
90
91 @Override
92 public void seekTo(long pos) {
93 mSeekToCalled = true;
94 mSeekPosition = pos;
95 if (mCountDownLatch != null) {
96 mCountDownLatch.countDown();
97 }
98 }
99
100 @Override
101 public void skipToNext() {
102 // No-op. This skipToNext() means 'skip to next item in the setNextDataSources()'
103 }
104
105 @Override
106 public int getPlayerState() {
107 return mLastPlayerState;
108 }
109
110 @Override
111 public long getCurrentPosition() {
112 return mCurrentPosition;
113 }
114
115 @Override
116 public long getBufferedPosition() {
117 return mBufferedPosition;
118 }
119
120 @Override
121 public float getPlaybackSpeed() {
122 return mPlaybackSpeed;
123 }
124
125 @Override
126 public int getBufferingState() {
127 return mLastBufferingState;
128 }
129
130 @Override
131 public void registerPlayerEventCallback(@NonNull Executor executor,
132 @NonNull PlayerEventCallback callback) {
133 if (callback == null || executor == null) {
134 throw new IllegalArgumentException("callback=" + callback + " executor=" + executor);
135 }
136 mCallbacks.put(callback, executor);
137 }
138
139 @Override
140 public void unregisterPlayerEventCallback(@NonNull PlayerEventCallback callback) {
141 mCallbacks.remove(callback);
142 }
143
144 public void notifyPlaybackState(final int state) {
145 mLastPlayerState = state;
146 for (int i = 0; i < mCallbacks.size(); i++) {
147 final PlayerEventCallback callback = mCallbacks.keyAt(i);
148 final Executor executor = mCallbacks.valueAt(i);
149 executor.execute(new Runnable() {
150 @Override
151 public void run() {
152 callback.onPlayerStateChanged(MockPlayer.this, state);
153 }
154 });
155 }
156 }
157
158 public void notifyBufferingState(final MediaItem2 item, final int bufferingState) {
159 mLastBufferingState = bufferingState;
160 for (int i = 0; i < mCallbacks.size(); i++) {
161 final PlayerEventCallback callback = mCallbacks.keyAt(i);
162 final Executor executor = mCallbacks.valueAt(i);
163 executor.execute(new Runnable() {
164 @Override
165 public void run() {
166 callback.onBufferingStateChanged(
167 MockPlayer.this, item.getDataSourceDesc(), bufferingState);
168 }
169 });
170 }
171 }
172
173 public void notifyCurrentDataSourceChanged(final DataSourceDesc dsd) {
174 for (int i = 0; i < mCallbacks.size(); i++) {
175 final PlayerEventCallback callback = mCallbacks.keyAt(i);
176 final Executor executor = mCallbacks.valueAt(i);
177 executor.execute(new Runnable() {
178 @Override
179 public void run() {
180 callback.onCurrentDataSourceChanged(MockPlayer.this, dsd);
181 }
182 });
183 }
184 }
185
186 public void notifyMediaPrepared(final DataSourceDesc dsd) {
187 for (int i = 0; i < mCallbacks.size(); i++) {
188 final PlayerEventCallback callback = mCallbacks.keyAt(i);
189 final Executor executor = mCallbacks.valueAt(i);
190 executor.execute(new Runnable() {
191 @Override
192 public void run() {
193 callback.onMediaPrepared(MockPlayer.this, dsd);
194 }
195 });
196 }
197 }
198
199 public void notifyBufferingStateChanged(final DataSourceDesc dsd,
200 final @BuffState int buffState) {
201 for (int i = 0; i < mCallbacks.size(); i++) {
202 final PlayerEventCallback callback = mCallbacks.keyAt(i);
203 final Executor executor = mCallbacks.valueAt(i);
204 executor.execute(new Runnable() {
205 @Override
206 public void run() {
207 callback.onBufferingStateChanged(MockPlayer.this, dsd, buffState);
208 }
209 });
210 }
211 }
212
213 public void notifyPlaybackSpeedChanged(final float speed) {
214 for (int i = 0; i < mCallbacks.size(); i++) {
215 final PlayerEventCallback callback = mCallbacks.keyAt(i);
216 final Executor executor = mCallbacks.valueAt(i);
217 executor.execute(new Runnable() {
218 @Override
219 public void run() {
220 callback.onPlaybackSpeedChanged(MockPlayer.this, speed);
221 }
222 });
223 }
224 }
225
226 public void notifyError(int what) {
227 for (int i = 0; i < mCallbacks.size(); i++) {
228 final PlayerEventCallback callback = mCallbacks.keyAt(i);
229 final Executor executor = mCallbacks.valueAt(i);
230 // TODO: Uncomment or remove
231 //executor.execute(() -> callback.onError(null, what, 0));
232 }
233 }
234
235 @Override
236 public void setAudioAttributes(AudioAttributesCompat attributes) {
237 mAudioAttributes = attributes;
238 }
239
240 @Override
241 public AudioAttributesCompat getAudioAttributes() {
242 return mAudioAttributes;
243 }
244
245 @Override
246 public void setDataSource(@NonNull DataSourceDesc dsd) {
247 // TODO: Implement this
248 }
249
250 @Override
251 public void setNextDataSource(@NonNull DataSourceDesc dsd) {
252 // TODO: Implement this
253 }
254
255 @Override
256 public void setNextDataSources(@NonNull List<DataSourceDesc> dsds) {
257 // TODO: Implement this
258 }
259
260 @Override
261 public DataSourceDesc getCurrentDataSource() {
262 // TODO: Implement this
263 return null;
264 }
265
266 @Override
267 public void loopCurrent(boolean loop) {
268 // TODO: implement this
269 }
270
271 @Override
272 public void setPlaybackSpeed(float speed) {
273 mSetPlaybackSpeedCalled = true;
274 mPlaybackSpeed = speed;
275 if (mCountDownLatch != null) {
276 mCountDownLatch.countDown();
277 }
278 }
279
280 @Override
281 public void setPlayerVolume(float volume) {
282 // TODO: implement this
283 }
284
285 @Override
286 public float getPlayerVolume() {
287 // TODO: implement this
288 return -1;
289 }
290}