Skip to content

Most visited

Recently visited

navigation

ActivityTestRule

public class ActivityTestRule
extends Object implements TestRule

java.lang.Object
   ↳ android.support.test.rule.ActivityTestRule<T extends android.app.Activity>
Known Direct Subclasses


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 Test and before methods annotated with Before, and it will be terminated after the test is completed and methods annotated with After are finished.

The Activity can be manually launched with launchActivity(Intent), 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.

Summary

Constants

String FIELD_RESULT_CODE

String FIELD_RESULT_DATA

Public constructors

ActivityTestRule(Class<T> activityClass)

Similar to ActivityTestRule(Class, boolean) but with "touch mode" disabled.

ActivityTestRule(Class<T> activityClass, boolean initialTouchMode)

Similar to ActivityTestRule(Class, boolean, boolean) but defaults to launch the activity under test once per Test method.

ActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity)

Similar to ActivityTestRule(Class, String, int, boolean, boolean) but defaults to launch the Activity with the default target package name getTargetContext() and FLAG_ACTIVITY_NEW_TASK launch flag.

ActivityTestRule(SingleActivityFactory<T> activityFactory, boolean initialTouchMode, boolean launchActivity)

Creates an ActivityTestRule for the Activity under test.

ActivityTestRule(Class<T> activityClass, String targetPackage, int launchFlags, boolean initialTouchMode, boolean launchActivity)

Creates an ActivityTestRule for the Activity under test.

Public methods

Statement apply(Statement base, Description description)
void finishActivity()

Finishes the currently launched Activity.

T getActivity()
Instrumentation.ActivityResult getActivityResult()

This method can be used to retrieve the Activity result of an Activity that has called setResult.

T launchActivity(Intent startIntent)

Launches the Activity under test.

void runOnUiThread(Runnable runnable)

Helper method for running part of a method on the UI thread.

Protected methods

void afterActivityFinished()

Override this method to execute any code that should run after your Activity is finished.

void afterActivityLaunched()

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 Before.

void beforeActivityLaunched()

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

Intent getActivityIntent()

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

Inherited methods

From class java.lang.Object
From interface org.junit.rules.TestRule

Constants

FIELD_RESULT_CODE

String FIELD_RESULT_CODE

Constant Value: "mResultCode"

FIELD_RESULT_DATA

String FIELD_RESULT_DATA

Constant Value: "mResultData"

Public constructors

ActivityTestRule

ActivityTestRule (Class<T> activityClass)

Similar to ActivityTestRule(Class, boolean) but with "touch mode" disabled.

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

See also:

ActivityTestRule

ActivityTestRule (Class<T> activityClass,
                boolean initialTouchMode)

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

Parameters
activityClass Class: 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 (Class<T> activityClass,
                boolean initialTouchMode,
                boolean launchActivity)

Similar to ActivityTestRule(Class, String, int, boolean, boolean) but defaults to launch the Activity with the default target package name getTargetContext() and FLAG_ACTIVITY_NEW_TASK launch flag.

Parameters
activityClass Class: 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 Test method. It will be launched before the first Before method, and terminated after the last After method.

ActivityTestRule

ActivityTestRule (SingleActivityFactory<T> activityFactory,
                boolean initialTouchMode,
                boolean launchActivity)

Creates an ActivityTestRule for the Activity under test.

Parameters
activityFactory SingleActivityFactory: 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 Test method. It will be launched before the first Before method, and terminated after the last After method.

ActivityTestRule

ActivityTestRule (Class<T> activityClass,
                String targetPackage,
                int launchFlags,
                boolean initialTouchMode,
                boolean launchActivity)

Creates an ActivityTestRule for the Activity under test.

Parameters
activityClass Class: 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(android.content.Context, String). 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 Test method. It will be launched before the first Before method, and terminated after the last After method.

Public methods

apply

Statement apply (Statement base,
                Description description)

Parameters
base Statement

description Description

Returns
Statement

finishActivity

void finishActivity ()

Finishes the currently launched Activity.

Throws
IllegalStateException if the Activity is not running.

getActivity

T getActivity ()

Returns
T The activity under test.

getActivityResult

Instrumentation.ActivityResult getActivityResult ()

This method can be used to retrieve the Activity result of an Activity that has called setResult. Usually, the result is handled in onActivityResult of parent activity, that has called startActivityForResult.

This method must not be called before Activity.finish was called.

Returns
Instrumentation.ActivityResult the ActivityResult that was set most recently

launchActivity

T launchActivity (Intent startIntent)

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(Class, boolean, boolean).

Usage:

    @Test
    public void customIntentToStartActivity() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        mActivity = 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

void runOnUiThread (Runnable runnable)

Helper method for running part of a method on the UI thread.

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

Throws
Throwable

See also:

Protected methods

afterActivityFinished

void afterActivityFinished ()

Override this method to execute any code that should run after your Activity is finished. This method is called after each test method, including any method annotated with After.

afterActivityLaunched

void afterActivityLaunched ()

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 Before.

Prefer 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

void beforeActivityLaunched ()

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 Before.

getActivityIntent

Intent getActivityIntent ()

Override this method to set up a custom Intent as if supplied to startActivity(Intent). 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(Intent).

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(Class, String, int, boolean, boolean) are supplemented.

Returns
Intent The Intent as if supplied to startActivity(Intent).

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)