ActivityTestRule

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


此规则提供单个活动的功能测试。 Test注解的每个测试之前以及Before注解的方法之前,测试活动将启动。 测试完成后将终止并用After注解的方法结束。 在测试期间,您将能够直接操作您的活动。

Summary

Public constructors

ActivityTestRule(Class<T> activityClass)

类似于 ActivityTestRule(Class, boolean, boolean)但禁用了“触摸模式”。

ActivityTestRule(Class<T> activityClass, boolean initialTouchMode)

类似于 ActivityTestRule(Class, boolean, boolean)但默认情况下每 Test方法启动一次测试活动。

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

为测试活动创建一个 ActivityTestRule

Public methods

Statement apply(Statement base, Description description)
T getActivity()
T launchActivity(Intent startIntent)

启动测试中的活动。

Protected methods

void afterActivityFinished()

重写此方法以执行在 Activity完成后应运行的任何代码。

void afterActivityLaunched()

重写此方法以执行在启动 Activity之后,但在运行任何测试代码之前应运行的任何代码,包括使用 Before注解的任何方法。

void beforeActivityLaunched()

重写此方法以执行在创建并启动 Activity之前应运行的任何代码。

Intent getActivityIntent()

重写此方法来设置Intent,就像提供给 startActivity(Intent)

Inherited methods

From class android.support.test.rule.UiThreadTestRule
From class java.lang.Object
From interface org.junit.rules.TestRule

Public constructors

ActivityTestRule

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

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

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.

Public methods

apply

Statement apply (Statement base, 
                Description description)

Parameters
base Statement
description 描述
Returns
Statement

getActivity

T getActivity ()

Returns
T The activity under test.

launchActivity

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.

也可以看看:

Protected methods

afterActivityFinished

void afterActivityFinished ()

重写此方法以执行在Activity完成后应运行的任何代码。 在每种测试方法之后调用此方法,包括使用After注释的任何方法。

afterActivityLaunched

void afterActivityLaunched ()

覆盖此方法以执行在启动 Activity之后但在任何测试代码运行之前应运行的任何代码,包括使用 Before注解的任何方法。

比这个方法更喜欢Before 这个方法通常不应该直接在测试中被覆盖,只有ActivityTestRule的子类可以用来在活动被创建并且可见但测试运行时得到通知。

beforeActivityLaunched

void beforeActivityLaunched ()

重写此方法以执行在创建并启动Activity之前应运行的任何代码。 此方法在每个测试方法之前调用,包括用Before注解的任何方法。

getActivityIntent

Intent getActivityIntent ()

重写此方法来设置Intent,就像提供给 startActivity(Intent)

默认的意图(如果此方法返回null或未被覆盖)是:action = ACTION_MAIN flags = FLAG_ACTIVITY_NEW_TASK所有其他意图字段为空或空。

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