Commit 811cdf06 by eguven Committed by Oliver Woodman

Modify DashDownloaderTest to test if content length is stored

PiperOrigin-RevId: 250655481
parent bbf8a9ac
...@@ -35,6 +35,7 @@ import com.google.android.exoplayer2.offline.Downloader; ...@@ -35,6 +35,7 @@ import com.google.android.exoplayer2.offline.Downloader;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.DownloaderFactory; import com.google.android.exoplayer2.offline.DownloaderFactory;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.testutil.CacheAsserts.RequestSet;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
import com.google.android.exoplayer2.testutil.FakeDataSource; import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.FakeDataSource.Factory; import com.google.android.exoplayer2.testutil.FakeDataSource.Factory;
...@@ -108,7 +109,7 @@ public class DashDownloaderTest { ...@@ -108,7 +109,7 @@ public class DashDownloaderTest {
DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0)); DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0));
dashDownloader.download(progressListener); dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
...@@ -127,7 +128,7 @@ public class DashDownloaderTest { ...@@ -127,7 +128,7 @@ public class DashDownloaderTest {
DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0)); DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0));
dashDownloader.download(progressListener); dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
...@@ -146,7 +147,7 @@ public class DashDownloaderTest { ...@@ -146,7 +147,7 @@ public class DashDownloaderTest {
DashDownloader dashDownloader = DashDownloader dashDownloader =
getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0), new StreamKey(0, 1, 0)); getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0), new StreamKey(0, 1, 0));
dashDownloader.download(progressListener); dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
...@@ -167,7 +168,7 @@ public class DashDownloaderTest { ...@@ -167,7 +168,7 @@ public class DashDownloaderTest {
DashDownloader dashDownloader = getDashDownloader(fakeDataSet); DashDownloader dashDownloader = getDashDownloader(fakeDataSet);
dashDownloader.download(progressListener); dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
...@@ -256,7 +257,7 @@ public class DashDownloaderTest { ...@@ -256,7 +257,7 @@ public class DashDownloaderTest {
// Expected. // Expected.
} }
dashDownloader.download(progressListener); dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
......
...@@ -33,6 +33,7 @@ import com.google.android.exoplayer2.offline.DownloadRequest; ...@@ -33,6 +33,7 @@ import com.google.android.exoplayer2.offline.DownloadRequest;
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.testutil.CacheAsserts.RequestSet;
import com.google.android.exoplayer2.testutil.DummyMainThread; import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable; import com.google.android.exoplayer2.testutil.DummyMainThread.TestRunnable;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
...@@ -154,7 +155,7 @@ public class DownloadManagerDashTest { ...@@ -154,7 +155,7 @@ public class DownloadManagerDashTest {
public void testHandleDownloadRequest() throws Throwable { public void testHandleDownloadRequest() throws Throwable {
handleDownloadRequest(fakeStreamKey1, fakeStreamKey2); handleDownloadRequest(fakeStreamKey1, fakeStreamKey2);
blockUntilTasksCompleteAndThrowAnyDownloadError(); blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
...@@ -162,7 +163,7 @@ public class DownloadManagerDashTest { ...@@ -162,7 +163,7 @@ public class DownloadManagerDashTest {
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
handleDownloadRequest(fakeStreamKey2); handleDownloadRequest(fakeStreamKey2);
blockUntilTasksCompleteAndThrowAnyDownloadError(); blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
...@@ -176,7 +177,7 @@ public class DownloadManagerDashTest { ...@@ -176,7 +177,7 @@ public class DownloadManagerDashTest {
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
blockUntilTasksCompleteAndThrowAnyDownloadError(); blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
......
...@@ -44,6 +44,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; ...@@ -44,6 +44,7 @@ import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.DownloaderFactory; import com.google.android.exoplayer2.offline.DownloaderFactory;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist; import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist;
import com.google.android.exoplayer2.testutil.CacheAsserts.RequestSet;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
import com.google.android.exoplayer2.testutil.FakeDataSource.Factory; import com.google.android.exoplayer2.testutil.FakeDataSource.Factory;
import com.google.android.exoplayer2.upstream.DummyDataSource; import com.google.android.exoplayer2.upstream.DummyDataSource;
...@@ -129,12 +130,13 @@ public class HlsDownloaderTest { ...@@ -129,12 +130,13 @@ public class HlsDownloaderTest {
assertCachedData( assertCachedData(
cache, cache,
fakeDataSet, new RequestSet(fakeDataSet)
MASTER_PLAYLIST_URI, .subset(
MEDIA_PLAYLIST_1_URI, MASTER_PLAYLIST_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts", MEDIA_PLAYLIST_1_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts", MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts"); MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts"));
} }
@Test @Test
...@@ -186,11 +188,12 @@ public class HlsDownloaderTest { ...@@ -186,11 +188,12 @@ public class HlsDownloaderTest {
assertCachedData( assertCachedData(
cache, cache,
fakeDataSet, new RequestSet(fakeDataSet)
MEDIA_PLAYLIST_1_URI, .subset(
MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts", MEDIA_PLAYLIST_1_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts", MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts"); MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts"));
} }
@Test @Test
......
...@@ -33,59 +33,89 @@ import java.util.ArrayList; ...@@ -33,59 +33,89 @@ import java.util.ArrayList;
/** Assertion methods for {@link Cache}. */ /** Assertion methods for {@link Cache}. */
public final class CacheAsserts { public final class CacheAsserts {
/** /** Defines a set of data requests. */
* Asserts that the cache content is equal to the data in the {@code fakeDataSet}. public static final class RequestSet {
*
* @throws IOException If an error occurred reading from the Cache. private final FakeDataSet fakeDataSet;
*/ private DataSpec[] dataSpecs;
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet) throws IOException {
ArrayList<FakeData> allData = fakeDataSet.getAllData(); public RequestSet(FakeDataSet fakeDataSet) {
Uri[] uris = new Uri[allData.size()]; this.fakeDataSet = fakeDataSet;
for (int i = 0; i < allData.size(); i++) { ArrayList<FakeData> allData = fakeDataSet.getAllData();
uris[i] = allData.get(i).uri; dataSpecs = new DataSpec[allData.size()];
for (int i = 0; i < dataSpecs.length; i++) {
dataSpecs[i] = new DataSpec(allData.get(i).uri);
}
} }
assertCachedData(cache, fakeDataSet, uris);
}
/** public RequestSet subset(String... uriStrings) {
* Asserts that the cache content is equal to the given subset of data in the {@code fakeDataSet}. dataSpecs = new DataSpec[uriStrings.length];
* for (int i = 0; i < dataSpecs.length; i++) {
* @throws IOException If an error occurred reading from the Cache. dataSpecs[i] = new DataSpec(Uri.parse(uriStrings[i]));
*/ }
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet, String... uriStrings) return this;
throws IOException { }
Uri[] uris = new Uri[uriStrings.length];
for (int i = 0; i < uriStrings.length; i++) { public RequestSet subset(Uri... uris) {
uris[i] = Uri.parse(uriStrings[i]); dataSpecs = new DataSpec[uris.length];
for (int i = 0; i < dataSpecs.length; i++) {
dataSpecs[i] = new DataSpec(uris[i]);
}
return this;
}
public RequestSet subset(DataSpec... dataSpecs) {
this.dataSpecs = dataSpecs;
return this;
}
public int getCount() {
return dataSpecs.length;
}
public byte[] getData(int i) {
return fakeDataSet.getData(dataSpecs[i].uri).getData();
}
public DataSpec getDataSpec(int i) {
return dataSpecs[i];
}
public RequestSet useBoundedDataSpecFor(String uriString) {
FakeData data = fakeDataSet.getData(uriString);
for (int i = 0; i < dataSpecs.length; i++) {
DataSpec spec = dataSpecs[i];
if (spec.uri.getPath().equals(uriString)) {
dataSpecs[i] = spec.subrange(0, data.getData().length);
return this;
}
}
throw new IllegalStateException();
} }
assertCachedData(cache, fakeDataSet, uris);
} }
/** /**
* Asserts that the cache content is equal to the given subset of data in the {@code fakeDataSet}. * Asserts that the cache contains necessary data for the {@code requestSet}.
* *
* @throws IOException If an error occurred reading from the Cache. * @throws IOException If an error occurred reading from the Cache.
*/ */
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet, Uri... uris) public static void assertCachedData(Cache cache, RequestSet requestSet) throws IOException {
throws IOException {
int totalLength = 0; int totalLength = 0;
for (Uri uri : uris) { for (int i = 0; i < requestSet.getCount(); i++) {
byte[] data = fakeDataSet.getData(uri).getData(); byte[] data = requestSet.getData(i);
assertDataCached(cache, uri, data); assertDataCached(cache, requestSet.getDataSpec(i), data);
totalLength += data.length; totalLength += data.length;
} }
assertThat(cache.getCacheSpace()).isEqualTo(totalLength); assertThat(cache.getCacheSpace()).isEqualTo(totalLength);
} }
/** /**
* Asserts that the cache contains the given data for {@code uriString}. * Asserts that the cache content is equal to the data in the {@code fakeDataSet}.
* *
* @throws IOException If an error occurred reading from the Cache. * @throws IOException If an error occurred reading from the Cache.
*/ */
public static void assertDataCached(Cache cache, Uri uri, byte[] expected) throws IOException { public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet) throws IOException {
// TODO Make tests specify if the content length is stored in cache metadata. assertCachedData(cache, new RequestSet(fakeDataSet));
DataSpec dataSpec = new DataSpec(uri, 0, expected.length, null, 0);
assertDataCached(cache, dataSpec, expected);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment