Merge TimelineAsserts.FakeTimeline into FakeTimeline.

They serve the same purpose. One was defined as single window, multi-period
timeline, while the other was a multi-window, single-period-each timeline.

The combined FakeTimeline uses TimelineWindowDefinitions which allow multi-
window, multi-period fake timelines.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160808844
This commit is contained in:
tonihei 2017-07-03 02:07:39 -07:00 committed by Oliver Woodman
parent 19a3d94022
commit a9efb4553d
7 changed files with 70 additions and 76 deletions

View File

@ -15,8 +15,9 @@
*/ */
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import com.google.android.exoplayer2.testutil.FakeTimeline;
import com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition;
import com.google.android.exoplayer2.testutil.TimelineAsserts; import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.testutil.TimelineAsserts.FakeTimeline;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@ -29,7 +30,7 @@ public class TimelineTest extends TestCase {
} }
public void testSinglePeriodTimeline() { public void testSinglePeriodTimeline() {
Timeline timeline = new FakeTimeline(1, 111); Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(1, 111));
TimelineAsserts.assertWindowIds(timeline, 111); TimelineAsserts.assertWindowIds(timeline, 111);
TimelineAsserts.assertPeriodCounts(timeline, 1); TimelineAsserts.assertPeriodCounts(timeline, 1);
TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET); TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET);
@ -41,7 +42,7 @@ public class TimelineTest extends TestCase {
} }
public void testMultiPeriodTimeline() { public void testMultiPeriodTimeline() {
Timeline timeline = new FakeTimeline(5, 111); Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(5, 111));
TimelineAsserts.assertWindowIds(timeline, 111); TimelineAsserts.assertWindowIds(timeline, 111);
TimelineAsserts.assertPeriodCounts(timeline, 5); TimelineAsserts.assertPeriodCounts(timeline, 5);
TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET); TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET);

View File

