mirror of
https://github.com/samsonjs/media.git
synced 2026-04-10 12:05:47 +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
|
||||
public void setUp() {
|
||||
databaseProvider = TestUtil.getTestDatabaseProvider();
|
||||
databaseProvider = TestUtil.getInMemoryDatabaseProvider();
|
||||
database = databaseProvider.getWritableDatabase();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package com.google.android.exoplayer2.extractor.ogg;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import com.google.android.exoplayer2.testutil.ExtractorAsserts;
|
||||
|
|
@ -59,7 +58,7 @@ public final class OggExtractorTest {
|
|||
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
||||
TestUtil.createByteArray(7), // Laces
|
||||
new byte[] {0x01, 'v', 'o', 'r', 'b', 'i', 's'});
|
||||
assertThat(sniff(data)).isTrue();
|
||||
assertSniff(data, /* expectedResult= */ true);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -69,7 +68,7 @@ public final class OggExtractorTest {
|
|||
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
||||
TestUtil.createByteArray(5), // Laces
|
||||
new byte[] {0x7F, 'F', 'L', 'A', 'C'});
|
||||
assertThat(sniff(data)).isTrue();
|
||||
assertSniff(data, /* expectedResult= */ true);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -77,13 +76,13 @@ public final class OggExtractorTest {
|
|||
byte[] data =
|
||||
TestUtil.joinByteArrays(
|
||||
OggTestData.buildOggHeader(0x02, 0, 1000, 0x00), new byte[] {'O', 'p', 'u', 's'});
|
||||
assertThat(sniff(data)).isFalse();
|
||||
assertSniff(data, /* expectedResult= */ false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSniffFailsInvalidOggHeader() throws Exception {
|
||||
byte[] data = OggTestData.buildOggHeader(0x00, 0, 1000, 0x00);
|
||||
assertThat(sniff(data)).isFalse();
|
||||
assertSniff(data, /* expectedResult= */ false);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -93,16 +92,17 @@ public final class OggExtractorTest {
|
|||
OggTestData.buildOggHeader(0x02, 0, 1000, 1),
|
||||
TestUtil.createByteArray(7), // Laces
|
||||
new byte[] {0x7F, 'X', 'o', 'r', 'b', 'i', 's'});
|
||||
assertThat(sniff(data)).isFalse();
|
||||
assertSniff(data, /* expectedResult= */ false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSniffFailsEOF() throws Exception {
|
||||
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 =
|
||||
new FakeExtractorInput.Builder()
|
||||
.setData(data)
|
||||
|
|
@ -110,6 +110,6 @@ public final class OggExtractorTest {
|
|||
.setSimulateUnknownLength(true)
|
||||
.setSimulatePartialReads(true)
|
||||
.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");
|
||||
uri3 = Uri.parse("http://abc.com/media3");
|
||||
dummyMainThread = new DummyMainThread();
|
||||
downloadIndex = new DefaultDownloadIndex(TestUtil.getTestDatabaseProvider());
|
||||
downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||
downloaderFactory = new FakeDownloaderFactory();
|
||||
setUpDownloadManager(100);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -334,7 +334,7 @@ public class CachedContentIndexTest {
|
|||
}
|
||||
|
||||
private CachedContentIndex newInstance() {
|
||||
return new CachedContentIndex(TestUtil.getTestDatabaseProvider());
|
||||
return new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||
}
|
||||
|
||||
private CachedContentIndex newLegacyInstance() {
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ public final class CachedRegionTrackerTest {
|
|||
tracker = new CachedRegionTracker(cache, CACHE_KEY, CHUNK_INDEX);
|
||||
cacheDir =
|
||||
Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
||||
index = new CachedContentIndex(TestUtil.getTestDatabaseProvider());
|
||||
index = new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public class SimpleCacheSpanTest {
|
|||
public void setUp() throws Exception {
|
||||
cacheDir =
|
||||
Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
|
||||
index = new CachedContentIndex(TestUtil.getTestDatabaseProvider());
|
||||
index = new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||
}
|
||||
|
||||
@After
|
||||
|
|
|
|||
|
|
@ -322,7 +322,7 @@ public class SimpleCacheTest {
|
|||
@Test
|
||||
public void testExceptionDuringEvictionByLeastRecentlyUsedCacheEvictorNotHang() throws Exception {
|
||||
CachedContentIndex contentIndex =
|
||||
Mockito.spy(new CachedContentIndex(TestUtil.getTestDatabaseProvider()));
|
||||
Mockito.spy(new CachedContentIndex(TestUtil.getInMemoryDatabaseProvider()));
|
||||
SimpleCache simpleCache =
|
||||
new SimpleCache(
|
||||
cacheDir, new LeastRecentlyUsedCacheEvictor(20), contentIndex, /* fileIndex= */ null);
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ public class DownloadManagerDashTest {
|
|||
|
||||
fakeStreamKey1 = new StreamKey(0, 0, 0);
|
||||
fakeStreamKey2 = new StreamKey(0, 1, 0);
|
||||
downloadIndex = new DefaultDownloadIndex(TestUtil.getTestDatabaseProvider());
|
||||
downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||
createDownloadManager();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ public class DownloadServiceDashTest {
|
|||
dummyMainThread.runTestOnMainThread(
|
||||
() -> {
|
||||
DefaultDownloadIndex downloadIndex =
|
||||
new DefaultDownloadIndex(TestUtil.getTestDatabaseProvider());
|
||||
new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
|
||||
final DownloadManager dashDownloadManager =
|
||||
new DownloadManager(
|
||||
ApplicationProvider.getApplicationContext(),
|
||||
|
|
|
|||
|
|
@ -467,6 +467,7 @@ public final class ActionSchedule {
|
|||
return apply(new ThrowPlaybackException(tag, exception));
|
||||
}
|
||||
|
||||
/** Builds the schedule. */
|
||||
public ActionSchedule build() {
|
||||
CallbackAction callbackAction = new CallbackAction(tag);
|
||||
apply(callbackAction);
|
||||
|
|
|
|||
|
|
@ -45,6 +45,29 @@ public final class ExtractorAsserts {
|
|||
private static final String DUMP_EXTENSION = ".dump";
|
||||
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
|
||||
* Robolectric tests.
|
||||
|
|
@ -164,7 +187,7 @@ public final class ExtractorAsserts {
|
|||
.setSimulatePartialReads(simulatePartialReads).build();
|
||||
|
||||
if (sniffFirst) {
|
||||
assertThat(TestUtil.sniffTestData(extractor, input)).isTrue();
|
||||
assertSniff(extractor, input, /* expectedResult= */ true);
|
||||
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.PositionHolder;
|
||||
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.DataSpec;
|
||||
import com.google.android.exoplayer2.util.Assertions;
|
||||
|
|
@ -50,17 +49,14 @@ public class TestUtil {
|
|||
|
||||
private TestUtil() {}
|
||||
|
||||
public static boolean sniffTestData(Extractor extractor, FakeExtractorInput input)
|
||||
throws IOException, InterruptedException {
|
||||
while (true) {
|
||||
try {
|
||||
return extractor.sniff(input);
|
||||
} catch (SimulatedIOException e) {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an open {@link DataSource}, repeatedly calls {@link DataSource#read(byte[], int, int)}
|
||||
* until {@link C#RESULT_END_OF_INPUT} is returned.
|
||||
*
|
||||
* @param dataSource The source from which to read.
|
||||
* @return The concatenation of all read data.
|
||||
* @throws IOException If an error occurs reading from the source.
|
||||
*/
|
||||
public static byte[] readToEnd(DataSource dataSource) throws IOException {
|
||||
byte[] data = new byte[1024];
|
||||
int position = 0;
|
||||
|
|
@ -77,6 +73,14 @@ public class TestUtil {
|
|||
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 {
|
||||
byte[] data = new byte[length];
|
||||
int position = 0;
|
||||
|
|
@ -91,22 +95,49 @@ public class TestUtil {
|
|||
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) {
|
||||
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) {
|
||||
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) {
|
||||
byte[] source = new byte[length];
|
||||
random.nextBytes(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);
|
||||
for (int i = 0; i < length; i++) {
|
||||
builder.append((char) random.nextInt());
|
||||
|
|
@ -129,6 +160,12 @@ public class TestUtil {
|
|||
return byteArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Concatenates the provided byte arrays.
|
||||
*
|
||||
* @param byteArrays The byte arrays to concatenate.
|
||||
* @return The concatenated result.
|
||||
*/
|
||||
public static byte[] joinByteArrays(byte[]... byteArrays) {
|
||||
int length = 0;
|
||||
for (byte[] byteArray : byteArrays) {
|
||||
|
|
@ -143,24 +180,28 @@ public class TestUtil {
|
|||
return joined;
|
||||
}
|
||||
|
||||
/** Returns the bytes of an asset file. */
|
||||
public static byte[] getByteArray(Context context, String fileName) throws IOException {
|
||||
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 {
|
||||
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 {
|
||||
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));
|
||||
}
|
||||
|
||||
public static DatabaseProvider getTestDatabaseProvider() {
|
||||
// Provides an in-memory database.
|
||||
/** Returns a {@link DatabaseProvider} that provides an in-memory database. */
|
||||
public static DatabaseProvider getInMemoryDatabaseProvider() {
|
||||
return new DefaultDatabaseProvider(
|
||||
new SQLiteOpenHelper(
|
||||
/* context= */ null, /* name= */ null, /* factory= */ null, /* version= */ 1) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue