当 imageuri 作为额外传递时,Espresso 测试相机意图

Espresso test the camera intent when imageuri is passed as extra

我需要通过在 intent extra 中提供的路径创建图像文件来存根相机 intent。 Espresso 只能响应 activityresult。我在哪里可以执行操作以从 intent extra 传递的路径创建文件。

启动相机的代码

File destination = new File(Environment.getExternalStorageDirectory(), "app_name" + System.currentTimeMillis() + ".jpg");</p> <p>imageUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".fileprovider", destination); 意图意图=新意图(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);</p> <p>startActivityForResult(intent, AppConstants.REQUEST_CODE_CAMERA);

测试中存根意图的代码

Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null); intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);

您需要创建一个 IntentCallback 来拦截 Uri 值并在其中保存示例图像。

Kotlin 示例

  intentCallback = IntentCallback {
        if (it.action == "android.media.action.IMAGE_CAPTURE") {
            it.extras.getParcelable<Uri>("output").run {
                val inStream = Resources.getResource(sampleImageFileName).openStream()
                val outStream = activity.contentResolver.openOutputStream(this)
                ByteStreams.copy(inStream, outStream)
            }
        }
    }

您需要在意图触发事件之前注册您的回调

  IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback)

最后别忘了注销

  IntentMonitorRegistry.getInstance().removeIntentCallback(intentCallback)

伊斯梅尔的回答很完美。对于那些在 java 中寻找解决方案的人,这里是。

intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(
            new Instrumentation.ActivityResult(Activity.RESULT_OK, null));

IntentCallback intentCallback = new IntentCallback() {
        @Override
        public void onIntentSent(Intent intent) {
            if (intent.getAction().equals("android.media.action.IMAGE_CAPTURE")) {
                try {
                    Uri imageUri = intent.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
                    Context context = InstrumentationRegistry.getTargetContext();
                    Bitmap icon = BitmapFactory.decodeResource(
                            context.getResources(),
                            R.mipmap.ic_launcher);
                    OutputStream out = getTargetContext().getContentResolver().openOutputStream(imageUri);
                    icon.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();
                } catch (IOException e) {
                    GenericUtility.handleException(e);
                }
            }
        }
    };
    IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback);

 //Perform action here
 onView(withId(R.id.tv_take_photo)).perform(click());

Ismael 和 Gupta 的回答是正确的。对于那些想要完整示例的人,我根据他们在 Kotlin 中的示例做了一个完整的解决方案。下面的代码为多个 imageView 拍摄照片,并通过检查 imageView.tag 属性 验证是否在相应的 imageview 中加载了正确的图像。从开发代码来看,图片名称必须设置在 imageView.setTag(imageName)

private var imageName = "No Image Name"

@Test
fun verifyPhotoTaken() {
    intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(
            ActivityResult(Activity.RESULT_OK, null))

    takePhoto(R.id.imageview1, R.drawable.ic_launcher)
    takePhoto(R.id.imageview2, R.drawable.some_image)
}

private fun takePhoto(imageViewId : Int, resourceId : Int) {
    val cameraIntentCallback = intentCallback(resourceId)
    IntentMonitorRegistry.getInstance().addIntentCallback(cameraIntentCallback)
    onView(withId(imageViewId)).perform(click())
    onView(withId(imageViewId)).check(matches(hasDrawable(imageName)))
    IntentMonitorRegistry.getInstance().removeIntentCallback(cameraIntentCallback)
}

private fun intentCallback(resourceId : Int = R.drawable.ic_launcher) :IntentCallback  {
    return IntentCallback {
        if (it.action == MediaStore.ACTION_IMAGE_CAPTURE) {
            it.extras?.getParcelable<Uri>(MediaStore.EXTRA_OUTPUT).run {
                imageName = File(it.getParcelableExtra<Parcelable>(MediaStore.EXTRA_OUTPUT).toString()).name
                val context : Context = InstrumentationRegistry.getInstrumentation().targetContext
                val outStream = context.contentResolver.openOutputStream(this)
                val bitmap : Bitmap = BitmapFactory.decodeResource(context.resources, resourceId)
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
            }
        }
    }
}

fun hasDrawable(drawableResource: String) : BoundedMatcher<View, ImageView> {
    return object : BoundedMatcher<View, ImageView> (ImageView::class.java) {
        override fun describeTo(description: Description?) {
            description?.appendText("has drawable")
        }

        override fun matchesSafely(item: ImageView?): Boolean {
            return item?.drawable != null && item.tag.toString().contains(drawableResource)
        }

    }
}