From a71a87af5c5c54498e6160ffa00ca26abe604be6 Mon Sep 17 00:00:00 2001 From: tonihei Date: Wed, 28 Mar 2018 07:18:17 -0700 Subject: [PATCH] Amend WaitForTimelineChange action to allow waiting for arbitrary timeline. In some situations, the timeline can't be specified because it is created internally by the media source under test. If the test still needs to wait for a timeline update, this change allows to do that by specifying an expected timeline of null. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=190768386 --- .../android/exoplayer2/testutil/Action.java | 31 +++++++++++-------- .../exoplayer2/testutil/ActionSchedule.java | 5 +-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/Action.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/Action.java index 60cf6d278b..d7853e435f 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/Action.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/Action.java @@ -544,12 +544,16 @@ public abstract class Action { */ public static final class WaitForTimelineChanged extends Action { - private final Timeline expectedTimeline; + private final @Nullable Timeline expectedTimeline; /** + * Creates action waiting for a timeline change. + * * @param tag A tag to use for logging. + * @param expectedTimeline The expected timeline to wait for. If null, wait for any timeline + * change. */ - public WaitForTimelineChanged(String tag, Timeline expectedTimeline) { + public WaitForTimelineChanged(String tag, @Nullable Timeline expectedTimeline) { super(tag, "WaitForTimelineChanged"); this.expectedTimeline = expectedTimeline; } @@ -564,18 +568,19 @@ public abstract class Action { if (nextAction == null) { return; } - Player.EventListener listener = new Player.DefaultEventListener() { - @Override - public void onTimelineChanged(Timeline timeline, Object manifest, - @Player.TimelineChangeReason int reason) { - if (timeline.equals(expectedTimeline)) { - player.removeListener(this); - nextAction.schedule(player, trackSelector, surface, handler); - } - } - }; + Player.EventListener listener = + new Player.DefaultEventListener() { + @Override + public void onTimelineChanged( + Timeline timeline, Object manifest, @Player.TimelineChangeReason int reason) { + if (expectedTimeline == null || timeline.equals(expectedTimeline)) { + player.removeListener(this); + nextAction.schedule(player, trackSelector, surface, handler); + } + } + }; player.addListener(listener); - if (player.getCurrentTimeline().equals(expectedTimeline)) { + if (expectedTimeline != null && player.getCurrentTimeline().equals(expectedTimeline)) { player.removeListener(listener); nextAction.schedule(player, trackSelector, surface, handler); } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ActionSchedule.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ActionSchedule.java index 2ea8a50a84..841928c932 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ActionSchedule.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ActionSchedule.java @@ -378,10 +378,11 @@ public final class ActionSchedule { /** * Schedules a delay until the timeline changed to a specified expected timeline. * - * @param expectedTimeline The expected timeline to wait for. + * @param expectedTimeline The expected timeline to wait for. If null, wait for any timeline + * change. * @return The builder, for convenience. */ - public Builder waitForTimelineChanged(Timeline expectedTimeline) { + public Builder waitForTimelineChanged(@Nullable Timeline expectedTimeline) { return apply(new WaitForTimelineChanged(tag, expectedTimeline)); }