androidx.test.core.app

Interfaces

ActivityScenario.ActivityAction

ActivityAction interface should be implemented by any class whose instances are intended to be executed by the main thread.

Classes

ActivityScenario

ActivityScenario provides APIs to start and drive an Activity's lifecycle state for testing.

ApplicationProvider

Provides ability to retrieve the current application Context in tests.

Top-level functions summary

Boolean

Returns false if calling takeScreenshot will fail.

inline ActivityScenario<A>
<A : Activity> launchActivity(intent: Intent?, activityOptions: Bundle?)

Launches an activity of a given class and constructs ActivityScenario with the activity.

inline ActivityScenario<A>
<A : Activity> launchActivityForResult(
    intent: Intent?,
    activityOptions: Bundle?
)

Launches an activity of a given class and constructs ActivityScenario with the activity.

Bitmap

Captures an image of the device's screen into a Bitmap.

Top-level functions

canTakeScreenshot

@ExperimentalTestApi
fun canTakeScreenshot(): Boolean

Returns false if calling takeScreenshot will fail.

Taking a screenshot requires UiAutomation and can only be called off of the main thread. If this method returns false then attempting to take a screenshot will fail. Note that taking a screenshot may still fail if this method returns true, for example if the call to UiAutomation fails.

launchActivity

inline fun <A : Activity> launchActivity(
    intent: Intent? = null,
    activityOptions: Bundle? = null
): ActivityScenario<A>

Launches an activity of a given class and constructs ActivityScenario with the activity. Waits for the lifecycle state transitions to be complete.

Normally this would be State.RESUMED, but may be another state.

This method cannot be called from the main thread except in Robolectric tests.

Parameters
intent: Intent? = null

an intent to start activity or null to use the default one

activityOptions: Bundle? = null

an activity options bundle to be passed along with the intent to start activity

Returns
ActivityScenario<A>

ActivityScenario which you can use to make further state transitions

Throws
kotlin.AssertionError

if the lifecycle state transition never completes within the timeout

launchActivityForResult

inline fun <A : Activity> launchActivityForResult(
    intent: Intent? = null,
    activityOptions: Bundle? = null
): ActivityScenario<A>

Launches an activity of a given class and constructs ActivityScenario with the activity. Waits for the lifecycle state transitions to be complete. Broadcasts activity result.

Normally this would be State.RESUMED, but may be another state.

This method cannot be called from the main thread except in Robolectric tests.

Parameters
intent: Intent? = null

an intent to start activity or null to use the default one

activityOptions: Bundle? = null

an activity options bundle to be passed along with the intent to start activity

Returns
ActivityScenario<A>

ActivityScenario which you can use to make further state transitions

Throws
kotlin.AssertionError

if the lifecycle state transition never completes within the timeout

takeScreenshot

@ExperimentalTestApi
fun takeScreenshot(): Bitmap

Captures an image of the device's screen into a Bitmap.

This is essentially a wrapper for UIAutomation#takeScreenshot() that attempts to get a stable screenshot by forcing all the current application's root window views to redraw, and also handles cases where hardware renderer drawing is disabled.

This API is intended for use cases like debugging where an image of the entire screen is needed. For use cases where the image will be used for validation, its recommended to take a more isolated, targeted screenshot of a specific view or compose node. See androidx.test.core.view.captureToBitmap, androidx.test.espresso.screenshot.captureToBitmap and androidx.compose.ui.test.captureToImage.

This API does not support concurrent usage.

This API is currently experimental and subject to change or removal.

Returns
Bitmap

a Bitmap that contains the image

Throws
kotlin.IllegalStateException

if called on the main thread. This is a limitation of connecting to UiAutomation, RuntimeException if UiAutomation fails to take the screenshot