mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Testutils: Add missing Javadoc + Misc cleanup
PiperOrigin-RevId: 278835106
This commit is contained in:
parent
0a27d7b482
commit
4570cd37c5
12 changed files with 100 additions and 35 deletions
|
|
@ -38,7 +38,7 @@ public class VersionTableTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
databaseProvider = TestUtil.getTestDatabaseProvider();
|
databaseProvider = TestUtil.getInMemoryDatabaseProvider();
|
||||||
database = databaseProvider.getWritableDatabase();
|
database = databaseProvider.getWritableDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@
|
||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.extractor.ogg;
|
package com.google.android.exoplayer2.extractor.ogg;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
|
||||||
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import com.google.android.exoplayer2.testutil.ExtractorAsserts;
|
import com.google.android.exoplayer2.testutil.ExtractorAsserts;
|
||||||
|
|
@ -59,7 +58,7 @@ public final class OggExtractorTest {
|
||||||
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
||||||
TestUtil.createByteArray(7), // Laces
|
TestUtil.createByteArray(7), // Laces
|
||||||
new byte[] {0x01, 'v', 'o', 'r', 'b', 'i', 's'});
|
new byte[] {0x01, 'v', 'o', 'r', 'b', 'i', 's'});
|
||||||
assertThat(sniff(data)).isTrue();
|
assertSniff(data, /* expectedResult= */ true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -69,7 +68,7 @@ public final class OggExtractorTest {
|
||||||
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
||||||
TestUtil.createByteArray(5), // Laces
|
TestUtil.createByteArray(5), // Laces
|
||||||
new byte[] {0x7F, 'F', 'L', 'A', 'C'});
|
new byte[] {0x7F, 'F', 'L', 'A', 'C'});
|
||||||
assertThat(sniff(data)).isTrue();
|
assertSniff(data, /* expectedResult= */ true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -77,13 +76,13 @@ public final class OggExtractorTest {
|
||||||
byte[] data =
|
byte[] data =
|
||||||
TestUtil.joinByteArrays(
|
TestUtil.joinByteArrays(
|
||||||
OggTestData.buildOggHeader(0x02, 0, 1000, 0x00), new byte[] {'O', 'p', 'u', 's'});
|
OggTestData.buildOggHeader(0x02, 0, 1000, 0x00), new byte[] {'O', 'p', 'u', 's'});
|
||||||
assertThat(sniff(data)).isFalse();
|
assertSniff(data, /* expectedResult= */ false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSniffFailsInvalidOggHeader() throws Exception {
|
public void testSniffFailsInvalidOggHeader() throws Exception {
|
||||||
byte[] data = OggTestData.buildOggHeader(0x00, 0, 1000, 0x00);
|
byte[] data = OggTestData.buildOggHeader(0x00, 0, 1000, 0x00);
|
||||||
assertThat(sniff(data)).isFalse();
|
assertSniff(data, /* expectedResult= */ false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -93,16 +92,17 @@ public final class OggExtractorTest {
|
||||||
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
||||||
TestUtil.createByteArray(7), // Laces
|
TestUtil.createByteArray(7), // Laces
|
||||||
new byte[] {0x7F, 'X', 'o', 'r', 'b', 'i', 's'});
|
new byte[] {0x7F, 'X', 'o', 'r', 'b', 'i', 's'});
|
||||||
assertThat(sniff(data)).isFalse();
|
assertSniff(data, /* expectedResult= */ false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSniffFailsEOF() throws Exception {
|
public void testSniffFailsEOF() throws Exception {
|
||||||
byte[] data = OggTestData.buildOggHeader(0x02, 0, 1000, 0x00);
|
byte[] data = OggTestData.buildOggHeader(0x02, 0, 1000, 0x00);
|
||||||
assertThat(sniff(data)).isFalse();
|
assertSniff(data, /* expectedResult= */ false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean sniff(byte[] data) throws InterruptedException, IOException {
|
private void assertSniff(byte[] data, boolean expectedResult)
|
||||||
|
throws InterruptedException, IOException {
|
||||||
FakeExtractorInput input =
|
FakeExtractorInput input =
|
||||||
new FakeExtractorInput.Builder()
|
new FakeExtractorInput.Builder()
|
||||||
.setData(data)
|
.setData(data)
|
||||||
|
|
@ -110,6 +110,6 @@ public final class OggExtractorTest {
|
||||||
.setSimulateUnknownLength(true)
|
.setSimulateUnknownLength(true)
|
||||||
.setSimulatePartialReads(true)
|
.setSimulatePartialReads(true)
|
||||||
.build();
|
.build();
|
||||||
return TestUtil.sniffTestData(OGG_EXTRACTOR_FACTORY.create(), input);
|
ExtractorAsserts.assertSniff(OGG_EXTRACTOR_FACTORY.create(), input, expectedResult);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ public class DownloadManagerTest {
|
||||||
uri2 = Uri.parse("http://abc.com/media2");
|
uri2 = Uri.parse("http://abc.com/media2");
|
||||||
uri3 = Uri.parse("http://abc.com/media3");
|
uri3 = Uri.parse("http://abc.com/media3");
|
||||||
dummyMainThread = new DummyMainThread();
|
dummyMainThread = new DummyMainThread();
|
||||||
downloadIndex = new DefaultDownloadIndex(TestUtil.getTestDatabaseProvider());
|
downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||||
downloaderFactory = new FakeDownloaderFactory();
|
downloaderFactory = new FakeDownloaderFactory();
|
||||||
setUpDownloadManager(100);
|
setUpDownloadManager(100);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -334,7 +334,7 @@ public class CachedContentIndexTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private CachedContentIndex newInstance() {
|
private CachedContentIndex newInstance() {
|
||||||
return new CachedContentIndex(TestUtil.getTestDatabaseProvider());
|
return new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
private CachedContentIndex newLegacyInstance() {
|
private CachedContentIndex newLegacyInstance() {
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ public final class CachedRegionTrackerTest {
|
||||||
tracker = new CachedRegionTracker(cache, CACHE_KEY, CHUNK_INDEX);
|
tracker = new CachedRegionTracker(cache, CACHE_KEY, CHUNK_INDEX);
|
||||||
cacheDir =
|
cacheDir =
|
||||||
Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
||||||
index = new CachedContentIndex(TestUtil.getTestDatabaseProvider());
|
index = new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ public class SimpleCacheSpanTest {
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
cacheDir =
|
cacheDir =
|
||||||
Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
||||||
index = new CachedContentIndex(TestUtil.getTestDatabaseProvider());
|
index = new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
|
|
|
||||||
|
|
@ -322,7 +322,7 @@ public class SimpleCacheTest {
|
||||||
@Test
|
@Test
|
||||||
public void testExceptionDuringEvictionByLeastRecentlyUsedCacheEvictorNotHang() throws Exception {
|
public void testExceptionDuringEvictionByLeastRecentlyUsedCacheEvictorNotHang() throws Exception {
|
||||||
CachedContentIndex contentIndex =
|
CachedContentIndex contentIndex =
|
||||||
Mockito.spy(new CachedContentIndex(TestUtil.getTestDatabaseProvider()));
|
Mockito.spy(new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider()));
|
||||||
SimpleCache simpleCache =
|
SimpleCache simpleCache =
|
||||||
new SimpleCache(
|
new SimpleCache(
|
||||||
cacheDir, new LeastRecentlyUsedCacheEvictor(20), contentIndex, /* fileIndex= */ null);
|
cacheDir, new LeastRecentlyUsedCacheEvictor(20), contentIndex, /* fileIndex= */ null);
|
||||||
|
|
|
||||||
|
|
@ -97,7 +97,7 @@ public class DownloadManagerDashTest {
|
||||||
|
|
||||||
fakeStreamKey1 = new StreamKey(0, 0, 0);
|
fakeStreamKey1 = new StreamKey(0, 0, 0);
|
||||||
fakeStreamKey2 = new StreamKey(0, 1, 0);
|
fakeStreamKey2 = new StreamKey(0, 1, 0);
|
||||||
downloadIndex = new DefaultDownloadIndex(TestUtil.getTestDatabaseProvider());
|
downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||||
createDownloadManager();
|
createDownloadManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,7 +112,7 @@ public class DownloadServiceDashTest {
|
||||||
dummyMainThread.runTestOnMainThread(
|
dummyMainThread.runTestOnMainThread(
|
||||||
() -> {
|
() -> {
|
||||||
DefaultDownloadIndex downloadIndex =
|
DefaultDownloadIndex downloadIndex =
|
||||||
new DefaultDownloadIndex(TestUtil.getTestDatabaseProvider());
|
new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||||
final DownloadManager dashDownloadManager =
|
final DownloadManager dashDownloadManager =
|
||||||
new DownloadManager(
|
new DownloadManager(
|
||||||
ApplicationProvider.getApplicationContext(),
|
ApplicationProvider.getApplicationContext(),
|
||||||
|
|
|
||||||
|
|
@ -467,6 +467,7 @@ public final class ActionSchedule {
|
||||||
return apply(new ThrowPlaybackException(tag, exception));
|
return apply(new ThrowPlaybackException(tag, exception));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Builds the schedule. */
|
||||||
public ActionSchedule build() {
|
public ActionSchedule build() {
|
||||||
CallbackAction callbackAction = new CallbackAction(tag);
|
CallbackAction callbackAction = new CallbackAction(tag);
|
||||||
apply(callbackAction);
|
apply(callbackAction);
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,29 @@ public final class ExtractorAsserts {
|
||||||
private static final String DUMP_EXTENSION = ".dump";
|
private static final String DUMP_EXTENSION = ".dump";
|
||||||
private static final String UNKNOWN_LENGTH_EXTENSION = ".unklen" + DUMP_EXTENSION;
|
private static final String UNKNOWN_LENGTH_EXTENSION = ".unklen" + DUMP_EXTENSION;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Asserts that {@link Extractor#sniff(ExtractorInput)} returns the {@code expectedResult} for a
|
||||||
|
* given {@code input}, retrying repeatedly when {@link SimulatedIOException} is thrown.
|
||||||
|
*
|
||||||
|
* @param extractor The extractor to test.
|
||||||
|
* @param input The extractor input.
|
||||||
|
* @param expectedResult The expected return value.
|
||||||
|
* @throws IOException If reading from the input fails.
|
||||||
|
* @throws InterruptedException If interrupted while reading from the input.
|
||||||
|
*/
|
||||||
|
public static void assertSniff(
|
||||||
|
Extractor extractor, FakeExtractorInput input, boolean expectedResult)
|
||||||
|
throws IOException, InterruptedException {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
assertThat(extractor.sniff(input)).isEqualTo(expectedResult);
|
||||||
|
return;
|
||||||
|
} catch (SimulatedIOException e) {
|
||||||
|
// Ignore.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that an extractor behaves correctly given valid input data. Can only be used from
|
* Asserts that an extractor behaves correctly given valid input data. Can only be used from
|
||||||
* Robolectric tests.
|
* Robolectric tests.
|
||||||
|
|
@ -164,7 +187,7 @@ public final class ExtractorAsserts {
|
||||||
.setSimulatePartialReads(simulatePartialReads).build();
|
.setSimulatePartialReads(simulatePartialReads).build();
|
||||||
|
|
||||||
if (sniffFirst) {
|
if (sniffFirst) {
|
||||||
assertThat(TestUtil.sniffTestData(extractor, input)).isTrue();
|
assertSniff(extractor, input, /* expectedResult= */ true);
|
||||||
input.resetPeekPosition();
|
input.resetPeekPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,6 @@ import com.google.android.exoplayer2.extractor.Extractor;
|
||||||
import com.google.android.exoplayer2.extractor.ExtractorInput;
|
import com.google.android.exoplayer2.extractor.ExtractorInput;
|
||||||
import com.google.android.exoplayer2.extractor.PositionHolder;
|
import com.google.android.exoplayer2.extractor.PositionHolder;
|
||||||
import com.google.android.exoplayer2.extractor.SeekMap;
|
import com.google.android.exoplayer2.extractor.SeekMap;
|
||||||
import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException;
|
|
||||||
import com.google.android.exoplayer2.upstream.DataSource;
|
import com.google.android.exoplayer2.upstream.DataSource;
|
||||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
import com.google.android.exoplayer2.util.Assertions;
|
||||||
|
|
@ -50,17 +49,14 @@ public class TestUtil {
|
||||||
|
|
||||||
private TestUtil() {}
|
private TestUtil() {}
|
||||||
|
|
||||||
public static boolean sniffTestData(Extractor extractor, FakeExtractorInput input)
|
/**
|
||||||
throws IOException, InterruptedException {
|
* Given an open {@link DataSource}, repeatedly calls {@link DataSource#read(byte[], int, int)}
|
||||||
while (true) {
|
* until {@link C#RESULT_END_OF_INPUT} is returned.
|
||||||
try {
|
*
|
||||||
return extractor.sniff(input);
|
* @param dataSource The source from which to read.
|
||||||
} catch (SimulatedIOException e) {
|
* @return The concatenation of all read data.
|
||||||
// Ignore.
|
* @throws IOException If an error occurs reading from the source.
|
||||||
}
|
*/
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] readToEnd(DataSource dataSource) throws IOException {
|
public static byte[] readToEnd(DataSource dataSource) throws IOException {
|
||||||
byte[] data = new byte[1024];
|
byte[] data = new byte[1024];
|
||||||
int position = 0;
|
int position = 0;
|
||||||
|
|
@ -77,6 +73,14 @@ public class TestUtil {
|
||||||
return Arrays.copyOf(data, position);
|
return Arrays.copyOf(data, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given an open {@link DataSource}, repeatedly calls {@link DataSource#read(byte[], int, int)}
|
||||||
|
* until exactly {@code length} bytes have been read.
|
||||||
|
*
|
||||||
|
* @param dataSource The source from which to read.
|
||||||
|
* @return The read data.
|
||||||
|
* @throws IOException If an error occurs reading from the source.
|
||||||
|
*/
|
||||||
public static byte[] readExactly(DataSource dataSource, int length) throws IOException {
|
public static byte[] readExactly(DataSource dataSource, int length) throws IOException {
|
||||||
byte[] data = new byte[length];
|
byte[] data = new byte[length];
|
||||||
int position = 0;
|
int position = 0;
|
||||||
|
|
@ -91,22 +95,49 @@ public class TestUtil {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Equivalent to {@code buildTestData(length, length)}.
|
||||||
|
*
|
||||||
|
* @param length The length of the array.
|
||||||
|
* @return The generated array.
|
||||||
|
*/
|
||||||
public static byte[] buildTestData(int length) {
|
public static byte[] buildTestData(int length) {
|
||||||
return buildTestData(length, length);
|
return buildTestData(length, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates an array of random bytes with the specified length.
|
||||||
|
*
|
||||||
|
* @param length The length of the array.
|
||||||
|
* @param seed A seed for an internally created {@link Random source of randomness}.
|
||||||
|
* @return The generated array.
|
||||||
|
*/
|
||||||
public static byte[] buildTestData(int length, int seed) {
|
public static byte[] buildTestData(int length, int seed) {
|
||||||
return buildTestData(length, new Random(seed));
|
return buildTestData(length, new Random(seed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates an array of random bytes with the specified length.
|
||||||
|
*
|
||||||
|
* @param length The length of the array.
|
||||||
|
* @param random A source of randomness.
|
||||||
|
* @return The generated array.
|
||||||
|
*/
|
||||||
public static byte[] buildTestData(int length, Random random) {
|
public static byte[] buildTestData(int length, Random random) {
|
||||||
byte[] source = new byte[length];
|
byte[] source = new byte[length];
|
||||||
random.nextBytes(source);
|
random.nextBytes(source);
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String buildTestString(int maxLength, Random random) {
|
/**
|
||||||
int length = random.nextInt(maxLength);
|
* Generates a random string with the specified maximum length.
|
||||||
|
*
|
||||||
|
* @param maximumLength The maximum length of the string.
|
||||||
|
* @param random A source of randomness.
|
||||||
|
* @return The generated string.
|
||||||
|
*/
|
||||||
|
public static String buildTestString(int maximumLength, Random random) {
|
||||||
|
int length = random.nextInt(maximumLength);
|
||||||
StringBuilder builder = new StringBuilder(length);
|
StringBuilder builder = new StringBuilder(length);
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
builder.append((char) random.nextInt());
|
builder.append((char) random.nextInt());
|
||||||
|
|
@ -129,6 +160,12 @@ public class TestUtil {
|
||||||
return byteArray;
|
return byteArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Concatenates the provided byte arrays.
|
||||||
|
*
|
||||||
|
* @param byteArrays The byte arrays to concatenate.
|
||||||
|
* @return The concatenated result.
|
||||||
|
*/
|
||||||
public static byte[] joinByteArrays(byte[]... byteArrays) {
|
public static byte[] joinByteArrays(byte[]... byteArrays) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
for (byte[] byteArray : byteArrays) {
|
for (byte[] byteArray : byteArrays) {
|
||||||
|
|
@ -143,24 +180,28 @@ public class TestUtil {
|
||||||
return joined;
|
return joined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the bytes of an asset file. */
|
||||||
public static byte[] getByteArray(Context context, String fileName) throws IOException {
|
public static byte[] getByteArray(Context context, String fileName) throws IOException {
|
||||||
return Util.toByteArray(getInputStream(context, fileName));
|
return Util.toByteArray(getInputStream(context, fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns an {@link InputStream} for reading from an asset file. */
|
||||||
public static InputStream getInputStream(Context context, String fileName) throws IOException {
|
public static InputStream getInputStream(Context context, String fileName) throws IOException {
|
||||||
return context.getResources().getAssets().open(fileName);
|
return context.getResources().getAssets().open(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns a {@link String} read from an asset file. */
|
||||||
public static String getString(Context context, String fileName) throws IOException {
|
public static String getString(Context context, String fileName) throws IOException {
|
||||||
return Util.fromUtf8Bytes(getByteArray(context, fileName));
|
return Util.fromUtf8Bytes(getByteArray(context, fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Bitmap readBitmapFromFile(Context context, String fileName) throws IOException {
|
/** Returns a {@link Bitmap} read from an asset file. */
|
||||||
|
public static Bitmap getBitmap(Context context, String fileName) throws IOException {
|
||||||
return BitmapFactory.decodeStream(getInputStream(context, fileName));
|
return BitmapFactory.decodeStream(getInputStream(context, fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DatabaseProvider getTestDatabaseProvider() {
|
/** Returns a {@link DatabaseProvider} that provides an in-memory database. */
|
||||||
// Provides an in-memory database.
|
public static DatabaseProvider getInMemoryDatabaseProvider() {
|
||||||
return new DefaultDatabaseProvider(
|
return new DefaultDatabaseProvider(
|
||||||
new SQLiteOpenHelper(
|
new SQLiteOpenHelper(
|
||||||
/* context= */ null, /* name= */ null, /* factory= */ null, /* version= */ 1) {
|
/* context= */ null, /* name= */ null, /* factory= */ null, /* version= */ 1) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue