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.nio.file.Path createTempDirDeletedOnExit()
      Deprecated.
      Temporary directories and files should be deleted after each test, not kept around for the lifetime of the JVM.
      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.nio.file.Path getJava11Jdk()  
      static java.lang.String getKotlinVersionForTests()
      Returns the Kotlin version to be used in new project templates and integration tests.
      static java.lang.String getLatestAndroidPlatform()  
      static java.nio.file.Path getMockJdk()  
      static java.nio.file.Path getNdk()
      Returns the path to checked-in NDK.
      static java.nio.file.Path 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.nio.file.Path getSdk()
      Returns the SDK directory.
      static java.nio.file.Path getTestOutputDir()
      Returns a directory which tests can output data to.
      static java.nio.file.Path 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 java.nio.file.Path resolvePlatformPath​(java.lang.String path)
      Returns a file at path relative to the root for getLatestAndroidPlatform().
      static java.nio.file.Path resolveWorkspacePath​(java.lang.String relativePath)
      Given a relative path to a file or directory from the base of the current workspace, returns the absolute path.
      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 the Kotlin version to be used in new project templates and integration tests. If you are looking for the version of the Kotlin IDE plugin, prefer using KotlinCompilerVersion.getVersion() instead.
      • createTempDirDeletedOnExit

        @Deprecated
        public static java.nio.file.Path createTempDirDeletedOnExit()
                                                             throws java.io.IOException
        Deprecated.
        Temporary directories and files should be deleted after each test, not kept around for the lifetime of the JVM. This can be achieved by using a TemporaryFolder rule, or by calling the PathUtils.deleteRecursivelyIfExists(Path) method in tearDown.
        Creates a temporary directory that is deleted when the JVM exits.
        Throws:
        java.io.IOException
      • getWorkspaceRoot

        @NonNull
        public static java.nio.file.Path 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.

        TestUtils.getWorkspaceRoot().resolve("tools/adt/idea/android/testSrc");

        TestUtils.getWorkspaceRoot().resolve("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 resolveWorkspacePath(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).
      • resolveWorkspacePath

        @NonNull
        public static java.nio.file.Path resolveWorkspacePath​(@NonNull
                                                              java.lang.String relativePath)
        Given a relative path to a file or directory from the base of the current workspace, returns the absolute path.

        For example:

        TestUtils.resolveWorkspacePath("tools/adt/idea/android/testSrc");

        TestUtils.resolveWorkspacePath("prebuilts/studio/jdk");

        This method guarantees the file or directory 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.nio.file.Path getTestOutputDir()
                                                   throws java.io.IOException
        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.
        Throws:
        java.io.IOException
      • resolvePlatformPath

        @NonNull
        public static java.nio.file.Path resolvePlatformPath​(@NonNull
                                                             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.
        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.nio.file.Path getSdk()
        Returns the SDK directory.
        Returns:
        a valid Path 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.nio.file.Path getMockJdk()
      • getNdk

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

        @NonNull
        public static java.nio.file.Path 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.
        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.nio.file.Path 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()