Search in sources :

Example 1 with TestRunnable

use of com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable in project ExoPlayer by google.

the class FakeClockTest method createHandler_removeMessages_removesMessages.

@Test
public void createHandler_removeMessages_removesMessages() {
    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= */
    2);
    handler.removeMessages(/* what= */
    2);
    handler.removeCallbacksAndMessages(messageToken);
    fakeClock.advanceTime(50);
    ShadowLooper.idleMainLooper();
    shadowOf(handlerThread.getLooper()).idle();
    assertThat(callback.messages).containsExactly(new MessageData(/* what= */
    3, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null));
    assertThat(testRunnable1.hasRun).isTrue();
    assertThat(testRunnable2.hasRun).isTrue();
    // Assert that message with same "what" on other handler wasn't removed.
    assertThat(otherCallback.messages).containsExactly(new MessageData(/* what= */
    2, /* arg1= */
    0, /* arg2= */
    0, /* obj=*/
    null));
}
Also used : HandlerThread(android.os.HandlerThread) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 2 with TestRunnable

use of com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable in project ExoPlayer by google.

the class FakeClockTest method createHandler_postDelayed_triggersMessagesUpToCurrentTime.

@Test
public void createHandler_postDelayed_triggersMessagesUpToCurrentTime() {
    HandlerThread handlerThread = new HandlerThread("FakeClockTest");
    handlerThread.start();
    FakeClock fakeClock = new FakeClock(0);
    HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), /* callback= */
    null);
    TestRunnable[] testRunnables = { new TestRunnable(), new TestRunnable(), new TestRunnable(), new TestRunnable(), new TestRunnable() };
    handler.postDelayed(testRunnables[0], 0);
    handler.postDelayed(testRunnables[1], 100);
    handler.postDelayed(testRunnables[2], 200);
    ShadowLooper.idleMainLooper();
    shadowOf(handler.getLooper()).idle();
    assertTestRunnableStates(new boolean[] { true, false, false, false, false }, testRunnables);
    fakeClock.advanceTime(150);
    handler.postDelayed(testRunnables[3], 50);
    handler.postDelayed(testRunnables[4], 100);
    ShadowLooper.idleMainLooper();
    shadowOf(handler.getLooper()).idle();
    assertTestRunnableStates(new boolean[] { true, true, false, false, false }, testRunnables);
    fakeClock.advanceTime(50);
    shadowOf(handler.getLooper()).idle();
    assertTestRunnableStates(new boolean[] { true, true, true, true, false }, testRunnables);
    fakeClock.advanceTime(1000);
    shadowOf(handler.getLooper()).idle();
    assertTestRunnableStates(new boolean[] { true, true, true, true, true }, testRunnables);
}
Also used : HandlerThread(android.os.HandlerThread) HandlerWrapper(com.google.android.exoplayer2.util.HandlerWrapper) Test(org.junit.Test)

Example 3 with TestRunnable

use of com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable in project ExoPlayer by google.

the class FakeDataSourceTest method testRunnable.

@Test
public void testRunnable() throws IOException {
    TestRunnable[] runnables = new TestRunnable[3];
    for (int i = 0; i < 3; i++) {
        runnables[i] = new TestRunnable();
    }
    FakeDataSource dataSource = new FakeDataSource(new FakeDataSet().newData(uri.toString()).appendReadData(TEST_DATA).appendReadAction(runnables[0]).appendReadData(TEST_DATA).appendReadAction(runnables[1]).appendReadAction(runnables[2]).appendReadData(TEST_DATA).endData());
    assertThat(dataSource.open(new DataSpec(uri))).isEqualTo(45);
    assertThat(dataSource.read(BUFFER, 0, BUFFER.length)).isEqualTo(15);
    assertBuffer(TEST_DATA);
    for (int i = 0; i < 3; i++) {
        assertThat(runnables[i].ran).isFalse();
    }
    assertThat(dataSource.read(BUFFER, 0, BUFFER.length)).isEqualTo(15);
    assertBuffer(TEST_DATA);
    assertThat(runnables[0].ran).isTrue();
    assertThat(runnables[1].ran).isFalse();
    assertThat(runnables[2].ran).isFalse();
    assertThat(dataSource.read(BUFFER, 0, BUFFER.length)).isEqualTo(15);
    assertBuffer(TEST_DATA);
    for (int i = 0; i < 3; i++) {
        assertThat(runnables[i].ran).isTrue();
    }
    assertThat(dataSource.read(BUFFER, 0, BUFFER.length)).isEqualTo(C.RESULT_END_OF_INPUT);
    dataSource.close();
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 4 with TestRunnable

use of com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable 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)

Aggregations

Test (org.junit.Test)4 HandlerThread (android.os.HandlerThread)3 HandlerWrapper (com.google.android.exoplayer2.util.HandlerWrapper)3 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)1