Modify DashDownloaderTest to test if content length is stored

PiperOrigin-RevId: 250655481
This commit is contained in:
eguven 2019-05-30 10:47:28 +01:00 committed by Toni
parent e2452f8103
commit 00b26a51df
4 changed files with 90 additions and 55 deletions

View File

@ -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 {
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 {
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 {
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 {
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 {
// Expected. // Expected.
} }
dashDownloader.download(progressListener); dashDownloader.download(progressListener);
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test

View File

@ -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;
@ -153,7 +154,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
@ -161,7 +162,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
@ -175,7 +176,7 @@ public class DownloadManagerDashTest {
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
blockUntilTasksCompleteAndThrowAnyDownloadError(); blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, fakeDataSet); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test

View File

@ -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 {
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 {
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

View File

@ -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);
}
}
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. * @throws IOException If an error occurred reading from the Cache.
*/ */
public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet, String... uriStrings) public static void assertCachedData(Cache cache, RequestSet requestSet) throws IOException {
throws IOException {
Uri[] uris = new Uri[uriStrings.length];
for (int i = 0; i < uriStrings.length; i++) {
uris[i] = Uri.parse(uriStrings[i]);
}
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, Uri... uris)
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);
} }
/** /**