public
class
ActivityTestRule
extends UiThreadTestRule
java.lang.Object | ||
↳ | android.support.test.rule.UiThreadTestRule | |
↳ | android.support.test.rule.ActivityTestRule<T extends android.app.Activity> |
Known Direct Subclasses
IntentsTestRule<T extends Activity>
|
This rule provides functional testing of a single activity. The activity under test will be
launched before each test annotated with
Test
and before
methods annotated with
Before
. It
will be terminated after the test is completed and methods annotated with
After
are
finished. During the duration of the test you will be able to manipulate your Activity directly.
Public constructors | |
---|---|
ActivityTestRule(Class<T> activityClass)
Similar to |
|
ActivityTestRule(Class<T> activityClass, boolean initialTouchMode)
Similar to |
|
ActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity)
Creates an |
Public methods | |
---|---|
Statement
|
apply(Statement base, Description description)
|
T
|
getActivity()
|
T
|
launchActivity(Intent startIntent)
Launches the Activity under test. |
Protected methods | |
---|---|
void
|
afterActivityFinished()
Override this method to execute any code that should run after your |
void
|
afterActivityLaunched()
Override this method to execute any code that should run after your |
void
|
beforeActivityLaunched()
Override this method to execute any code that should run before your |
Intent
|
getActivityIntent()
Override this method to set up Intent as if supplied to
|
Inherited methods | |
---|---|
From
class
android.support.test.rule.UiThreadTestRule
| |
From
class
java.lang.Object
| |
From
interface
org.junit.rules.TestRule
|
ActivityTestRule (Class<T> activityClass)
Similar to ActivityTestRule(Class, boolean, 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 |
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 |
ActivityTestRule (Class<T> activityClass, 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 |
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.
|
Statement apply (Statement base, Description description)
Parameters | |
---|---|
base |
Statement
|
description |
Description
|
Returns | |
---|---|
Statement |
T getActivity ()
Returns | |
---|---|
T |
The activity under test. |
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); }
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. |
See also:
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
.
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.
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
.
Intent getActivityIntent ()
Override this method to set up Intent as if supplied to
startActivity(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.
Returns | |
---|---|
Intent |
The Intent as if supplied to startActivity(Intent) .
|