Search in sources :

Example 11 with ShadowLooper

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

Example 12 with ShadowLooper

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

Example 13 with ShadowLooper

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

Example 14 with ShadowLooper

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

Example 15 with ShadowLooper

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());
}
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)

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