TestUtil
@UnstableApi
class TestUtil
Utility methods for tests.
Summary
Constants |
|
|---|---|
const Float |
PSNR_THRESHOLD = 35.0fLuma PSNR values between 30 and 50 are considered good for lossy compression (See Quality estimation with PSNR ). |
Public functions |
|
|---|---|
java-static Unit |
assertBitmapsAreSimilar(Asserts whether actual bitmap is very similar to the expected bitmap at some quality level. |
java-static Unit |
assertBufferInfosEqual(Returns whether two |
java-static Unit |
assertDataSourceContent(Asserts that data read from a |
java-static Unit |
<T : Any?, F : T?> assertForwardingClassForwardsAllMethods(Use reflection to assert that calling every non-final method declared on |
java-static Unit |
<T : Any?, F : T?> assertForwardingClassForwardsAllMethodsExcept(Use reflection to assert that calling every non-final, non-excluded method declared on |
java-static Unit |
<T> assertSubclassOverridesAllMethods(Use reflection to assert that every non-final method declared on |
java-static Unit |
<T> assertSubclassOverridesAllMethodsExcept(Use reflection to assert that every non-final, non-excluded method declared on |
java-static Unit |
assertTimelinesSame(Asserts that the actual timelines are the same to the expected timelines. |
java-static Uri! |
buildAssetUri(assetPath: String!)Returns the |
java-static FloatArray<Float>! |
buildFloatTestSamples(length: Int, random: Random!)Returns an array of random floats between |
java-static MediaItem! |
Returns a |
java-static ByteArray<Byte>! |
buildTestData(length: Int)Equivalent to |
java-static ByteArray<Byte>! |
buildTestData(length: Int, random: Random!)Generates an array of random bytes with the specified length. |
java-static ByteArray<Byte>! |
buildTestData(length: Int, seed: Int)Generates an array of random bytes with the specified length. |
java-static String! |
buildTestString(length: Int, random: Random!)Generates a random string with the specified length. |
java-static ByteArray<Byte>! |
createByteArray(byteBuffer: ByteBuffer!)Gets the underlying data of the |
java-static ByteArray<Byte>! |
createByteArray(bytes: IntArray!)Converts an array of integers in the range [0, 255] into an equivalent byte array. |
java-static ByteBuffer! |
createByteBuffer(data: FloatArray!)Creates a |
java-static ByteBuffer! |
createByteBuffer(data: IntArray!)Creates a |
java-static ByteBuffer! |
createByteBuffer(data: ShortArray!)Creates a |
java-static ByteBuffer! |
createByteBuffer(data: ByteArray!)Creates a |
java-static AudioProcessor! |
createByteCountingAudioProcessor(byteCount: AtomicInteger!)Returns an |
java-static ImmutableList<Byte!>! |
createByteList(bytes: IntArray!)Converts an array of integers in the range [0, 255] into an equivalent byte list. |
java-static File! |
createExternalCacheFile(context: Context!, fileName: String!)Creates a |
java-static File! |
createExternalCacheFile(Creates a |
java-static FloatArray<Float>! |
createFloatArray(byteBuffer: ByteBuffer!)Gets the underlying data of the |
java-static IntArray<Int>! |
createInt24Array(byteBuffer: ByteBuffer!)Gets the underlying data of the |
java-static ByteBuffer! |
createInt24ByteBuffer(data: IntArray!)Creates a |
java-static IntArray<Int>! |
createIntArray(byteBuffer: ByteBuffer!)Gets the underlying data of the |
java-static MetadataInputBuffer! |
createMetadataInputBuffer(data: ByteArray!)Create a new |
java-static ShortArray<Short>! |
createShortArray(byteBuffer: ByteBuffer!)Gets the underlying data of the |
java-static SurfaceView! |
createSurfaceView(context: Context!)Creates a |
java-static File! |
createTestFile(directory: File!, name: String!)Writes one byte long test data to the file and returns it. |
java-static File! |
createTestFile(file: File!, length: Long)Writes test data with the specified length to the file and returns it. |
java-static File! |
createTestFile(directory: File!, name: String!, length: Long)Writes test data with the specified length to the file and returns it. |
java-static FakeExtractorOutput! |
extractAllSamplesFromByteArray(extractor: Extractor!, data: ByteArray!)Extracts all samples from the given byte array into a |
java-static FakeExtractorOutput! |
extractAllSamplesFromFile(Extracts all samples from the given file into a |
java-static FakeExtractorOutput! |
extractAllSamplesFromFilePath(extractor: Extractor!, filePath: String!)Extracts all samples from the given file into a |
java-static SeekMap! |
extractSeekMap(Reads from the given input using the given |
java-static Float |
generateFloatInRange(random: Random!, range: Range<Float!>!)Returns a randomly generated float within the specified range, using |
java-static Long |
generateLong(random: Random!, origin: Long, bound: Long)Returns a long between |
java-static ByteArray<Byte>! |
getByteArray(context: Context!, fileName: String!)Returns the bytes of an asset file. |
java-static ByteArray<Byte>! |
getByteArrayFromFilePath(filePath: String!)Returns the bytes of a file using its file path. |
java-static ImmutableList<@Player.Command Int!>! |
getCommandsAsList(commands: Player.Commands!)Returns an |
java-static ImmutableList<@Player.Event Int!>! |
getEventsAsList(events: Player.Events!)Returns an |
java-static ExtractorInput! |
getExtractorInputFromPosition(Returns an |
java-static DatabaseProvider! |
Returns a |
java-static Class<Any!>? |
getInnerClass(clazz: Class<Any!>!, className: String!)Returns an inner class of |
java-static InputStream! |
getInputStream(context: Context!, fileName: String!)Returns an |
java-static ByteBuffer! |
getNonRandomByteBuffer(frameCount: Int, bytesPerFrame: Int)Returns a non-random |
java-static ByteBuffer! |
getPeriodicSamplesBuffer(sampleCount: Int, period: Int)Returns a |
java-static (Mutable)List<Method!>! |
getPublicMethods(clazz: Class<Any!>!)Returns all the public methods of a Java class (except those defined by |
java-static (Mutable)Iterable<Method!>! |
getPublicOverridableMethods(clazz: Class<Any!>!)Returns all the public overridable methods of a Java class (except those defined by |
java-static String! |
Returns a |
java-static Bundle! |
Returns a |
java-static Unit |
repeatFlakyTest(maxRepetitions: Int, testImpl: ThrowingRunnable!)Repeats the potentially flaky test multiple times if needed. |
java-static Format! |
retrieveTrackFormat(Returns the |
java-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. |
java-static Boolean |
timelinesAreSame(thisTimeline: Timeline!, thatTimeline: Timeline!)Returns true if |
Constants
PSNR_THRESHOLD
const val PSNR_THRESHOLD = 35.0f: Float
Luma PSNR values between 30 and 50 are considered good for lossy compression (See Quality estimation with PSNR ).
Public functions
assertBitmapsAreSimilar
java-static fun assertBitmapsAreSimilar(
expectedBitmap: Bitmap!,
actualBitmap: Bitmap!,
psnrThresholdDb: Double
): Unit
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
java-static fun assertBufferInfosEqual(
expected: MediaCodec.BufferInfo!,
actual: MediaCodec.BufferInfo!
): Unit
Returns whether two BufferInfos are equal.
assertDataSourceContent
java-static fun assertDataSourceContent(
dataSource: DataSource!,
dataSpec: DataSpec!,
expectedData: ByteArray!,
expectKnownLength: Boolean
): Unit
Asserts that data read from a DataSource matches expected.
| Parameters | |
|---|---|
dataSource: DataSource! |
The |
dataSpec: DataSpec! |
The |
expectedData: ByteArray! |
The expected data. |
expectKnownLength: Boolean |
Whether to assert that |
| Throws | |
|---|---|
java.io.IOException |
If an error occurs reading fom the |
assertForwardingClassForwardsAllMethods
java-static fun <T : Any?, F : T?> assertForwardingClassForwardsAllMethods(
superType: Class<T!>!,
forwardingInstanceFactory: Function<T!, F!>!
): Unit
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
java-static fun <T : Any?, F : T?> assertForwardingClassForwardsAllMethodsExcept(
superType: Class<T!>!,
forwardingInstanceFactory: Function<T!, F!>!,
excludedMethods: (Mutable)Set<String!>!
): Unit
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
java-static fun <T> assertSubclassOverridesAllMethods(
superType: Class<T!>!,
subType: Class<T!>!
): Unit
Use reflection to assert that every non-final method declared on superType is overridden by subType.
assertSubclassOverridesAllMethodsExcept
java-static fun <T> assertSubclassOverridesAllMethodsExcept(
superType: Class<T!>!,
subType: Class<T!>!,
excludedMethods: (Mutable)Set<String!>!
): Unit
Use reflection to assert that every non-final, non-excluded method declared on
superType is overridden by subType.
assertTimelinesSame
java-static fun assertTimelinesSame(
actualTimelines: (Mutable)List<Timeline!>!,
expectedTimelines: (Mutable)List<Timeline!>!
): Unit
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
java-static fun buildAssetUri(assetPath: String!): Uri!
Returns the Uri for the given asset path.
buildFloatTestSamples
java-static fun buildFloatTestSamples(length: Int, random: Random!): FloatArray<Float>!
Returns an array of random floats between [-1; 1] with the specified length.
buildFullyCustomizedMediaItem
java-static fun buildFullyCustomizedMediaItem(): MediaItem!
Returns a MediaItem that has all fields set to non-default values.
buildTestData
java-static fun buildTestData(length: Int): ByteArray<Byte>!
Equivalent to buildTestData(length, length).
| Parameters | |
|---|---|
length: Int |
The length of the array. |
buildTestData
java-static fun buildTestData(length: Int, random: Random!): ByteArray<Byte>!
Generates an array of random bytes with the specified length.
buildTestData
java-static fun buildTestData(length: Int, seed: Int): ByteArray<Byte>!
Generates an array of random bytes with the specified length.
| Parameters | |
|---|---|
length: Int |
The length of the array. |
seed: Int |
A seed for an internally created |
buildTestString
java-static fun buildTestString(length: Int, random: Random!): String!
Generates a random string with the specified length.
| Returns | |
|---|---|
String! |
The generated string. |
createByteArray
java-static fun createByteArray(byteBuffer: ByteBuffer!): ByteArray<Byte>!
Gets the underlying data of the ByteBuffer as a byte[].
createByteArray
java-static fun createByteArray(bytes: IntArray!): ByteArray<Byte>!
Converts an array of integers in the range [0, 255] into an equivalent byte array.
| Parameters | |
|---|---|
bytes: IntArray! |
An array of integers, all of which must be in the range [0, 255]. |
createByteBuffer
java-static fun createByteBuffer(data: FloatArray!): ByteBuffer!
Creates a ByteBuffer containing the data.
createByteBuffer
java-static fun createByteBuffer(data: IntArray!): ByteBuffer!
Creates a ByteBuffer containing the data.
createByteBuffer
java-static fun createByteBuffer(data: ShortArray!): ByteBuffer!
Creates a ByteBuffer containing the data.
createByteBuffer
java-static fun createByteBuffer(data: ByteArray!): ByteBuffer!
Creates a ByteBuffer containing the data.
createByteCountingAudioProcessor
java-static fun createByteCountingAudioProcessor(byteCount: AtomicInteger!): AudioProcessor!
Returns an AudioProcessor that counts the number of bytes input to it.
createByteList
java-static fun createByteList(bytes: IntArray!): ImmutableList<Byte!>!
Converts an array of integers in the range [0, 255] into an equivalent byte list.
| Parameters | |
|---|---|
bytes: IntArray! |
An array of integers, all of which must be in the range [0, 255]. |
| Returns | |
|---|---|
ImmutableList<Byte!>! |
The equivalent byte list. |
createExternalCacheFile
java-static fun createExternalCacheFile(context: Context!, fileName: String!): File!
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
java-static fun createExternalCacheFile(
context: Context!,
directoryName: String!,
fileName: String!
): File!
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 |
directoryName: String! |
The directory name within the external cache to save the file in. |
fileName: String! |
The filename to save to the cache. |
| Throws | |
|---|---|
java.io.IOException |
createFloatArray
java-static fun createFloatArray(byteBuffer: ByteBuffer!): FloatArray<Float>!
Gets the underlying data of the ByteBuffer as a float[].
createInt24Array
java-static fun createInt24Array(byteBuffer: ByteBuffer!): IntArray<Int>!
Gets the underlying data of the ByteBuffer as 24-bit integer values in int[].
createInt24ByteBuffer
java-static fun createInt24ByteBuffer(data: IntArray!): ByteBuffer!
Creates a ByteBuffer with the contents of data as 24-bit integers.
createIntArray
java-static fun createIntArray(byteBuffer: ByteBuffer!): IntArray<Int>!
Gets the underlying data of the ByteBuffer as a int[].
createMetadataInputBuffer
java-static fun createMetadataInputBuffer(data: ByteArray!): MetadataInputBuffer!
Create a new MetadataInputBuffer and copy data into the backing ByteBuffer.
createShortArray
java-static fun createShortArray(byteBuffer: ByteBuffer!): ShortArray<Short>!
Gets the underlying data of the ByteBuffer as a short[].
createSurfaceView
java-static fun createSurfaceView(context: Context!): SurfaceView!
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
java-static fun createTestFile(directory: File!, name: String!): File!
Writes one byte long test data to the file and returns it.
| Throws | |
|---|---|
java.io.IOException |
createTestFile
java-static fun createTestFile(file: File!, length: Long): File!
Writes test data with the specified length to the file and returns it.
| Throws | |
|---|---|
java.io.IOException |
createTestFile
java-static fun createTestFile(directory: File!, name: String!, length: Long): File!
Writes test data with the specified length to the file and returns it.
| Throws | |
|---|---|
java.io.IOException |
extractAllSamplesFromByteArray
java-static fun extractAllSamplesFromByteArray(extractor: Extractor!, data: ByteArray!): FakeExtractorOutput!
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
java-static fun extractAllSamplesFromFile(
extractor: Extractor!,
context: Context!,
fileName: String!
): FakeExtractorOutput!
Extracts all samples from the given file into a FakeTrackOutput.
| Parameters | |
|---|---|
extractor: Extractor! |
The |
context: Context! |
A |
fileName: String! |
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
java-static fun extractAllSamplesFromFilePath(extractor: Extractor!, filePath: String!): FakeExtractorOutput!
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
java-static fun extractSeekMap(
extractor: Extractor!,
output: FakeExtractorOutput!,
dataSource: DataSource!,
uri: Uri!
): SeekMap!
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 |
output: FakeExtractorOutput! |
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
java-static fun generateFloatInRange(random: Random!, range: Range<Float!>!): Float
Returns a randomly generated float within the specified range, using random as random number generator.
range must be a bounded range.
generateLong
java-static fun generateLong(random: Random!, origin: Long, bound: Long): Long
Returns a long between origin (inclusive) and bound (exclusive), given
random.
getByteArray
java-static fun getByteArray(context: Context!, fileName: String!): ByteArray<Byte>!
Returns the bytes of an asset file.
| Throws | |
|---|---|
java.io.IOException |
getByteArrayFromFilePath
java-static fun getByteArrayFromFilePath(filePath: String!): ByteArray<Byte>!
Returns the bytes of a file using its file path.
| Throws | |
|---|---|
java.io.IOException |
getCommandsAsList
java-static fun getCommandsAsList(commands: Player.Commands!): ImmutableList<@Player.Command Int!>!
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
java-static fun getEventsAsList(events: Player.Events!): ImmutableList<@Player.Event Int!>!
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
java-static fun getExtractorInputFromPosition(
dataSource: DataSource!,
position: Long,
uri: Uri!
): ExtractorInput!
Returns an ExtractorInput to read from the given input at given position.
| Throws | |
|---|---|
java.io.IOException |
getInMemoryDatabaseProvider
java-static fun getInMemoryDatabaseProvider(): DatabaseProvider!
Returns a DatabaseProvider that provides an in-memory database.
getInnerClass
java-static fun getInnerClass(clazz: Class<Any!>!, className: String!): Class<Any!>?
Returns an inner class of clazz called className if it exists, otherwise null.
getInputStream
java-static fun getInputStream(context: Context!, fileName: String!): InputStream!
Returns an InputStream for reading from an asset file.
| Throws | |
|---|---|
java.io.IOException |
getNonRandomByteBuffer
java-static fun getNonRandomByteBuffer(frameCount: Int, bytesPerFrame: Int): ByteBuffer!
Returns a non-random ByteBuffer filled with frameCount * bytesPerFrame bytes.
getPeriodicSamplesBuffer
java-static fun getPeriodicSamplesBuffer(sampleCount: Int, period: Int): ByteBuffer!
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.
getPublicMethods
java-static fun getPublicMethods(clazz: Class<Any!>!): (Mutable)List<Method!>!
Returns all the public methods of a Java class (except those defined by Object).
getPublicOverridableMethods
java-static fun getPublicOverridableMethods(clazz: Class<Any!>!): (Mutable)Iterable<Method!>!
Returns all the public overridable methods of a Java class (except those defined by Object).
getString
java-static fun getString(context: Context!, fileName: String!): String!
Returns a String read from an asset file.
| Throws | |
|---|---|
java.io.IOException |
getThrowingBundle
java-static fun getThrowingBundle(): Bundle!
Returns a Bundle that will throw an exception at the first attempt to read a value.
repeatFlakyTest
java-static fun repeatFlakyTest(maxRepetitions: Int, testImpl: ThrowingRunnable!): Unit
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 | |
|---|---|
maxRepetitions: Int |
The maximum number of repetitions before failing the test. |
testImpl: ThrowingRunnable! |
The test implementation. |
retrieveTrackFormat
java-static fun retrieveTrackFormat(
context: Context!,
fileUri: String!,
@C.TrackType trackType: Int
): Format!
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 |
fileUri: String! |
The media file uri. |
@C.TrackType trackType: Int |
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
java-static fun seekToTimeUs(
extractor: Extractor!,
seekMap: SeekMap!,
seekTimeUs: Long,
dataSource: DataSource!,
trackOutput: FakeTrackOutput!,
uri: Uri!
): Int
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 |
seekTimeUs: Long |
The seek time, in micro-seconds. |
dataSource: DataSource! |
The |
trackOutput: FakeTrackOutput! |
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 |