Fix test which is relying on wrong Robolectric behavior.

Robolectric calls messages posted to the main thread immediately inline. This
will change in the upcoming release and will break tests implicitly relying
on that behavior.

Update ConcatenatingMediaSourceTest to actually wait for the posted message.

PiperOrigin-RevId: 242631531
This commit is contained in:
tonihei 2019-04-09 10:46:17 +01:00 committed by Oliver Woodman
parent 18dcad9b8d
commit 14d40b0faf

View File

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.source;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import android.os.ConditionVariable; import android.os.ConditionVariable;
import android.os.Handler; import android.os.Handler;
@ -417,60 +416,101 @@ public final class ConcatenatingMediaSourceTest {
@Test @Test
public void testCustomCallbackBeforePreparationAddSingle() { public void testCustomCallbackBeforePreparationAddSingle() {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.addMediaSource(createFakeMediaSource(), new Handler(), runnable); DummyMainThread dummyMainThread = new DummyMainThread();
verify(runnable).run(); dummyMainThread.runOnMainThread(
() ->
mediaSource.addMediaSource(
createFakeMediaSource(), new Handler(), runnableInvoked::open));
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test
public void testCustomCallbackBeforePreparationAddMultiple() { public void testCustomCallbackBeforePreparationAddMultiple() {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.addMediaSources( DummyMainThread dummyMainThread = new DummyMainThread();
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}), dummyMainThread.runOnMainThread(
new Handler(), () ->
runnable); mediaSource.addMediaSources(
verify(runnable).run(); Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}),
new Handler(),
runnableInvoked::open));
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test
public void testCustomCallbackBeforePreparationAddSingleWithIndex() { public void testCustomCallbackBeforePreparationAddSingleWithIndex() {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.addMediaSource(/* index */ 0, createFakeMediaSource(), new Handler(), runnable); DummyMainThread dummyMainThread = new DummyMainThread();
verify(runnable).run(); dummyMainThread.runOnMainThread(
() ->
mediaSource.addMediaSource(
/* index */ 0, createFakeMediaSource(), new Handler(), runnableInvoked::open));
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test
public void testCustomCallbackBeforePreparationAddMultipleWithIndex() { public void testCustomCallbackBeforePreparationAddMultipleWithIndex() {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.addMediaSources( DummyMainThread dummyMainThread = new DummyMainThread();
/* index */ 0, dummyMainThread.runOnMainThread(
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}), () ->
new Handler(), mediaSource.addMediaSources(
runnable); /* index */ 0,
verify(runnable).run(); Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}),
new Handler(),
runnableInvoked::open));
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test
public void testCustomCallbackBeforePreparationRemove() { public void testCustomCallbackBeforePreparationRemove() {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.addMediaSource(createFakeMediaSource()); DummyMainThread dummyMainThread = new DummyMainThread();
mediaSource.removeMediaSource(/* index */ 0, new Handler(), runnable); dummyMainThread.runOnMainThread(
verify(runnable).run(); () -> {
mediaSource.addMediaSource(createFakeMediaSource());
mediaSource.removeMediaSource(/* index */ 0, new Handler(), runnableInvoked::open);
});
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test
public void testCustomCallbackBeforePreparationMove() { public void testCustomCallbackBeforePreparationMove() {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.addMediaSources( DummyMainThread dummyMainThread = new DummyMainThread();
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()})); dummyMainThread.runOnMainThread(
mediaSource.moveMediaSource(/* fromIndex */ 1, /* toIndex */ 0, new Handler(), runnable); () -> {
verify(runnable).run(); mediaSource.addMediaSources(
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}));
mediaSource.moveMediaSource(
/* fromIndex */ 1, /* toIndex */ 0, new Handler(), runnableInvoked::open);
});
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test
@ -997,11 +1037,19 @@ public final class ConcatenatingMediaSourceTest {
@Test @Test
public void testCustomCallbackBeforePreparationSetShuffleOrder() throws Exception { public void testCustomCallbackBeforePreparationSetShuffleOrder() throws Exception {
Runnable runnable = mock(Runnable.class); ConditionVariable runnableInvoked = new ConditionVariable();
mediaSource.setShuffleOrder(
new ShuffleOrder.UnshuffledShuffleOrder(/* length= */ 0), new Handler(), runnable);
verify(runnable).run(); DummyMainThread dummyMainThread = new DummyMainThread();
dummyMainThread.runOnMainThread(
() ->
mediaSource.setShuffleOrder(
new ShuffleOrder.UnshuffledShuffleOrder(/* length= */ 0),
new Handler(),
runnableInvoked::open));
runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS);
dummyMainThread.release();
assertThat(runnableInvoked.block(0)).isTrue();
} }
@Test @Test