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 voidassertBitmapsAreSimilar(Bitmap expectedBitmap, Bitmap actualBitmap, double psnrThresholdDb)Asserts whether actual bitmap is very similar to the expected bitmap at some quality level.static voidassertBufferInfosEqual(MediaCodec.BufferInfo expected, MediaCodec.BufferInfo actual)Returns whether twoBufferInfosare equal.static voidassertDataSourceContent(DataSource dataSource, DataSpec dataSpec, byte[] expectedData, boolean expectKnownLength)Asserts that data read from aDataSourcematchesexpected.static voidassertTimelinesSame(List<Timeline> actualTimelines, List<Timeline> expectedTimelines)Asserts that the actual timelines are the same to the expected timelines.static UribuildAssetUri(String assetPath)Returns theUrifor 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 StringbuildTestString(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 ByteBuffercreateByteBuffer(float[] data)Creates aByteBuffercontaining thedata.static ByteBuffercreateByteBuffer(short[] data)Creates aByteBuffercontaining 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 theByteBufferas afloat[].static MetadataInputBuffercreateMetadataInputBuffer(byte[] data)static FilecreateTestFile(File file, long length)Writes test data with the specified length to the file and returns it.static FilecreateTestFile(File directory, String name)Writes one byte long test data to the file and returns it.static FilecreateTestFile(File directory, String name, long length)Writes test data with the specified length to the file and returns it.static FakeExtractorOutputextractAllSamplesFromFile(Extractor extractor, Context context, String fileName)Extracts all samples from the given file into aFakeTrackOutput.static FakeExtractorOutputextractAllSamplesFromFilePath(Extractor extractor, String filePath)Extracts all samples from the given file into aFakeTrackOutput.static SeekMapextractSeekMap(Extractor extractor, FakeExtractorOutput output, DataSource dataSource, Uri uri)static BitmapgetBitmap(Context context, String fileName)Returns aBitmapread 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 ExtractorInputgetExtractorInputFromPosition(DataSource dataSource, long position, Uri uri)Returns anExtractorInputto read from the given input at given position.static DatabaseProvidergetInMemoryDatabaseProvider()Returns aDatabaseProviderthat provides an in-memory database.static InputStreamgetInputStream(Context context, String fileName)Returns anInputStreamfor 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 StringgetString(Context context, String fileName)Returns aStringread from an asset file.static intseekToTimeUs(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 booleantimelinesAreSame(Timeline thisTimeline, Timeline thatTimeline)Returns true ifthisTimelineis equal tothatTimeline, ignoringTimeline.Window.uidandTimeline.Period.uidvalues, 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 theByteBufferas afloat[].
-
createByteBuffer
public static ByteBuffer createByteBuffer(float[] data)
Creates aByteBuffercontaining thedata.
-
createByteBuffer
public static ByteBuffer createByteBuffer(short[] data)
Creates aByteBuffercontaining 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 anInputStreamfor reading from an asset file.- Throws:
IOException
-
getString
public static String getString(Context context, String fileName) throws IOException
Returns aStringread from an asset file.- Throws:
IOException
-
getBitmap
public static Bitmap getBitmap(Context context, String fileName) throws IOException
Returns aBitmapread from an asset file.- Throws:
IOException
-
getInMemoryDatabaseProvider
public static DatabaseProvider getInMemoryDatabaseProvider()
Returns aDatabaseProviderthat 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 ifthisTimelineis equal tothatTimeline, ignoringTimeline.Window.uidandTimeline.Period.uidvalues, and shuffle order.
-
assertDataSourceContent
public static void assertDataSourceContent(DataSource dataSource, DataSpec dataSpec, byte[] expectedData, boolean expectKnownLength) throws IOException
Asserts that data read from aDataSourcematchesexpected.- Parameters:
dataSource- TheDataSourcethrough which to read.dataSpec- TheDataSpecto 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_UNSETis returned.- Throws:
IOException- If an error occurs reading fom theDataSource.
-
assertBufferInfosEqual
public static void assertBufferInfosEqual(MediaCodec.BufferInfo expected, MediaCodec.BufferInfo actual)
Returns whether twoBufferInfosare 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 theUrifor 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 theSeekMapand all of the track formats have been identified, or until the extractor encounters EOF.- Parameters:
extractor- TheExtractorto extractor from input.output- TheFakeTrackOutputto store the extractedSeekMapand track.dataSource- TheDataSourcethat 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- TheExtractorto be used.context- AContext.fileName- The name of the input file.- Returns:
- The
FakeTrackOutputcontaining 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- TheExtractorto be used.filePath- The file path.- Returns:
- The
FakeTrackOutputcontaining 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- TheExtractorto extract from input.seekMap- TheSeekMapof the stream from the given input.seekTimeUs- The seek time, in micro-seconds.trackOutput- TheFakeTrackOutputto store the extracted samples.dataSource- TheDataSourcethat 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
trackOutputafter the seek is completed, orC.INDEX_UNSETif the seek is completed without any extracted sample. - Throws:
IOException
-
getExtractorInputFromPosition
public static ExtractorInput getExtractorInputFromPosition(DataSource dataSource, long position, Uri uri) throws IOException
Returns anExtractorInputto read from the given input at given position.- Throws:
IOException
-
createMetadataInputBuffer
public static MetadataInputBuffer createMetadataInputBuffer(byte[] data)
-
-