【问题标题】:Android Integration Test: How to mock ClipData of intent of the onActivityResult using the Expresso intentAndroid 集成测试:如何使用 Expresso 意图模拟 onActivityResult 的意图的 ClipData
【发布时间】: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


    【解决方案1】:

    ClipData 对象创建为

    1. 创建ClipDescriptionClipData.Item 对象

      val clipDescription = ClipDescription("Dummy", arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN))
      
      val uri = Uri.parse("http://www.google.com");
      
      val clipItem = ClipData.Item(uri)
      
    2. 创建ClipData 对象并将其在意图对象上设置为:

      val _clipData = ClipData(clipDescription, clipItem)
      
      resultData.clipData = _clipData
      

    您可以使用addItem 作为_clipData.addItem(clipItem) 添加更多项目。您可以使用ClipDescriptionClipData.Item 类的其他构造函数和方法来添加更多数据。

    或者,您可以根据您的模拟框架创建实际的模拟对象并模拟相同使用的对象的相应方法。

    【讨论】:

      猜你喜欢
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多