ActivityTestRule


class ActivityTestRule<T : Activity?> : TestRule

Known direct subclasses
IntentsTestRule

This class is deprecated.

Use androidx.test.espresso.intent.rule.IntentsRule, and if necessary in conjunction with androidx.test.core.app.ActivityScenario or instead.


This rule provides functional testing of a single Activity. When launchActivity is set to true in the constructor, the Activity under test will be launched before each test annotated with org.junit.Test and before methods annotated with , and it will be terminated after the test is completed and methods annotated with org.junit.After are finished.

The Activity can be manually launched with launchActivity, and manually finished with finishActivity. If the Activity is running at the end of the test, the test rule will finish it.

During the duration of the test you will be able to manipulate your Activity directly using the reference obtained from getActivity. If the Activity is finished and relaunched, the reference returned by getActivity will always point to the current instance of the Activity.

Parameters
<T : Activity?>

The Activity class under test

Summary

Public constructors

ActivityTestRule(activityClass: Class<T!>!)

Similar to ActivityTestRule but with "touch mode" disabled.

ActivityTestRule(activityClass: Class<T!>!, initialTouchMode: Boolean)

Similar to ActivityTestRule but defaults to launch the activity under test once per org.junit.Test method.

ActivityTestRule(
    activityClass: Class<T!>!,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)

Similar to ActivityTestRule but defaults to launch the Activity with the default target package name getTargetContext and FLAG_ACTIVITY_NEW_TASK launch flag.

ActivityTestRule(
    activityFactory: SingleActivityFactory<T!>!,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)

Creates an ActivityTestRule for the Activity under test.

ActivityTestRule(
    activityClass: Class<T!>!,
    targetPackage: String,
    launchFlags: Int,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)

Creates an ActivityTestRule for the Activity under test.

Public functions

Statement!
apply(base: Statement!, description: Description!)
Unit

Finishes the currently launched Activity.

T!

Returns the reference to the activity under test.

T!
launchActivity(startIntent: Intent?)

Launches the Activity under test.

Unit
runOnUiThread(runnable: Runnable!)

Helper method for running part of a method on the UI thread, blocking until it is complete.

Protected functions

Unit

Override this method to execute any code that should run after the currently launched is finished.

Unit

Override this method to execute any code that should run after your Activity is launched, but before any test code is run including any method annotated with .

Unit

Override this method to execute any code that should run before your Activity is created and launched.

Intent!

Override this method to set up a custom Intent as if supplied to startActivity.

Public properties

volatile Instrumentation.ActivityResult!

Public constructors

ActivityTestRule

ActivityTestRule(activityClass: Class<T!>!)

Similar to ActivityTestRule but with "touch mode" disabled.

Parameters
activityClass: Class<T!>!

The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml

See also
ActivityTestRule

ActivityTestRule

ActivityTestRule(activityClass: Class<T!>!, initialTouchMode: Boolean)

Similar to ActivityTestRule but defaults to launch the activity under test once per org.junit.Test method. It is launched before the first org.junit.Before method, and terminated after the last org.junit.After method.

Parameters
activityClass: Class<T!>!

The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml

initialTouchMode: Boolean

true if the Activity should be placed into "touch mode" when started

See also
ActivityTestRule

ActivityTestRule

ActivityTestRule(
    activityClass: Class<T!>!,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)

Similar to ActivityTestRule but defaults to launch the Activity with the default target package name getTargetContext and FLAG_ACTIVITY_NEW_TASK launch flag.

Parameters
activityClass: Class<T!>!

The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml

initialTouchMode: Boolean

true if the Activity should be placed into "touch mode" when started

launchActivity: Boolean

true if the Activity should be launched once per org.junit.Test method. It will be launched before the first org.junit.Before method, and terminated after the last org.junit.After method.

ActivityTestRule

ActivityTestRule(
    activityFactory: SingleActivityFactory<T!>!,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)

Creates an ActivityTestRule for the Activity under test.

Parameters
activityFactory: SingleActivityFactory<T!>!

factory to be used for creating Activity instance

initialTouchMode: Boolean

true if the Activity should be placed into "touch mode" when started

launchActivity: Boolean

true if the Activity should be launched once per org.junit.Test method. It will be launched before the first org.junit.Before method, and terminated after the last org.junit.After method.

ActivityTestRule

ActivityTestRule(
    activityClass: Class<T!>!,
    targetPackage: String,
    launchFlags: Int,
    initialTouchMode: Boolean,
    launchActivity: Boolean
)

