mirror of
https://github.com/androidx/media.git
synced 2025-04-30 06:46:50 +08:00
Misc cleanup in session tests
PiperOrigin-RevId: 432430345 (cherry picked from commit 8e98187a1e08167cbef08c94e632c8d84a4667f1)
This commit is contained in:
parent
50550ab191
commit
4456a865cc
@ -119,7 +119,7 @@ public class MediaSessionKeyEventTest {
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() throws Exception {
|
||||
public void tearDown() throws Exception {
|
||||
handler.postAndSync(
|
||||
() -> {
|
||||
if (mediaPlayer != null) {
|
||||
@ -130,15 +130,6 @@ public class MediaSessionKeyEventTest {
|
||||
session.release();
|
||||
}
|
||||
|
||||
private void dispatchMediaKeyEvent(int keyCode, boolean doubleTap) {
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
||||
if (doubleTap) {
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void playKeyEvent() throws Exception {
|
||||
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, false);
|
||||
@ -202,6 +193,15 @@ public class MediaSessionKeyEventTest {
|
||||
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
|
||||
}
|
||||
|
||||
private void dispatchMediaKeyEvent(int keyCode, boolean doubleTap) {
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
||||
if (doubleTap) {
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
|
||||
audioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode));
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestSessionCallback implements MediaSession.SessionCallback {
|
||||
|
||||
@Override
|
||||
|
@ -88,7 +88,7 @@ public class MediaSessionPermissionTest {
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
public void tearDown() {
|
||||
if (session != null) {
|
||||
session.release();
|
||||
session = null;
|
||||
@ -97,54 +97,6 @@ public class MediaSessionPermissionTest {
|
||||
callback = null;
|
||||
}
|
||||
|
||||
private void createSessionWithAvailableCommands(
|
||||
SessionCommands sessionCommands, Player.Commands playerCommands) {
|
||||
player =
|
||||
new MockPlayer.Builder()
|
||||
.setApplicationLooper(threadTestRule.getHandler().getLooper())
|
||||
.build();
|
||||
callback =
|
||||
new MySessionCallback() {
|
||||
@Override
|
||||
public MediaSession.ConnectionResult onConnect(
|
||||
MediaSession session, ControllerInfo controller) {
|
||||
if (!TextUtils.equals(SUPPORT_APP_PACKAGE_NAME, controller.getPackageName())) {
|
||||
return MediaSession.ConnectionResult.reject();
|
||||
}
|
||||
return MediaSession.ConnectionResult.accept(sessionCommands, playerCommands);
|
||||
}
|
||||
};
|
||||
if (this.session != null) {
|
||||
this.session.release();
|
||||
}
|
||||
this.session =
|
||||
new MediaSession.Builder(context, player)
|
||||
.setId(SESSION_ID)
|
||||
.setSessionCallback(callback)
|
||||
.build();
|
||||
}
|
||||
|
||||
private SessionCommands createSessionCommandsWith(SessionCommand command) {
|
||||
return new SessionCommands.Builder().add(command).build();
|
||||
}
|
||||
|
||||
private void testOnCommandRequest(int commandCode, PermissionTestTask runnable) throws Exception {
|
||||
createSessionWithAvailableCommands(
|
||||
SessionCommands.EMPTY, createPlayerCommandsWith(commandCode));
|
||||
runnable.run(controllerTestRule.createRemoteController(session.getToken()));
|
||||
|
||||
assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
|
||||
assertThat(callback.onCommandRequestCalled).isTrue();
|
||||
assertThat(callback.command).isEqualTo(commandCode);
|
||||
|
||||
createSessionWithAvailableCommands(
|
||||
SessionCommands.EMPTY, createPlayerCommandsWithout(commandCode));
|
||||
runnable.run(controllerTestRule.createRemoteController(session.getToken()));
|
||||
|
||||
assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
|
||||
assertThat(callback.onCommandRequestCalled).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void play() throws Exception {
|
||||
testOnCommandRequest(COMMAND_PLAY_PAUSE, RemoteMediaController::play);
|
||||
@ -408,4 +360,52 @@ public class MediaSessionPermissionTest {
|
||||
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
|
||||
}
|
||||
}
|
||||
|
||||
private void createSessionWithAvailableCommands(
|
||||
SessionCommands sessionCommands, Player.Commands playerCommands) {
|
||||
player =
|
||||
new MockPlayer.Builder()
|
||||
.setApplicationLooper(threadTestRule.getHandler().getLooper())
|
||||
.build();
|
||||
callback =
|
||||
new MySessionCallback() {
|
||||
@Override
|
||||
public MediaSession.ConnectionResult onConnect(
|
||||
MediaSession session, ControllerInfo controller) {
|
||||
if (!TextUtils.equals(SUPPORT_APP_PACKAGE_NAME, controller.getPackageName())) {
|
||||
return MediaSession.ConnectionResult.reject();
|
||||
}
|
||||
return MediaSession.ConnectionResult.accept(sessionCommands, playerCommands);
|
||||
}
|
||||
};
|
||||
if (this.session != null) {
|
||||
this.session.release();
|
||||
}
|
||||
this.session =
|
||||
new MediaSession.Builder(context, player)
|
||||
.setId(SESSION_ID)
|
||||
.setSessionCallback(callback)
|
||||
.build();
|
||||
}
|
||||
|
||||
private SessionCommands createSessionCommandsWith(SessionCommand command) {
|
||||
return new SessionCommands.Builder().add(command).build();
|
||||
}
|
||||
|
||||
private void testOnCommandRequest(int commandCode, PermissionTestTask runnable) throws Exception {
|
||||
createSessionWithAvailableCommands(
|
||||
SessionCommands.EMPTY, createPlayerCommandsWith(commandCode));
|
||||
runnable.run(controllerTestRule.createRemoteController(session.getToken()));
|
||||
|
||||
assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
|
||||
assertThat(callback.onCommandRequestCalled).isTrue();
|
||||
assertThat(callback.command).isEqualTo(commandCode);
|
||||
|
||||
createSessionWithAvailableCommands(
|
||||
SessionCommands.EMPTY, createPlayerCommandsWithout(commandCode));
|
||||
runnable.run(controllerTestRule.createRemoteController(session.getToken()));
|
||||
|
||||
assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
|
||||
assertThat(callback.onCommandRequestCalled).isFalse();
|
||||
}
|
||||
}
|
||||
|
@ -84,10 +84,9 @@ public class MediaSessionPlayerTest {
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
if (session != null) {
|
||||
session.release();
|
||||
}
|
||||
public void tearDown() throws Exception {
|
||||
controller.release();
|
||||
session.release();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -47,6 +47,7 @@ import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Rule;
|
||||
@ -106,6 +107,16 @@ public class MediaSessionTest {
|
||||
.get(TIMEOUT_MS, MILLISECONDS);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if ((controller != null)) {
|
||||
threadTestRule.getHandler().postAndSync(() -> controller.release());
|
||||
}
|
||||
if (session != null) {
|
||||
session.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builder() {
|
||||
MediaSession.Builder builder;
|
||||
|
Loading…
x
Reference in New Issue
Block a user