Skip to content

Commit

Permalink
Use a helper function and Truth Correspondence instead of NoUidTimeline
Browse files Browse the repository at this point in the history
NoUidTimeline still exists as a private detail of TestUtil, but it no
longer extends ForwardingTimeline because the interactions are quite
hard to reason about.

#minor-release

PiperOrigin-RevId: 457703593
  • Loading branch information
icbaker authored and rohitjoins committed Jul 7, 2022
1 parent bfa663d commit 292f6de
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 65 deletions.
Expand Up @@ -61,6 +61,7 @@
import static com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_DURATION_US;
import static com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
import static com.google.android.exoplayer2.testutil.TestUtil.assertTimelinesSame;
import static com.google.android.exoplayer2.testutil.TestUtil.timelinesAreSame;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -140,7 +141,6 @@
import com.google.android.exoplayer2.testutil.FakeTrackSelection;
import com.google.android.exoplayer2.testutil.FakeTrackSelector;
import com.google.android.exoplayer2.testutil.FakeVideoRenderer;
import com.google.android.exoplayer2.testutil.NoUidTimeline;
import com.google.android.exoplayer2.testutil.TestExoPlayerBuilder;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.upstream.Allocation;
Expand Down Expand Up @@ -12247,6 +12247,6 @@ public Loader.LoadErrorAction onLoadError(
* Returns an argument matcher for {@link Timeline} instances that ignores period and window uids.
*/
private static ArgumentMatcher<Timeline> noUid(Timeline timeline) {
return argument -> new NoUidTimeline(timeline).equals(new NoUidTimeline(argument));
return argument -> timelinesAreSame(argument, timeline);
}
}
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.android.exoplayer2.testutil;

import static com.google.android.exoplayer2.testutil.TestUtil.timelinesAreSame;

import android.os.Looper;
import android.view.Surface;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -763,7 +765,7 @@ public WaitForTimelineChanged(
@Nullable Timeline expectedTimeline,
@Player.TimelineChangeReason int expectedReason) {
super(tag, "WaitForTimelineChanged");
this.expectedTimeline = expectedTimeline != null ? new NoUidTimeline(expectedTimeline) : null;
this.expectedTimeline = expectedTimeline;
this.ignoreExpectedReason = false;
this.expectedReason = expectedReason;
}
Expand Down Expand Up @@ -795,16 +797,16 @@ protected void doActionAndScheduleNextImpl(
@Override
public void onTimelineChanged(
Timeline timeline, @Player.TimelineChangeReason int reason) {
if ((expectedTimeline == null || new NoUidTimeline(timeline).equals(expectedTimeline))
if ((expectedTimeline == null || timelinesAreSame(timeline, expectedTimeline))
&& (ignoreExpectedReason || expectedReason == reason)) {
player.removeListener(this);
nextAction.schedule(player, trackSelector, surface, handler);
}
}
};
player.addListener(listener);
Timeline currentTimeline = new NoUidTimeline(player.getCurrentTimeline());
if (currentTimeline.equals(expectedTimeline)) {
if (expectedTimeline != null
&& timelinesAreSame(player.getCurrentTimeline(), expectedTimeline)) {
player.removeListener(listener);
nextAction.schedule(player, trackSelector, surface, handler);
}
Expand Down
Expand Up @@ -42,6 +42,7 @@
import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.util.HandlerWrapper;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -534,11 +535,8 @@ public ExoPlayerTestRunner blockUntilActionScheduleFinished(long timeoutMs)
* @param timelines A list of expected {@link Timeline}s.
*/
public void assertTimelinesSame(Timeline... timelines) {
assertThat(this.timelines).hasSize(timelines.length);
for (int i = 0; i < timelines.length; i++) {
assertThat(new NoUidTimeline(timelines[i]))
.isEqualTo(new NoUidTimeline(this.timelines.get(i)));
}
TestUtil.assertTimelinesSame(
ImmutableList.copyOf(this.timelines), ImmutableList.copyOf(timelines));
}

/**
Expand Down

This file was deleted.

Expand Up @@ -43,6 +43,7 @@
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import com.google.common.primitives.Bytes;
import com.google.common.truth.Correspondence;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -205,11 +206,20 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
*/
public static void assertTimelinesSame(
List<Timeline> actualTimelines, List<Timeline> expectedTimelines) {
assertThat(actualTimelines).hasSize(expectedTimelines.size());
for (int i = 0; i < actualTimelines.size(); i++) {
assertThat(new NoUidTimeline(actualTimelines.get(i)))
.isEqualTo(new NoUidTimeline(expectedTimelines.get(i)));
}
assertThat(actualTimelines)
.comparingElementsUsing(
Correspondence.from(
TestUtil::timelinesAreSame, "is equal to (ignoring Window.uid and Period.uid)"))
.containsExactlyElementsIn(expectedTimelines)
.inOrder();
}

/**
* Returns true if {@code thisTimeline} is equal to {@code thatTimeline}, ignoring {@link
* Timeline.Window#uid} and {@link Timeline.Period#uid} values.
*/
public static boolean timelinesAreSame(Timeline thisTimeline, Timeline thatTimeline) {
return new NoUidTimeline(thisTimeline).equals(new NoUidTimeline(thatTimeline));
}

/**
Expand Down Expand Up @@ -492,4 +502,47 @@ public static List<Method> getPublicMethods(Class<?> clazz) {

return list;
}

private static final class NoUidTimeline extends Timeline {

private final Timeline delegate;

public NoUidTimeline(Timeline timeline) {
this.delegate = timeline;
}

@Override
public int getWindowCount() {
return delegate.getWindowCount();
}

@Override
public Window getWindow(int windowIndex, Window window, long defaultPositionProjectionUs) {
delegate.getWindow(windowIndex, window, defaultPositionProjectionUs);
window.uid = 0;
return window;
}

@Override
public int getPeriodCount() {
return delegate.getPeriodCount();
}

@Override
public Period getPeriod(int periodIndex, Period period, boolean setIds) {
delegate.getPeriod(periodIndex, period, setIds);
period.uid = 0;
return period;
}

@Override
public int getIndexOfPeriod(Object uid) {
return delegate.getIndexOfPeriod(uid);
}

@Override
public Object getUidOfPeriod(int periodIndex) {
return 0;
}
}
}

0 comments on commit 292f6de

Please sign in to comment.