From d148db5725ea352cb26737c9e5dc62d819801633 Mon Sep 17 00:00:00 2001 From: kimvde Date: Tue, 8 Dec 2020 08:58:29 +0000 Subject: [PATCH] Transformer: add a setter for the looper PiperOrigin-RevId: 346270250 --- .../robolectric/RobolectricUtil.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/robolectricutils/src/main/java/com/google/android/exoplayer2/robolectric/RobolectricUtil.java b/robolectricutils/src/main/java/com/google/android/exoplayer2/robolectric/RobolectricUtil.java index 0374559be9..a46fad9c0d 100644 --- a/robolectricutils/src/main/java/com/google/android/exoplayer2/robolectric/RobolectricUtil.java +++ b/robolectricutils/src/main/java/com/google/android/exoplayer2/robolectric/RobolectricUtil.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.robolectric; +import static org.robolectric.Shadows.shadowOf; + import android.os.Looper; import com.google.android.exoplayer2.util.Clock; import com.google.android.exoplayer2.util.ConditionVariable; @@ -69,6 +71,8 @@ public final class RobolectricUtil { * Runs tasks of the main Robolectric {@link Looper} until the {@code condition} returns {@code * true}. * + *

Must be called on the main test thread. + * * @param condition The condition. * @param timeoutMs The timeout in milliseconds. * @param clock The {@link Clock} to measure the timeout. @@ -76,15 +80,47 @@ public final class RobolectricUtil { */ public static void runMainLooperUntil(Supplier condition, long timeoutMs, Clock clock) throws TimeoutException { - if (Looper.myLooper() != Looper.getMainLooper()) { + runLooperUntil(Looper.getMainLooper(), condition, timeoutMs, clock); + } + + /** + * Runs tasks of the {@code looper} until the {@code condition} returns {@code true}. + * + *

Must be called on the thread corresponding to the {@code looper}. + * + * @param looper The {@link Looper}. + * @param condition The condition. + * @throws TimeoutException If the {@link #DEFAULT_TIMEOUT_MS} is exceeded. + */ + public static void runLooperUntil(Looper looper, Supplier condition) + throws TimeoutException { + runLooperUntil(looper, condition, DEFAULT_TIMEOUT_MS * 1000000, Clock.DEFAULT); + } + + /** + * Runs tasks of the {@code looper} until the {@code condition} returns {@code true}. + * + *

Must be called on the thread corresponding to the {@code looper}. + * + * @param looper The {@link Looper}. + * @param condition The condition. + * @param timeoutMs The timeout in milliseconds. + * @param clock The {@link Clock} to measure the timeout. + * @throws TimeoutException If the {@code timeoutMs timeout} is exceeded. + */ + public static void runLooperUntil( + Looper looper, Supplier condition, long timeoutMs, Clock clock) + throws TimeoutException { + if (Looper.myLooper() != looper) { throw new IllegalStateException(); } + ShadowLooper shadowLooper = shadowOf(looper); long timeoutTimeMs = clock.currentTimeMillis() + timeoutMs; while (!condition.get()) { if (clock.currentTimeMillis() >= timeoutTimeMs) { throw new TimeoutException(); } - ShadowLooper.runMainLooperOneTask(); + shadowLooper.runOneTask(); } } }