Issue
I need to stub the camera intent by creating a image file at the path provided in the intent extra. Espresso can only respond with activityresult. Where can i perform the operation to create the file at passed path from intent extra.
Code for launching camera
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);
Code for stubbing intent in test
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null);
intending(hasAction(MediaStore.ACTION_IMAGE_CAPTURE)).respondWith(result);
Solution
You need to create a IntentCallback to intercept the Uri
value and save a sample image there.
Sample in 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)
}
}
}
You need to register your callback before the intent trigger event
IntentMonitorRegistry.getInstance().addIntentCallback(intentCallback)
And don't to forget to unregister at end
IntentMonitorRegistry.getInstance().removeIntentCallback(intentCallback)
Answered By - Ismael Di Vita
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.