Class TestUtils
- java.lang.Object
-
- com.android.testutils.TestUtils
-
public class TestUtils extends java.lang.ObjectUtility methods to deal with loading the test data.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringKOTLIN_VERSION_FOR_TESTSKotlin version that is used in AGP integration tests.
-
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.PathcreateTempDirDeletedOnExit()Deprecated.Temporary directories and files should be deleted after each test, not kept around for the lifetime of the JVM.static voiddisableIfOnWindowsWithBazel()static voideventually(java.lang.Runnable runnable)Asserts that a runnable will eventually not throw an assertion exception.static voideventually(java.lang.Runnable runnable, java.time.Duration duration)Asserts that a runnable will eventually not throwAssertionErrorbeforetimeoutMsmilliseconds have ellapsedstatic java.nio.file.PathgetAapt2()Returns the checked in AAPT2 binary that is shipped with the gradle plugin.static java.lang.StringgetDesugarLibConfigContent()static java.nio.file.PathgetDesugarLibJar()static java.lang.StringgetDiff(java.lang.String[] before, java.lang.String[] after)static java.lang.StringgetDiff(java.lang.String[] before, java.lang.String[] after, int windowSize)static java.lang.StringgetDiff(java.lang.String before, java.lang.String after)static java.lang.StringgetDiff(java.lang.String before, java.lang.String after, int windowSize)static java.nio.file.PathgetJava11Jdk()static java.lang.StringgetLatestAndroidPlatform()static java.nio.file.PathgetLocalMavenRepoFile(java.lang.String path)Returns the path to a file in the local maven repository.static java.nio.file.PathgetMockJdk()static java.nio.file.PathgetNdk()Returns the path to checked-in NDK.static java.nio.file.PathgetPrebuiltOfflineMavenRepo()Returns the prebuilt offline Maven repository used during IDE tests.static java.lang.StringgetRelativeSdk()Returns the SDK directory relative to the workspace.static java.nio.file.PathgetRemoteSdk()Returns the remote SDK directory.static java.nio.file.PathgetSdk()Returns the SDK directory.static java.nio.file.PathgetTestOutputDir()Returns a directory which tests can output data to.static java.nio.file.PathgetWorkspaceRoot()Returns the root of the entire Android Studio codebase.static voidlaunchProcess(java.lang.Class toRun, java.lang.String... args)Launches a new process to execute the classtoRunmain() method and blocks until the process exits.static java.nio.file.PathresolvePlatformPath(java.lang.String path)Returns a file atpathrelative to the root forgetLatestAndroidPlatform().static java.nio.file.PathresolveWorkspacePath(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 booleanrunningFromBazel()Checks if tests were started by Bazel.static booleanrunningWithJdk11Plus(java.lang.String version)Checks if tests are running with Jdk 11 or above.static voidwaitForFileSystemTick()Sleeps the current thread for enough time to ensure that the local file system had enough time to notice a "tick".static voidwaitForFileSystemTick(long currentTimestamp)Sleeps the current thread for enough time to ensure that the local file system had enough time to notice a "tick".static booleanworkspaceFileExists(java.lang.String path)Returns true if the file exists in the workspace.
-
-
-
Field Detail
-
KOTLIN_VERSION_FOR_TESTS
public static final java.lang.String KOTLIN_VERSION_FOR_TESTS
Kotlin version that is used in AGP integration tests. Please note that this version does not have to be the same as the version of kotlinc used to build AGP (in Gradle or Bazel).This version needs to be present in prebuilts for tests to pass (see tools/base/bazel/README.md).
- See Also:
- Constant Field Values
-
-
Method Detail
-
createTempDirDeletedOnExit
@Deprecated public static java.nio.file.Path createTempDirDeletedOnExit() throws java.io.IOExceptionDeprecated.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 aTemporaryFolderrule, or by calling thePathUtils.deleteRecursivelyIfExists(Path)method intearDown.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.
-
workspaceFileExists
public static boolean workspaceFileExists(@NonNull java.lang.String path)Returns true if the file exists in the workspace.
-
getTestOutputDir
@NonNull public static java.nio.file.Path getTestOutputDir() throws java.io.IOExceptionReturns 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 atpathrelative to the root forgetLatestAndroidPlatform().- 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()
-
getLocalMavenRepoFile
@NonNull public static java.nio.file.Path getLocalMavenRepoFile(@NonNull java.lang.String path)Returns the path to a file in the local maven repository.- Parameters:
path- the path of the file relative to the maven repository root- Throws:
java.lang.IllegalArgumentException- if the path results in a file that's not found.
-
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.IOExceptionSleeps 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 interruptedjava.io.IOException- issues creating a temporary file
-
waitForFileSystemTick
public static void waitForFileSystemTick(long currentTimestamp) throws java.lang.InterruptedException, java.io.IOExceptionSleeps 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 interruptedjava.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 toeventually(Runnable, Duration), but using a default timeout- Parameters:
runnable- a description of the failure, if the condition never becomestrue
-
eventually
public static void eventually(@NonNull java.lang.Runnable runnable, java.time.Duration duration)Asserts that a runnable will eventually not throwAssertionErrorbeforetimeoutMsmilliseconds have ellapsed- Parameters:
runnable- a description of the failure, if the condition never becomestrueduration- 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 classtoRunmain() 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 classtoRunmain() 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()
-
-