diff --git a/constants.gradle b/constants.gradle index dcadcceb4f..8871236417 100644 --- a/constants.gradle +++ b/constants.gradle @@ -33,6 +33,7 @@ project.ext { robolectricVersion = '3.7.1' autoValueVersion = '1.6' checkerframeworkVersion = '2.5.0' + testRunnerVersion = '1.0.2' modulePrefix = ':' if (gradle.ext.has('exoplayerModulePrefix')) { modulePrefix += gradle.ext.exoplayerModulePrefix diff --git a/library/core/build.gradle b/library/core/build.gradle index d2fa5e25f8..bb331b615c 100644 --- a/library/core/build.gradle +++ b/library/core/build.gradle @@ -22,6 +22,13 @@ android { minSdkVersion project.ext.minSdkVersion targetSdkVersion project.ext.targetSdkVersion 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. @@ -51,6 +58,8 @@ dependencies { androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:' + dexmakerVersion androidTestImplementation 'com.google.truth:truth:' + truthVersion 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 'junit:junit:' + junitVersion testImplementation 'org.mockito:mockito-core:' + mockitoVersion diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java index 3465393853..1133928e91 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java @@ -16,8 +16,8 @@ package com.google.android.exoplayer2.upstream; 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.ContentResolver; import android.content.ContentValues; @@ -28,48 +28,58 @@ import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.support.annotation.NonNull; 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.testutil.TestUtil; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; -/** - * Unit tests for {@link ContentDataSource}. - */ -public final class ContentDataSourceTest extends InstrumentationTestCase { +/** Unit tests for {@link ContentDataSource}. */ +@RunWith(AndroidJUnit4.class) +public final class ContentDataSourceTest { private static final String AUTHORITY = "com.google.android.exoplayer2.core.test"; private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3"; + @Test public void testRead() throws Exception { - assertData(getInstrumentation(), 0, C.LENGTH_UNSET, false); + assertData(0, C.LENGTH_UNSET, false); } + @Test public void testReadPipeMode() throws Exception { - assertData(getInstrumentation(), 0, C.LENGTH_UNSET, true); + assertData(0, C.LENGTH_UNSET, true); } + @Test public void testReadFixedLength() throws Exception { - assertData(getInstrumentation(), 0, 100, false); + assertData(0, 100, false); } + @Test public void testReadFromOffsetToEndOfInput() throws Exception { - assertData(getInstrumentation(), 1, C.LENGTH_UNSET, false); + assertData(1, C.LENGTH_UNSET, false); } + @Test public void testReadFromOffsetToEndOfInputPipeMode() throws Exception { - assertData(getInstrumentation(), 1, C.LENGTH_UNSET, true); + assertData(1, C.LENGTH_UNSET, true); } + @Test public void testReadFromOffsetFixedLength() throws Exception { - assertData(getInstrumentation(), 1, 100, false); + assertData(1, 100, false); } + @Test 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); DataSpec dataSpec = new DataSpec(contentUri); try { @@ -83,13 +93,14 @@ public final class ContentDataSourceTest extends InstrumentationTestCase { } } - private static void assertData(Instrumentation instrumentation, int offset, int length, - boolean pipeMode) throws IOException { + private static void assertData(int offset, int length, boolean pipeMode) throws IOException { Uri contentUri = TestContentProvider.buildUri(DATA_PATH, pipeMode); - ContentDataSource dataSource = new ContentDataSource(instrumentation.getContext()); + ContentDataSource dataSource = + new ContentDataSource(InstrumentationRegistry.getTargetContext()); try { 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, length == C.LENGTH_UNSET ? completeData.length : offset + length); TestUtil.assertDataSourceContent(dataSource, dataSpec, expectedData, !pipeMode); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java index 58531346ab..be4a2a96dc 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/CachedContentIndexTest.java @@ -19,7 +19,8 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertWithMessage; import android.net.Uri; -import android.test.InstrumentationTestCase; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; import android.util.SparseArray; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.util.Util; @@ -29,9 +30,14 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.Collection; import java.util.Set; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; /** Tests {@link CachedContentIndex}. */ -public class CachedContentIndexTest extends InstrumentationTestCase { +@RunWith(AndroidJUnit4.class) +public class CachedContentIndexTest { private final byte[] testIndexV1File = { 0, 0, 0, 1, // version @@ -70,19 +76,19 @@ public class CachedContentIndexTest extends InstrumentationTestCase { private CachedContentIndex index; private File cacheDir; - @Override + @Before public void setUp() throws Exception { - super.setUp(); - cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); + cacheDir = + Util.createTempDirectory(InstrumentationRegistry.getTargetContext(), "ExoPlayerTest"); index = new CachedContentIndex(cacheDir); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() { Util.recursiveDelete(cacheDir); - super.tearDown(); } + @Test public void testAddGetRemove() throws Exception { final String key1 = "key1"; final String key2 = "key2"; @@ -132,10 +138,12 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(cacheSpanFile.exists()).isTrue(); } + @Test public void testStoreAndLoad() throws Exception { assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir)); } + @Test public void testLoadV1() throws Exception { FileOutputStream fos = new FileOutputStream(new File(cacheDir, CachedContentIndex.FILE_NAME)); fos.write(testIndexV1File); @@ -153,6 +161,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(ContentMetadataInternal.getContentLength(metadata2)).isEqualTo(2560); } + @Test public void testLoadV2() throws Exception { FileOutputStream fos = new FileOutputStream(new File(cacheDir, CachedContentIndex.FILE_NAME)); fos.write(testIndexV2File); @@ -171,7 +180,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(ContentMetadataInternal.getContentLength(metadata2)).isEqualTo(2560); } - public void testAssignIdForKeyAndGetKeyForId() throws Exception { + @Test + public void testAssignIdForKeyAndGetKeyForId() { final String key1 = "key1"; final String key2 = "key2"; int id1 = index.assignIdForKey(key1); @@ -183,7 +193,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(index.assignIdForKey(key2)).isEqualTo(id2); } - public void testGetNewId() throws Exception { + @Test + public void testGetNewId() { SparseArray idToKey = new SparseArray<>(); assertThat(CachedContentIndex.getNewId(idToKey)).isEqualTo(0); idToKey.put(10, ""); @@ -194,6 +205,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(CachedContentIndex.getNewId(idToKey)).isEqualTo(1); } + @Test public void testEncryption() throws Exception { byte[] key = "Bar12345Bar12345".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 { assertStoredAndLoadedEqual(index, new CachedContentIndex(cacheDir, key)); } - public void testRemoveEmptyNotLockedCachedContent() throws Exception { + @Test + public void testRemoveEmptyNotLockedCachedContent() { CachedContent cachedContent = index.getOrAdd("key1"); index.maybeRemove(cachedContent.key); @@ -258,6 +271,7 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(index.get(cachedContent.key)).isNull(); } + @Test public void testCantRemoveNotEmptyCachedContent() throws Exception { CachedContent cachedContent = index.getOrAdd("key1"); File cacheSpanFile = @@ -270,7 +284,8 @@ public class CachedContentIndexTest extends InstrumentationTestCase { assertThat(index.get(cachedContent.key)).isNotNull(); } - public void testCantRemoveLockedCachedContent() throws Exception { + @Test + public void testCantRemoveLockedCachedContent() { CachedContent cachedContent = index.getOrAdd("key1"); cachedContent.setLocked(true); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java index 637a19cdd2..afbbf6605f 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheSpanTest.java @@ -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.assertWithMessage; -import android.test.InstrumentationTestCase; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; import com.google.android.exoplayer2.util.Util; import java.io.File; import java.io.FileOutputStream; @@ -26,11 +27,14 @@ import java.io.IOException; import java.util.HashMap; import java.util.Set; 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}. - */ -public class SimpleCacheSpanTest extends InstrumentationTestCase { +/** Unit tests for {@link SimpleCacheSpan}. */ +@RunWith(AndroidJUnit4.class) +public class SimpleCacheSpanTest { private CachedContentIndex index; private File cacheDir; @@ -49,19 +53,19 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase { return SimpleCacheSpan.createCacheEntry(cacheFile, index); } - @Override - protected void setUp() throws Exception { - super.setUp(); - cacheDir = Util.createTempDirectory(getInstrumentation().getContext(), "ExoPlayerTest"); + @Before + public void setUp() throws Exception { + cacheDir = + Util.createTempDirectory(InstrumentationRegistry.getTargetContext(), "ExoPlayerTest"); index = new CachedContentIndex(cacheDir); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() { Util.recursiveDelete(cacheDir); - super.tearDown(); } + @Test public void testCacheFile() throws Exception { assertCacheSpan("key1", 0, 0); assertCacheSpan("key2", 1, 2); @@ -80,6 +84,7 @@ public class SimpleCacheSpanTest extends InstrumentationTestCase { + "A paragraph-separator character \u2029", 1, 2); } + @Test public void testUpgradeFileName() throws Exception { String key = "asd\u00aa"; int id = index.assignIdForKey(key);