See my code:
doSomeActionWillStartNewIntentToSelectImage();
// check started intent
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
Assert.assertThat(activity, new StartedMatcher(intent));
// simulate the returning result
shadowOf(activity).receiveResult(
new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI),
Activity.RESULT_OK,
new Intent().setData(activity.drawableId2Uri(R.drawable.icon_ok)));
But it reports an error:
java.lang.RuntimeException: No intent matches
Intent{action=android.intent.action.PICK, data=content://media/internal/images/media} among
[Intent{action=android.intent.action.PICK, data=content://media/internal/images/media}]
at com.xtremelabs.robolectric.shadows.ShadowActivity.receiveResult(ShadowActivity.java:381)
It’s strange that Intent{action=android.intent.action.PICK, data=content://media/internal/images/media} is exactly the same as the later one.
Where is wrong and how to fix it?
The intent passed into
receiveResultis not the same one started by the activity.The correct test code should be: