Search in sources :

Example 16 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class TestPlayerRunHelper method playUntilPosition.

/**
 * Calls {@link Player#play()}, runs tasks of the main {@link Looper} until the {@code player}
 * reaches the specified position or a playback error occurs, and then pauses the {@code player}.
 *
 * <p>If a playback error occurs it will be thrown wrapped in an {@link IllegalStateException}.
 *
 * @param player The {@link Player}.
 * @param mediaItemIndex The index of the media item.
 * @param positionMs The position within the media item, in milliseconds.
 * @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
 *     exceeded.
 */
public static void playUntilPosition(ExoPlayer player, int mediaItemIndex, long positionMs) throws TimeoutException {
    verifyMainTestThread(player);
    Looper applicationLooper = Util.getCurrentOrMainLooper();
    AtomicBoolean messageHandled = new AtomicBoolean(false);
    player.createMessage((messageType, payload) -> {
        // Block playback thread until pause command has been sent from test thread.
        ConditionVariable blockPlaybackThreadCondition = new ConditionVariable();
        player.getClock().createHandler(applicationLooper, /* callback= */
        null).post(() -> {
            player.pause();
            messageHandled.set(true);
            blockPlaybackThreadCondition.open();
        });
        try {
            player.getClock().onThreadBlocked();
            blockPlaybackThreadCondition.block();
        } catch (InterruptedException e) {
        // Ignore.
        }
    }).setPosition(mediaItemIndex, positionMs).send();
    player.play();
    runMainLooperUntil(() -> messageHandled.get() || player.getPlayerError() != null);
    if (player.getPlayerError() != null) {
        throw new IllegalStateException(player.getPlayerError());
    }
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) Looper(android.os.Looper) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 17 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class TestDownloadManagerListener method blockUntilIdle.

/**
 * Blocks until the manager is idle.
 */
public void blockUntilIdle() throws InterruptedException {
    idleCondition.close();
    // If the manager is already idle the condition will be opened by the code immediately below.
    // Else it will be opened by onIdle().
    ConditionVariable checkedOnMainThread = createRobolectricConditionVariable();
    new Handler(downloadManager.getApplicationLooper()).post(() -> {
        if (downloadManager.isIdle()) {
            idleCondition.open();
        }
        checkedOnMainThread.open();
    });
    assertThat(checkedOnMainThread.block(TIMEOUT_MS)).isTrue();
    assertThat(idleCondition.block(TIMEOUT_MS)).isTrue();
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) RobolectricUtil.createRobolectricConditionVariable(com.google.android.exoplayer2.robolectric.RobolectricUtil.createRobolectricConditionVariable) Handler(android.os.Handler)

Example 18 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class DownloadServiceDashTest method removeBeforeDownloadComplete.

@Ignore("Internal ref: b/78877092")
@Test
public void removeBeforeDownloadComplete() throws Throwable {
    pauseDownloadCondition = new ConditionVariable();
    downloadKeys(fakeStreamKey1, fakeStreamKey2);
    removeAll();
    downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
    assertCacheEmpty(cache);
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class CronetDataSource method getStatus.

private static int getStatus(UrlRequest request) throws InterruptedException {
    final ConditionVariable conditionVariable = new ConditionVariable();
    final int[] statusHolder = new int[1];
    request.getStatus(new UrlRequest.StatusListener() {

        @Override
        public void onStatus(int status) {
            statusHolder[0] = status;
            conditionVariable.open();
        }
    });
    conditionVariable.block();
    return statusHolder[0];
}
Also used : ConditionVariable(com.google.android.exoplayer2.util.ConditionVariable) UrlRequest(org.chromium.net.UrlRequest)

Example 20 with ConditionVariable

use of com.google.android.exoplayer2.util.ConditionVariable in project ExoPlayer by google.

the class FakeClockTest method createHandler_blockingThreadWithOnBusyWaiting_canBeUnblockedByOtherThread.

@Test
public void createHandler_blockingThreadWithOnBusyWaiting_canBeUnblockedByOtherThread() {
    HandlerThread handlerThread1 = new HandlerThread("FakeClockTest");
    handlerThread1.start();
    HandlerThread handlerThread2 = new HandlerThread("FakeClockTest");
    handlerThread2.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0, /* isAutoAdvancing= */
    true);
    HandlerWrapper handler1 = fakeClock.createHandler(handlerThread1.getLooper(), /* callback= */
    null);
    HandlerWrapper handler2 = fakeClock.createHandler(handlerThread2.getLooper(), /* callback= */
    null);
    ArrayList<Integer> executionOrder = new ArrayList<>();
    handler1.post(() -> {
        executionOrder.add(1);
        ConditionVariable blockingCondition = new ConditionVariable();
        handler2.postDelayed(() -> {
            executionOrder.add(2);
            blockingCondition.open();
        }, /* delayMs= */
        50);
        handler1.post(() -> executionOrder.add(4));
        fakeClock.onThreadBlocked();
        blockingCondition.block();
        executionOrder.add(3);
    });
    ShadowLooper.idleMainLooper();
    shadowOf(handler1.getLooper()).idle();
    shadowOf(handler2.getLooper()).idle();
    assertThat(executionOrder).containsExactly(1, 2, 3, 4).inOrder();
}
Also used : ConditionVariable(android.os.ConditionVariable) HandlerThread(android.os.HandlerThread) ArrayList(java.util.ArrayList) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 ConditionVariable (android.os.ConditionVariable)12 HttpDataSourceException (com.google.android.exoplayer2.upstream.HttpDataSource.HttpDataSourceException)9 ConditionVariable (com.google.android.exoplayer2.util.ConditionVariable)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 InterruptedIOException (java.io.InterruptedIOException)4 SocketTimeoutException (java.net.SocketTimeoutException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HandlerThread (android.os.HandlerThread)2 MediaPeriod (com.google.android.exoplayer2.source.MediaPeriod)2 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Handler (android.os.Handler)1 Looper (android.os.Looper)1 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)1 MediaItem (com.google.android.exoplayer2.MediaItem)1 PlaybackException (com.google.android.exoplayer2.PlaybackException)1 Player (com.google.android.exoplayer2.Player)1 RobolectricUtil.createRobolectricConditionVariable (com.google.android.exoplayer2.robolectric.RobolectricUtil.createRobolectricConditionVariable)1