Search in sources :

Example 6 with HandlerWrapper

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

the class FakeClockTest method createHandler_withIsAutoAdvancing_advancesTimeToNextMessages.

@Test
public void createHandler_withIsAutoAdvancing_advancesTimeToNextMessages() {
    HandlerThread handlerThread = new HandlerThread("FakeClockTest");
    handlerThread.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0, /* isAutoAdvancing= */
    true);
    HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), /* callback= */
    null);
    // Post a series of immediate and delayed messages.
    ArrayList<Long> clockTimes = new ArrayList<>();
    handler.post(() -> {
        handler.postDelayed(() -> clockTimes.add(fakeClock.elapsedRealtime()), /* delayMs= */
        100);
        handler.postDelayed(() -> clockTimes.add(fakeClock.elapsedRealtime()), /* delayMs= */
        50);
        handler.post(() -> clockTimes.add(fakeClock.elapsedRealtime()));
        handler.postDelayed(() -> {
            clockTimes.add(fakeClock.elapsedRealtime());
            handler.postDelayed(() -> clockTimes.add(fakeClock.elapsedRealtime()), /* delayMs= */
            50);
        }, /* delayMs= */
        20);
    });
    ShadowLooper.idleMainLooper();
    shadowOf(handler.getLooper()).idle();
    assertThat(clockTimes).containsExactly(0L, 20L, 50L, 70L, 100L).inOrder();
}
Also used : HandlerThread(android.os.HandlerThread) ArrayList(java.util.ArrayList) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 7 with HandlerWrapper

use of com.google.android.exoplayer2.util.HandlerWrapper 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)

Example 8 with HandlerWrapper

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

the class FakeClockTest method createHandler_removeAllMessages_removesAllMessages.

@Test
public void createHandler_removeAllMessages_removesAllMessages() {
    HandlerThread handlerThread = new HandlerThread("FakeClockTest");
    handlerThread.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0);
    TestCallback callback = new TestCallback();
    HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), callback);
    TestCallback otherCallback = new TestCallback();
    HandlerWrapper otherHandler = fakeClock.createHandler(handlerThread.getLooper(), otherCallback);
    TestRunnable testRunnable1 = new TestRunnable();
    TestRunnable testRunnable2 = new TestRunnable();
    Object messageToken = new Object();
    handler.obtainMessage(/* what= */
    1, /* obj= */
    messageToken).sendToTarget();
    handler.sendEmptyMessageDelayed(/* what= */
    2, /* delayMs= */
    50);
    handler.post(testRunnable1);
    handler.postDelayed(testRunnable2, /* delayMs= */
    25);
    handler.sendEmptyMessage(/* what= */
    3);
    otherHandler.sendEmptyMessage(/* what= */
    1);
    handler.removeCallbacksAndMessages(/* token= */
    null);
    fakeClock.advanceTime(50);
    ShadowLooper.idleMainLooper();
    shadowOf(handlerThread.getLooper()).idle();
    assertThat(callback.messages).isEmpty();
    assertThat(testRunnable1.hasRun).isFalse();
    assertThat(testRunnable2.hasRun).isFalse();
    // Assert that message on other handler wasn't removed.
    assertThat(otherCallback.messages).containsExactly(new MessageData(/* what= */
    1, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null));
}
Also used : HandlerThread(android.os.HandlerThread) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 9 with HandlerWrapper

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

the class FakeClockTest method createHandler_sendMessageAtFrontOfQueue_sendsMessageFirst.

@Test
public void createHandler_sendMessageAtFrontOfQueue_sendsMessageFirst() {
    HandlerThread handlerThread = new HandlerThread("FakeClockTest");
    handlerThread.start();
    FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
    0);
    TestCallback callback = new TestCallback();
    HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), callback);
    handler.obtainMessage(/* what= */
    1).sendToTarget();
    handler.sendMessageAtFrontOfQueue(handler.obtainMessage(/* what= */
    2));
    handler.sendMessageAtFrontOfQueue(handler.obtainMessage(/* what= */
    3));
    handler.obtainMessage(/* what= */
    4).sendToTarget();
    ShadowLooper.idleMainLooper();
    shadowOf(handler.getLooper()).idle();
    assertThat(callback.messages).containsExactly(new MessageData(/* what= */
    3, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null), new MessageData(/* what= */
    2, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null), new MessageData(/* what= */
    1, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null), new MessageData(/* what= */
    4, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null)).inOrder();
}
Also used : HandlerThread(android.os.HandlerThread) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Aggregations

HandlerThread (android.os.HandlerThread)9 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)3 ConditionVariable (android.os.ConditionVariable)2