Package com.google.android.exoplayer2.testutil

## 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 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 Uri` | `buildAssetUri​(String assetPath)` | Returns the `Uri` for the given asset path.
    `static byte[]` | `buildTestData​(int length)` | Equivalent to `buildTestData(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 ImmutableList<Byte>` | `createByteList​(int... bytes)` | Converts an array of integers in the range [0, 255] into an equivalent byte list.
    `static MetadataInputBuffer` | `createMetadataInputBuffer​(byte[] data)` | Create a new `MetadataInputBuffer` and copy `data` into the backing `ByteBuffer`.
    `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 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 `SeekMap` 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 ExtractorInput` | `getExtractorInputFromPosition​(DataSource dataSource, long position, Uri uri)` | Returns an `ExtractorInput` to read from the given input at given position.
    `static DatabaseProvider` | `getInMemoryDatabaseProvider()` | 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 String` | `getString​(Context context, String fileName)` | Returns a `String` 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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### buildTestData

          public static byte[] buildTestData​(int length)

      Equivalent to `buildTestData(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 created `source 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.

    - #### 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`

    - #### getInputStream

          public static InputStream getInputStream​(Context context,
                                                   String fileName)
                                            throws IOException

      Returns an `InputStream` for reading from an asset file.
      Throws:
        `IOException`

    - #### getString

          public static String getString​(Context context,
                                         String fileName)
                                  throws IOException

      Returns a `String` read from an asset file.
      Throws:
        `IOException`

    - #### getBitmap

          public static Bitmap getBitmap​(Context context,
                                         String fileName)
                                  throws IOException

      Returns a `Bitmap` read from an asset file.
      Throws:
        `IOException`

    - #### getInMemoryDatabaseProvider

          public static DatabaseProvider getInMemoryDatabaseProvider()

      Returns a `DatabaseProvider` that provides an in-memory database.

    - #### assertDataSourceContent

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

      Asserts that data read from a `DataSource` matches `expected`.
      Parameters:
        `dataSource` - The `DataSource` through which to read.
        `dataSpec` - The `DataSpec` to use when opening the `DataSource`.
        `expectedData` - The expected data.
        `expectKnownLength` - Whether to assert that `DataSource.open(com.google.android.exoplayer2.upstream.DataSpec)` returns the expected data length. If false then it's asserted that `C.LENGTH_UNSET` is returned.
      Throws:
        `IOException` - If an error occurs reading fom the `DataSource`.

    - #### assertBufferInfosEqual

          public static void assertBufferInfosEqual​(MediaCodec.BufferInfo expected,
                                                    MediaCodec.BufferInfo actual)

      Returns whether two `BufferInfos` 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 the `Uri` 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 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` - The `Extractor` to extractor from input.
        `output` - The `FakeTrackOutput` to store the extracted `SeekMap` and track.
        `dataSource` - The `DataSource` 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 any `SeekMap`.

    - #### extractAllSamplesFromFile

          public static FakeExtractorOutput extractAllSamplesFromFile​(Extractor extractor,
                                                                      Context context,
                                                                      String fileName)
                                                               throws IOException

      Extracts all samples from the given file into a `FakeTrackOutput`.
      Parameters:
        `extractor` - The `Extractor` to extractor from input.
        `context` - A `Context`.
        `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 any `SeekMap`.

    - #### 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` - The `Extractor` to extract from input.
        `seekMap` - The `SeekMap` of the stream from the given input.
        `seekTimeUs` - The seek time, in micro-seconds.
        `trackOutput` - The `FakeTrackOutput` to store the extracted samples.
        `dataSource` - The `DataSource` 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, or `C.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 an `ExtractorInput` to read from the given input at given position.
      Throws:
        `IOException`

    - #### createMetadataInputBuffer

          public static MetadataInputBuffer createMetadataInputBuffer​(byte[] data)

      Create a new `MetadataInputBuffer` and copy `data` into the backing `ByteBuffer`.