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

时间:2017-05-22 13:20:57

标签: android android-intent android-espresso

我需要通过在intent extra中提供的路径上创建一个图像文件来保持相机意图。 浓缩咖啡只能响应活动结果。我在哪里可以执行操作来从intent extra的传递路径创建文件。

启动相机的代码

if($1 in a) $0=$0 FS a[$1]

File destination = new File(Environment.getExternalStorageDirectory(), "app_name" + System.currentTimeMillis() + ".jpg");

imageUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".fileprovider", destination); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

测试中存根意图的代码

startActivityForResult(intent, AppConstants.REQUEST_CODE_CAMERA);

3 个答案:

答案 0 :(得分:7)

Ismael的回答是完美的。对于那些在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());

答案 1 :(得分:6)

您需要创建一个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)

答案 2 :(得分:0)

Ismael和Gupta的答案是正确的。对于那些想要完整示例的人,我根据他们在Kotlin中的示例提出了完整的解决方案。以下代码为多个imageView拍照,并通过检查imageview属性来验证相应的imageView.tag中是否加载了正确的图像。根据开发代码,必须在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)
        }

    }
}