ActivityTestRule

public class ActivityTestRule<T extends Activity>


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, 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 extends Activity>

The Activity class under test

Summary

Public constructors

ActivityTestRule(Class<T> activityClass)

Similar to ActivityTestRule but with "touch mode" disabled.

ActivityTestRule(Class<T> activityClass, boolean initialTouchMode)

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

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

Creates an ActivityTestRule for the Activity under test.

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

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

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

Finishes the currently launched Activity.

T

Returns the reference to the activity under test.

ActivityResult

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

T
launchActivity(Intent startIntent)

Launches the Activity under test.

void

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

Public constructors

ActivityTestRule

public ActivityTestRule(Class<T> activityClass)

Similar to ActivityTestRule but with "touch mode" disabled.

Parameters
Class<T> activityClass

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

See also
ActivityTestRule

ActivityTestRule

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

Similar to ActivityTestRule 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
Class<T> activityClass

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

boolean initialTouchMode

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

See also
ActivityTestRule

ActivityTestRule

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

Creates an ActivityTestRule for the Activity under test.

Parameters
SingleActivityFactory<T> activityFactory

factory to be used for creating Activity instance

boolean initialTouchMode

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

boolean launchActivity

true if the Activity should be launched once per Test method. It will be launched before the first Beforemethod, and terminated after the last Aftermethod.

ActivityTestRule

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

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

Parameters
Class<T> activityClass

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

boolean initialTouchMode

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

boolean launchActivity

true if the Activity should be launched once per Test method. It will be launched before the first Beforemethod, and terminated after the last Aftermethod.

ActivityTestRule

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

Creates an ActivityTestRule for the Activity under test.

Parameters
Class<T> activityClass

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

String targetPackage

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.

int launchFlags

launch flags to start the Activity under test.

boolean initialTouchMode

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

boolean launchActivity

true if the Activity should be launched once per Test method. It will be launched before the first Beforemethod, and terminated after the last Aftermethod.

Public methods

apply

public Statement apply(Statement base, Description description)

finishActivity

public void finishActivity()

Finishes the currently launched Activity.

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

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

getActivity

public T getActivity()

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 Activity#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 Activity#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.

getActivityResult

public ActivityResult getActivityResult()

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

This method must not be called before Activity.finish was called or after the activity was already destroyed.

Note: This method assumes Activity#setResult(int) is called no later than in onPause.

Returns
ActivityResult

the ActivityResult that was set most recently

Throws
java.lang.IllegalStateException java.lang.IllegalStateException

if the activity is not in finishing state.

launchActivity

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

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
Intent startIntent

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

Returns
T

the Activity launched by this rule.

runOnUiThread

public void runOnUiThread(Runnable runnable)

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 Runnable#run() method

Throws
java.lang.Throwable java.lang.Throwable
See also
UiThreadTest