@UnstableApi
public class TestUtil


Utility methods for tests.

Summary

Public methods

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

Returns whether two BufferInfos are equal.

static void
assertDataSourceContent(
    DataSource dataSource,
    DataSpec dataSpec,
    byte[] expectedData,
    boolean expectKnownLength
)

Asserts that data read from a DataSource matches expected.

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 the Uri for the given asset path.

static MediaItem

Returns a MediaItem that has all fields set to non-default values.

static byte[]
buildTestData(int length)

Equivalent to buildTestData(length, length).

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(int[] bytes)

Converts an array of integers in the range [0, 255] into an equivalent byte array.

static ByteBuffer
createByteBuffer(float[] data)

Creates a ByteBuffer containing the data.

static ByteBuffer
createByteBuffer(short[] data)

Creates a ByteBuffer containing the data.

static ImmutableList<Byte>
createByteList(int[] bytes)

Converts an array of integers in the range [0, 255] into an equivalent byte list.

static float[]

Gets the underlying data of the ByteBuffer as a float[].

static MetadataInputBuffer

Create a new MetadataInputBuffer and copy data into the backing .

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
extractAllSamplesFromFile(
    Extractor extractor,
    Context context,
    String fileName
)

Extracts all samples from the given file into a FakeTrackOutput.

static FakeExtractorOutput

Extracts all samples from the given file into a FakeTrackOutput.

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 and all of the track formats have been identified, or until the extractor encounters EOF.

static Bitmap
getBitmap(Context context, String fileName)

Returns a Bitmap read from an asset file.

static byte[]
getByteArray(Context context, String fileName)

Returns the bytes of an asset file.

static byte[]

Returns the bytes of a file using its file path.

static ExtractorInput
getExtractorInputFromPosition(
    DataSource dataSource,
    long position,
    Uri uri
)

Returns an ExtractorInput to read from the given input at given position.

static DatabaseProvider

Returns a DatabaseProvider that provides an in-memory database.

static InputStream
getInputStream(Context context, String fileName)

Returns an InputStream for reading from an asset file.

static List<Method>

Returns all the public methods of a Java class (except those defined by Object).

static String
getString(Context context, String fileName)

Returns a String read from an asset file.

static Bundle

Returns a Bundle that will throw an exception at the first attempt to read a value.

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 if thisTimeline is equal to thatTimeline, ignoring uid and uid values, and shuffle order.

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.

Parameters
Bitmap expectedBitmap

The expected bitmap.

Bitmap actualBitmap

The actual bitmap.

double psnrThresholdDb

The PSNR threshold (in dB), at or above which bitmaps are considered very similar.

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 DataSource through which to read.

DataSpec dataSpec

The DataSpec to use when opening the DataSource.

byte[] expectedData

The expected data.

boolean expectKnownLength

Whether to assert that open returns the expected data length. If false then it's asserted that LENGTH_UNSET is returned.

Throws
java.io.IOException

If an error occurs reading fom the DataSource.

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.
Parameters
List<Timeline> actualTimelines

A list of actual timelines.

List<Timeline> expectedTimelines

A list of expected timelines.

buildAssetUri

public static Uri buildAssetUri(String assetPath)

Returns the Uri for the given asset path.

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 source of randomness.

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(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(short[] data)

Creates a ByteBuffer containing the data.

createByteList

public static ImmutableList<BytecreateByteList(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.

createFloatArray

public static float[] createFloatArray(ByteBuffer byteBuffer)

Gets the underlying data of the ByteBuffer as a float[].

createMetadataInputBuffer

public static MetadataInputBuffer createMetadataInputBuffer(byte[] data)

Create a new MetadataInputBuffer and copy data into the backing .

createTestFile

public static File createTestFile(File directory, String name)

Writes one byte long test data to the file and returns it.

createTestFile

public static File createTestFile(File file, long length)

Writes test data with the specified length to the file and returns it.

createTestFile

public static File createTestFile(File directory, String name, long length)

Writes test data with the specified length to the file and returns it.

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 Extractor to be used.

Context context

A Context.

String fileName

The name of the input file.

Returns
FakeExtractorOutput

The FakeTrackOutput containing the extracted samples.

Throws
java.io.IOException

If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any SeekMap.

extractAllSamplesFromFilePath

public static FakeExtractorOutput extractAllSamplesFromFilePath(Extractor extractor, String filePath)

Extracts all samples from the given file into a FakeTrackOutput.

Parameters
Extractor extractor

The Extractor to be used.

String filePath

The file path.

Returns
FakeExtractorOutput

The FakeTrackOutput containing the extracted samples.

Throws
java.io.IOException

If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any SeekMap.

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 and all of the track formats have been identified, or until the extractor encounters EOF.

Parameters
Extractor extractor

The Extractor to extractor from input.

FakeExtractorOutput output

The FakeTrackOutput to store the extracted SeekMap and track.

DataSource dataSource

The DataSource that will be used to read from the input.

Uri uri

The Uri of the input.

Returns
SeekMap

The extracted SeekMap.

Throws
java.io.IOException

If an error occurred reading from the input, or if the extractor finishes reading from input without extracting any SeekMap.

getBitmap

public static Bitmap getBitmap(Context context, String fileName)

Returns a Bitmap read from an asset file.

getByteArray

public static byte[] getByteArray(Context context, String fileName)

Returns the bytes of an asset file.

getByteArrayFromFilePath

public static byte[] getByteArrayFromFilePath(String filePath)

Returns the bytes of a file using its file path.

getExtractorInputFromPosition

public static ExtractorInput getExtractorInputFromPosition(
    DataSource dataSource,
    long position,
    Uri uri
)

Returns an ExtractorInput to read from the given input at given position.

getInMemoryDatabaseProvider

public static DatabaseProvider getInMemoryDatabaseProvider()

Returns a DatabaseProvider that provides an in-memory database.

getInputStream

public static InputStream getInputStream(Context context, String fileName)

Returns an InputStream for reading from an asset file.

getPublicMethods

public static List<MethodgetPublicMethods(Class<Object> clazz)

Returns all the public 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.

getThrowingBundle

public static Bundle getThrowingBundle()

Returns a Bundle that will throw an exception at the first attempt to read a value.

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 Extractor to extract from input.

SeekMap seekMap

The SeekMap of the stream from the given input.

long seekTimeUs

The seek time, in micro-seconds.

DataSource dataSource

The DataSource that will be used to read from the input.

FakeTrackOutput trackOutput

The FakeTrackOutput to store the extracted samples.

Uri uri

The Uri of the input.

Returns
int

The index of the first extracted sample written to the given trackOutput after the seek is completed, or INDEX_UNSET if the seek is completed without any extracted sample.

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.