@ -22,8 +22,9 @@ import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.Timeline.Period; import com.google.android.exoplayer2.Timeline.Period;
import com.google.android.exoplayer2.Timeline.Window; import com.google.android.exoplayer2.Timeline.Window;
import com.google.android.exoplayer2.testutil.FakeMediaSource; import com.google.android.exoplayer2.testutil.FakeMediaSource;
import com.google.android.exoplayer2.testutil.FakeTimeline;
import com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition;
import com.google.android.exoplayer2.testutil.TimelineAsserts; import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.testutil.TimelineAsserts.FakeTimeline;
/** /**
* Unit tests for {@link ClippingMediaSource}. * Unit tests for {@link ClippingMediaSource}.
@ -101,7 +102,8 @@ public final class ClippingMediaSourceTest extends InstrumentationTestCase {
} }
public void testWindowAndPeriodIndices() { public void testWindowAndPeriodIndices() {
Timeline timeline = new FakeTimeline(1, 111); Timeline timeline = new FakeTimeline(
new TimelineWindowDefinition(1, 111, true, false, TEST_PERIOD_DURATION_US));
Timeline clippedTimeline = getClippedTimeline(timeline, TEST_CLIP_AMOUNT_US, Timeline clippedTimeline = getClippedTimeline(timeline, TEST_CLIP_AMOUNT_US,
TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US); TEST_PERIOD_DURATION_US - TEST_CLIP_AMOUNT_US);
TimelineAsserts.assertWindowIds(clippedTimeline, 111); TimelineAsserts.assertWindowIds(clippedTimeline, 111);

View File

@ -19,8 +19,9 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.testutil.FakeMediaSource; import com.google.android.exoplayer2.testutil.FakeMediaSource;
import com.google.android.exoplayer2.testutil.FakeTimeline;
import com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition;
import com.google.android.exoplayer2.testutil.TimelineAsserts; import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.testutil.TimelineAsserts.FakeTimeline;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@ -29,7 +30,7 @@ import junit.framework.TestCase;
public final class ConcatenatingMediaSourceTest extends TestCase { public final class ConcatenatingMediaSourceTest extends TestCase {
public void testSingleMediaSource() { public void testSingleMediaSource() {
Timeline timeline = getConcatenatedTimeline(false, new FakeTimeline(3, 111)); Timeline timeline = getConcatenatedTimeline(false, createFakeTimeline(3, 111));
TimelineAsserts.assertWindowIds(timeline, 111); TimelineAsserts.assertWindowIds(timeline, 111);
TimelineAsserts.assertPeriodCounts(timeline, 3); TimelineAsserts.assertPeriodCounts(timeline, 3);
TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET); TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET);
@ -39,7 +40,7 @@ public final class ConcatenatingMediaSourceTest extends TestCase {
TimelineAsserts.assertNextWindowIndices(timeline, ExoPlayer.REPEAT_MODE_ONE, 0); TimelineAsserts.assertNextWindowIndices(timeline, ExoPlayer.REPEAT_MODE_ONE, 0);
TimelineAsserts.assertNextWindowIndices(timeline, ExoPlayer.REPEAT_MODE_ALL, 0); TimelineAsserts.assertNextWindowIndices(timeline, ExoPlayer.REPEAT_MODE_ALL, 0);
timeline = getConcatenatedTimeline(true, new FakeTimeline(3, 111)); timeline = getConcatenatedTimeline(true, createFakeTimeline(3, 111));
TimelineAsserts.assertWindowIds(timeline, 111); TimelineAsserts.assertWindowIds(timeline, 111);
TimelineAsserts.assertPeriodCounts(timeline, 3); TimelineAsserts.assertPeriodCounts(timeline, 3);
TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET); TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, C.INDEX_UNSET);
@ -51,8 +52,8 @@ public final class ConcatenatingMediaSourceTest extends TestCase {
} }
public void testMultipleMediaSources() { public void testMultipleMediaSources() {
Timeline[] timelines = { new FakeTimeline(3, 111), new FakeTimeline(1, 222), Timeline[] timelines = { createFakeTimeline(3, 111), createFakeTimeline(1, 222),
new FakeTimeline(3, 333) }; createFakeTimeline(3, 333) };
Timeline timeline = getConcatenatedTimeline(false, timelines); Timeline timeline = getConcatenatedTimeline(false, timelines);
TimelineAsserts.assertWindowIds(timeline, 111, 222, 333); TimelineAsserts.assertWindowIds(timeline, 111, 222, 333);
TimelineAsserts.assertPeriodCounts(timeline, 3, 1, 3); TimelineAsserts.assertPeriodCounts(timeline, 3, 1, 3);
@ -80,8 +81,8 @@ public final class ConcatenatingMediaSourceTest extends TestCase {
public void testNestedMediaSources() { public void testNestedMediaSources() {
Timeline timeline = getConcatenatedTimeline(false, Timeline timeline = getConcatenatedTimeline(false,
getConcatenatedTimeline(false, new FakeTimeline(1, 111), new FakeTimeline(1, 222)), getConcatenatedTimeline(false, createFakeTimeline(1, 111), createFakeTimeline(1, 222)),
getConcatenatedTimeline(true, new FakeTimeline(1, 333), new FakeTimeline(1, 444))); getConcatenatedTimeline(true, createFakeTimeline(1, 333), createFakeTimeline(1, 444)));
TimelineAsserts.assertWindowIds(timeline, 111, 222, 333, 444); TimelineAsserts.assertWindowIds(timeline, 111, 222, 333, 444);
TimelineAsserts.assertPeriodCounts(timeline, 1, 1, 1, 1); TimelineAsserts.assertPeriodCounts(timeline, 1, 1, 1, 1);
TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF, TimelineAsserts.assertPreviousWindowIndices(timeline, ExoPlayer.REPEAT_MODE_OFF,
@ -108,5 +109,8 @@ public final class ConcatenatingMediaSourceTest extends TestCase {
new ConcatenatingMediaSource(isRepeatOneAtomic, mediaSources)); new ConcatenatingMediaSource(isRepeatOneAtomic, mediaSources));
} }
private static FakeTimeline createFakeTimeline(int periodCount, int windowId) {
return new FakeTimeline(new TimelineWindowDefinition(periodCount, windowId));
}
} }

View File

@ -26,8 +26,9 @@ import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.MediaSource.Listener; import com.google.android.exoplayer2.source.MediaSource.Listener;
import com.google.android.exoplayer2.testutil.FakeMediaSource; import com.google.android.exoplayer2.testutil.FakeMediaSource;
import com.google.android.exoplayer2.testutil.FakeTimeline;
import com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition;
import com.google.android.exoplayer2.testutil.TimelineAsserts; import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.testutil.TimelineAsserts.FakeTimeline;
import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.trackselection.TrackSelectionArray;
import com.google.android.exoplayer2.upstream.Allocator; import com.google.android.exoplayer2.upstream.Allocator;
import java.io.IOException; import java.io.IOException;
@ -175,7 +176,7 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase {
TimelineAsserts.assertWindowIds(timeline, 111, null); TimelineAsserts.assertWindowIds(timeline, 111, null);
TimelineAsserts.assertWindowIsDynamic(timeline, false, true); TimelineAsserts.assertWindowIsDynamic(timeline, false, true);
lazySources[1].triggerTimelineUpdate(new FakeTimeline(9, 999)); lazySources[1].triggerTimelineUpdate(createFakeTimeline(8));
waitForTimelineUpdate(); waitForTimelineUpdate();
TimelineAsserts.assertPeriodCounts(timeline, 1, 9); TimelineAsserts.assertPeriodCounts(timeline, 1, 9);
TimelineAsserts.assertWindowIds(timeline, 111, 999); TimelineAsserts.assertWindowIds(timeline, 111, 999);
@ -194,7 +195,7 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase {
TimelineAsserts.assertWindowIds(timeline, null, 111, 222, 999); TimelineAsserts.assertWindowIds(timeline, null, 111, 222, 999);
TimelineAsserts.assertWindowIsDynamic(timeline, true, false, false, false); TimelineAsserts.assertWindowIsDynamic(timeline, true, false, false, false);
lazySources[3].triggerTimelineUpdate(new FakeTimeline(8, 888)); lazySources[3].triggerTimelineUpdate(createFakeTimeline(7));
waitForTimelineUpdate(); waitForTimelineUpdate();
TimelineAsserts.assertPeriodCounts(timeline, 8, 1, 2, 9); TimelineAsserts.assertPeriodCounts(timeline, 8, 1, 2, 9);
TimelineAsserts.assertWindowIds(timeline, 888, 111, 222, 999); TimelineAsserts.assertWindowIds(timeline, 888, 111, 222, 999);
@ -207,7 +208,7 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase {
public void testIllegalArguments() { public void testIllegalArguments() {
DynamicConcatenatingMediaSource mediaSource = new DynamicConcatenatingMediaSource(); DynamicConcatenatingMediaSource mediaSource = new DynamicConcatenatingMediaSource();
MediaSource validSource = new FakeMediaSource(new FakeTimeline(1, 1), null); MediaSource validSource = new FakeMediaSource(createFakeTimeline(1), null);
// Null sources. // Null sources.
try { try {
@ -235,9 +236,7 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase {
} }
mediaSources = new MediaSource[] { mediaSources = new MediaSource[] {
new FakeMediaSource(new FakeTimeline(1, 1), null), new FakeMediaSource(createFakeTimeline(2), null), validSource };
validSource
};
try { try {
mediaSource.addMediaSources(Arrays.asList(mediaSources)); mediaSource.addMediaSources(Arrays.asList(mediaSources));
fail("Duplicate mediaSource not allowed."); fail("Duplicate mediaSource not allowed.");
@ -270,14 +269,18 @@ public final class DynamicConcatenatingMediaSourceTest extends TestCase {
timelineUpdated = false; timelineUpdated = false;
} }
private FakeMediaSource[] createMediaSources(int count) { private static FakeMediaSource[] createMediaSources(int count) {
FakeMediaSource[] sources = new FakeMediaSource[count]; FakeMediaSource[] sources = new FakeMediaSource[count];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
sources[i] = new FakeMediaSource(new FakeTimeline(i + 1, (i + 1) * 111), null); sources[i] = new FakeMediaSource(createFakeTimeline(i), null);
} }
return sources; return sources;
} }
private static FakeTimeline createFakeTimeline(int index) {
return new FakeTimeline(new TimelineWindowDefinition(index + 1, (index + 1) * 111));
}
private static class LazyMediaSource implements MediaSource { private static class LazyMediaSource implements MediaSource {
private Listener listener; private Listener listener;

View File

@ -19,8 +19,9 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.testutil.FakeMediaSource; import com.google.android.exoplayer2.testutil.FakeMediaSource;
import com.google.android.exoplayer2.testutil.FakeTimeline;
import com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition;
import com.google.android.exoplayer2.testutil.TimelineAsserts; import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.testutil.TimelineAsserts.FakeTimeline;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
@ -31,11 +32,9 @@ public class LoopingMediaSourceTest extends TestCase {
private final Timeline multiWindowTimeline; private final Timeline multiWindowTimeline;
public LoopingMediaSourceTest() { public LoopingMediaSourceTest() {
multiWindowTimeline = TimelineAsserts.extractTimelineFromMediaSource( multiWindowTimeline = TimelineAsserts.extractTimelineFromMediaSource(new FakeMediaSource(
new ConcatenatingMediaSource( new FakeTimeline(new TimelineWindowDefinition(1, 111),
new FakeMediaSource(new FakeTimeline(1, 111), null), new TimelineWindowDefinition(1, 222), new TimelineWindowDefinition(1, 333)), null));
new FakeMediaSource(new FakeTimeline(1, 222), null),
new FakeMediaSource(new FakeTimeline(1, 333), null)));
} }
public void testSingleLoop() { public void testSingleLoop() {

View File

@ -17,6 +17,7 @@ package com.google.android.exoplayer2.testutil;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.Util;
/** /**
* Fake {@link Timeline} which can be setup to return custom {@link TimelineWindowDefinition}s. * Fake {@link Timeline} which can be setup to return custom {@link TimelineWindowDefinition}s.
@ -28,11 +29,26 @@ public final class FakeTimeline extends Timeline {
*/ */
public static final class TimelineWindowDefinition { public static final class TimelineWindowDefinition {
private static final int WINDOW_DURATION_US = 100000;
public final int periodCount;
public final Object id;
public final boolean isSeekable; public final boolean isSeekable;
public final boolean isDynamic; public final boolean isDynamic;
public final long durationUs; public final long durationUs;
public TimelineWindowDefinition(int periodCount, Object id) {
this(periodCount, id, true, false, WINDOW_DURATION_US);
}
public TimelineWindowDefinition(boolean isSeekable, boolean isDynamic, long durationUs) { public TimelineWindowDefinition(boolean isSeekable, boolean isDynamic, long durationUs) {
this(1, 0, isSeekable, isDynamic, durationUs);
}
public TimelineWindowDefinition(int periodCount, Object id, boolean isSeekable,
boolean isDynamic, long durationUs) {
this.periodCount = periodCount;
this.id = id;
this.isSeekable = isSeekable; this.isSeekable = isSeekable;
this.isDynamic = isDynamic; this.isDynamic = isDynamic;
this.durationUs = durationUs; this.durationUs = durationUs;
@ -41,9 +57,15 @@ public final class FakeTimeline extends Timeline {
} }
private final TimelineWindowDefinition[] windowDefinitions; private final TimelineWindowDefinition[] windowDefinitions;
private final int[] periodOffsets;
public FakeTimeline(TimelineWindowDefinition... windowDefinitions) { public FakeTimeline(TimelineWindowDefinition... windowDefinitions) {
this.windowDefinitions = windowDefinitions; this.windowDefinitions = windowDefinitions;
periodOffsets = new int[windowDefinitions.length + 1];
periodOffsets[0] = 0;
for (int i = 0; i < windowDefinitions.length; i++) {
periodOffsets[i + 1] = periodOffsets[i] + windowDefinitions[i].periodCount;
}
} }
@Override @Override
@ -55,21 +77,26 @@ public final class FakeTimeline extends Timeline {
public Window getWindow(int windowIndex, Window window, boolean setIds, public Window getWindow(int windowIndex, Window window, boolean setIds,
long defaultPositionProjectionUs) { long defaultPositionProjectionUs) {
TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex]; TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex];
Object id = setIds ? windowIndex : null; Object id = setIds ? windowDefinition.id : null;
return window.set(id, C.TIME_UNSET, C.TIME_UNSET, windowDefinition.isSeekable, return window.set(id, C.TIME_UNSET, C.TIME_UNSET, windowDefinition.isSeekable,
windowDefinition.isDynamic, 0, windowDefinition.durationUs, windowIndex, windowIndex, 0); windowDefinition.isDynamic, 0, windowDefinition.durationUs, periodOffsets[windowIndex],
periodOffsets[windowIndex + 1] - 1, 0);
} }
@Override @Override
public int getPeriodCount() { public int getPeriodCount() {
return windowDefinitions.length; return periodOffsets[periodOffsets.length - 1];
} }
@Override @Override
public Period getPeriod(int periodIndex, Period period, boolean setIds) { public Period getPeriod(int periodIndex, Period period, boolean setIds) {
TimelineWindowDefinition windowDefinition = windowDefinitions[periodIndex]; int windowIndex = Util.binarySearchFloor(periodOffsets, periodIndex, true, false);
Object id = setIds ? periodIndex : null; int windowPeriodIndex = periodIndex - periodOffsets[windowIndex];
return period.set(id, id, periodIndex, windowDefinition.durationUs, 0); TimelineWindowDefinition windowDefinition = windowDefinitions[windowIndex];
Object id = setIds ? windowPeriodIndex : null;
Object uid = setIds ? periodIndex : null;
long periodDurationUs = windowDefinition.durationUs / windowDefinition.periodCount;
return period.set(id, uid, windowIndex, periodDurationUs, periodDurationUs * windowPeriodIndex);
} }
@Override @Override
@ -78,7 +105,7 @@ public final class FakeTimeline extends Timeline {
return C.INDEX_UNSET; return C.INDEX_UNSET;
} }
int index = (Integer) uid; int index = (Integer) uid;
return index >= 0 && index < windowDefinitions.length ? index : C.INDEX_UNSET; return index >= 0 && index < getPeriodCount() ? index : C.INDEX_UNSET;
} }
} }

View File

@ -32,48 +32,6 @@ public final class TimelineAsserts {
private TimelineAsserts() {} private TimelineAsserts() {}
/**
* Fake timeline with multiple periods and user-defined window id.
*/
public static final class FakeTimeline extends Timeline {
private static final int WINDOW_DURATION_US = 1000000;
private final int periodCount;
private final int id;
public FakeTimeline(int periodCount, int id) {
this.periodCount = periodCount;
this.id = id;
}
@Override
public int getWindowCount() {
return 1;
}
@Override
public Window getWindow(int windowIndex, Window window, boolean setIds,
long defaultPositionProjectionUs) {
return window.set(id, 0, 0, true, false, 0, WINDOW_DURATION_US, 0, periodCount - 1, 0);
}
@Override
public int getPeriodCount() {
return periodCount;
}
@Override
public Period getPeriod(int periodIndex, Period period, boolean setIds) {
return period.set(periodIndex, null, 0, WINDOW_DURATION_US, 0);
}
@Override
public int getIndexOfPeriod(Object uid) {
return C.INDEX_UNSET;
}
}
/** /**
* Extracts the timeline from a media source. * Extracts the timeline from a media source.
*/ */