Skip to content

Commit

Permalink
Modify DashDownloaderTest to test if content length is stored
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 250655481
  • Loading branch information
erdemguven authored and ojw28 committed May 31, 2019
1 parent bbf8a9a commit 811cdf0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.DownloaderFactory;
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.FakeDataSource;
import com.google.android.exoplayer2.testutil.FakeDataSource.Factory;
Expand Down Expand Up @@ -108,7 +109,7 @@ public void testDownloadRepresentation() throws Exception {

DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0));
dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand All @@ -127,7 +128,7 @@ public void testDownloadRepresentationInSmallParts() throws Exception {

DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0));
dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand All @@ -146,7 +147,7 @@ public void testDownloadRepresentations() throws Exception {
DashDownloader dashDownloader =
getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0), new StreamKey(0, 1, 0));
dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand All @@ -167,7 +168,7 @@ public void testDownloadAllRepresentations() throws Exception {

DashDownloader dashDownloader = getDashDownloader(fakeDataSet);
dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand Down Expand Up @@ -256,7 +257,7 @@ public void testDownloadRepresentationFailure() throws Exception {
// Expected.
}
dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper;
import com.google.android.exoplayer2.offline.StreamKey;
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.TestRunnable;
import com.google.android.exoplayer2.testutil.FakeDataSet;
Expand Down Expand Up @@ -154,15 +155,15 @@ public void testSaveAndLoadActionFile() throws Throwable {
public void testHandleDownloadRequest() throws Throwable {
handleDownloadRequest(fakeStreamKey1, fakeStreamKey2);
blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
public void testHandleMultipleDownloadRequest() throws Throwable {
handleDownloadRequest(fakeStreamKey1);
handleDownloadRequest(fakeStreamKey2);
blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand All @@ -176,7 +177,7 @@ public void testHandleInterferingDownloadRequest() throws Throwable {
handleDownloadRequest(fakeStreamKey1);

blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet);
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.android.exoplayer2.offline.DownloaderFactory;
import com.google.android.exoplayer2.offline.StreamKey;
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.FakeDataSource.Factory;
import com.google.android.exoplayer2.upstream.DummyDataSource;
Expand Down Expand Up @@ -129,12 +130,13 @@ public void testDownloadRepresentation() throws Exception {

assertCachedData(
cache,
fakeDataSet,
MASTER_PLAYLIST_URI,
MEDIA_PLAYLIST_1_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts");
new RequestSet(fakeDataSet)
.subset(
MASTER_PLAYLIST_URI,
MEDIA_PLAYLIST_1_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts"));
}

@Test
Expand Down Expand Up @@ -186,11 +188,12 @@ public void testDownloadMediaPlaylist() throws Exception {

assertCachedData(
cache,
fakeDataSet,
MEDIA_PLAYLIST_1_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts");
new RequestSet(fakeDataSet)
.subset(
MEDIA_PLAYLIST_1_URI,
MEDIA_PLAYLIST_1_DIR + "fileSequence0.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence1.ts",
MEDIA_PLAYLIST_1_DIR + "fileSequence2.ts"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,59 +33,89 @@
/** Assertion methods for {@link Cache}. */
public final class CacheAsserts {

/**
* Asserts that the cache content is equal to the data in the {@code fakeDataSet}.
*
* @throws IOException If an error occurred reading from the Cache.
*/
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet) throws IOException {
ArrayList<FakeData> allData = fakeDataSet.getAllData();
Uri[] uris = new Uri[allData.size()];
for (int i = 0; i < allData.size(); i++) {
uris[i] = allData.get(i).uri;
/** Defines a set of data requests. */
public static final class RequestSet {

private final FakeDataSet fakeDataSet;
private DataSpec[] dataSpecs;

public RequestSet(FakeDataSet fakeDataSet) {
this.fakeDataSet = fakeDataSet;
ArrayList<FakeData> allData = fakeDataSet.getAllData();
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);
}

/**
* Asserts that the cache content is equal to the given subset of data in the {@code fakeDataSet}.
*
* @throws IOException If an error occurred reading from the Cache.
*/
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet, String... uriStrings)
throws IOException {
Uri[] uris = new Uri[uriStrings.length];
for (int i = 0; i < uriStrings.length; i++) {
uris[i] = Uri.parse(uriStrings[i]);
public RequestSet subset(String... uriStrings) {
dataSpecs = new DataSpec[uriStrings.length];
for (int i = 0; i < dataSpecs.length; i++) {
dataSpecs[i] = new DataSpec(Uri.parse(uriStrings[i]));
}
return this;
}

public RequestSet subset(Uri... uris) {
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.
*/
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet, Uri... uris)
throws IOException {
public static void assertCachedData(Cache cache, RequestSet requestSet) throws IOException {
int totalLength = 0;
for (Uri uri : uris) {
byte[] data = fakeDataSet.getData(uri).getData();
assertDataCached(cache, uri, data);
for (int i = 0; i < requestSet.getCount(); i++) {
byte[] data = requestSet.getData(i);
assertDataCached(cache, requestSet.getDataSpec(i), data);
totalLength += data.length;
}
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.
*/
public static void assertDataCached(Cache cache, Uri uri, byte[] expected) throws IOException {
// TODO Make tests specify if the content length is stored in cache metadata.
DataSpec dataSpec = new DataSpec(uri, 0, expected.length, null, 0);
assertDataCached(cache, dataSpec, expected);
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet) throws IOException {
assertCachedData(cache, new RequestSet(fakeDataSet));
}

/**
Expand Down

0 comments on commit 811cdf0

Please sign in to comment.