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