Misc cleanup in session tests
PiperOrigin-RevId: 432430345
This commit is contained in:
parent
c2a3249f98
commit
8e98187a1e
@ -119,7 +119,7 @@ public class MediaSessionKeyEventTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanUp() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
handler.postAndSync(
|
handler.postAndSync(
|
||||||
() -> {
|
() -> {
|
||||||
if (mediaPlayer != null) {
|
if (mediaPlayer != null) {
|
||||||
@ -130,15 +130,6 @@ public class MediaSessionKeyEventTest {
|
|||||||
session.release();
|
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
|
@Test
|
||||||
public void playKeyEvent() throws Exception {
|
public void playKeyEvent() throws Exception {
|
||||||
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, false);
|
dispatchMediaKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY, false);
|
||||||
@ -202,6 +193,15 @@ public class MediaSessionKeyEventTest {
|
|||||||
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
|
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 {
|
private static class TestSessionCallback implements MediaSession.SessionCallback {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -88,7 +88,7 @@ public class MediaSessionPermissionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanUp() {
|
public void tearDown() {
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
session.release();
|
session.release();
|
||||||
session = null;
|
session = null;
|
||||||
@ -97,54 +97,6 @@ public class MediaSessionPermissionTest {
|
|||||||
callback = null;
|
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
|
@Test
|
||||||
public void play() throws Exception {
|
public void play() throws Exception {
|
||||||
testOnCommandRequest(COMMAND_PLAY_PAUSE, RemoteMediaController::play);
|
testOnCommandRequest(COMMAND_PLAY_PAUSE, RemoteMediaController::play);
|
||||||
@ -408,4 +360,52 @@ public class MediaSessionPermissionTest {
|
|||||||
return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
|
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
|
@After
|
||||||
public void cleanUp() {
|
public void tearDown() throws Exception {
|
||||||
if (session != null) {
|
controller.release();
|
||||||
session.release();
|
session.release();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -47,6 +47,7 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
@ -106,6 +107,16 @@ public class MediaSessionTest {
|
|||||||
.get(TIMEOUT_MS, MILLISECONDS);
|
.get(TIMEOUT_MS, MILLISECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
if ((controller != null)) {
|
||||||
|
threadTestRule.getHandler().postAndSync(() -> controller.release());
|
||||||
|
}
|
||||||
|
if (session != null) {
|
||||||
|
session.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void builder() {
|
public void builder() {
|
||||||
MediaSession.Builder builder;
|
MediaSession.Builder builder;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user