Commit 88af9448 by olly Committed by Oliver Woodman

Move CachedContentIndex and SimpleCacheSpan tests to robolectric

PiperOrigin-RevId: 223518390
parent beae7c9d
...@@ -20,8 +20,6 @@ import static com.google.common.truth.Truth.assertWithMessage; ...@@ -20,8 +20,6 @@ import static com.google.common.truth.Truth.assertWithMessage;
import android.net.Uri; import android.net.Uri;
import android.util.SparseArray; import android.util.SparseArray;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.File; import java.io.File;
...@@ -34,9 +32,11 @@ import org.junit.After; ...@@ -34,9 +32,11 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
/** Tests {@link CachedContentIndex}. */ /** Tests {@link CachedContentIndex}. */
@RunWith(AndroidJUnit4.class) @RunWith(RobolectricTestRunner.class)
public class CachedContentIndexTest { public class CachedContentIndexTest {
private final byte[] testIndexV1File = { private final byte[] testIndexV1File = {
...@@ -79,7 +79,7 @@ public class CachedContentIndexTest { ...@@ -79,7 +79,7 @@ public class CachedContentIndexTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
cacheDir = cacheDir =
Util.createTempDirectory(InstrumentationRegistry.getTargetContext(), "ExoPlayerTest"); Util.createTempDirectory(RuntimeEnvironment.application, "ExoPlayerTest");
index = new CachedContentIndex(cacheDir); index = new CachedContentIndex(cacheDir);
} }
......
...@@ -18,8 +18,6 @@ package com.google.android.exoplayer2.upstream.cache; ...@@ -18,8 +18,6 @@ package com.google.android.exoplayer2.upstream.cache;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -31,14 +29,13 @@ import org.junit.After; ...@@ -31,14 +29,13 @@ import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
/** Unit tests for {@link SimpleCacheSpan}. */ /** Unit tests for {@link SimpleCacheSpan}. */
@RunWith(AndroidJUnit4.class) @RunWith(RobolectricTestRunner.class)
public class SimpleCacheSpanTest { public class SimpleCacheSpanTest {
private CachedContentIndex index;
private File cacheDir;
public static File createCacheSpanFile(File cacheDir, int id, long offset, int length, public static File createCacheSpanFile(File cacheDir, int id, long offset, int length,
long lastAccessTimestamp) throws IOException { long lastAccessTimestamp) throws IOException {
File cacheFile = SimpleCacheSpan.getCacheFile(cacheDir, id, offset, lastAccessTimestamp); File cacheFile = SimpleCacheSpan.getCacheFile(cacheDir, id, offset, lastAccessTimestamp);
...@@ -46,17 +43,13 @@ public class SimpleCacheSpanTest { ...@@ -46,17 +43,13 @@ public class SimpleCacheSpanTest {
return cacheFile; return cacheFile;
} }
public static CacheSpan createCacheSpan(CachedContentIndex index, File cacheDir, String key, private CachedContentIndex index;
long offset, int length, long lastAccessTimestamp) throws IOException { private File cacheDir;
int id = index.assignIdForKey(key);
File cacheFile = createCacheSpanFile(cacheDir, id, offset, length, lastAccessTimestamp);
return SimpleCacheSpan.createCacheEntry(cacheFile, index);
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
cacheDir = cacheDir =
Util.createTempDirectory(InstrumentationRegistry.getTargetContext(), "ExoPlayerTest"); Util.createTempDirectory(RuntimeEnvironment.application, "ExoPlayerTest");
index = new CachedContentIndex(cacheDir); index = new CachedContentIndex(cacheDir);
} }
...@@ -86,12 +79,12 @@ public class SimpleCacheSpanTest { ...@@ -86,12 +79,12 @@ public class SimpleCacheSpanTest {
@Test @Test
public void testUpgradeFileName() throws Exception { public void testUpgradeFileName() throws Exception {
String key = "asd\u00aa"; String key = "abc%def";
int id = index.assignIdForKey(key); int id = index.assignIdForKey(key);
File v3file = createTestFile(id + ".0.1.v3.exo"); File v3file = createTestFile(id + ".0.1.v3.exo");
File v2file = createTestFile("asd%aa.1.2.v2.exo"); File v2file = createTestFile("abc%25def.1.2.v2.exo"); // %25 is '%' after escaping
File wrongEscapedV2file = createTestFile("asd%za.3.4.v2.exo"); File wrongEscapedV2file = createTestFile("abc%2Gdef.3.4.v2.exo"); // 2G is invalid hex
File v1File = createTestFile("asd\u00aa.5.6.v1.exo"); File v1File = createTestFile("abc%def.5.6.v1.exo"); // V1 did not escape
for (File file : cacheDir.listFiles()) { for (File file : cacheDir.listFiles()) {
SimpleCacheSpan cacheEntry = SimpleCacheSpan.createCacheEntry(file, index); SimpleCacheSpan cacheEntry = SimpleCacheSpan.createCacheEntry(file, index);
......
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