首页 > 解决方案 > 如何模拟用于浓缩咖啡测试的相机输入?

问题描述

我在我的应用程序中使用相机进行条形码检测,我想编写浓缩咖啡测试。问题是我不知道如何在浓缩咖啡中模拟相机,教程中只介绍了系统相机应用程序,但我有自己的相机源等。

有人有这方面的经验吗?

标签: androidtestingandroid-espressoandroid-instrumentation

解决方案


要使用 espresso 模拟自定义相机意图,您可以使用这种意图:

Intents.intending(IntentMatchers.hasComponent(YouCustomCameraView::class.java.name))
            .respondWith(ActivityResult(Activity.RESULT_OK, yourCustomIntent))

并将此依赖项添加到您的 gradle 以使用带有 espresso 的意图模拟

 androidTestImplementation('com.android.support.test.espresso:espresso-intents:2.2')

因此,当您执行应启动 YourCustomCameraView 的单击时,它将直接为您提供此 ActivityResult,而不是使用 yourCustomIntent。相机视图无法启动。


推荐阅读