Commit 45d51216 by christosts Committed by Ian Baker

Remove CountDownLatch from MockPlayer

The MockPlayer has a single CountDownLatch field and multiple boolean
flags that track if a player method was called. Upon calling the methods
the latch count. Tests set the latch count to match exactly with the
number of expected player interactions then block the test thread until
the latch reaches zero and assert the respective method flags are true.

This is subject to false positives. If the underneath implementation
changes and call more player method, then the test thread will unblock
as soon as a certain number of interactions is performed, which may be
less than what the test expected originally. However, the test may stil
pass if the player thread had enough time to update the expected method
flag.

This change removes the single CountDownLatch and the boolean flags and
instead it adds APIs to query the MockPlayer if a method has been called
and await until a method is called. Internally, the MockPlayer has a
ConditionVariable per method.

PiperOrigin-RevId: 432399077
parent a73a9e9c
...@@ -181,7 +181,6 @@ public class MediaSessionAndControllerTest { ...@@ -181,7 +181,6 @@ public class MediaSessionAndControllerTest {
MockPlayer player = MockPlayer player =
new MockPlayer.Builder() new MockPlayer.Builder()
.setApplicationLooper(threadTestRule.getHandler().getLooper()) .setApplicationLooper(threadTestRule.getHandler().getLooper())
.setLatchCount(1)
.build(); .build();
MediaSession session = MediaSession session =
sessionTestRule.ensureReleaseAfterTest( sessionTestRule.ensureReleaseAfterTest(
...@@ -190,8 +189,7 @@ public class MediaSessionAndControllerTest { ...@@ -190,8 +189,7 @@ public class MediaSessionAndControllerTest {
threadTestRule.getHandler().postAndSync(controller::play); threadTestRule.getHandler().postAndSync(controller::play);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
assertThat(player.playCalled).isTrue();
} }
@Test @Test
......
...@@ -74,7 +74,6 @@ public class MediaSessionCallbackTest { ...@@ -74,7 +74,6 @@ public class MediaSessionCallbackTest {
context = ApplicationProvider.getApplicationContext(); context = ApplicationProvider.getApplicationContext();
player = player =
new MockPlayer.Builder() new MockPlayer.Builder()
.setLatchCount(1)
.setApplicationLooper(threadTestRule.getHandler().getLooper()) .setApplicationLooper(threadTestRule.getHandler().getLooper())
.build(); .build();
} }
...@@ -157,15 +156,14 @@ public class MediaSessionCallbackTest { ...@@ -157,15 +156,14 @@ public class MediaSessionCallbackTest {
controllerTestRule.createRemoteController(session.getToken()); controllerTestRule.createRemoteController(session.getToken());
controller.prepare(); controller.prepare();
assertThat(player.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse(); Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.prepareCalled).isFalse(); assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PREPARE)).isFalse();
assertThat(commands).hasSize(1); assertThat(commands).hasSize(1);
assertThat(commands.get(0)).isEqualTo(Player.COMMAND_PREPARE); assertThat(commands.get(0)).isEqualTo(Player.COMMAND_PREPARE);
controller.play(); controller.play();
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
assertThat(player.playCalled).isTrue(); assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PREPARE)).isFalse();
assertThat(player.prepareCalled).isFalse();
assertThat(commands).hasSize(2); assertThat(commands).hasSize(2);
assertThat(commands.get(1)).isEqualTo(Player.COMMAND_PLAY_PAUSE); assertThat(commands.get(1)).isEqualTo(Player.COMMAND_PLAY_PAUSE);
} }
......
...@@ -89,8 +89,7 @@ public class MediaSessionKeyEventTest { ...@@ -89,8 +89,7 @@ public class MediaSessionKeyEventTest {
Context context = ApplicationProvider.getApplicationContext(); Context context = ApplicationProvider.getApplicationContext();
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
handler = threadTestRule.getHandler(); handler = threadTestRule.getHandler();
player = player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
sessionCallback = new TestSessionCallback(); sessionCallback = new TestSessionCallback();
session = new MediaSession.Builder(context, player).setSessionCallback(sessionCallback).build(); session = new MediaSession.Builder(context, player).setSessionCallback(sessionCallback).build();
...@@ -143,43 +142,43 @@ public class MediaSessionKeyEventTest { ...@@ -143,43 +142,43 @@ public class MediaSessionKeyEventTest {
@Test @Test
public void playKeyEvent() throws Exception { public void playKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
} }
@Test @Test
public void pauseKeyEvent() throws Exception { public void pauseKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PAUSE, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.pauseCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
} }
@Test @Test
public void nextKeyEvent() throws Exception { public void nextKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
} }
@Test @Test
public void previousKeyEvent() throws Exception { public void previousKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToPreviousCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_PREVIOUS, TIMEOUT_MS);
} }
@Test @Test
public void stopKeyEvent() throws Exception { public void stopKeyEvent() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_STOP, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_STOP, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.stopCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_STOP, TIMEOUT_MS);
} }
@Test @Test
public void playPauseKeyEvent_play() throws Exception { public void playPauseKeyEvent_play() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.playCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
} }
@Test @Test
...@@ -188,18 +187,19 @@ public class MediaSessionKeyEventTest { ...@@ -188,18 +187,19 @@ public class MediaSessionKeyEventTest {
() -> { () -> {
player.playWhenReady = true; player.playWhenReady = true;
}); });
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, false);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.pauseCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_PAUSE, TIMEOUT_MS);
} }
@Test @Test
public void playPauseKeyEvent_doubleTapIsTranslatedToSkipToNext() throws Exception { public void playPauseKeyEvent_doubleTapIsTranslatedToSkipToNext() throws Exception {
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, true); dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, true);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(player.seekToNextCalled).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO_NEXT, TIMEOUT_MS);
assertThat(player.playCalled).isFalse(); assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
assertThat(player.pauseCalled).isFalse(); assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
} }
private static class TestSessionCallback implements MediaSession.SessionCallback { private static class TestSessionCallback implements MediaSession.SessionCallback {
......
...@@ -101,7 +101,6 @@ public class MediaSessionPermissionTest { ...@@ -101,7 +101,6 @@ public class MediaSessionPermissionTest {
SessionCommands sessionCommands, Player.Commands playerCommands) { SessionCommands sessionCommands, Player.Commands playerCommands) {
player = player =
new MockPlayer.Builder() new MockPlayer.Builder()
.setLatchCount(1)
.setApplicationLooper(threadTestRule.getHandler().getLooper()) .setApplicationLooper(threadTestRule.getHandler().getLooper())
.build(); .build();
callback = callback =
......
...@@ -78,8 +78,7 @@ public class MediaSessionTest { ...@@ -78,8 +78,7 @@ public class MediaSessionTest {
public void setUp() throws Exception { public void setUp() throws Exception {
context = ApplicationProvider.getApplicationContext(); context = ApplicationProvider.getApplicationContext();
handler = threadTestRule.getHandler(); handler = threadTestRule.getHandler();
player = player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
session = session =
sessionTestRule.ensureReleaseAfterTest( sessionTestRule.ensureReleaseAfterTest(
...@@ -394,8 +393,7 @@ public class MediaSessionTest { ...@@ -394,8 +393,7 @@ public class MediaSessionTest {
long testSeekPositionMs = 1234; long testSeekPositionMs = 1234;
controllerCompat.getTransportControls().seekTo(testSeekPositionMs); controllerCompat.getTransportControls().seekTo(testSeekPositionMs);
assertThat(player.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue(); player.awaitMethodCalled(MockPlayer.METHOD_SEEK_TO, TIMEOUT_MS);
assertThat(player.seekToCalled).isTrue();
assertThat(player.seekPositionMs).isEqualTo(testSeekPositionMs); assertThat(player.seekPositionMs).isEqualTo(testSeekPositionMs);
} }
......
...@@ -134,8 +134,7 @@ public class MockMediaLibraryService extends MediaLibraryService { ...@@ -134,8 +134,7 @@ public class MockMediaLibraryService extends MediaLibraryService {
return (MediaLibrarySession) onGetSessionHandler.onGetSession(controllerInfo); return (MediaLibrarySession) onGetSessionHandler.onGetSession(controllerInfo);
} }
MockPlayer player = MockPlayer player = new MockPlayer.Builder().setApplicationLooper(handler.getLooper()).build();
new MockPlayer.Builder().setLatchCount(1).setApplicationLooper(handler.getLooper()).build();
MediaLibrarySessionCallback callback = registry.getSessionCallback(); MediaLibrarySessionCallback callback = registry.getSessionCallback();
session = session =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment