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>
|
此规则提供单个活动的功能测试。 在Test
注解的每个测试之前以及Before
注解的方法之前,测试活动将启动。 测试完成后将终止并用After
注解的方法结束。 在测试期间,您将能够直接操作您的活动。
Public constructors |
|
---|---|
ActivityTestRule(Class<T> activityClass) 类似于 |
|
ActivityTestRule(Class<T> activityClass, boolean initialTouchMode) 类似于 |
|
ActivityTestRule(Class<T> activityClass, boolean initialTouchMode, boolean launchActivity) 为测试活动创建一个 |
Public methods |
|
---|---|
Statement |
apply(Statement base, Description description) |
T |
getActivity() |
T |
launchActivity(Intent startIntent) 启动测试中的活动。 |
Protected methods |
|
---|---|
void |
afterActivityFinished() 重写此方法以执行在 |
void |
afterActivityLaunched() 重写此方法以执行在启动 |
void |
beforeActivityLaunched() 重写此方法以执行在创建并启动 |
Intent |
getActivityIntent() 重写此方法来设置Intent,就像提供给 |
Inherited methods |
|
---|---|
From class android.support.test.rule.UiThreadTestRule
|
|
From class java.lang.Object
|
|
From interface org.junit.rules.TestRule
|
ActivityTestRule (Class<T> activityClass)
类似于 ActivityTestRule(Class, boolean, boolean)
但禁用了“触摸模式”。
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)
与ActivityTestRule(Class, boolean, boolean)
类似,但默认情况下每Test
方法启动一次测试活动。 它在第一个Before
方法之前启动,并在最后一个After
方法之后终止。
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)
为测试活动创建一个 ActivityTestRule
。
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 |
描述
|
Returns | |
---|---|
Statement |
T getActivity ()
Returns | |
---|---|
T |
The activity under test. |
T launchActivity (Intent startIntent)
启动测试中的活动。
不要直接调用此方法,除非你明确要求不使用launchActivity标志手动懒洋洋地启动活动 ActivityTestRule(Class, boolean, boolean)
。
用法:
@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. |
也可以看看:
void afterActivityFinished ()
重写此方法以执行在Activity
完成后应运行的任何代码。 在每种测试方法之后调用此方法,包括使用After
注释的任何方法。
void afterActivityLaunched ()
覆盖此方法以执行在启动 Activity
之后但在任何测试代码运行之前应运行的任何代码,包括使用 Before
注解的任何方法。
比这个方法更喜欢Before
。 这个方法通常不应该直接在测试中被覆盖,只有ActivityTestRule的子类可以用来在活动被创建并且可见但测试运行时得到通知。
void beforeActivityLaunched ()
重写此方法以执行在创建并启动Activity
之前应运行的任何代码。 此方法在每个测试方法之前调用,包括用Before
注解的任何方法。
Intent getActivityIntent ()
重写此方法来设置Intent,就像提供给 startActivity(Intent)
。
默认的意图(如果此方法返回null或未被覆盖)是:action = ACTION_MAIN
flags = FLAG_ACTIVITY_NEW_TASK
所有其他意图字段为空或空。
Returns | |
---|---|
Intent |
The Intent as if supplied to startActivity(Intent) . |