【发布时间】:2020-03-10 16:57:12
【问题描述】:
我正在使用 Kotlin 开发一个 Android 应用程序。我正在使用 Expresso 框架为我的应用程序编写集成测试。现在,我正在努力模拟 onActivityResult 回调意图的 ClipData。我正在使用 Expresso 框架的 expresso-intent 来模拟意图。
以下是我的onActivityResult回调方法的实现。
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.i(TAG, "Start handleGalleryActivityResult")
if (data?.clipData?.itemCount == null) {
return
}
if (data?.clipData?.itemCount as Int > 0) {
Log.i(TAG, "handleGalleryActivityResult: clipData count is greater than zero")
for (i in 0 until data?.clipData?.itemCount as Int) {
Log.i(TAG, "Processing index ${i}")
if (data?.clipData?.getItemAt(i)?.uri != null) {
val file: File = File(data?.clipData?.getItemAt(i)?.uri?.path)
Log.i(TAG, "Picked gallery file ${data?.clipData?.getItemAt(i)?.uri?.path}")
} else {
Log.i(TAG, "Picked gallery file at index ${i} is null")
}
}
}
}
正如您在 onActivityResult 回调方法中看到的,我正在检索意图的 clipData。
我正在编写一个模拟返回意图的测试,如下所示。
@Test fun filesAreUploadedToServerWhenPickedUpFromGallery() {
this.launchActivityWithIntent()
val resultData = Intent()
val result = Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)
intending(IntentMatchers.hasAction(Intent.ACTION_PICK)).respondWith(result)
onView(withId(R.id.camera_image_btn_gallery)).perform(click())
//the rest of the code goes here
}
正如您在我的代码中看到的那样,我正在模拟这样返回的意图。
intending(IntentMatchers.hasAction(Intent.ACTION_PICK)).respondWith(result)
我的问题是如何将剪辑数据传递给意图来模拟它?
【问题讨论】:
标签: android android-intent integration-testing