Commit 531eb15f by eguven Committed by Oliver Woodman

Move DashDownloadTestBase assert methods to CacheAsserts

CacheAsserts contains cache assertion methods for testing. It's easier to use in tests than DashDownloadTestBase which requires to be extended.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159688808
parent 8e49cab8
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
*/ */
package com.google.android.exoplayer2.upstream.cache; package com.google.android.exoplayer2.upstream.cache;
import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCacheEmpty;
import android.net.Uri; import android.net.Uri;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import android.test.MoreAsserts; import android.test.MoreAsserts;
...@@ -38,27 +40,29 @@ public class CacheDataSourceTest extends InstrumentationTestCase { ...@@ -38,27 +40,29 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
private static final String KEY_1 = "key 1"; private static final String KEY_1 = "key 1";
private static final String KEY_2 = "key 2"; private static final String KEY_2 = "key 2";
private File cacheDir; private File tempFolder;
private SimpleCache simpleCache; private SimpleCache cache;
@Override @Override
protected void setUp() throws Exception { public void setUp() throws Exception {
cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); super.setUp();
simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); tempFolder = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest");
cache = new SimpleCache(tempFolder, new NoOpCacheEvictor());
} }
@Override @Override
protected void tearDown() throws Exception { public void tearDown() throws Exception {
Util.recursiveDelete(cacheDir); Util.recursiveDelete(tempFolder);
super.tearDown();
} }
public void testMaxCacheFileSize() throws Exception { public void testMaxCacheFileSize() throws Exception {
CacheDataSource cacheDataSource = createCacheDataSource(false, false); CacheDataSource cacheDataSource = createCacheDataSource(false, false);
assertReadDataContentLength(cacheDataSource, false, false); assertReadDataContentLength(cacheDataSource, false, false);
File[] files = cacheDir.listFiles(); for (String key : cache.getKeys()) {
for (File file : files) { for (CacheSpan cacheSpan : cache.getCachedSpans(key)) {
if (!file.getName().equals(CachedContentIndex.FILE_NAME)) { assertTrue(cacheSpan.length <= MAX_CACHE_FILE_SIZE);
assertTrue(file.length() <= MAX_CACHE_FILE_SIZE); assertTrue(cacheSpan.file.length() <= MAX_CACHE_FILE_SIZE);
} }
} }
} }
...@@ -104,7 +108,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { ...@@ -104,7 +108,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
// Read partial at EOS but don't cross it so length is unknown // Read partial at EOS but don't cross it so length is unknown
CacheDataSource cacheDataSource = createCacheDataSource(false, true); CacheDataSource cacheDataSource = createCacheDataSource(false, true);
assertReadData(cacheDataSource, true, TEST_DATA.length - 2, 2); 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. // 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. // 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 { ...@@ -124,13 +128,13 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
CacheDataSource cacheDataSource = createCacheDataSource(false, true, CacheDataSource cacheDataSource = createCacheDataSource(false, true,
CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS); CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
assertReadData(cacheDataSource, true, 0, C.LENGTH_UNSET); assertReadData(cacheDataSource, true, 0, C.LENGTH_UNSET);
MoreAsserts.assertEmpty(simpleCache.getKeys()); MoreAsserts.assertEmpty(cache.getKeys());
} }
public void testReadOnlyCache() throws Exception { public void testReadOnlyCache() throws Exception {
CacheDataSource cacheDataSource = createCacheDataSource(false, false, 0, null); CacheDataSource cacheDataSource = createCacheDataSource(false, false, 0, null);
assertReadDataContentLength(cacheDataSource, false, false); assertReadDataContentLength(cacheDataSource, false, false);
assertEquals(0, cacheDir.list().length); assertCacheEmpty(cache);
} }
private void assertCacheAndRead(boolean unboundedRequest, boolean simulateUnknownLength) private void assertCacheAndRead(boolean unboundedRequest, boolean simulateUnknownLength)
...@@ -155,7 +159,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { ...@@ -155,7 +159,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
assertReadData(cacheDataSource, unknownLength, 0, length); assertReadData(cacheDataSource, unknownLength, 0, length);
assertEquals("When the range specified, CacheDataSource doesn't reach EOS so shouldn't cache " assertEquals("When the range specified, CacheDataSource doesn't reach EOS so shouldn't cache "
+ "content length", !unboundedRequest ? C.LENGTH_UNSET : TEST_DATA.length, + "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, private void assertReadData(CacheDataSource cacheDataSource, boolean unknownLength, int position,
...@@ -192,7 +196,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { ...@@ -192,7 +196,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
private CacheDataSource createCacheDataSource(boolean setReadException, private CacheDataSource createCacheDataSource(boolean setReadException,
boolean simulateUnknownLength, @CacheDataSource.Flags int flags) { boolean simulateUnknownLength, @CacheDataSource.Flags int flags) {
return createCacheDataSource(setReadException, simulateUnknownLength, 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, private CacheDataSource createCacheDataSource(boolean setReadException,
...@@ -204,7 +208,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase { ...@@ -204,7 +208,7 @@ public class CacheDataSourceTest extends InstrumentationTestCase {
if (setReadException) { if (setReadException) {
fakeData.appendReadError(new IOException("Shouldn't read from upstream")); 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); flags, null);
} }
......
/*
* 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() {}
}
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