From 05e55f37ebe9f75b64d532041bc26e8bb0f85959 Mon Sep 17 00:00:00 2001 From: eguven Date: Mon, 22 Jan 2018 03:44:53 -0800 Subject: [PATCH] Fix DashDownloaderTest.testDownloadManifestFailure ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=182754993 --- .../dash/offline/DashDownloaderTest.java | 4 ++- .../exoplayer2/testutil/CacheAsserts.java | 27 +++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java b/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java index ed50c4b2a3..6967e867e9 100644 --- a/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java +++ b/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java @@ -20,6 +20,7 @@ import static com.google.android.exoplayer2.source.dash.offline.DashDownloadTest import static com.google.android.exoplayer2.source.dash.offline.DashDownloadTestData.TEST_MPD_URI; import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCacheEmpty; import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCachedData; +import static com.google.android.exoplayer2.testutil.CacheAsserts.assertDataCached; import android.test.InstrumentationTestCase; import com.google.android.exoplayer2.C; @@ -94,7 +95,8 @@ public class DashDownloaderTest extends InstrumentationTestCase { } catch (IOException e) { // ignore } - // TODO fix and enable assertDataCached(cache, TEST_MPD_URI, testMpdFirstPart); + DataSpec dataSpec = new DataSpec(TEST_MPD_URI, 0, testMpdFirstPart.length, null); + assertDataCached(cache, dataSpec, testMpdFirstPart); // on the second try it downloads the rest of the data DashManifest manifest = dashDownloader.getManifest(); diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java index d2a1bffce6..3530b55191 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java @@ -22,13 +22,11 @@ import android.net.Uri; import android.test.MoreAsserts; import com.google.android.exoplayer2.testutil.FakeDataSet.FakeData; import com.google.android.exoplayer2.upstream.DataSource; -import com.google.android.exoplayer2.upstream.DataSourceInputStream; import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DummyDataSource; import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheUtil; -import com.google.android.exoplayer2.util.Util; import java.io.IOException; import java.util.ArrayList; @@ -99,19 +97,26 @@ public final class CacheAsserts { * @throws IOException If an error occurred reading from the Cache. */ public static void assertDataCached(Cache cache, Uri uri, byte[] expected) throws IOException { - DataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0); DataSpec dataSpec = new DataSpec(uri, DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH); - DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, dataSpec); - byte[] bytes = null; + assertDataCached(cache, dataSpec, expected); + } + + /** + * Asserts that the cache contains the given data for {@code dataSpec}. + * + * @throws IOException If an error occurred reading from the Cache. + */ + public static void assertDataCached(Cache cache, DataSpec dataSpec, byte[] expected) + throws IOException { + DataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0); + dataSource.open(dataSpec); try { - bytes = Util.toByteArray(inputStream); - } catch (IOException e) { - // Ignore + byte[] bytes = TestUtil.readToEnd(dataSource); + MoreAsserts.assertEquals( + "Cached data doesn't match expected for '" + dataSpec.uri + "',", expected, bytes); } finally { - inputStream.close(); + dataSource.close(); } - MoreAsserts.assertEquals( - "Cached data doesn't match expected for '" + uri + "',", expected, bytes); } /** Asserts that there is no cache content for the given {@code uriStrings}. */