Class TestUtils


  • public class TestUtils
    extends java.lang.Object
    Utility methods to deal with loading the test data.
    • Constructor Summary

      Constructors 
      Constructor Description
      TestUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.io.File createTempDirDeletedOnExit()  
      static void deleteFile​(java.io.File dir)  
      static void disableIfOnWindowsWithBazel()  
      static void eventually​(java.lang.Runnable runnable)
      Asserts that a runnable will eventually not throw an assertion exception.
      static void eventually​(java.lang.Runnable runnable, java.time.Duration duration)
      Asserts that a runnable will eventually not throw AssertionError before timeoutMs milliseconds have ellapsed
      static java.nio.file.Path getAapt2()
      Returns the checked in AAPT2 binary that is shipped with the gradle plugin.
      static java.lang.String getDesugarLibConfigContent()  
      static java.nio.file.Path getDesugarLibJar()  
      static java.lang.String getDiff​(java.lang.String[] before, java.lang.String[] after)  
      static java.lang.String getDiff​(java.lang.String[] before, java.lang.String[] after, int windowSize)  
      static java.lang.String getDiff​(java.lang.String before, java.lang.String after)  
      static java.lang.String getDiff​(java.lang.String before, java.lang.String after, int windowSize)  
      static java.io.File getJava11Jdk()  
      static java.lang.String getKotlinVersionForTests()
      Returns Kotlin version that is used in new project templates and integration tests.
      static java.lang.String getLatestAndroidPlatform()  
      static java.io.File getMockJdk()  
      static java.io.File getNdk()
      Returns the path to checked-in NDK.
      static java.io.File getPlatformFile​(java.lang.String path)
      Returns a file at path relative to the root for getLatestAndroidPlatform().
      static java.io.File getPrebuiltOfflineMavenRepo()
      Returns the prebuilt offline Maven repository used during IDE tests.
      static java.lang.String getRelativeSdk()
      Returns the SDK directory relative to the workspace.
      static java.nio.file.Path getRemoteSdk()
      Returns the remote SDK directory.
      static java.io.File getRoot​(java.lang.String... names)
      Deprecated.
      static java.io.File getSdk()
      Returns the SDK directory.
      static java.io.File getTestOutputDir()  
      static java.io.File getWorkspaceFile​(java.lang.String path)
      Given a full path to a file from the base of the current workspace, return the file.
      static java.io.File getWorkspaceRoot()
      Returns the root of the entire Android Studio codebase.
      static void launchProcess​(java.lang.Class toRun, java.lang.String... args)
      Launches a new process to execute the class toRun main() method and blocks until the process exits.
      static boolean runningFromBazel()
      Checks if tests were started by Bazel.
      static boolean runningWithJdk11Plus​(java.lang.String version)
      Checks if tests are running with Jdk 11 or above.
      static void waitForFileSystemTick()
      Sleeps the current thread for enough time to ensure that the local file system had enough time to notice a "tick".
      static void waitForFileSystemTick​(long currentTimestamp)
      Sleeps the current thread for enough time to ensure that the local file system had enough time to notice a "tick".
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TestUtils

        public TestUtils()
    • Method Detail

      • getKotlinVersionForTests

        @NonNull
        public static java.lang.String getKotlinVersionForTests()
        Returns Kotlin version that is used in new project templates and integration tests.

        This version is determined based on the checked-in kotlin-plugin prebuilt, and should be in sync with the version in:

        • buildSrc/base/dependencies.properties
        • tools/base/third_party/BUILD (this is generated from dependencies.properties)
        • tools/base/build-system/integration-test/application/BUILD
        • tools/base/build-system/integration-test/databinding/BUILD.bazel
        • tools/adt/idea/android/BUILD
        • tools/base/.idea/libraries definition for kotlin-stdlib-jdk8
        • tools/idea/.idea/libraries definition for kotlin-stdlib-jdk8
      • getRoot

        @Deprecated
        @NonNull
        public static java.io.File getRoot​(java.lang.String... names)
        Deprecated.
        Returns a File for the subfolder of the test resource data.
      • deleteFile

        public static void deleteFile​(java.io.File dir)
      • createTempDirDeletedOnExit

        public static java.io.File createTempDirDeletedOnExit()
      • getWorkspaceRoot

        @NonNull
        public static java.io.File getWorkspaceRoot()
        Returns the root of the entire Android Studio codebase.

        From this path, you should be able to access any file in the workspace via its full path, e.g.

        new File(TestUtils.getWorkspaceRoot(), "tools/adt/idea/android/testSrc"); new File(TestUtils.getWorkspaceRoot(), "prebuilts/studio/jdk");

        If this method is called by code run via IntelliJ / Gradle, it will simply walk its ancestor tree looking for the WORKSPACE file at its root; if called from Bazel, it will simply return the runfiles directory (which should be a mirror of the WORKSPACE root except only populated with explicitly declared dependencies).

        Instead of calling this directly, prefer calling getWorkspaceFile(String) as it is more resilient to cross-platform testing.

        Throws:
        java.lang.IllegalStateException - if the current directory of the test is not a subdirectory of the workspace directory when this method is called. This shouldn't happen if the test is run by Bazel or run by IntelliJ with default configuration settings (where the working directory is initialized to the module root).
      • getWorkspaceFile

        @NonNull
        public static java.io.File getWorkspaceFile​(@NonNull
                                                    java.lang.String path)
        Given a full path to a file from the base of the current workspace, return the file. e.g. TestUtils.getWorkspaceFile("tools/adt/idea/android/testSrc"); TestUtils.getWorkspaceFile("prebuilts/studio/jdk"); This method guarantees the file exists, throwing an exception if not found, so tests can safely use the file immediately after receiving it. In order to have the same method call work on both Windows and non-Windows machines, if the current OS is Windows and the target path is found with a common windows extension on it, then it will automatically be returned, e.g. "/path/to/binary" -> "/path/to/binary.exe".
        Throws:
        java.lang.IllegalArgumentException - if the path results in a file that's not found.
      • getTestOutputDir

        @NonNull
        public static java.io.File getTestOutputDir()
        Returns:
        a directory which tests can output data to. If running through Bazel's test runner, this returns the directory as specified by the TEST_UNDECLARED_OUTPUTS_DIR environment variable. Data written to this directory will be zipped and made available under the WORKSPACE_ROOT/bazel-testlogs/ after the test completes. For non-Bazel runs, this currently returns a tmp directory that is deleted on exit.
      • getPlatformFile

        @NonNull
        public static java.io.File getPlatformFile​(java.lang.String path)
        Returns a file at path relative to the root for getLatestAndroidPlatform().
        Throws:
        java.lang.IllegalStateException - if the current OS is not supported.
        java.lang.IllegalArgumentException - if the path results in a file not found.
      • runningFromBazel

        public static boolean runningFromBazel()
        Checks if tests were started by Bazel.
      • runningWithJdk11Plus

        public static boolean runningWithJdk11Plus​(java.lang.String version)
        Checks if tests are running with Jdk 11 or above.
      • getRelativeSdk

        @NonNull
        public static java.lang.String getRelativeSdk()
        Returns the SDK directory relative to the workspace.
        Returns:
        a valid File object pointing at the SDK directory.
        Throws:
        java.lang.IllegalStateException - if the current OS is not supported.
        java.lang.IllegalArgumentException - if the path results in a file not found.
      • getSdk

        @NonNull
        public static java.io.File getSdk()
        Returns the SDK directory.
        Returns:
        a valid File object pointing at the SDK directory.
        Throws:
        java.lang.IllegalStateException - if the current OS is not supported.
        java.lang.IllegalArgumentException - if the path results in a file not found.
      • getMockJdk

        @NonNull
        public static java.io.File getMockJdk()
      • getNdk

        @NonNull
        public static java.io.File getNdk()
        Returns the path to checked-in NDK.
        See Also:
        getSdk()
      • getPrebuiltOfflineMavenRepo

        public static java.io.File getPrebuiltOfflineMavenRepo()
        Returns the prebuilt offline Maven repository used during IDE tests.
      • getRemoteSdk

        @NonNull
        public static java.nio.file.Path getRemoteSdk()
        Returns the remote SDK directory.
        Returns:
        a valid File object pointing at the remote SDK directory.
        Throws:
        java.lang.IllegalArgumentException - if the path results in a file not found.
      • getAapt2

        @NonNull
        public static java.nio.file.Path getAapt2()
        Returns the checked in AAPT2 binary that is shipped with the gradle plugin.
      • getDesugarLibJar

        @NonNull
        public static java.nio.file.Path getDesugarLibJar()
      • getDesugarLibConfigContent

        @NonNull
        public static java.lang.String getDesugarLibConfigContent()
                                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • getJava11Jdk

        @NonNull
        public static java.io.File getJava11Jdk()
      • getLatestAndroidPlatform

        @NonNull
        public static java.lang.String getLatestAndroidPlatform()
      • waitForFileSystemTick

        public static void waitForFileSystemTick()
                                          throws java.lang.InterruptedException,
                                                 java.io.IOException
        Sleeps the current thread for enough time to ensure that the local file system had enough time to notice a "tick". This method is usually called in tests when it is necessary to ensure filesystem writes are detected through timestamp modification.
        Throws:
        java.lang.InterruptedException - waiting interrupted
        java.io.IOException - issues creating a temporary file
      • waitForFileSystemTick

        public static void waitForFileSystemTick​(long currentTimestamp)
                                          throws java.lang.InterruptedException,
                                                 java.io.IOException
        Sleeps the current thread for enough time to ensure that the local file system had enough time to notice a "tick". This method is usually called in tests when it is necessary to ensure filesystem writes are detected through timestamp modification.
        Parameters:
        currentTimestamp - last timestamp read from disk
        Throws:
        java.lang.InterruptedException - waiting interrupted
        java.io.IOException - issues creating a temporary file
      • getDiff

        @NonNull
        public static java.lang.String getDiff​(@NonNull
                                               java.lang.String before,
                                               @NonNull
                                               java.lang.String after)
      • getDiff

        @NonNull
        public static java.lang.String getDiff​(@NonNull
                                               java.lang.String before,
                                               @NonNull
                                               java.lang.String after,
                                               int windowSize)
      • getDiff

        @NonNull
        public static java.lang.String getDiff​(@NonNull
                                               java.lang.String[] before,
                                               @NonNull
                                               java.lang.String[] after)
      • getDiff

        public static java.lang.String getDiff​(@NonNull
                                               java.lang.String[] before,
                                               @NonNull
                                               java.lang.String[] after,
                                               int windowSize)
      • eventually

        public static void eventually​(@NonNull
                                      java.lang.Runnable runnable)
        Asserts that a runnable will eventually not throw an assertion exception. Equivalent to eventually(Runnable, Duration), but using a default timeout
        Parameters:
        runnable - a description of the failure, if the condition never becomes true
      • eventually

        public static void eventually​(@NonNull
                                      java.lang.Runnable runnable,
                                      java.time.Duration duration)
        Asserts that a runnable will eventually not throw AssertionError before timeoutMs milliseconds have ellapsed
        Parameters:
        runnable - a description of the failure, if the condition never becomes true
        duration - the timeout for the predicate to become true
      • launchProcess

        public static void launchProcess​(@NonNull
                                         java.lang.Class toRun,
                                         java.lang.String... args)
        Launches a new process to execute the class toRun main() method and blocks until the process exits.
        Parameters:
        toRun - the class whose main() method will be executed in a new process.
        args - the arguments for the class toRun main() method
        Throws:
        java.lang.RuntimeException - if any (checked or runtime) exception occurs or the process returns an exit value other than 0
      • disableIfOnWindowsWithBazel

        public static void disableIfOnWindowsWithBazel()