Skip to content

Commit 1ca8dff

Browse files
committed
Add failing unit test
1 parent 3e2ee25 commit 1ca8dff

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

libraries/test_session_current/src/androidTest/java/androidx/media3/session/MediaSessionServiceTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
import android.content.Context;
2727
import android.os.Bundle;
2828
import android.os.Handler;
29+
import android.os.HandlerThread;
2930
import android.os.Looper;
3031
import android.support.v4.media.session.MediaControllerCompat;
3132
import android.support.v4.media.session.MediaSessionCompat;
3233
import android.support.v4.media.session.PlaybackStateCompat;
34+
import androidx.core.content.ContextCompat;
3335
import androidx.media3.common.ForwardingPlayer;
3436
import androidx.media3.common.MediaItem;
37+
import androidx.media3.common.PlaybackParameters;
3538
import androidx.media3.common.Player;
3639
import androidx.media3.common.util.ConditionVariable;
3740
import androidx.media3.exoplayer.ExoPlayer;
@@ -47,6 +50,8 @@
4750
import androidx.test.filters.MediumTest;
4851
import com.google.common.collect.ImmutableList;
4952
import com.google.common.collect.Lists;
53+
import com.google.common.util.concurrent.FutureCallback;
54+
import com.google.common.util.concurrent.Futures;
5055
import com.google.common.util.concurrent.ListenableFuture;
5156
import com.google.common.util.concurrent.SettableFuture;
5257
import java.util.ArrayList;
@@ -242,6 +247,70 @@ public ListenableFuture<List<MediaItem>> onAddMediaItems(
242247
service.blockUntilAllControllersUnbind(TIMEOUT_MS);
243248
}
244249

250+
@Test
251+
public void onPlayRequested_doesNotCauseDeadlock_ifPlaybackThreadIsNotMain() throws Exception {
252+
HandlerThread ht = new HandlerThread("MSSTest:PlaybackThread");
253+
ht.start();
254+
TestServiceRegistry testServiceRegistry = TestServiceRegistry.getInstance();
255+
ConditionVariable playerToldToSpeedUp = new ConditionVariable();
256+
AtomicReference<MediaController> mediaController = new AtomicReference<>();
257+
AtomicReference<MediaSession> mediaSession = new AtomicReference<>();
258+
testServiceRegistry.setOnGetSessionHandler(
259+
controllerInfo -> {
260+
MockMediaSessionService service =
261+
(MockMediaSessionService) testServiceRegistry.getServiceInstance();
262+
Player player = new ExoPlayer.Builder(service).setLooper(ht.getLooper()).build();
263+
player.addListener(
264+
new Player.Listener() {
265+
@Override
266+
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
267+
if (playbackParameters.speed == 2f) {
268+
playerToldToSpeedUp.open();
269+
}
270+
}
271+
});
272+
mediaSession.set(new MediaSession.Builder(service, player).build());
273+
return mediaSession.get();
274+
});
275+
TestHandler handler = new TestHandler(Looper.getMainLooper());
276+
TestHandler bgHandler = new TestHandler(ht.getLooper());
277+
handler.post(
278+
() -> {
279+
ListenableFuture<MediaController> controllerFuture =
280+
new MediaController.Builder(context, token).buildAsync();
281+
Futures.addCallback(
282+
controllerFuture,
283+
new FutureCallback<MediaController>() {
284+
@Override
285+
public void onSuccess(MediaController controller) {
286+
controller.addMediaItem(new MediaItem.Builder().setMediaId("media_id").build());
287+
controller.play();
288+
bgHandler.post(
289+
() ->
290+
handler.post(
291+
() -> {
292+
controller.setPlaybackSpeed(2f);
293+
mediaController.set(controller);
294+
}));
295+
}
296+
297+
@Override
298+
public void onFailure(Throwable t) {
299+
throw new RuntimeException(t);
300+
}
301+
},
302+
ContextCompat.getMainExecutor(context));
303+
});
304+
playerToldToSpeedUp.block(TIMEOUT_MS);
305+
if (!playerToldToSpeedUp.isOpen()) {
306+
ht.interrupt(); // avoid deadlocking the test forever.
307+
}
308+
assertThat(playerToldToSpeedUp.isOpen()).isTrue();
309+
handler.postAndSync(() -> mediaController.get().release());
310+
mediaSession.get().release();
311+
ht.quitSafely();
312+
}
313+
245314
@Test
246315
public void onCreate_withCustomLayout_correctSessionStateFromOnConnect() throws Exception {
247316
SessionCommand command1 = new SessionCommand("command1", Bundle.EMPTY);

0 commit comments

Comments
 (0)