首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Espresso测试相机的意图当imageuri被作为额外的

Espresso测试相机的意图当imageuri被作为额外的
EN

Stack Overflow用户
提问于 2017-05-22 13:20:56
回答 3查看 2.4K关注 0票数 5

我需要存根相机的意图,创建一个图像文件在路径提供的意图额外。浓缩咖啡只能有积极的效果。我可以在哪里执行操作,以便在传递的路径上创建文件。

用于发射相机的代码

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);

代码用于测试中的固执意图

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-08 22:35:04

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

Kotlin样本

代码语言:javascript
复制
  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)
            }
        }
    }

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

代码语言:javascript
复制
  IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback)

最后别忘了注销

代码语言:javascript
复制
  IntentMonitorRegistry.getInstance().removeIntentCallback(intentCallback)
票数 7
EN

Stack Overflow用户

发布于 2017-10-09 10:31:57

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

代码语言:javascript
复制
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());
票数 11
EN

Stack Overflow用户

发布于 2019-04-12 02:19:47

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

代码语言:javascript
复制
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)
        }

    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44114008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档