public final class Intents
extends Object
java.lang.Object | |
↳ | android.support.test.espresso.intent.Intents |
Intents可以验证被测试应用程序发出的意图并对其进行存根化。
一个简单验证传出意图的示例测试:
public void testValidateIntentSentToPackage() {
// User action that results in an external "phone" activity being launched.
user.clickOnView(system.getView(R.id.callButton));
// Using a canned RecordedIntentMatcher to validate that an intent resolving
// to the "phone" activity has been sent.
intended(toPackage("com.android.phone"));
}
具有意图存根的示例测试:
public void testActivityResultIsHandledProperly() {
// Build a result to return when a particular activity is launched.
Intent resultData = new Intent();
String phoneNumber = "123-345-6789";
resultData.putExtra("phone", phoneNumber);
ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);
// Set up result stubbing when an intent sent to "contacts" is seen.
intending(toPackage("com.android.contacts")).respondWith(result));
// User action that results in "contacts" activity being launched.
// Launching activity expects phoneNumber to be returned and displays it on the screen.
user.clickOnView(system.getView(R.id.pickButton));
// Assert that data we set up above is shown.
assertTrue(user.waitForText(phoneNumber));
}
Public methods |
|
---|---|
static void |
assertNoUnverifiedIntents() 声明Intents没有任何未经验证的意图。 |
static void |
init() 初始化意图并开始记录意图。 |
static void |
intended(Matcher<Intent> matcher, VerificationMode verificationMode) 断言给定的匹配器匹配测试中的应用程序发送的指定数量的意图。 |
static void |
intended(Matcher<Intent> matcher) 断言给定的匹配器匹配被测应用程序发送的一个且仅有的一个意图。 |
static OngoingStubbing |
intending(Matcher<Intent> matcher) 启用存根意图响应。 |
static void |
release() 清除意图状态。 |
static VerificationMode |
times(int times) 允许验证被测应用程序发送的特定数量的意图。 |
Inherited methods |
|
---|---|
From class java.lang.Object
|
void assertNoUnverifiedIntents ()
声明Intents没有任何未经验证的意图。 验证意图后,您可以使用此方法确保没有意外的情况发出。 这相当于Mockito中的verifyNoMoreInteractions()。
void init ()
初始化意图并开始记录意图。 必须在触发任何发出意图需要验证或存根的行为之前被调用。 这与MockitoAnnotations.initMocks类似。
void intended (Matcher<Intent> matcher, VerificationMode verificationMode)
断言给定的匹配器匹配测试中的应用程序发送的指定数量的意图。 这与Mockito中的验证(模拟,次数(num))相当。 验证不必按照意图发送的顺序进行。 意图从Intents.init被调用时开始记录。
Parameters | |
---|---|
matcher |
Matcher : the Matcher to be applied to captured intents |
verificationMode |
VerificationMode
|
Throws | |
---|---|
|
if the given Matcher did not match the expected number of recorded intents |
void intended (Matcher<Intent> matcher)
断言给定的匹配器匹配被测应用程序发送的一个且仅有的一个意图。 这相当于Mockito中的验证(模拟,时间(1))。 验证不必按照意图发送的顺序进行。 意图从Intents.init被调用时开始记录。
Parameters | |
---|---|
matcher |
Matcher : the Matcher to be applied to captured intents |
Throws | |
---|---|
|
if the given Matcher did not match any or matched more than one of the recorded intents |
OngoingStubbing intending (Matcher<Intent> matcher)
启用存根意图响应。 这种方法与Mockito.when相似,当启动intent的活动需要返回数据时(特别是在目标活动是外部的情况下),该方法特别有用。 在这种情况下,测试作者可以调用意向(匹配器).thenRespond(myResponse)并验证启动活动是否正确处理结果。 注意:目标活动不会启动。
Parameters | |
---|---|
matcher |
Matcher : the Matcher that matches intents for which stubbed response should be provided |
Returns | |
---|---|
OngoingStubbing |
OngoingStubbing object to set stubbed response |
void release ()
清除意图状态。 必须在每个测试用例之后调用。
VerificationMode times (int times)
允许验证被测应用程序发送的特定数量的意图。 这相当于Mockito中的次数(num)。
Parameters | |
---|---|
times |
int : the number of times that the intent should be matched. |
Returns | |
---|---|
VerificationMode |