From a60938db96e923fe2365f7e97854b4e4987ff714 Mon Sep 17 00:00:00 2001 From: tonihei Date: Wed, 27 Jan 2021 12:39:39 +0000 Subject: [PATCH] Fix triggering messages sent from non-Looper threads. This can happen for instrumented tests that are run on a non-Looper thread. If these tests send a message to a Looper thread to start the test procedure, they should just triger the message directly as before. PiperOrigin-RevId: 354066836 --- .../android/exoplayer2/testutil/FakeClock.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java index 4dd32f6cc5..6266749838 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java @@ -149,10 +149,16 @@ public class FakeClock implements Clock { protected synchronized void addPendingHandlerMessage(HandlerMessage message) { handlerMessages.add(message); if (!waitingForMessage) { - // If this isn't executed from inside a message created by this class, make sure the current - // looper message is finished before handling the new message. - waitingForMessage = true; - new Handler(checkNotNull(Looper.myLooper())).post(this::onMessageHandled); + // This method isn't executed from inside a looper message created by this class. + @Nullable Looper currentLooper = Looper.myLooper(); + if (currentLooper == null) { + // This message is triggered from a non-looper thread, so just execute it directly. + maybeTriggerMessage(); + } else { + // Make sure the current looper message is finished before handling the new message. + waitingForMessage = true; + new Handler(checkNotNull(Looper.myLooper())).post(this::onMessageHandled); + } } }