Class TestUtil


  • public class TestUtil
    extends Object
    Utility methods for tests.
    • 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.
      • createFloatArray

        public static float[] createFloatArray​(ByteBuffer byteBuffer)
        Gets the underlying data of the ByteBuffer as a float[].
      • 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<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
      • getByteArrayFromFilePath

        public static byte[] getByteArrayFromFilePath​(String filePath)
                                               throws IOException
        Returns the bytes of a file using its file path.
        Throws:
        IOException
      • 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:
        actualTimelines - A list of actual timelines.
        expectedTimelines - A list of expected timelines.
      • 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.
      • extractAllSamplesFromFilePath

        public static FakeExtractorOutput extractAllSamplesFromFilePath​(Extractor extractor,
                                                                        String filePath)
                                                                 throws IOException
        Extracts all samples from the given file into a FakeTrackOutput.
        Parameters:
        extractor - The Extractor to be used.
        filePath - The file path.
        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
      • getPublicMethods

        public static List<Method> getPublicMethods​(Class<?> clazz)
        Returns all the public methods of a Java class (except those defined by Object).