diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java index 5c342ae3d3..ca7d5d6214 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java @@ -15,6 +15,8 @@ */ package com.google.android.exoplayer2.upstream.cache; +import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCacheEmpty; + import android.net.Uri; import android.test.InstrumentationTestCase; import android.test.MoreAsserts; @@ -38,27 +40,29 @@ public class CacheDataSourceTest extends InstrumentationTestCase { private static final String KEY_1 = "key 1"; private static final String KEY_2 = "key 2"; - private File cacheDir; - private SimpleCache simpleCache; + private File tempFolder; + private SimpleCache cache; @Override - protected void setUp() throws Exception { - cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); - simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); + public void setUp() throws Exception { + super.setUp(); + tempFolder = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); + cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); } @Override - protected void tearDown() throws Exception { - Util.recursiveDelete(cacheDir); + public void tearDown() throws Exception { + Util.recursiveDelete(tempFolder); + super.tearDown(); } public void testMaxCacheFileSize() throws Exception { CacheDataSource cacheDataSource = createCacheDataSource(false, false); assertReadDataContentLength(cacheDataSource, false, false); - File[] files = cacheDir.listFiles(); - for (File file : files) { - if (!file.getName().equals(CachedContentIndex.FILE_NAME)) { - assertTrue(file.length() <= MAX_CACHE_FILE_SIZE); + for (String key : cache.getKeys()) { + for (CacheSpan cacheSpan : cache.getCachedSpans(key)) { + assertTrue(cacheSpan.length <= MAX_CACHE_FILE_SIZE); + assertTrue(cacheSpan.file.length() <= MAX_CACHE_FILE_SIZE); } } } @@ -104,7 +108,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { // Read partial at EOS but don't cross it so length is unknown CacheDataSource cacheDataSource = createCacheDataSource(false, true); assertReadData(cacheDataSource, true, TEST_DATA.length - 2, 2); - assertEquals(C.LENGTH_UNSET, simpleCache.getContentLength(KEY_1)); + assertEquals(C.LENGTH_UNSET, cache.getContentLength(KEY_1)); // Now do an unbounded request for whole data. This will cause a bounded request from upstream. // End of data from upstream shouldn't be mixed up with EOS and cause length set wrong. @@ -124,13 +128,13 @@ public class CacheDataSourceTest extends InstrumentationTestCase { CacheDataSource cacheDataSource = createCacheDataSource(false, true, CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS); assertReadData(cacheDataSource, true, 0, C.LENGTH_UNSET); - MoreAsserts.assertEmpty(simpleCache.getKeys()); + MoreAsserts.assertEmpty(cache.getKeys()); } public void testReadOnlyCache() throws Exception { CacheDataSource cacheDataSource = createCacheDataSource(false, false, 0, null); assertReadDataContentLength(cacheDataSource, false, false); - assertEquals(0, cacheDir.list().length); + assertCacheEmpty(cache); } private void assertCacheAndRead(boolean unboundedRequest, boolean simulateUnknownLength) @@ -155,7 +159,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { assertReadData(cacheDataSource, unknownLength, 0, length); assertEquals("When the range specified, CacheDataSource doesn't reach EOS so shouldn't cache " + "content length", !unboundedRequest ? C.LENGTH_UNSET : TEST_DATA.length, - simpleCache.getContentLength(KEY_1)); + cache.getContentLength(KEY_1)); } private void assertReadData(CacheDataSource cacheDataSource, boolean unknownLength, int position, @@ -192,7 +196,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { private CacheDataSource createCacheDataSource(boolean setReadException, boolean simulateUnknownLength, @CacheDataSource.Flags int flags) { return createCacheDataSource(setReadException, simulateUnknownLength, flags, - new CacheDataSink(simpleCache, MAX_CACHE_FILE_SIZE)); + new CacheDataSink(cache, MAX_CACHE_FILE_SIZE)); } private CacheDataSource createCacheDataSource(boolean setReadException, @@ -204,7 +208,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { if (setReadException) { fakeData.appendReadError(new IOException("Shouldn't read from upstream")); } - return new CacheDataSource(simpleCache, upstream, new FileDataSource(), cacheWriteDataSink, + return new CacheDataSource(cache, upstream, new FileDataSource(), cacheWriteDataSink, flags, null); } 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 new file mode 100644 index 0000000000..3494998e04 --- /dev/null +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/CacheAsserts.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.testutil; + +import static junit.framework.Assert.assertEquals; + +import android.net.Uri; +import android.test.MoreAsserts; +import com.google.android.exoplayer2.testutil.FakeDataSource.FakeData; +import com.google.android.exoplayer2.testutil.FakeDataSource.FakeDataSet; +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 java.io.ByteArrayOutputStream; +import java.io.IOException; +import junit.framework.Assert; + +/** + * Assertion methods for {@link Cache}. + */ +public final class CacheAsserts { + + /** Asserts that the cache content is equal to the data in the {@code fakeDataSet}. */ + public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet) throws IOException { + int totalLength = 0; + for (FakeData fakeData : fakeDataSet.getAllData()) { + byte[] data = fakeData.getData(); + assertCachedData(cache, fakeData.uri, data); + totalLength += data.length; + } + assertEquals(totalLength, cache.getCacheSpace()); + } + + /** + * Asserts that the cache content for the given {@code uriStrings} are equal to the data in the + * {@code fakeDataSet}. + */ + public static void assertCachedData(Cache cache, FakeDataSet fakeDataSet, String... uriStrings) + throws IOException { + for (String uriString : uriStrings) { + assertCachedData(cache, uriString, fakeDataSet.getData(uriString).getData()); + } + } + + /** + * Asserts that the cache content for the given {@code uriString} is equal to the {@code + * expected}. + */ + public static void assertCachedData(Cache cache, String uriString, byte[] expected) + throws IOException { + CacheDataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, + new DataSpec(Uri.parse(uriString), DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH)); + try { + inputStream.open(); + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } catch (IOException e) { + // Ignore + } finally { + inputStream.close(); + } + MoreAsserts.assertEquals("Cached data doesn't match expected for '" + uriString + "',", + expected, outputStream.toByteArray()); + } + + /** Asserts that there is no cache content for the given {@code uriStrings}. */ + public static void assertNoCachedData(Cache cache, String... uriStrings) { + for (String uriString : uriStrings) { + Assert.assertNull("There is cached data for '" + uriString + "',", + cache.getCachedSpans(CacheUtil.generateKey(Uri.parse(uriString)))); + } + } + + /** + * Asserts that the cache is empty. + * + * @param cache + */ + public static void assertCacheEmpty(Cache cache) { + assertEquals(0, cache.getCacheSpace()); + } + + private CacheAsserts() {} + +}