Class TestUtil
- java.lang.Object
-
- com.google.android.exoplayer2.testutil.TestUtil
-
public class TestUtil extends Object
Utility methods for tests.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
assertBitmapsAreSimilar(Bitmap expectedBitmap, Bitmap actualBitmap, double psnrThresholdDb)
Asserts whether actual bitmap is very similar to the expected bitmap at some quality level.static void
assertBufferInfosEqual(MediaCodec.BufferInfo expected, MediaCodec.BufferInfo actual)
Returns whether twoBufferInfos
are equal.static void
assertDataSourceContent(DataSource dataSource, DataSpec dataSpec, byte[] expectedData, boolean expectKnownLength)
Asserts that data read from aDataSource
matchesexpected
.static void
assertTimelinesSame(List<Timeline> actualTimelines, List<Timeline> expectedTimelines)
Asserts that the actual timelines are the same to the expected timelines.static Uri
buildAssetUri(String assetPath)
Returns theUri
for the given asset path.static byte[]
buildTestData(int length)
Equivalent tobuildTestData(length, length)
.static byte[]
buildTestData(int length, int seed)
Generates an array of random bytes with the specified length.static byte[]
buildTestData(int length, Random random)
Generates an array of random bytes with the specified length.static String
buildTestString(int length, Random random)
Generates a random string with the specified length.static byte[]
createByteArray(int... bytes)
Converts an array of integers in the range [0, 255] into an equivalent byte array.static ByteBuffer
createByteBuffer(float[] data)
Creates aByteBuffer
containing thedata
.static ByteBuffer
createByteBuffer(short[] data)
Creates aByteBuffer
containing thedata
.static ImmutableList<Byte>
createByteList(int... bytes)
Converts an array of integers in the range [0, 255] into an equivalent byte list.static float[]
createFloatArray(ByteBuffer byteBuffer)
Gets the underlying data of theByteBuffer
as afloat[]
.static MetadataInputBuffer
createMetadataInputBuffer(byte[] data)
static File
createTestFile(File file, long length)
Writes test data with the specified length to the file and returns it.static File
createTestFile(File directory, String name)
Writes one byte long test data to the file and returns it.static File
createTestFile(File directory, String name, long length)
Writes test data with the specified length to the file and returns it.static FakeExtractorOutput
extractAllSamplesFromFile(Extractor extractor, Context context, String fileName)
Extracts all samples from the given file into aFakeTrackOutput
.static FakeExtractorOutput
extractAllSamplesFromFilePath(Extractor extractor, String filePath)
Extracts all samples from the given file into aFakeTrackOutput
.static SeekMap
extractSeekMap(Extractor extractor, FakeExtractorOutput output, DataSource dataSource, Uri uri)
static Bitmap
getBitmap(Context context, String fileName)
Returns aBitmap
read from an asset file.static byte[]
getByteArray(Context context, String fileName)
Returns the bytes of an asset file.static byte[]
getByteArrayFromFilePath(String filePath)
Returns the bytes of a file using its file path.static ExtractorInput
getExtractorInputFromPosition(DataSource dataSource, long position, Uri uri)
Returns anExtractorInput
to read from the given input at given position.static DatabaseProvider
getInMemoryDatabaseProvider()
Returns aDatabaseProvider
that provides an in-memory database.static InputStream
getInputStream(Context context, String fileName)
Returns anInputStream
for reading from an asset file.static List<Method>
getPublicMethods(Class<?> clazz)
Returns all the public methods of a Java class (except those defined byObject
).static String
getString(Context context, String fileName)
Returns aString
read from an asset file.static int
seekToTimeUs(Extractor extractor, SeekMap seekMap, long seekTimeUs, DataSource dataSource, FakeTrackOutput trackOutput, Uri uri)
Seeks to the given seek time of the stream from the given input, and keeps reading from the input until we can extract at least one sample following the seek position, or until end-of-input is reached.static boolean
timelinesAreSame(Timeline thisTimeline, Timeline thatTimeline)
Returns true ifthisTimeline
is equal tothatTimeline
, ignoringTimeline.Window.uid
andTimeline.Period.uid
values, and shuffle order.
-
-
-
Method Detail
-
buildTestData
public static byte[] buildTestData(int length)
Equivalent tobuildTestData(length, length)
.- Parameters:
length
- The length of the array.- Returns:
- The generated array.
-
buildTestData
public static byte[] buildTestData(int length, int seed)
Generates an array of random bytes with the specified length.- Parameters:
length
- The length of the array.seed
- A seed for an internally createdsource of randomness
.- Returns:
- The generated array.
-
buildTestData
public static byte[] buildTestData(int length, Random random)
Generates an array of random bytes with the specified length.- Parameters:
length
- The length of the array.random
- A source of randomness.- Returns:
- The generated array.
-
buildTestString
public static String buildTestString(int length, Random random)
Generates a random string with the specified length.- Parameters:
length
- The length of the string.random
- A source of randomness.- Returns:
- The generated string.
-
createByteArray
public static byte[] createByteArray(int... bytes)
Converts an array of integers in the range [0, 255] into an equivalent byte array.- Parameters:
bytes
- An array of integers, all of which must be in the range [0, 255].- Returns:
- The equivalent byte array.
-
createFloatArray
public static float[] createFloatArray(ByteBuffer byteBuffer)
Gets the underlying data of theByteBuffer
as afloat[]
.
-
createByteBuffer
public static ByteBuffer createByteBuffer(float[] data)
Creates aByteBuffer
containing thedata
.
-
createByteBuffer
public static ByteBuffer createByteBuffer(short[] data)
Creates aByteBuffer
containing thedata
.
-
createByteList
public static ImmutableList<Byte> createByteList(int... bytes)
Converts an array of integers in the range [0, 255] into an equivalent byte list.- Parameters:
bytes
- An array of integers, all of which must be in the range [0, 255].- Returns:
- The equivalent byte list.
-
createTestFile
public static File createTestFile(File directory, String name) throws IOException
Writes one byte long test data to the file and returns it.- Throws:
IOException
-
createTestFile
public static File createTestFile(File directory, String name, long length) throws IOException
Writes test data with the specified length to the file and returns it.- Throws:
IOException
-
createTestFile
public static File createTestFile(File file, long length) throws IOException
Writes test data with the specified length to the file and returns it.- Throws:
IOException
-
getByteArray
public static byte[] getByteArray(Context context, String fileName) throws IOException
Returns the bytes of an asset file.- Throws:
IOException
-
getByteArrayFromFilePath
public static byte[] getByteArrayFromFilePath(String filePath) throws IOException
Returns the bytes of a file using its file path.- Throws:
IOException
-
getInputStream
public static InputStream getInputStream(Context context, String fileName) throws IOException
Returns anInputStream
for reading from an asset file.- Throws:
IOException
-
getString
public static String getString(Context context, String fileName) throws IOException
Returns aString
read from an asset file.- Throws:
IOException
-
getBitmap
public static Bitmap getBitmap(Context context, String fileName) throws IOException
Returns aBitmap
read from an asset file.- Throws:
IOException
-
getInMemoryDatabaseProvider
public static DatabaseProvider getInMemoryDatabaseProvider()
Returns aDatabaseProvider
that provides an in-memory database.
-
assertTimelinesSame
public static void assertTimelinesSame(List<Timeline> actualTimelines, List<Timeline> expectedTimelines)
Asserts that the actual timelines are the same to the expected timelines. This assert differs from testing equality by not comparing:- Period IDs, which may be different due to ID mapping of child source period IDs.
- Shuffle order, which by default is random and non-deterministic.
-
timelinesAreSame
public static boolean timelinesAreSame(Timeline thisTimeline, Timeline thatTimeline)
Returns true ifthisTimeline
is equal tothatTimeline
, ignoringTimeline.Window.uid
andTimeline.Period.uid
values, and shuffle order.
-
assertDataSourceContent
public static void assertDataSourceContent(DataSource dataSource, DataSpec dataSpec, byte[] expectedData, boolean expectKnownLength) throws IOException
Asserts that data read from aDataSource
matchesexpected
.- Parameters:
dataSource
- TheDataSource
through which to read.dataSpec
- TheDataSpec
to use when opening theDataSource
.expectedData
- The expected data.expectKnownLength
- Whether to assert thatDataSource.open(com.google.android.exoplayer2.upstream.DataSpec)
returns the expected data length. If false then it's asserted thatC.LENGTH_UNSET
is returned.- Throws:
IOException
- If an error occurs reading fom theDataSource
.
-
assertBufferInfosEqual
public static void assertBufferInfosEqual(MediaCodec.BufferInfo expected, MediaCodec.BufferInfo actual)
Returns whether twoBufferInfos
are equal.
-
assertBitmapsAreSimilar
public static void assertBitmapsAreSimilar(Bitmap expectedBitmap, Bitmap actualBitmap, double psnrThresholdDb)
Asserts whether actual bitmap is very similar to the expected bitmap at some quality level.This is defined as their PSNR value is greater than or equal to the threshold. The higher the threshold, the more similar they are.
- Parameters:
expectedBitmap
- The expected bitmap.actualBitmap
- The actual bitmap.psnrThresholdDb
- The PSNR threshold (in dB), at or above which bitmaps are considered very similar.
-
buildAssetUri
public static Uri buildAssetUri(String assetPath)
Returns theUri
for the given asset path.
-
extractSeekMap
public static SeekMap extractSeekMap(Extractor extractor, FakeExtractorOutput output, DataSource dataSource, Uri uri) throws IOException
Reads from the given input using the givenExtractor
, until it can produce theSeekMap
and all of the track formats have been identified, or until the extractor encounters EOF.- Parameters:
extractor
- TheExtractor
to extractor from input.output
- TheFakeTrackOutput
to store the extractedSeekMap
and track.dataSource
- TheDataSource
that will be used to read from the input.uri
- The Uri of the input.- Returns:
- The extracted
SeekMap
. - Throws:
IOException
- If an error occurred reading from the input, or if the extractor finishes reading from input without extracting anySeekMap
.
-
extractAllSamplesFromFile
public static FakeExtractorOutput extractAllSamplesFromFile(Extractor extractor, Context context, String fileName) throws IOException
Extracts all samples from the given file into aFakeTrackOutput
.- Parameters:
extractor
- TheExtractor
to be used.context
- AContext
.fileName
- The name of the input file.- Returns:
- The
FakeTrackOutput
containing the extracted samples. - Throws:
IOException
- If an error occurred reading from the input, or if the extractor finishes reading from input without extracting anySeekMap
.
-
extractAllSamplesFromFilePath
public static FakeExtractorOutput extractAllSamplesFromFilePath(Extractor extractor, String filePath) throws IOException
Extracts all samples from the given file into aFakeTrackOutput
.- Parameters:
extractor
- TheExtractor
to be used.filePath
- The file path.- Returns:
- The
FakeTrackOutput
containing the extracted samples. - Throws:
IOException
- If an error occurred reading from the input, or if the extractor finishes reading from input without extracting anySeekMap
.
-
seekToTimeUs
public static int seekToTimeUs(Extractor extractor, SeekMap seekMap, long seekTimeUs, DataSource dataSource, FakeTrackOutput trackOutput, Uri uri) throws IOException
Seeks to the given seek time of the stream from the given input, and keeps reading from the input until we can extract at least one sample following the seek position, or until end-of-input is reached.- Parameters:
extractor
- TheExtractor
to extract from input.seekMap
- TheSeekMap
of the stream from the given input.seekTimeUs
- The seek time, in micro-seconds.trackOutput
- TheFakeTrackOutput
to store the extracted samples.dataSource
- TheDataSource
that will be used to read from the input.uri
- The Uri of the input.- Returns:
- The index of the first extracted sample written to the given
trackOutput
after the seek is completed, orC.INDEX_UNSET
if the seek is completed without any extracted sample. - Throws:
IOException
-
getExtractorInputFromPosition
public static ExtractorInput getExtractorInputFromPosition(DataSource dataSource, long position, Uri uri) throws IOException
Returns anExtractorInput
to read from the given input at given position.- Throws:
IOException
-
createMetadataInputBuffer
public static MetadataInputBuffer createMetadataInputBuffer(byte[] data)
-
-