Skip to content

Commit ad57b41

Browse files
icbakerSheenaChhabra
authored andcommitted
Clearly define the consistency requirements for SequenceableLoader
Add a test for this consistency in `CompositeSequenceableLoaderTest`, and also make the `CompositeSequenceableLoaderTest.FakeSequenceableLoader` implementation more realistic. #minor-release PiperOrigin-RevId: 604604103 (cherry picked from commit db74bb9)
1 parent 4a95217 commit ad57b41

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

‎libraries/exoplayer/src/test/java/androidx/media3/exoplayer/source/CompositeSequenceableLoaderTest.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package androidx.media3.exoplayer.source;
1717

18+
import static androidx.media3.common.util.Assertions.checkArgument;
19+
import static androidx.media3.common.util.Assertions.checkState;
1820
import static com.google.common.truth.Truth.assertThat;
1921

2022
import androidx.media3.common.C;
@@ -29,7 +31,8 @@ public final class CompositeSequenceableLoaderTest {
2931

3032
/**
3133
* Tests that {@link CompositeSequenceableLoader#getBufferedPositionUs()} returns minimum buffered
32-
* position among all sub-loaders.
34+
* position among all sub-loaders, and is consistent with {@link
35+
* CompositeSequenceableLoader#isLoading()}.
3336
*/
3437
@Test
3538
public void getBufferedPositionUsReturnsMinimumLoaderBufferedPosition() {
@@ -40,11 +43,13 @@ public void getBufferedPositionUsReturnsMinimumLoaderBufferedPosition() {
4043
CompositeSequenceableLoader compositeSequenceableLoader =
4144
new CompositeSequenceableLoader(new SequenceableLoader[] {loader1, loader2});
4245
assertThat(compositeSequenceableLoader.getBufferedPositionUs()).isEqualTo(1000);
46+
assertThat(compositeSequenceableLoader.isLoading()).isTrue();
4347
}
4448

4549
/**
4650
* Tests that {@link CompositeSequenceableLoader#getBufferedPositionUs()} returns minimum buffered
47-
* position that is not {@link C#TIME_END_OF_SOURCE} among all sub-loaders.
51+
* position that is not {@link C#TIME_END_OF_SOURCE} among all sub-loaders, and is consistent with
52+
* {@link CompositeSequenceableLoader#isLoading()}.
4853
*/
4954
@Test
5055
public void getBufferedPositionUsReturnsMinimumNonEndOfSourceLoaderBufferedPosition() {
@@ -59,11 +64,13 @@ public void getBufferedPositionUsReturnsMinimumNonEndOfSourceLoaderBufferedPosit
5964
CompositeSequenceableLoader compositeSequenceableLoader =
6065
new CompositeSequenceableLoader(new SequenceableLoader[] {loader1, loader2, loader3});
6166
assertThat(compositeSequenceableLoader.getBufferedPositionUs()).isEqualTo(1000);
67+
assertThat(compositeSequenceableLoader.isLoading()).isTrue();
6268
}
6369

6470
/**
6571
* Tests that {@link CompositeSequenceableLoader#getBufferedPositionUs()} returns {@link
66-
* C#TIME_END_OF_SOURCE} when all sub-loaders have buffered till end-of-source.
72+
* C#TIME_END_OF_SOURCE} when all sub-loaders have buffered till end-of-source, and is consistent
73+
* with {@link CompositeSequenceableLoader#isLoading()}.
6774
*/
6875
@Test
6976
public void getBufferedPositionUsReturnsEndOfSourceWhenAllLoaderBufferedTillEndOfSource() {
@@ -78,11 +85,13 @@ public void getBufferedPositionUsReturnsEndOfSourceWhenAllLoaderBufferedTillEndO
7885
CompositeSequenceableLoader compositeSequenceableLoader =
7986
new CompositeSequenceableLoader(new SequenceableLoader[] {loader1, loader2});
8087
assertThat(compositeSequenceableLoader.getBufferedPositionUs()).isEqualTo(C.TIME_END_OF_SOURCE);
88+
assertThat(compositeSequenceableLoader.isLoading()).isFalse();
8189
}
8290

8391
/**
8492
* Tests that {@link CompositeSequenceableLoader#getNextLoadPositionUs()} returns minimum next
85-
* load position among all sub-loaders.
93+
* load position among all sub-loaders, and is consistent with {@link
94+
* CompositeSequenceableLoader#isLoading()}.
8695
*/
8796
@Test
8897
public void getNextLoadPositionUsReturnMinimumLoaderNextLoadPositionUs() {
@@ -93,11 +102,13 @@ public void getNextLoadPositionUsReturnMinimumLoaderNextLoadPositionUs() {
93102
CompositeSequenceableLoader compositeSequenceableLoader =
94103
new CompositeSequenceableLoader(new SequenceableLoader[] {loader1, loader2});
95104
assertThat(compositeSequenceableLoader.getNextLoadPositionUs()).isEqualTo(2000);
105+
assertThat(compositeSequenceableLoader.isLoading()).isTrue();
96106
}
97107

98108
/**
99109
* Tests that {@link CompositeSequenceableLoader#getNextLoadPositionUs()} returns minimum next
100-
* load position that is not {@link C#TIME_END_OF_SOURCE} among all sub-loaders.
110+
* load position that is not {@link C#TIME_END_OF_SOURCE} among all sub-loaders, and is consistent
111+
* with {@link CompositeSequenceableLoader#isLoading()}.
101112
*/
102113
@Test
103114
public void getNextLoadPositionUsReturnMinimumNonEndOfSourceLoaderNextLoadPositionUs() {
@@ -111,11 +122,13 @@ public void getNextLoadPositionUsReturnMinimumNonEndOfSourceLoaderNextLoadPositi
111122
CompositeSequenceableLoader compositeSequenceableLoader =
112123
new CompositeSequenceableLoader(new SequenceableLoader[] {loader1, loader2, loader3});
113124
assertThat(compositeSequenceableLoader.getNextLoadPositionUs()).isEqualTo(2000);
125+
assertThat(compositeSequenceableLoader.isLoading()).isTrue();
114126
}
115127

116128
/**
117129
* Tests that {@link CompositeSequenceableLoader#getNextLoadPositionUs()} returns {@link
118-
* C#TIME_END_OF_SOURCE} when all sub-loaders have next load position at end-of-source.
130+
* C#TIME_END_OF_SOURCE} when all sub-loaders have next load position at end-of-source, and is
131+
* consistent with {@link CompositeSequenceableLoader#isLoading()}.
119132
*/
120133
@Test
121134
public void getNextLoadPositionUsReturnsEndOfSourceWhenAllLoaderLoadingLastChunk() {
@@ -128,6 +141,7 @@ public void getNextLoadPositionUsReturnsEndOfSourceWhenAllLoaderLoadingLastChunk
128141
CompositeSequenceableLoader compositeSequenceableLoader =
129142
new CompositeSequenceableLoader(new SequenceableLoader[] {loader1, loader2});
130143
assertThat(compositeSequenceableLoader.getNextLoadPositionUs()).isEqualTo(C.TIME_END_OF_SOURCE);
144+
assertThat(compositeSequenceableLoader.isLoading()).isFalse();
131145
}
132146

133147
/**
@@ -246,6 +260,9 @@ private static class FakeSequenceableLoader implements SequenceableLoader {
246260
private int nextChunkDurationUs;
247261

248262
private FakeSequenceableLoader(long bufferedPositionUs, long nextLoadPositionUs) {
263+
if (bufferedPositionUs == C.TIME_END_OF_SOURCE) {
264+
checkArgument(nextLoadPositionUs == C.TIME_END_OF_SOURCE);
265+
}
249266
this.bufferedPositionUs = bufferedPositionUs;
250267
this.nextLoadPositionUs = nextLoadPositionUs;
251268
}
@@ -263,17 +280,22 @@ public long getNextLoadPositionUs() {
263280
@Override
264281
public boolean continueLoading(LoadingInfo loadingInfo) {
265282
numInvocations++;
266-
boolean loaded = nextChunkDurationUs != 0;
267-
// The current chunk has been loaded, advance to next chunk.
283+
268284
bufferedPositionUs = nextLoadPositionUs;
285+
if (nextLoadPositionUs == C.TIME_END_OF_SOURCE) {
286+
return false;
287+
}
288+
289+
long oldNextLoadPositionUs = nextLoadPositionUs;
290+
// The current chunk has been loaded, advance to next chunk.
269291
nextLoadPositionUs += nextChunkDurationUs;
270292
nextChunkDurationUs = 0;
271-
return loaded;
293+
return oldNextLoadPositionUs != nextLoadPositionUs;
272294
}
273295

274296
@Override
275297
public boolean isLoading() {
276-
return nextChunkDurationUs != 0;
298+
return nextLoadPositionUs != C.TIME_END_OF_SOURCE;
277299
}
278300

279301
@Override
@@ -282,6 +304,7 @@ public void reevaluateBuffer(long positionUs) {
282304
}
283305

284306
private void setNextChunkDurationUs(int nextChunkDurationUs) {
307+
checkState(nextLoadPositionUs != C.TIME_END_OF_SOURCE);
285308
this.nextChunkDurationUs = nextChunkDurationUs;
286309
}
287310
}

0 commit comments

Comments
 (0)