Skip to content

Commit

Permalink
Add test action to wait for an isLoading change.
Browse files Browse the repository at this point in the history
This allows to wait until loading started or finished.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=215704424
  • Loading branch information
tonihei authored and ojw28 committed Oct 31, 2018
1 parent 7a3447f commit eef7e28
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Expand Up @@ -686,6 +686,56 @@ protected void doActionImpl(
}
}

/**
* Waits for a specified loading state, returning either immediately or after a call to {@link
* Player.EventListener#onLoadingChanged(boolean)}.
*/
public static final class WaitForIsLoading extends Action {

private final boolean targetIsLoading;

/**
* @param tag A tag to use for logging.
* @param targetIsLoading The loading state to wait for.
*/
public WaitForIsLoading(String tag, boolean targetIsLoading) {
super(tag, "WaitForIsLoading");
this.targetIsLoading = targetIsLoading;
}

@Override
protected void doActionAndScheduleNextImpl(
final SimpleExoPlayer player,
final DefaultTrackSelector trackSelector,
final Surface surface,
final HandlerWrapper handler,
final ActionNode nextAction) {
if (nextAction == null) {
return;
}
if (targetIsLoading == player.isLoading()) {
nextAction.schedule(player, trackSelector, surface, handler);
} else {
player.addListener(
new Player.EventListener() {
@Override
public void onLoadingChanged(boolean isLoading) {
if (targetIsLoading == isLoading) {
player.removeListener(this);
nextAction.schedule(player, trackSelector, surface, handler);
}
}
});
}
}

@Override
protected void doActionImpl(
SimpleExoPlayer player, DefaultTrackSelector trackSelector, Surface surface) {
// Not triggered.
}
}

/**
* Waits for {@link Player.EventListener#onSeekProcessed()}.
*/
Expand Down
Expand Up @@ -41,6 +41,7 @@
import com.google.android.exoplayer2.testutil.Action.SetVideoSurface;
import com.google.android.exoplayer2.testutil.Action.Stop;
import com.google.android.exoplayer2.testutil.Action.ThrowPlaybackException;
import com.google.android.exoplayer2.testutil.Action.WaitForIsLoading;
import com.google.android.exoplayer2.testutil.Action.WaitForPlaybackState;
import com.google.android.exoplayer2.testutil.Action.WaitForPositionDiscontinuity;
import com.google.android.exoplayer2.testutil.Action.WaitForSeekProcessed;
Expand Down Expand Up @@ -414,6 +415,16 @@ public Builder waitForPlaybackState(int targetPlaybackState) {
return apply(new WaitForPlaybackState(tag, targetPlaybackState));
}

/**
* Schedules a delay until {@code player.isLoading()} changes to the specified value.
*
* @param targetIsLoading The target value of {@code player.isLoading()}.
* @return The builder, for convenience.
*/
public Builder waitForIsLoading(boolean targetIsLoading) {
return apply(new WaitForIsLoading(tag, targetIsLoading));
}

/**
* Schedules a {@link Runnable} to be executed.
*
Expand Down

0 comments on commit eef7e28

Please sign in to comment.