|
16 | 16 | package androidx.media3.session;
|
17 | 17 |
|
18 | 18 | import static androidx.media3.test.session.common.TestUtils.TIMEOUT_MS;
|
| 19 | +import static androidx.media3.test.session.common.TestUtils.getEventsAsList; |
19 | 20 | import static com.google.common.truth.Truth.assertThat;
|
20 | 21 | import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
| 22 | +import static org.junit.Assume.assumeTrue; |
21 | 23 |
|
22 | 24 | import android.content.Context;
|
| 25 | +import android.media.AudioManager; |
23 | 26 | import android.os.Bundle;
|
24 | 27 | import android.os.RemoteException;
|
25 | 28 | import android.support.v4.media.session.MediaSessionCompat;
|
26 | 29 | import android.support.v4.media.session.PlaybackStateCompat;
|
| 30 | +import androidx.media.VolumeProviderCompat; |
| 31 | +import androidx.media3.common.AudioAttributes; |
27 | 32 | import androidx.media3.common.C;
|
| 33 | +import androidx.media3.common.DeviceInfo; |
28 | 34 | import androidx.media3.common.FlagSet;
|
| 35 | +import androidx.media3.common.MediaMetadata; |
29 | 36 | import androidx.media3.common.Player;
|
| 37 | +import androidx.media3.common.util.Util; |
30 | 38 | import androidx.media3.test.session.common.CommonConstants;
|
31 | 39 | import androidx.media3.test.session.common.HandlerThreadTestRule;
|
32 | 40 | import androidx.media3.test.session.common.MainLooperTestRule;
|
|
40 | 48 | import java.util.List;
|
41 | 49 | import java.util.concurrent.CopyOnWriteArrayList;
|
42 | 50 | import java.util.concurrent.CountDownLatch;
|
| 51 | +import java.util.concurrent.atomic.AtomicInteger; |
43 | 52 | import java.util.concurrent.atomic.AtomicReference;
|
44 | 53 | import org.junit.After;
|
45 | 54 | import org.junit.Before;
|
@@ -190,4 +199,161 @@ public void onExtrasChanged(MediaController controller, Bundle extras) {
|
190 | 199 | assertThat(countDownLatch.await(1_000, MILLISECONDS)).isTrue();
|
191 | 200 | assertThat(TestUtils.equals(receivedSessionExtras.get(0), sessionExtras)).isTrue();
|
192 | 201 | }
|
| 202 | + |
| 203 | + @Test |
| 204 | + public void onPlaylistMetadataChanged() throws Exception { |
| 205 | + MediaController controller = controllerTestRule.createController(session.getSessionToken()); |
| 206 | + CountDownLatch latch = new CountDownLatch(2); |
| 207 | + AtomicReference<MediaMetadata> playlistMetadataParamRef = new AtomicReference<>(); |
| 208 | + AtomicReference<MediaMetadata> playlistMetadataGetterRef = new AtomicReference<>(); |
| 209 | + AtomicReference<MediaMetadata> playlistMetadataOnEventsRef = new AtomicReference<>(); |
| 210 | + AtomicReference<Player.Events> onEvents = new AtomicReference<>(); |
| 211 | + Player.Listener listener = |
| 212 | + new Player.Listener() { |
| 213 | + @Override |
| 214 | + public void onPlaylistMetadataChanged(MediaMetadata mediaMetadata) { |
| 215 | + playlistMetadataParamRef.set(mediaMetadata); |
| 216 | + playlistMetadataGetterRef.set(controller.getPlaylistMetadata()); |
| 217 | + latch.countDown(); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public void onEvents(Player player, Player.Events events) { |
| 222 | + onEvents.set(events); |
| 223 | + playlistMetadataOnEventsRef.set(player.getPlaylistMetadata()); |
| 224 | + latch.countDown(); |
| 225 | + } |
| 226 | + }; |
| 227 | + threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener)); |
| 228 | + |
| 229 | + session.setQueueTitle("queue-title"); |
| 230 | + |
| 231 | + assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); |
| 232 | + assertThat(playlistMetadataParamRef.get().title).isEqualTo("queue-title"); |
| 233 | + assertThat(playlistMetadataGetterRef.get()).isEqualTo(playlistMetadataParamRef.get()); |
| 234 | + assertThat(playlistMetadataOnEventsRef.get()).isEqualTo(playlistMetadataParamRef.get()); |
| 235 | + assertThat(getEventsAsList(onEvents.get())) |
| 236 | + .containsExactly(Player.EVENT_PLAYLIST_METADATA_CHANGED); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void onAudioAttributesChanged() throws Exception { |
| 241 | + // We need to trigger MediaControllerCompat.Callback.onAudioInfoChanged in order to raise the |
| 242 | + // onAudioAttributesChanged() callback. In API 21 and 22, onAudioInfoChanged is not called when |
| 243 | + // playback is changed to local. |
| 244 | + assumeTrue(Util.SDK_INT != 21 && Util.SDK_INT != 22); |
| 245 | + |
| 246 | + session.setPlaybackToRemote( |
| 247 | + /* volumeControl= */ VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE, |
| 248 | + /* maxVolume= */ 100, |
| 249 | + /* currentVolume= */ 50); |
| 250 | + MediaController controller = controllerTestRule.createController(session.getSessionToken()); |
| 251 | + CountDownLatch latch = new CountDownLatch(2); |
| 252 | + AtomicReference<AudioAttributes> audioAttributesParamRef = new AtomicReference<>(); |
| 253 | + AtomicReference<AudioAttributes> audioAttributesGetterRef = new AtomicReference<>(); |
| 254 | + AtomicReference<AudioAttributes> audioAttributesOnEventsRef = new AtomicReference<>(); |
| 255 | + AtomicReference<Player.Events> onEvents = new AtomicReference<>(); |
| 256 | + Player.Listener listener = |
| 257 | + new Player.Listener() { |
| 258 | + @Override |
| 259 | + public void onAudioAttributesChanged(AudioAttributes audioAttributes) { |
| 260 | + audioAttributesParamRef.set(audioAttributes); |
| 261 | + audioAttributesGetterRef.set(controller.getAudioAttributes()); |
| 262 | + latch.countDown(); |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public void onEvents(Player player, Player.Events events) { |
| 267 | + onEvents.set(events); |
| 268 | + audioAttributesOnEventsRef.set(player.getAudioAttributes()); |
| 269 | + latch.countDown(); |
| 270 | + } |
| 271 | + }; |
| 272 | + threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener)); |
| 273 | + |
| 274 | + session.setPlaybackToLocal(AudioManager.STREAM_ALARM); |
| 275 | + |
| 276 | + assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); |
| 277 | + assertThat(audioAttributesGetterRef.get().contentType).isEqualTo(AudioManager.STREAM_ALARM); |
| 278 | + assertThat(audioAttributesGetterRef.get()).isEqualTo(audioAttributesParamRef.get()); |
| 279 | + assertThat(audioAttributesOnEventsRef.get()).isEqualTo(audioAttributesParamRef.get()); |
| 280 | + assertThat(getEventsAsList(onEvents.get())).contains(Player.EVENT_AUDIO_ATTRIBUTES_CHANGED); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + public void onDeviceInfoChanged() throws Exception { |
| 285 | + MediaController controller = controllerTestRule.createController(session.getSessionToken()); |
| 286 | + CountDownLatch latch = new CountDownLatch(2); |
| 287 | + AtomicReference<DeviceInfo> deviceInfoParamRef = new AtomicReference<>(); |
| 288 | + AtomicReference<DeviceInfo> deviceInfoGetterRef = new AtomicReference<>(); |
| 289 | + AtomicReference<DeviceInfo> deviceInfoOnEventsRef = new AtomicReference<>(); |
| 290 | + AtomicReference<Player.Events> onEvents = new AtomicReference<>(); |
| 291 | + Player.Listener listener = |
| 292 | + new Player.Listener() { |
| 293 | + @Override |
| 294 | + public void onDeviceInfoChanged(DeviceInfo deviceInfo) { |
| 295 | + deviceInfoParamRef.set(deviceInfo); |
| 296 | + deviceInfoGetterRef.set(controller.getDeviceInfo()); |
| 297 | + latch.countDown(); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + public void onEvents(Player player, Player.Events events) { |
| 302 | + deviceInfoOnEventsRef.set(player.getDeviceInfo()); |
| 303 | + onEvents.set(events); |
| 304 | + latch.countDown(); |
| 305 | + } |
| 306 | + }; |
| 307 | + threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener)); |
| 308 | + |
| 309 | + session.setPlaybackToRemote( |
| 310 | + /* volumeControl= */ VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE, |
| 311 | + /* maxVolume= */ 100, |
| 312 | + /* currentVolume= */ 50); |
| 313 | + |
| 314 | + assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); |
| 315 | + assertThat(deviceInfoParamRef.get().playbackType).isEqualTo(DeviceInfo.PLAYBACK_TYPE_REMOTE); |
| 316 | + assertThat(deviceInfoParamRef.get().maxVolume).isEqualTo(100); |
| 317 | + assertThat(deviceInfoGetterRef.get()).isEqualTo(deviceInfoParamRef.get()); |
| 318 | + assertThat(deviceInfoOnEventsRef.get()).isEqualTo(deviceInfoGetterRef.get()); |
| 319 | + assertThat(getEventsAsList(onEvents.get())).contains(Player.EVENT_DEVICE_VOLUME_CHANGED); |
| 320 | + } |
| 321 | + |
| 322 | + @Test |
| 323 | + public void onDeviceVolumeChanged() throws Exception { |
| 324 | + MediaController controller = controllerTestRule.createController(session.getSessionToken()); |
| 325 | + CountDownLatch latch = new CountDownLatch(2); |
| 326 | + AtomicInteger deviceVolumeParam = new AtomicInteger(); |
| 327 | + AtomicInteger deviceVolumeGetter = new AtomicInteger(); |
| 328 | + AtomicInteger deviceVolumeOnEvents = new AtomicInteger(); |
| 329 | + AtomicReference<Player.Events> onEvents = new AtomicReference<>(); |
| 330 | + Player.Listener listener = |
| 331 | + new Player.Listener() { |
| 332 | + @Override |
| 333 | + public void onDeviceVolumeChanged(int volume, boolean muted) { |
| 334 | + deviceVolumeParam.set(volume); |
| 335 | + deviceVolumeGetter.set(controller.getDeviceVolume()); |
| 336 | + latch.countDown(); |
| 337 | + } |
| 338 | + |
| 339 | + @Override |
| 340 | + public void onEvents(Player player, Player.Events events) { |
| 341 | + deviceVolumeOnEvents.set(player.getDeviceVolume()); |
| 342 | + onEvents.set(events); |
| 343 | + latch.countDown(); |
| 344 | + } |
| 345 | + }; |
| 346 | + threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener)); |
| 347 | + |
| 348 | + session.setPlaybackToRemote( |
| 349 | + /* volumeControl= */ VolumeProviderCompat.VOLUME_CONTROL_ABSOLUTE, |
| 350 | + /* maxVolume= */ 100, |
| 351 | + /* currentVolume= */ 50); |
| 352 | + |
| 353 | + assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); |
| 354 | + assertThat(deviceVolumeParam.get()).isEqualTo(50); |
| 355 | + assertThat(deviceVolumeGetter.get()).isEqualTo(50); |
| 356 | + assertThat(deviceVolumeOnEvents.get()).isEqualTo(50); |
| 357 | + assertThat(getEventsAsList(onEvents.get())).contains(Player.EVENT_DEVICE_VOLUME_CHANGED); |
| 358 | + } |
193 | 359 | }
|
0 commit comments