[Espresso 4 Android Doc] 5. Espresso 意图

声明:本系列文章是对 Android Testing Support Library官方文档的翻译,水平有限,欢迎批评指正。

1. Espresso 概览
2. Espresso 设置说明
3. Espresso 基础
4. Espresso 备忘录
5. Espresso 意图
6. Espresso 高级示例
7. Espresso Web
8. AndroidJUnitRunner
9. ATSL 中的 JUnit4 规则
10. UI Automator
11. 可访问性检查

Espresso-Intents 是 Espresso 的一个扩展,它使验证和存根待测应用向外发出的意图成为可能。它类似于 Mockito,但是针对的是 Android 的意图。

下载 Espresso-Intents

  • 确保你已经安装了 Android Support Repository(参考 instructions)。
  • 打开你应用的 ​build.gradle​ 文件。这一般不是顶级的 ​build.gralde​ 文件,而是 ​app/build.gradle​。

在 dependencies 中添加以下行:

androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

Espresso-Intents 只兼容 Espresso 2.1+ 和 testing support library 0.3。所以确保你也更新了以下行:

androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

IntentsTestRule

当使用 Espresso-Intents 时,应当用 ​IntentsTestRule​ 替换 ​ActivityTestRule​。IntentsTestRule​ 使得在 UI 功能测试中使用 Espresso-Intents API 变得简单。该类是 ​ActivityTestRule​ 的扩展,它会在每一个被 ​@Test​ 注解的测试执行前初始化 Espresso-Intents,然后在测试执行完后释放 Espresso-Intents。被启动的 activity 会在每个测试执行完后被终止掉,此规则也适用于 ​ActivityTestRule​。

验证意图(Intent validation)

Espresso-Intents 会记录待测应用里所有尝试启动 Activity 意图。使用 intended API(与 ​Mockito.verify​ 类似)你可以断言特定的意图是否被发出。

验证外发意图的简单示例:

@Test
public void validateIntentSentToPackage() {
    // 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"));
}

意图存根(Intent stubbing)

使用 intending API(与 ​Mockito.when​ 类似)你可以为通过 startActivityForResult 启动的 Activity 提供一个响应结果(尤其是外部的 Activity,因为我们不能操作外部 activity 的用户界面,也不能控制 ​ActivityResult​ 返回给待测 Activity)。

使用意图存根的示例:

@Test
public void activityResult_IsHandledProperly() {
    // 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.
    onView(withId(R.id.pickButton)).perform(click());

    // Assert that data we set up above is shown.
    onView(withId(R.id.phoneNumber).check(matches(withText(phoneNumber)));
}

意图匹配器(Intent Matchers)

​intending​ 和 ​intended​ 方法用一个 hamcrest ​Matcher<Intent>​ 作为参数。 Hamcrest 是匹配器对象(也称为约束或断言)库。有以下选项:

  • 使用现有的意图匹配器:最简单的选择,绝大多数情况的首选。
  • 自己实现意图匹配器,最灵活的选择(参考 Hamcrest 教程 的 “Writing custom matchers” 章节)

以下是一个使用现有的意图匹配器验证意图的示例:

intended(allOf(
    hasAction(equalTo(Intent.ACTION_VIEW)),
    hasCategories(hasItem(equalTo(Intent.CATEGORY_BROWSABLE))),
    hasData(hasHost(equalTo("www.google.com"))),
    hasExtras(allOf(
        hasEntry(equalTo("key1"), equalTo("value1")),
        hasEntry(equalTo("key2"), equalTo("value2")))),
        toPackage("com.android.browser")));
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Instrumentation介绍 Instrumentation是个什么东西? Instrumentation测...
    打不死的小强qz阅读 7,866评论 2 39
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,650评论 25 709
  • 爱有好多种:对朋友的爱,对家人的爱,对老师的爱等等。而有一种爱它不比亲情之爱轻,不比恋人之爱少,是歌手与歌迷之间的...
    弓长木水阅读 255评论 0 1
  • 前言:此文献给那些在大学中正处于迷茫,不知何去何从的大学生们。希望通过阅读本文章的内容,可以给广大大学生们一点启示...
    CooperNiu阅读 2,306评论 17 22