use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCallOnExceptionForExceptionInOnPreExecute.
@Test
public void shouldCallOnExceptionForExceptionInOnPreExecute() {
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
final boolean[] callRecord = new boolean[] { false, false, false };
final boolean[] correctAnswer = new boolean[] { false, true, true };
Executors.newSingleThreadExecutor().submit(new AndroidCallable<String>() {
@Override
public void onPreCall() {
throw new RuntimeException();
}
@Override
public String doInBackground() throws Exception {
return "";
}
@Override
public void onSuccess(String result) {
callRecord[0] = true;
}
@Override
public void onException(Exception e) {
callRecord[1] = true;
}
@Override
public void onFinally() {
callRecord[2] = true;
}
});
// Run all the pending tasks on the ui thread
while (!callRecord[callRecord.length - 1]) looper.runToEndOfTasks();
assertThat(callRecord, equalTo(correctAnswer));
}
use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class AsynchronousMediaCodecCallbackTest method getOutputFormat_withConsecutiveFlushAndPendingFormatFromFirstFlush_returnsPendingFormat.
@Test
public void getOutputFormat_withConsecutiveFlushAndPendingFormatFromFirstFlush_returnsPendingFormat() {
MediaCodec.BufferInfo outInfo = new MediaCodec.BufferInfo();
AtomicInteger flushCompleted = new AtomicInteger();
Handler callbackThreadHandler = new Handler(callbackThread.getLooper());
ShadowLooper shadowCallbackLooper = shadowOf(callbackThread.getLooper());
shadowCallbackLooper.pause();
asynchronousMediaCodecCallback.onOutputFormatChanged(codec, createMediaFormat("format0"));
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, /* index= */
0, new MediaCodec.BufferInfo());
// Flush and progress the looper so that flush is completed.
asynchronousMediaCodecCallback.flush(/* codec= */
null);
callbackThreadHandler.post(flushCompleted::incrementAndGet);
shadowCallbackLooper.idle();
// Flush again, the pending format from the first flush should remain as pending.
asynchronousMediaCodecCallback.flush(/* codec= */
null);
callbackThreadHandler.post(flushCompleted::incrementAndGet);
shadowCallbackLooper.idle();
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, /* index= */
1, new MediaCodec.BufferInfo());
assertThat(flushCompleted.get()).isEqualTo(2);
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outInfo)).isEqualTo(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED);
assertThat(asynchronousMediaCodecCallback.getOutputFormat().getString("name")).isEqualTo("format0");
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outInfo)).isEqualTo(1);
}
use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class AsynchronousMediaCodecCallbackTest method getOutputFormat_afterFlushWithPendingFormat_returnsPendingFormat.
@Test
public void getOutputFormat_afterFlushWithPendingFormat_returnsPendingFormat() {
MediaCodec.BufferInfo outInfo = new MediaCodec.BufferInfo();
AtomicBoolean flushCompleted = new AtomicBoolean();
Looper callbackThreadLooper = callbackThread.getLooper();
ShadowLooper shadowCallbackLooper = shadowOf(callbackThreadLooper);
shadowCallbackLooper.pause();
asynchronousMediaCodecCallback.onOutputFormatChanged(codec, createMediaFormat("format0"));
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, /* index= */
0, new MediaCodec.BufferInfo());
asynchronousMediaCodecCallback.onOutputFormatChanged(codec, createMediaFormat("format1"));
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, /* index= */
1, new MediaCodec.BufferInfo());
asynchronousMediaCodecCallback.flush(/* codec= */
null);
new Handler(callbackThreadLooper).post(() -> flushCompleted.set(true));
// Progress the looper so that flush is completed
shadowCallbackLooper.idle();
// Enqueue an output buffer to make the pending format available.
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, /* index= */
2, new MediaCodec.BufferInfo());
assertThat(flushCompleted.get()).isTrue();
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outInfo)).isEqualTo(MediaCodec.INFO_OUTPUT_FORMAT_CHANGED);
assertThat(asynchronousMediaCodecCallback.getOutputFormat().getString("name")).isEqualTo("format1");
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(outInfo)).isEqualTo(2);
}
use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class AsynchronousMediaCodecCallbackTest method dequeOutputBufferIndex_withPendingFlush_returnsTryAgain.
@Test
public void dequeOutputBufferIndex_withPendingFlush_returnsTryAgain() {
AtomicBoolean beforeFlushCompletes = new AtomicBoolean();
AtomicBoolean flushCompleted = new AtomicBoolean();
Looper callbackThreadLooper = callbackThread.getLooper();
Handler callbackHandler = new Handler(callbackThreadLooper);
ShadowLooper shadowCallbackLooper = shadowOf(callbackThreadLooper);
// Pause the callback thread so that flush() never completes.
shadowCallbackLooper.pause();
// Send two output buffers to the callback and then flush().
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 0, bufferInfo);
asynchronousMediaCodecCallback.onOutputBufferAvailable(codec, 1, bufferInfo);
callbackHandler.post(() -> beforeFlushCompletes.set(true));
asynchronousMediaCodecCallback.flush(/* codec= */
null);
callbackHandler.post(() -> flushCompleted.set(true));
while (beforeFlushCompletes.get()) {
shadowCallbackLooper.runOneTask();
}
assertThat(flushCompleted.get()).isFalse();
assertThat(asynchronousMediaCodecCallback.dequeueOutputBufferIndex(new MediaCodec.BufferInfo())).isEqualTo(MediaCodec.INFO_TRY_AGAIN_LATER);
}
use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class MediaCodecVideoRendererTest method replaceStream_whenNotStarted_doesNotRenderFirstFrameOfNewStream.
@Test
public void replaceStream_whenNotStarted_doesNotRenderFirstFrameOfNewStream() throws Exception {
ShadowLooper shadowLooper = shadowOf(testMainLooper);
FakeSampleStream fakeSampleStream1 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
true, /* individualAllocationSize= */
1024), /* mediaSourceEventDispatcher= */
null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
fakeSampleStream1.writeData(/* startPositionUs= */
0);
FakeSampleStream fakeSampleStream2 = new FakeSampleStream(new DefaultAllocator(/* trimOnReset= */
true, /* individualAllocationSize= */
1024), /* mediaSourceEventDispatcher= */
null, DrmSessionManager.DRM_UNSUPPORTED, new DrmSessionEventListener.EventDispatcher(), /* initialFormat= */
VIDEO_H264, ImmutableList.of(oneByteSample(/* timeUs= */
0, C.BUFFER_FLAG_KEY_FRAME), END_OF_STREAM_ITEM));
fakeSampleStream2.writeData(/* startPositionUs= */
0);
mediaCodecVideoRenderer.enable(RendererConfiguration.DEFAULT, new Format[] { VIDEO_H264 }, fakeSampleStream1, /* positionUs= */
0, /* joining= */
false, /* mayRenderStartOfStream= */
true, /* startPositionUs= */
0, /* offsetUs */
0);
boolean replacedStream = false;
for (int i = 0; i < 10; i++) {
mediaCodecVideoRenderer.render(/* positionUs= */
i * 10, SystemClock.elapsedRealtime() * 1000);
if (!replacedStream && mediaCodecVideoRenderer.hasReadStreamToEnd()) {
mediaCodecVideoRenderer.replaceStream(new Format[] { VIDEO_H264 }, fakeSampleStream2, /* startPositionUs= */
100, /* offsetUs= */
100);
replacedStream = true;
}
}
shadowLooper.idle();
verify(eventListener).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
anyLong());
// Render to streamOffsetUs and verify the new first frame gets rendered.
mediaCodecVideoRenderer.render(/* positionUs= */
100, SystemClock.elapsedRealtime() * 1000);
shadowLooper.idle();
verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
anyLong());
}
Aggregations