use of com.google.android.exoplayer2.util.HandlerWrapper in project ExoPlayer by google.
the class FakeClockTest method createHandler_sendEmptyMessage_triggersMessageAtCorrectTime.
@Test
public void createHandler_sendEmptyMessage_triggersMessageAtCorrectTime() {
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.sendEmptyMessage(/* what= */
1);
handler.sendEmptyMessageAtTime(/* what= */
2, /* uptimeMs= */
fakeClock.uptimeMillis() + 60);
handler.sendEmptyMessageDelayed(/* what= */
3, /* delayMs= */
50);
handler.sendEmptyMessage(/* what= */
4);
ShadowLooper.idleMainLooper();
shadowOf(handler.getLooper()).idle();
assertThat(callback.messages).containsExactly(new MessageData(/* what= */
1, /* arg1= */
0, /* arg2= */
0, /* obj=*/
null), new MessageData(/* what= */
4, /* arg1= */
0, /* arg2= */
0, /* obj=*/
null)).inOrder();
fakeClock.advanceTime(50);
shadowOf(handler.getLooper()).idle();
assertThat(callback.messages).hasSize(3);
assertThat(Iterables.getLast(callback.messages)).isEqualTo(new MessageData(/* what= */
3, /* arg1= */
0, /* arg2= */
0, /* obj=*/
null));
fakeClock.advanceTime(50);
shadowOf(handler.getLooper()).idle();
assertThat(callback.messages).hasSize(4);
assertThat(Iterables.getLast(callback.messages)).isEqualTo(new MessageData(/* what= */
2, /* arg1= */
0, /* arg2= */
0, /* obj=*/
null));
}
use of com.google.android.exoplayer2.util.HandlerWrapper 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));
}
use of com.google.android.exoplayer2.util.HandlerWrapper in project ExoPlayer by google.
the class FakeClockTest method createHandler_multiThreadCommunication_deliversMessagesDeterministicallyInOrder.
@Test
public void createHandler_multiThreadCommunication_deliversMessagesDeterministicallyInOrder() {
HandlerThread handlerThread1 = new HandlerThread("FakeClockTest");
handlerThread1.start();
HandlerThread handlerThread2 = new HandlerThread("FakeClockTest");
handlerThread2.start();
FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
0);
HandlerWrapper handler1 = fakeClock.createHandler(handlerThread1.getLooper(), /* callback= */
null);
HandlerWrapper handler2 = fakeClock.createHandler(handlerThread2.getLooper(), /* callback= */
null);
ConditionVariable messagesFinished = new ConditionVariable();
ArrayList<Integer> executionOrder = new ArrayList<>();
handler1.post(() -> {
executionOrder.add(1);
handler2.post(() -> executionOrder.add(2));
handler1.post(() -> executionOrder.add(3));
handler2.post(() -> {
executionOrder.add(4);
handler2.post(() -> executionOrder.add(7));
handler1.post(() -> {
executionOrder.add(8);
messagesFinished.open();
});
});
handler2.post(() -> executionOrder.add(5));
handler1.post(() -> executionOrder.add(6));
});
ShadowLooper.idleMainLooper();
messagesFinished.block();
assertThat(executionOrder).containsExactly(1, 2, 3, 4, 5, 6, 7, 8).inOrder();
}
use of com.google.android.exoplayer2.util.HandlerWrapper 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);
}
use of com.google.android.exoplayer2.util.HandlerWrapper in project ExoPlayer by google.
the class FakeClockTest method createHandler_obtainMessageSendToTarget_triggersMessage.
@Test
public void createHandler_obtainMessageSendToTarget_triggersMessage() {
HandlerThread handlerThread = new HandlerThread("FakeClockTest");
handlerThread.start();
FakeClock fakeClock = new FakeClock(/* initialTimeMs= */
0);
TestCallback callback = new TestCallback();
HandlerWrapper handler = fakeClock.createHandler(handlerThread.getLooper(), callback);
Object testObject = new Object();
handler.obtainMessage(/* what= */
1).sendToTarget();
handler.obtainMessage(/* what= */
2, /* obj= */
testObject).sendToTarget();
handler.obtainMessage(/* what= */
3, /* arg1= */
99, /* arg2= */
44).sendToTarget();
handler.obtainMessage(/* what= */
4, /* arg1= */
88, /* arg2= */
33, /* obj=*/
testObject).sendToTarget();
ShadowLooper.idleMainLooper();
shadowOf(handler.getLooper()).idle();
assertThat(callback.messages).containsExactly(new MessageData(/* what= */
1, /* arg1= */
0, /* arg2= */
0, /* obj=*/
null), new MessageData(/* what= */
2, /* arg1= */
0, /* arg2= */
0, /* obj=*/
testObject), new MessageData(/* what= */
3, /* arg1= */
99, /* arg2= */
44, /* obj=*/
null), new MessageData(/* what= */
4, /* arg1= */
88, /* arg2= */
33, /* obj=*/
testObject)).inOrder();
}
Aggregations