Commit 2716ff89 by samrobinson Committed by Marc Baechinger

Simplify TransformerEndToEndTest and TransformerTestRunner.

PiperOrigin-RevId: 497335605
parent c0bbfe92
...@@ -17,12 +17,12 @@ ...@@ -17,12 +17,12 @@
package com.google.android.exoplayer2.transformer; package com.google.android.exoplayer2.transformer;
import static com.google.android.exoplayer2.robolectric.RobolectricUtil.runLooperUntil; import static com.google.android.exoplayer2.robolectric.RobolectricUtil.runLooperUntil;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.robolectric.RobolectricUtil; import com.google.android.exoplayer2.robolectric.RobolectricUtil;
import java.util.Objects;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.checkerframework.checker.nullness.compatqual.NullableType; import org.checkerframework.checker.nullness.compatqual.NullableType;
...@@ -32,55 +32,27 @@ public final class TransformerTestRunner { ...@@ -32,55 +32,27 @@ public final class TransformerTestRunner {
private TransformerTestRunner() {} private TransformerTestRunner() {}
/** /**
* Runs tasks of the {@link Transformer#getApplicationLooper() transformer Looper} until the * Runs tasks of the {@linkplain Transformer#getApplicationLooper() transformer Looper} until the
* current {@link Transformer transformation} completes. * {@linkplain Transformer transformation} ends.
* *
* @param transformer The {@link Transformer}. * @param transformer The {@link Transformer}.
* @return The {@link TransformationResult}.
* @throws TransformationException If the transformation threw an exception.
* @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is * @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
* exceeded. * exceeded.
* @throws IllegalStateException If the method is not called from the main thread, or if the * @throws IllegalStateException If the method is not called from the main thread.
* transformation completes with error.
*/ */
public static void runUntilCompleted(Transformer transformer) throws TimeoutException { public static TransformationResult runLooper(Transformer transformer)
@Nullable Exception exception = runUntilListenerCalled(transformer); throws TransformationException, TimeoutException {
if (exception != null) { AtomicReference<@NullableType TransformationResult> transformationResultRef =
throw new IllegalStateException(exception);
}
}
/**
* Runs tasks of the {@link Transformer#getApplicationLooper() transformer Looper} until a {@link
* Transformer} error occurs.
*
* @param transformer The {@link Transformer}.
* @return The raised exception.
* @throws TimeoutException If the {@link RobolectricUtil#DEFAULT_TIMEOUT_MS default timeout} is
* exceeded.
* @throws IllegalStateException If the method is not called from the main thread, or if the
* transformation completes without error.
*/
public static TransformationException runUntilError(Transformer transformer)
throws TimeoutException {
@Nullable TransformationException exception = runUntilListenerCalled(transformer);
if (exception == null) {
throw new IllegalStateException("The transformation completed without error.");
}
return exception;
}
@Nullable
private static TransformationException runUntilListenerCalled(Transformer transformer)
throws TimeoutException {
AtomicBoolean transformationCompleted = new AtomicBoolean();
AtomicReference<@NullableType TransformationException> transformationException =
new AtomicReference<>(); new AtomicReference<>();
transformer.addListener( transformer.addListener(
new Transformer.Listener() { new Transformer.Listener() {
@Override @Override
public void onTransformationCompleted( public void onTransformationCompleted(
MediaItem inputMediaItem, TransformationResult transformationResult) { MediaItem inputMediaItem, TransformationResult result) {
transformationCompleted.set(true); transformationResultRef.set(result);
} }
@Override @Override
...@@ -88,13 +60,19 @@ public final class TransformerTestRunner { ...@@ -88,13 +60,19 @@ public final class TransformerTestRunner {
MediaItem inputMediaItem, MediaItem inputMediaItem,
TransformationResult result, TransformationResult result,
TransformationException exception) { TransformationException exception) {
transformationException.set(exception); if (!Objects.equals(result.transformationException, exception)) {
result = result.buildUpon().setTransformationException(exception).build();
}
transformationResultRef.set(result);
} }
}); });
runLooperUntil( runLooperUntil(transformer.getApplicationLooper(), () -> transformationResultRef.get() != null);
transformer.getApplicationLooper(),
() -> transformationCompleted.get() || transformationException.get() != null); TransformationResult result = checkNotNull(transformationResultRef.get());
if (result.transformationException != null) {
throw result.transformationException;
}
return transformationException.get(); return result;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment