MediaControllerListenerTest: use local MediaController instances

Replace the instance-field MediaController with a local instance created
in each test.

PiperOrigin-RevId: 480039768
This commit is contained in:
christosts 2022-10-10 10:26:45 +00:00 committed by Marc Baechinger
parent 5d6d069e4e
commit ba1ecafb8f

View File

@ -123,7 +123,6 @@ public class MediaControllerListenerTest {
Context context; Context context;
private RemoteMediaSession remoteSession; private RemoteMediaSession remoteSession;
private List<RemoteMediaSession> sessions; private List<RemoteMediaSession> sessions;
private MediaController controller;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
@ -200,6 +199,7 @@ public class MediaControllerListenerTest {
@Test @Test
public void connection_sessionReleased() throws Exception { public void connection_sessionReleased() throws Exception {
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
MediaController controller =
controllerTestRule.createController( controllerTestRule.createController(
remoteSession.getToken(), remoteSession.getToken(),
/* connectionHints= */ null, /* connectionHints= */ null,
@ -246,7 +246,7 @@ public class MediaControllerListenerTest {
@LargeTest @LargeTest
public void noInteractionAfterSessionClose_session() throws Exception { public void noInteractionAfterSessionClose_session() throws Exception {
SessionToken token = remoteSession.getToken(); SessionToken token = remoteSession.getToken();
controller = controllerTestRule.createController(token); MediaController controller = controllerTestRule.createController(token);
testControllerAfterSessionIsClosed(DEFAULT_TEST_NAME); testControllerAfterSessionIsClosed(DEFAULT_TEST_NAME);
} }
@ -254,11 +254,11 @@ public class MediaControllerListenerTest {
@LargeTest @LargeTest
public void noInteractionAfterControllerClose_session() throws Exception { public void noInteractionAfterControllerClose_session() throws Exception {
SessionToken token = remoteSession.getToken(); SessionToken token = remoteSession.getToken();
controller = controllerTestRule.createController(token); MediaController controller = controllerTestRule.createController(token);
threadTestRule.getHandler().postAndSync(controller::release); threadTestRule.getHandler().postAndSync(controller::release);
// release is done immediately for session. // release is done immediately for session.
testNoInteraction(); testNoInteraction(controller);
// Test whether the controller is notified about later release of the session or // Test whether the controller is notified about later release of the session or
// re-creation. // re-creation.
@ -337,7 +337,7 @@ public class MediaControllerListenerTest {
AtomicBoolean shuffleModeEnabledRef = new AtomicBoolean(); AtomicBoolean shuffleModeEnabledRef = new AtomicBoolean();
AtomicInteger repeatModeRef = new AtomicInteger(); AtomicInteger repeatModeRef = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(7); CountDownLatch latch = new CountDownLatch(7);
controller = controllerTestRule.createController(remoteSession.getToken()); MediaController controller = controllerTestRule.createController(remoteSession.getToken());
threadTestRule threadTestRule
.getHandler() .getHandler()
.postAndSync( .postAndSync(
@ -435,7 +435,7 @@ public class MediaControllerListenerTest {
int testMediaItemIndex = 1; int testMediaItemIndex = 1;
int testPeriodIndex = 2; int testPeriodIndex = 2;
controller = controllerTestRule.createController(remoteSession.getToken()); MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
AtomicLong currentPositionMsRef = new AtomicLong(); AtomicLong currentPositionMsRef = new AtomicLong();
@ -642,7 +642,7 @@ public class MediaControllerListenerTest {
long testCurrentLiveOffsetMs = 10; long testCurrentLiveOffsetMs = 10;
long testContentBufferedPositionMs = 240; long testContentBufferedPositionMs = 240;
controller = controllerTestRule.createController(remoteSession.getToken()); MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
AtomicReference<PlaybackParameters> playbackParametersRef = new AtomicReference<>(); AtomicReference<PlaybackParameters> playbackParametersRef = new AtomicReference<>();
@ -1189,7 +1189,7 @@ public class MediaControllerListenerTest {
long testCurrentLiveOffsetMs = 10; long testCurrentLiveOffsetMs = 10;
long testContentBufferedPositionMs = 240; long testContentBufferedPositionMs = 240;
controller = controllerTestRule.createController(remoteSession.getToken()); MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(1); CountDownLatch latch = new CountDownLatch(1);
AtomicBoolean playWhenReadyRef = new AtomicBoolean(); AtomicBoolean playWhenReadyRef = new AtomicBoolean();
@ -1693,6 +1693,7 @@ public class MediaControllerListenerTest {
latch.countDown(); latch.countDown();
} }
}; };
MediaController controller =
controllerTestRule.createController( controllerTestRule.createController(
remoteSession.getToken(), /* connectionHints= */ null, listener); remoteSession.getToken(), /* connectionHints= */ null, listener);
@ -1896,6 +1897,7 @@ public class MediaControllerListenerTest {
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS)); return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
} }
}; };
MediaController controller =
controllerTestRule.createController( controllerTestRule.createController(
remoteSession.getToken(), /* connectionHints= */ null, listener); remoteSession.getToken(), /* connectionHints= */ null, listener);
@ -1951,7 +1953,9 @@ public class MediaControllerListenerTest {
} }
}; };
RemoteMediaSession session = createRemoteMediaSession(TEST_WITH_CUSTOM_COMMANDS); RemoteMediaSession session = createRemoteMediaSession(TEST_WITH_CUSTOM_COMMANDS);
controllerTestRule.createController(session.getToken(), /* connectionHints= */ null, listener); MediaController controller =
controllerTestRule.createController(
session.getToken(), /* connectionHints= */ null, listener);
session.setCustomLayout(buttons); session.setCustomLayout(buttons);
@ -1979,6 +1983,7 @@ public class MediaControllerListenerTest {
latch.countDown(); latch.countDown();
} }
}; };
MediaController controller =
controllerTestRule.createController( controllerTestRule.createController(
remoteSession.getToken(), /* connectionHints= */ null, listener); remoteSession.getToken(), /* connectionHints= */ null, listener);
@ -2005,6 +2010,7 @@ public class MediaControllerListenerTest {
}; };
Bundle connectionHints = new Bundle(); Bundle connectionHints = new Bundle();
connectionHints.putString(KEY_CONTROLLER, "controller_key_1"); connectionHints.putString(KEY_CONTROLLER, "controller_key_1");
MediaController controller =
controllerTestRule.createController(remoteSession.getToken(), connectionHints, listener); controllerTestRule.createController(remoteSession.getToken(), connectionHints, listener);
remoteSession.setSessionExtras("controller_key_1", sessionExtras); remoteSession.setSessionExtras("controller_key_1", sessionExtras);
@ -2593,22 +2599,23 @@ public class MediaControllerListenerTest {
} }
private void testControllerAfterSessionIsClosed(String id) throws Exception { private void testControllerAfterSessionIsClosed(String id) throws Exception {
// This cause session service to be died. MediaController controller = controllerTestRule.createController(remoteSession.getToken());
// This causes the session service to die.
remoteSession.release(); remoteSession.release();
// controllerTestRule.waitForDisconnect(controller, true); // controllerTestRule.waitForDisconnect(controller, true);
testNoInteraction(); testNoInteraction(controller);
// Ensure that the controller cannot use newly create session with the same ID. // Ensure that the controller cannot use newly create session with the same ID.
// Recreated session has different session stub, so previously created controller // Recreated session has different session stub, so previously created controller
// shouldn't be available. // shouldn't be available.
remoteSession = createRemoteMediaSession(id); remoteSession = createRemoteMediaSession(id);
testNoInteraction(); testNoInteraction(controller);
} }
// Test that session and controller doesn't interact. // Test that session and controller doesn't interact.
// Note that this method can be called after the session is died, so session may not have // Note that this method can be called after the session is died, so session may not have
// valid player. // valid player.
private void testNoInteraction() throws Exception { private void testNoInteraction(MediaController controller) throws Exception {
// TODO: check that calls from the controller to session shouldn't be delivered. // TODO: check that calls from the controller to session shouldn't be delivered.
// Calls from the session to controller shouldn't be delivered. // Calls from the session to controller shouldn't be delivered.