Commit f3e650b8 by hoangtc Committed by Andrew Lewis

Update InstrumentationTestCase to use JUnit4.

InstrumentationTestCase has been deprecated, and it does not offer some useful
features, such as targeting SDK version level for tests, or skipping tests if
necessary.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=197141082
parent 2b9c31a1
...@@ -33,6 +33,7 @@ project.ext { ...@@ -33,6 +33,7 @@ project.ext {
robolectricVersion = '3.7.1' robolectricVersion = '3.7.1'
autoValueVersion = '1.6' autoValueVersion = '1.6'
checkerframeworkVersion = '2.5.0' checkerframeworkVersion = '2.5.0'
testRunnerVersion = '1.0.2'
modulePrefix = ':' modulePrefix = ':'
if (gradle.ext.has('exoplayerModulePrefix')) { if (gradle.ext.has('exoplayerModulePrefix')) {
modulePrefix += gradle.ext.exoplayerModulePrefix modulePrefix += gradle.ext.exoplayerModulePrefix
......
...@@ -22,6 +22,13 @@ android { ...@@ -22,6 +22,13 @@ android {
minSdkVersion project.ext.minSdkVersion minSdkVersion project.ext.minSdkVersion
targetSdkVersion project.ext.targetSdkVersion targetSdkVersion project.ext.targetSdkVersion
consumerProguardFiles 'proguard-rules.txt' consumerProguardFiles 'proguard-rules.txt'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
} }
// Workaround to prevent circular dependency on project :testutils. // Workaround to prevent circular dependency on project :testutils.
...@@ -51,6 +58,8 @@ dependencies { ...@@ -51,6 +58,8 @@ dependencies {
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:' + dexmakerVersion androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:' + dexmakerVersion
androidTestImplementation 'com.google.truth:truth:' + truthVersion androidTestImplementation 'com.google.truth:truth:' + truthVersion
androidTestImplementation 'org.mockito:mockito-core:' + mockitoVersion androidTestImplementation 'org.mockito:mockito-core:' + mockitoVersion
androidTestImplementation 'com.android.support.test:runner:' + testRunnerVersion
androidTestUtil 'com.android.support.test:orchestrator:' + testRunnerVersion
testImplementation 'com.google.truth:truth:' + truthVersion testImplementation 'com.google.truth:truth:' + truthVersion
testImplementation 'junit:junit:' + junitVersion testImplementation 'junit:junit:' + junitVersion
testImplementation 'org.mockito:mockito-core:' + mockitoVersion testImplementation 'org.mockito:mockito-core:' + mockitoVersion
......
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
package com.google.android.exoplayer2.upstream; package com.google.android.exoplayer2.upstream;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static junit.framework.Assert.fail;
import android.app.Instrumentation;
import android.content.ContentProvider; import android.content.ContentProvider;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.ContentValues; import android.content.ContentValues;
...@@ -28,48 +28,58 @@ import android.os.Bundle; ...@@ -28,48 +28,58 @@ import android.os.Bundle;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.test.InstrumentationTestCase; import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
/** /** Unit tests for {@link ContentDataSource}. */
* Unit tests for {@link ContentDataSource}. @RunWith(AndroidJUnit4.class)
*/ public final class ContentDataSourceTest {
public final class ContentDataSourceTest extends InstrumentationTestCase {
private static final String AUTHORITY = "com.google.android.exoplayer2.core.test"; private static final String AUTHORITY = "com.google.android.exoplayer2.core.test";
private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3"; private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3";
@Test
public void testRead() throws Exception { public void testRead() throws Exception {
assertData(getInstrumentation(), 0, C.LENGTH_UNSET, false); assertData(0, C.LENGTH_UNSET, false);
} }
@Test
public void testReadPipeMode() throws Exception { public void testReadPipeMode() throws Exception {
assertData(getInstrumentation(), 0, C.LENGTH_UNSET, true); assertData(0, C.LENGTH_UNSET, true);
} }
@Test
public void testReadFixedLength() throws Exception { public void testReadFixedLength() throws Exception {
assertData(getInstrumentation(), 0, 100, false); assertData(0, 100, false);
} }
@Test
public void testReadFromOffsetToEndOfInput() throws Exception { public void testReadFromOffsetToEndOfInput() throws Exception {
assertData(getInstrumentation(), 1, C.LENGTH_UNSET, false); assertData(1, C.LENGTH_UNSET, false);
} }
@Test
public void testReadFromOffsetToEndOfInputPipeMode() throws Exception { public void testReadFromOffsetToEndOfInputPipeMode() throws Exception {
assertData(getInstrumentation(), 1, C.LENGTH_UNSET, true); assertData(1, C.LENGTH_UNSET, true);
} }
@Test
public void testReadFromOffsetFixedLength() throws Exception { public void testReadFromOffsetFixedLength() throws Exception {
assertData(getInstrumentation(), 1, 100, false); assertData(1, 100, false);
} }
@Test
public void testReadInvalidUri() throws Exception { public void testReadInvalidUri() throws Exception {
ContentDataSource dataSource = new ContentDataSource(getInstrumentation().getContext()); ContentDataSource dataSource =
new ContentDataSource(InstrumentationRegistry.getTargetContext());
Uri contentUri = TestContentProvider.buildUri("does/not.exist", false); Uri contentUri = TestContentProvider.buildUri("does/not.exist", false);
DataSpec dataSpec = new DataSpec(contentUri); DataSpec dataSpec = new DataSpec(contentUri);
try { try {
...@@ -83,13 +93,14 @@ public final class ContentDataSourceTest extends InstrumentationTestCase { ...@@ -83,13 +93,14 @@ public final class ContentDataSourceTest extends InstrumentationTestCase {
} }
} }
private static void assertData(Instrumentation instrumentation, int offset, int length, private static void assertData(int offset, int length, boolean pipeMode) throws IOException {
boolean pipeMode) throws IOException {
Uri contentUri = TestContentProvider.buildUri(DATA_PATH, pipeMode); Uri contentUri = TestContentProvider.buildUri(DATA_PATH, pipeMode);
ContentDataSource dataSource = new ContentDataSource(instrumentation.getContext()); ContentDataSource dataSource =
new ContentDataSource(InstrumentationRegistry.getTargetContext());
try { try {
DataSpec dataSpec = new DataSpec(contentUri, offset, length, null); DataSpec dataSpec = new DataSpec(contentUri, offset, length, null);
byte[] completeData = TestUtil.getByteArray(instrumentation.getContext(), DATA_PATH); byte[] completeData =
TestUtil.getByteArray(InstrumentationRegistry.getTargetContext(), DATA_PATH);
byte[] expectedData = Arrays.copyOfRange(completeData, offset, byte[] expectedData = Arrays.copyOfRange(completeData, offset,
length == C.LENGTH_UNSET ? completeData.length : offset + length); length == C.LENGTH_UNSET ? completeData.length : offset + length);
TestUtil.assertDataSourceContent(dataSource, dataSpec, expectedData, !pipeMode); TestUtil.assertDataSourceContent(dataSource, dataSpec, expectedData, !pipeMode);
......
...@@ -19,7 +19,8 @@ import static com.google.common.truth.Truth.assertThat; ...@@ -19,7 +19,8 @@ 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 android.net.Uri; import android.net.Uri;
import android.test.InstrumentationTestCase; import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.SparseArray; import android.util.SparseArray;
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;
...@@ -29,9 +30,14 @@ import java.io.FileOutputStream; ...@@ -29,9 +30,14 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Set; import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Tests {@link CachedContentIndex}. */ /** Tests {@link CachedContentIndex}. */
public class CachedContentIndexTest extends InstrumentationTestCase { @RunWith(AndroidJUnit4.class)
public class CachedContentIndexTest {
private final byte[] testIndexV1File = { private final byte[] testIndexV1File = {
0, 0, 0, 1, // version 0, 0, 0, 1, // version
...@@ -70,19 +76,19 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -70,19 +76,19 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
private CachedContentIndex index; private CachedContentIndex index;
private File cacheDir; private File cacheDir;
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); cacheDir =
cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); Util.createTempDirectory(InstrumentationRegistry.getTargetContext(), "ExoPlayerTest");
index = new CachedContentIndex(cacheDir); index = new CachedContentIndex(cacheDir);
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() {
Util.recursiveDelete(cacheDir); Util.recursiveDelete(cacheDir);
super.tearDown();
} }
@Test
public void testAddGetRemove() throws Exception { public void testAddGetRemove() throws Exception {
final String key1 = "key1"; final String key1 = "key1";
final String key2 = "key2"; final String key2 = "key2";
...@@ -132,10 +138,12 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -132,10 +138,12 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(cacheSpanFile.exists()).isTrue(); assertThat(cacheSpanFile.exists()).isTrue();
} }
@Test
public void testStoreAndLoad() throws Exception { public void testStoreAndLoad() throws Exception {
assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir)); assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir));
} }
@Test
public void testLoadV1() throws Exception { public void testLoadV1() throws Exception {
FileOutputStream fos = new FileOutputStream(new File(cacheDir, CachedContentIndex.FILE_NAME)); FileOutputStream fos = new FileOutputStream(new File(cacheDir, CachedContentIndex.FILE_NAME));
fos.write(testIndexV1File); fos.write(testIndexV1File);
...@@ -153,6 +161,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -153,6 +161,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(ContentMetadataInternal.getContentLength(metadata2)).isEqualTo(2560); assertThat(ContentMetadataInternal.getContentLength(metadata2)).isEqualTo(2560);
} }
@Test
public void testLoadV2() throws Exception { public void testLoadV2() throws Exception {
FileOutputStream fos = new FileOutputStream(new File(cacheDir, CachedContentIndex.FILE_NAME)); FileOutputStream fos = new FileOutputStream(new File(cacheDir, CachedContentIndex.FILE_NAME));
fos.write(testIndexV2File); fos.write(testIndexV2File);
...@@ -171,7 +180,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -171,7 +180,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(ContentMetadataInternal.getContentLength(metadata2)).isEqualTo(2560); assertThat(ContentMetadataInternal.getContentLength(metadata2)).isEqualTo(2560);
} }
public void testAssignIdForKeyAndGetKeyForId() throws Exception { @Test
public void testAssignIdForKeyAndGetKeyForId() {
final String key1 = "key1"; final String key1 = "key1";
final String key2 = "key2"; final String key2 = "key2";
int id1 = index.assignIdForKey(key1); int id1 = index.assignIdForKey(key1);
...@@ -183,7 +193,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -183,7 +193,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(index.assignIdForKey(key2)).isEqualTo(id2); assertThat(index.assignIdForKey(key2)).isEqualTo(id2);
} }
public void testGetNewId() throws Exception { @Test
public void testGetNewId() {
SparseArray<String> idToKey = new SparseArray<>(); SparseArray<String> idToKey = new SparseArray<>();
assertThat(CachedContentIndex.getNewId(idToKey)).isEqualTo(0); assertThat(CachedContentIndex.getNewId(idToKey)).isEqualTo(0);
idToKey.put(10, ""); idToKey.put(10, "");
...@@ -194,6 +205,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -194,6 +205,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(CachedContentIndex.getNewId(idToKey)).isEqualTo(1); assertThat(CachedContentIndex.getNewId(idToKey)).isEqualTo(1);
} }
@Test
public void testEncryption() throws Exception { public void testEncryption() throws Exception {
byte[] key = "Bar12345Bar12345".getBytes(C.UTF8_NAME); // 128 bit key byte[] key = "Bar12345Bar12345".getBytes(C.UTF8_NAME); // 128 bit key
byte[] key2 = "Foo12345Foo12345".getBytes(C.UTF8_NAME); // 128 bit key byte[] key2 = "Foo12345Foo12345".getBytes(C.UTF8_NAME); // 128 bit key
...@@ -250,7 +262,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -250,7 +262,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir, key)); assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir, key));
} }
public void testRemoveEmptyNotLockedCachedContent() throws Exception { @Test
public void testRemoveEmptyNotLockedCachedContent() {
CachedContent cachedContent = index.getOrAdd("key1"); CachedContent cachedContent = index.getOrAdd("key1");
index.maybeRemove(cachedContent.key); index.maybeRemove(cachedContent.key);
...@@ -258,6 +271,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -258,6 +271,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(index.get(cachedContent.key)).isNull(); assertThat(index.get(cachedContent.key)).isNull();
} }
@Test
public void testCantRemoveNotEmptyCachedContent() throws Exception { public void testCantRemoveNotEmptyCachedContent() throws Exception {
CachedContent cachedContent = index.getOrAdd("key1"); CachedContent cachedContent = index.getOrAdd("key1");
File cacheSpanFile = File cacheSpanFile =
...@@ -270,7 +284,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { ...@@ -270,7 +284,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase {
assertThat(index.get(cachedContent.key)).isNotNull(); assertThat(index.get(cachedContent.key)).isNotNull();
} }
public void testCantRemoveLockedCachedContent() throws Exception { @Test
public void testCantRemoveLockedCachedContent() {
CachedContent cachedContent = index.getOrAdd("key1"); CachedContent cachedContent = index.getOrAdd("key1");
cachedContent.setLocked(true); cachedContent.setLocked(true);
......
...@@ -18,7 +18,8 @@ package com.google.android.exoplayer2.upstream.cache; ...@@ -18,7 +18,8 @@ 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 android.test.InstrumentationTestCase; import android.support.test.InstrumentationRegistry;
import android.support.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;
...@@ -26,11 +27,14 @@ import java.io.IOException; ...@@ -26,11 +27,14 @@ import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/** /** Unit tests for {@link SimpleCacheSpan}. */
* Unit tests for {@link SimpleCacheSpan}. @RunWith(AndroidJUnit4.class)
*/ public class SimpleCacheSpanTest {
public class SimpleCacheSpanTest extends InstrumentationTestCase {
private CachedContentIndex index; private CachedContentIndex index;
private File cacheDir; private File cacheDir;
...@@ -49,19 +53,19 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase { ...@@ -49,19 +53,19 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase {
return SimpleCacheSpan.createCacheEntry(cacheFile, index); return SimpleCacheSpan.createCacheEntry(cacheFile, index);
} }
@Override @Before
protected void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); cacheDir =
cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); Util.createTempDirectory(InstrumentationRegistry.getTargetContext(), "ExoPlayerTest");
index = new CachedContentIndex(cacheDir); index = new CachedContentIndex(cacheDir);
} }
@Override @After
protected void tearDown() throws Exception { public void tearDown() {
Util.recursiveDelete(cacheDir); Util.recursiveDelete(cacheDir);
super.tearDown();
} }
@Test
public void testCacheFile() throws Exception { public void testCacheFile() throws Exception {
assertCacheSpan("key1", 0, 0); assertCacheSpan("key1", 0, 0);
assertCacheSpan("key2", 1, 2); assertCacheSpan("key2", 1, 2);
...@@ -80,6 +84,7 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase { ...@@ -80,6 +84,7 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase {
+ "A paragraph-separator character \u2029", 1, 2); + "A paragraph-separator character \u2029", 1, 2);
} }
@Test
public void testUpgradeFileName() throws Exception { public void testUpgradeFileName() throws Exception {
String key = "asd\u00aa"; String key = "asd\u00aa";
int id = index.assignIdForKey(key); int id = index.assignIdForKey(key);
......
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