TestUtil
@UnstableApi
public class TestUtil
Utility methods for tests.
Summary
Constants |
|
|---|---|
static final float |
PSNR_THRESHOLD = 35.0fLuma PSNR values between 30 and 50 are considered good for lossy compression (See Quality estimation with PSNR ). |
Public methods |
|
|---|---|
static void |
assertBitmapsAreSimilar(Asserts whether actual bitmap is very similar to the expected bitmap at some quality level. |
static void |
assertBufferInfosEqual(Returns whether two |
static void |
assertDataSourceContent(Asserts that data read from a |
static void |
<T extends Object, F extends T> assertForwardingClassForwardsAllMethods(Use reflection to assert that calling every non-final method declared on |
static void |
<T extends Object, F extends T> assertForwardingClassForwardsAllMethodsExcept(Use reflection to assert that calling every non-final, non-excluded method declared on |
static void |
<T> assertSubclassOverridesAllMethods(Use reflection to assert that every non-final method declared on |
static void |
<T> assertSubclassOverridesAllMethodsExcept(Use reflection to assert that every non-final, non-excluded method declared on |
static void |
assertTimelinesSame(Asserts that the actual timelines are the same to the expected timelines. |
static Uri |
buildAssetUri(String assetPath)Returns the |
static float[] |
buildFloatTestSamples(int length, Random random)Returns an array of random floats between |
static MediaItem |
Returns a |
static byte[] |
buildTestData(int length)Equivalent to |
static byte[] |
buildTestData(int length, Random random)Generates an array of random bytes with the specified length. |
static byte[] |
buildTestData(int length, int seed)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(ByteBuffer byteBuffer)Gets the underlying data of the |
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 a |
static ByteBuffer |
createByteBuffer(int[] data)Creates a |
static ByteBuffer |
createByteBuffer(short[] data)Creates a |
static ByteBuffer |
createByteBuffer(byte[] data)Creates a |
static AudioProcessor |
createByteCountingAudioProcessor(AtomicInteger byteCount)Returns an |
static ImmutableList<Byte> |
createByteList(int[] bytes)Converts an array of integers in the range [0, 255] into an equivalent byte list. |
static File |
createExternalCacheFile(Context context, String fileName)Creates a |
static File |
createExternalCacheFile(Creates a |
static float[] |
createFloatArray(ByteBuffer byteBuffer)Gets the underlying data of the |
static int[] |
createInt24Array(ByteBuffer byteBuffer)Gets the underlying data of the |
static ByteBuffer |
createInt24ByteBuffer(int[] data)Creates a |
static int[] |
createIntArray(ByteBuffer byteBuffer)Gets the underlying data of the |
static MetadataInputBuffer |
createMetadataInputBuffer(byte[] data)Create a new |
static short[] |
createShortArray(ByteBuffer byteBuffer)Gets the underlying data of the |
static SurfaceView |
createSurfaceView(Context context)Creates a |
static File |
createTestFile(File directory, String name)Writes one byte long test data to the file and returns it. |
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, long length)Writes test data with the specified length to the file and returns it. |
static FakeExtractorOutput |
extractAllSamplesFromByteArray(Extractor extractor, byte[] data)Extracts all samples from the given byte array into a |
static FakeExtractorOutput |
extractAllSamplesFromFile(Extracts all samples from the given file into a |
static FakeExtractorOutput |
extractAllSamplesFromFilePath(Extractor extractor, String filePath)Extracts all samples from the given file into a |
static SeekMap |
extractSeekMap(Reads from the given input using the given |
static float |
generateFloatInRange(Random random, Range<Float> range)Returns a randomly generated float within the specified range, using |
static long |
generateLong(Random random, long origin, long bound)Returns a long between |
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 ImmutableList<@Player.Command Integer> |
getCommandsAsList(Player.Commands commands)Returns an |
static ImmutableList<@Player.Event Integer> |
getEventsAsList(Player.Events events)Returns an |
static ExtractorInput |
getExtractorInputFromPosition(Returns an |
static DatabaseProvider |
Returns a |
static @Nullable Class<Object> |
getInnerClass(Class<Object> clazz, String className)Returns an inner class of |
static InputStream |
getInputStream(Context context, String fileName)Returns an |
static ByteBuffer |
getNonRandomByteBuffer(int frameCount, int bytesPerFrame)Returns a non-random |
static ByteBuffer |
getPeriodicSamplesBuffer(int sampleCount, int period)Returns a |
static List<Method> |
getPublicMethods(Class<Object> clazz)Returns all the public methods of a Java class (except those defined by |
static Iterable<Method> |
getPublicOverridableMethods(Class<Object> clazz)Returns all the public overridable methods of a Java class (except those defined by |
static String |
Returns a |
static Bundle |
Returns a |
static void |
repeatFlakyTest(int maxRepetitions, ThrowingRunnable testImpl)Repeats the potentially flaky test multiple times if needed. |
static Format |
retrieveTrackFormat(Returns the |
static int |
seekToTimeUs(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 if |
Constants
PSNR_THRESHOLD
public static final float PSNR_THRESHOLD = 35.0f
Luma PSNR values between 30 and 50 are considered good for lossy compression (See Quality estimation with PSNR ).
Public methods
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.
assertBufferInfosEqual
public static void assertBufferInfosEqual(
MediaCodec.BufferInfo expected,
MediaCodec.BufferInfo actual
)
Returns whether two BufferInfos are equal.
assertDataSourceContent
public static void assertDataSourceContent(
DataSource dataSource,
DataSpec dataSpec,
byte[] expectedData,
boolean expectKnownLength
)
Asserts that data read from a DataSource matches expected.
| Parameters | |
|---|---|
DataSource dataSource |
The |
DataSpec dataSpec |
The |
byte[] expectedData |
The expected data. |
boolean expectKnownLength |
Whether to assert that |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs reading fom the |
assertForwardingClassForwardsAllMethods
public static void <T extends Object, F extends T> assertForwardingClassForwardsAllMethods(
Class<T> superType,
Function<T, F> forwardingInstanceFactory
)
Use reflection to assert that calling every non-final method declared on superType on an instance of forwardingType results in the call being forwarded to the
superType delegate.
assertForwardingClassForwardsAllMethodsExcept
public static void <T extends Object, F extends T> assertForwardingClassForwardsAllMethodsExcept(
Class<T> superType,
Function<T, F> forwardingInstanceFactory,
Set<String> excludedMethods
)
Use reflection to assert that calling every non-final, non-excluded method declared on
superType on an instance of forwardingType results in the call being forwarded to the superType delegate.
assertSubclassOverridesAllMethods
public static void <T> assertSubclassOverridesAllMethods(
Class<T> superType,
Class<T> subType
)
Use reflection to assert that every non-final method declared on superType is overridden by subType.
assertSubclassOverridesAllMethodsExcept
public static void <T> assertSubclassOverridesAllMethodsExcept(
Class<T> superType,
Class<T> subType,
Set<String> excludedMethods
)
Use reflection to assert that every non-final, non-excluded method declared on
superType is overridden by subType.
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.
buildAssetUri
public static Uri buildAssetUri(String assetPath)
Returns the Uri for the given asset path.
buildFloatTestSamples
public static float[] buildFloatTestSamples(int length, Random random)
Returns an array of random floats between [-1; 1] with the specified length.
buildFullyCustomizedMediaItem
public static MediaItem buildFullyCustomizedMediaItem()
Returns a MediaItem that has all fields set to non-default values.
buildTestData
public static byte[] buildTestData(int length)
Equivalent to buildTestData(length, length).
| Parameters | |
|---|---|
int length |
The length of the array. |
| Returns | |
|---|---|
byte[] |
The generated array. |
buildTestData
public static byte[] buildTestData(int length, Random random)
Generates an array of random bytes with the specified length.
| Parameters | |
|---|---|
int length |
The length of the array. |
Random random |
A source of randomness. |
| Returns | |
|---|---|
byte[] |
The generated array. |
buildTestData
public static byte[] buildTestData(int length, int seed)
Generates an array of random bytes with the specified length.
| Parameters | |
|---|---|
int length |
The length of the array. |
int seed |
A seed for an internally created |
| Returns | |
|---|---|
byte[] |
The generated array. |
buildTestString
public static String buildTestString(int length, Random random)
Generates a random string with the specified length.
| Parameters | |
|---|---|
int length |
The length of the string. |
Random random |
A source of randomness. |
| Returns | |
|---|---|
String |
The generated string. |
createByteArray
public static byte[] createByteArray(ByteBuffer byteBuffer)
Gets the underlying data of the ByteBuffer as a byte[].
createByteArray
public static byte[] createByteArray(int[] bytes)
Converts an array of integers in the range [0, 255] into an equivalent byte array.
| Parameters | |
|---|---|
int[] bytes |
An array of integers, all of which must be in the range [0, 255]. |
| Returns | |
|---|---|
byte[] |
The equivalent byte array. |
createByteBuffer
public static ByteBuffer createByteBuffer(float[] data)
Creates a ByteBuffer containing the data.
createByteBuffer
public static ByteBuffer createByteBuffer(int[] data)
Creates a ByteBuffer containing the data.
createByteBuffer
public static ByteBuffer createByteBuffer(short[] data)
Creates a ByteBuffer containing the data.
createByteBuffer
public static ByteBuffer createByteBuffer(byte[] data)
Creates a ByteBuffer containing the data.
createByteCountingAudioProcessor
public static AudioProcessor createByteCountingAudioProcessor(AtomicInteger byteCount)
Returns an AudioProcessor that counts the number of bytes input to it.
createByteList
public static ImmutableList<Byte> createByteList(int[] bytes)
Converts an array of integers in the range [0, 255] into an equivalent byte list.
| Parameters | |
|---|---|
int[] bytes |
An array of integers, all of which must be in the range [0, 255]. |
| Returns | |
|---|---|
ImmutableList<Byte> |
The equivalent byte list. |
createExternalCacheFile
public static File createExternalCacheFile(Context context, String fileName)
Creates a File of the fileName in the application cache directory.
If a file of that name already exists, it is overwritten.
| Throws | |
|---|---|
java.io.IOException |
createExternalCacheFile
public static File createExternalCacheFile(
Context context,
String directoryName,
String fileName
)
Creates a File of the fileName in a directory directoryName within the application cache directory.
If a file of that name already exists, it is overwritten.
| Parameters | |
|---|---|
Context context |
The |
String directoryName |
The directory name within the external cache to save the file in. |
String fileName |
The filename to save to the cache. |
| Throws | |
|---|---|
java.io.IOException |
createFloatArray
public static float[] createFloatArray(ByteBuffer byteBuffer)
Gets the underlying data of the ByteBuffer as a float[].
createInt24Array
public static int[] createInt24Array(ByteBuffer byteBuffer)
Gets the underlying data of the ByteBuffer as 24-bit integer values in int[].
createInt24ByteBuffer
public static ByteBuffer createInt24ByteBuffer(int[] data)
Creates a ByteBuffer with the contents of data as 24-bit integers.
createIntArray
public static int[] createIntArray(ByteBuffer byteBuffer)
Gets the underlying data of the ByteBuffer as a int[].
createMetadataInputBuffer
public static MetadataInputBuffer createMetadataInputBuffer(byte[] data)
Create a new MetadataInputBuffer and copy data into the backing ByteBuffer.
createShortArray
public static short[] createShortArray(ByteBuffer byteBuffer)
Gets the underlying data of the ByteBuffer as a short[].
createSurfaceView
public static SurfaceView createSurfaceView(Context context)
Creates a SurfaceView for tests where the creation is moved to the main thread if run on a non-Looper thread. This is needed on API <26 where SurfaceView cannot be created on a non-Looper thread.
createTestFile
public static File createTestFile(File directory, String name)
Writes one byte long test data to the file and returns it.
| Throws | |
|---|---|
java.io.IOException |
createTestFile
public static File createTestFile(File file, long length)
Writes test data with the specified length to the file and returns it.
| Throws | |
|---|---|
java.io.IOException |
createTestFile
public static File createTestFile(File directory, String name, long length)
Writes test data with the specified length to the file and returns it.
| Throws | |
|---|---|
java.io.IOException |
extractAllSamplesFromByteArray
public static FakeExtractorOutput extractAllSamplesFromByteArray(Extractor extractor, byte[] data)
Extracts all samples from the given byte array into a FakeTrackOutput.
| Returns | |
|---|---|
FakeExtractorOutput |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any |
extractAllSamplesFromFile
public static FakeExtractorOutput extractAllSamplesFromFile(
Extractor extractor,
Context context,
String fileName
)
Extracts all samples from the given file into a FakeTrackOutput.
| Parameters | |
|---|---|
Extractor extractor |
The |
Context context |
A |
String fileName |
The name of the input file. |
| Returns | |
|---|---|
FakeExtractorOutput |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any |
extractAllSamplesFromFilePath
public static FakeExtractorOutput extractAllSamplesFromFilePath(Extractor extractor, String filePath)
Extracts all samples from the given file into a FakeTrackOutput.
| Returns | |
|---|---|
FakeExtractorOutput |
The |
| Throws | |
|---|---|
java.io.IOException |
If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any |
extractSeekMap
public static SeekMap extractSeekMap(
Extractor extractor,
FakeExtractorOutput output,
DataSource dataSource,
Uri uri
)
Reads from the given input using the given Extractor, until it can produce the SeekMap and all of the track formats have been identified, or until the extractor encounters EOF.
| Parameters | |
|---|---|
Extractor extractor |
The |
FakeExtractorOutput output |
The |
DataSource dataSource |
The |
Uri uri |
The Uri of the input. |
| Throws | |
|---|---|
java.io.IOException |
If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any |
generateFloatInRange
public static float generateFloatInRange(Random random, Range<Float> range)
Returns a randomly generated float within the specified range, using random as random number generator.
range must be a bounded range.
generateLong
public static long generateLong(Random random, long origin, long bound)
Returns a long between origin (inclusive) and bound (exclusive), given
random.
getByteArray
public static byte[] getByteArray(Context context, String fileName)
Returns the bytes of an asset file.
| Throws | |
|---|---|
java.io.IOException |
getByteArrayFromFilePath
public static byte[] getByteArrayFromFilePath(String filePath)
Returns the bytes of a file using its file path.
| Throws | |
|---|---|
java.io.IOException |
getCommandsAsList
public static ImmutableList<@Player.Command Integer> getCommandsAsList(Player.Commands commands)
Returns an ImmutableList with the Commands contained in commands. The contents of the list are in matching order with the Commands returned by get.
getEventsAsList
public static ImmutableList<@Player.Event Integer> getEventsAsList(Player.Events events)
Returns an ImmutableList with the Events contained in
events. The contents of the list are in matching order with the Events returned by get.
getExtractorInputFromPosition
public static ExtractorInput getExtractorInputFromPosition(
DataSource dataSource,
long position,
Uri uri
)
Returns an ExtractorInput to read from the given input at given position.
| Throws | |
|---|---|
java.io.IOException |
getInMemoryDatabaseProvider
public static DatabaseProvider getInMemoryDatabaseProvider()
Returns a DatabaseProvider that provides an in-memory database.
getInnerClass
public static @Nullable Class<Object> getInnerClass(Class<Object> clazz, String className)
Returns an inner class of clazz called className if it exists, otherwise null.
getInputStream
public static InputStream getInputStream(Context context, String fileName)
Returns an InputStream for reading from an asset file.
| Throws | |
|---|---|
java.io.IOException |
getNonRandomByteBuffer
public static ByteBuffer getNonRandomByteBuffer(int frameCount, int bytesPerFrame)
Returns a non-random ByteBuffer filled with frameCount * bytesPerFrame bytes.
getPeriodicSamplesBuffer
public static ByteBuffer getPeriodicSamplesBuffer(int sampleCount, int period)
Returns a ByteBuffer filled with alternating 16-bit PCM samples as per the provided period length.
The generated samples alternate between MAX_VALUE and MIN_VALUE every period / 2 samples.
| Parameters | |
|---|---|
int sampleCount |
Number of total PCM samples (not frames) to generate. |
int period |
Length in PCM samples of one full cycle. |
getPublicMethods
public static List<Method> getPublicMethods(Class<Object> clazz)
Returns all the public methods of a Java class (except those defined by Object).
getPublicOverridableMethods
public static Iterable<Method> getPublicOverridableMethods(Class<Object> clazz)
Returns all the public overridable methods of a Java class (except those defined by Object).
getString
public static String getString(Context context, String fileName)
Returns a String read from an asset file.
| Throws | |
|---|---|
java.io.IOException |
getThrowingBundle
public static Bundle getThrowingBundle()
Returns a Bundle that will throw an exception at the first attempt to read a value.
repeatFlakyTest
public static void repeatFlakyTest(int maxRepetitions, ThrowingRunnable testImpl)
Repeats the potentially flaky test multiple times if needed.
Only use this method for tests with inherent randomness or systems-under-test that are not fully controllable, and where the reason for the the flakiness can be explained. Don't use this method if the test should always pass reliably.
| Parameters | |
|---|---|
int maxRepetitions |
The maximum number of repetitions before failing the test. |
ThrowingRunnable testImpl |
The test implementation. |
retrieveTrackFormat
public static Format retrieveTrackFormat(
Context context,
String fileUri,
@C.TrackType int trackType
)
Returns the Format for a given C.TrackType from a media file.
If more than one track is present for the given C.TrackType then only one track's Format is returned.
| Parameters | |
|---|---|
Context context |
The |
String fileUri |
The media file uri. |
@C.TrackType int trackType |
The |
| Returns | |
|---|---|
Format |
The |
| Throws | |
|---|---|
java.util.concurrent.ExecutionException |
If an error occurred while retrieving file's metadata. |
java.lang.InterruptedException |
If interrupted while retrieving file's metadata. |
seekToTimeUs
public 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.
| Parameters | |
|---|---|
Extractor extractor |
The |
SeekMap seekMap |
The |
long seekTimeUs |
The seek time, in micro-seconds. |
DataSource dataSource |
The |
FakeTrackOutput trackOutput |
The |
Uri uri |
The Uri of the input. |
| Returns | |
|---|---|
int |
The index of the first extracted sample written to the given |
| Throws | |
|---|---|
java.io.IOException |
timelinesAreSame
public static boolean timelinesAreSame(Timeline thisTimeline, Timeline thatTimeline)
Returns true if thisTimeline is equal to thatTimeline, ignoring uid and uid values, and shuffle order.