Creates an ActivityTestRule for the Activity under test.

Parameters
activityClass: Class<T!>!

The activity under test. This must be a class in the instrumentation targetPackage specified in the AndroidManifest.xml

targetPackage: String

The name of the target package that the Activity is started under. This value is passed down to the start Intent using setClassName. Can not be null.

launchFlags: Int

launch flags to start the Activity under test.

initialTouchMode: Boolean

true if the Activity should be placed into "touch mode" when started

launchActivity: Boolean

true if the Activity should be launched once per org.junit.Test method. It will be launched before the first org.junit.Before method, and terminated after the last org.junit.After method.

Public functions

apply

fun apply(base: Statement!, description: Description!): Statement!

finishActivity

fun finishActivity(): Unit

Finishes the currently launched Activity.

Throws
java.lang.IllegalStateException

if the Activity is not running or failed to finish it.

getActivity

fun getActivity(): T!

Returns the reference to the activity under test.

The reference to the activity is assigned during the initial creation of the acivity and for every sinlge OnResumed lifecycle change.

Note: Lifecycle changes happen on the UI thread (not the instrumentation thread where this test code usually executes). Thus, the return value may vary depending on timing.

For example, if the activity is finished and relaunched, the reference returned by this method will point to the new instance of the activity assuming OnResumed was called prior to calling this method.

If the activity wasn't created yet or already finished, null will be returned.

Note: The activity reference is stored in a weak reference which means if the activity under test is detroyed (ex. back button was pressed) then the system no longer holds a strong reference to the acitivty and this refernce may get garbage collected.

launchActivity

fun launchActivity(startIntent: Intent?): T!

Launches the Activity under test.

Don't call this method directly, unless you explicitly requested not to lazily launch the Activity manually using the launchActivity flag in ActivityTestRule.

Usage:

   @Test
   public void customIntentToStartActivity() {
       Intent intent = new Intent(Intent.ACTION_PICK);
       activity = mActivityRule.launchActivity(intent);
   }
Note: Custom start Intents provided through this method will take precedence over default Intents that where created in the constructor and any Intent returned from getActivityIntent. The same override rules documented in getActivityIntent apply.
Parameters
startIntent: Intent?

The Intent that will be used to start the Activity under test. If startIntent is null, the Intent returned by getActivityIntent is used.

Returns
T!

the Activity launched by this rule.

runOnUiThread

fun runOnUiThread(runnable: Runnable!): Unit

Helper method for running part of a method on the UI thread, blocking until it is complete.

Note: In most cases it is simpler to annotate the test method with UiThreadTest.

Use this method if you need to switch in and out of the UI thread within your method.

Parameters
runnable: Runnable!

runnable containing test code in the run method

See also
UiThreadTest

Protected functions

afterActivityFinished

protected fun afterActivityFinished(): Unit

Override this method to execute any code that should run after the currently launched is finished. This method is called after each test method, including any method annotated with org.junit.After.

Prefer org.junit.After over this method. This method should usually not be overwritten directly in tests and only be used by subclasses of ActivityTestRule to get notified when the activity is created and visible but test runs.

afterActivityLaunched

protected fun afterActivityLaunched(): Unit

Override this method to execute any code that should run after your Activity is launched, but before any test code is run including any method annotated with .

Prefer org.junit.Before over this method. This method should usually not be overwritten directly in tests and only be used by subclasses of ActivityTestRule to get notified when the activity is created and visible but test runs.

beforeActivityLaunched

protected fun beforeActivityLaunched(): Unit

Override this method to execute any code that should run before your Activity is created and launched. This method is called before each test method, including any method annotated with org.junit.Before.

getActivityIntent

protected fun getActivityIntent(): Intent!

Override this method to set up a custom Intent as if supplied to startActivity. Custom Intents provided by this method will take precedence over default Intents that where created in the constructor but be overridden by any Intents passed in through launchActivity.

The default Intent (if this method returns null or is not overwritten) is: action = ACTION_MAIN flags = FLAG_ACTIVITY_NEW_TASK All other intent fields are null or empty.

If the custom Intent provided by this methods overrides any of the following fields:

These custom values will be used to start the Activity. However, if some of these values are not set, the default values documented in ActivityTestRule are supplemented.

Returns
Intent!

The Intent as if supplied to startActivity.

Public properties

activityResult

volatile val activityResultInstrumentation.ActivityResult!