Search in sources :

Example 41 with ShadowLooper

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();
    }
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) TimeoutException(java.util.concurrent.TimeoutException)

Example 42 with ShadowLooper

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ShadowLooper(org.robolectric.shadows.ShadowLooper) Looper(android.os.Looper) ShadowLooper(org.robolectric.shadows.ShadowLooper) Handler(android.os.Handler) Test(org.junit.Test)

Example 43 with ShadowLooper

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());
}
Also used : FakeSampleStream(com.google.android.exoplayer2.testutil.FakeSampleStream) ShadowLooper(org.robolectric.shadows.ShadowLooper) DefaultAllocator(com.google.android.exoplayer2.upstream.DefaultAllocator) DrmSessionEventListener(com.google.android.exoplayer2.drm.DrmSessionEventListener) Test(org.junit.Test)

Example 44 with ShadowLooper

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);
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) LooperMode(org.robolectric.annotation.LooperMode) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 45 with ShadowLooper

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));
}
Also used : ShadowLooper(org.robolectric.shadows.ShadowLooper) Test(org.junit.Test)

Aggregations

ShadowLooper (org.robolectric.shadows.ShadowLooper)66 Test (org.junit.Test)56 BackgroundThreadExecutor (org.prebid.mobile.tasksmanager.BackgroundThreadExecutor)42 HttpUrl (okhttp3.HttpUrl)35 MockResponse (okhttp3.mockwebserver.MockResponse)35 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)23 HashSet (java.util.HashSet)10 AdManagerAdRequest (com.google.android.gms.ads.admanager.AdManagerAdRequest)8 MoPubView (com.mopub.mobileads.MoPubView)7 Bundle (android.os.Bundle)5 Handler (android.os.Handler)4 MediaCodec (android.media.MediaCodec)3 Looper (android.os.Looper)3 Message (android.os.Message)3 NativeCustomTemplateAd (com.google.android.gms.ads.formats.NativeCustomTemplateAd)3 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)3 Scheduler (io.reactivex.rxjava3.core.Scheduler)3 Field (java.lang.reflect.Field)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3