use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class RobolectricUtil method runLooperUntil.
/**
* Runs tasks of the {@code looper} until the {@code condition} returns {@code true}.
*
* <p>Must be called on the thread corresponding to the {@code looper}.
*
* @param looper The {@link Looper}.
* @param condition The condition.
* @param timeoutMs The timeout in milliseconds.
* @param clock The {@link Clock} to measure the timeout.
* @throws TimeoutException If the {@code timeoutMs timeout} is exceeded.
*/
public static void runLooperUntil(Looper looper, Supplier<Boolean> condition, long timeoutMs, Clock clock) throws TimeoutException {
if (Looper.myLooper() != looper) {
throw new IllegalStateException();
}
ShadowLooper shadowLooper = shadowOf(looper);
long timeoutTimeMs = clock.currentTimeMillis() + timeoutMs;
while (!condition.get()) {
if (clock.currentTimeMillis() >= timeoutTimeMs) {
throw new TimeoutException();
}
shadowLooper.runOneTask();
}
}
use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class AsynchronousMediaCodecCallbackTest method dequeInputBufferIndex_withPendingFlush_returnsTryAgain.
@Test
public void dequeInputBufferIndex_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 input buffers to the callback and then flush().
asynchronousMediaCodecCallback.onInputBufferAvailable(codec, 0);
asynchronousMediaCodecCallback.onInputBufferAvailable(codec, 1);
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.dequeueInputBufferIndex()).isEqualTo(MediaCodec.INFO_TRY_AGAIN_LATER);
}
use of org.robolectric.shadows.ShadowLooper in project ExoPlayer by google.
the class MediaCodecVideoRendererTest method replaceStream_rendersFirstFrameOnlyAfterStartPosition.
@Test
public void replaceStream_rendersFirstFrameOnlyAfterStartPosition() 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= */
1_000_000, 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);
mediaCodecVideoRenderer.start();
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;
}
}
// Expect only the first frame of the first stream to have been rendered.
shadowLooper.idle();
verify(eventListener, times(2)).onRenderedFirstFrame(eq(surface), /* renderTimeMs= */
anyLong());
}
use of org.robolectric.shadows.ShadowLooper in project robolectric by robolectric.
the class LooperModeConfigurerClassTest method shouldUseLegacyShadows.
@Test
@LooperMode(Mode.LEGACY)
public void shouldUseLegacyShadows() {
assertThat(ConfigurationRegistry.get(LooperMode.Mode.class)).isSameInstanceAs(Mode.LEGACY);
ShadowLooper looper = Shadow.extract(Looper.getMainLooper());
assertThat(looper).isInstanceOf(ShadowLegacyLooper.class);
}
use of org.robolectric.shadows.ShadowLooper in project roboguice by roboguice.
the class AndroidCallableTest method shouldCallMethodsUsingProperThreads.
@Test
public void shouldCallMethodsUsingProperThreads() throws Exception {
final Thread fgThread = Thread.currentThread();
final Thread[] bgThread = { null };
final Thread[] answers = new Thread[5];
final ShadowLooper looper = Robolectric.shadowOf(Looper.getMainLooper());
Executors.newSingleThreadExecutor(new MyThreadFactory(bgThread)).submit(new StringAndroidCallable(answers, false));
// Run all the pending tasks on the ui thread
while (answers[answers.length - 1] == null) looper.runToEndOfTasks();
final Thread[] correctAnswer = new Thread[] { fgThread, bgThread[0], null, fgThread, fgThread };
assertThat(answers, equalTo(correctAnswer));
}
Aggregations