How to stub Intent.createChooser Intent using Espresso

≡放荡痞女 提交于 2021-01-28 23:18:48

问题


Problem

I have an image inside my app, and am sharing it to any other app that can handle image sharing, and the feature is working perectlty.

I am writing an Espresso UI test to intercept the intent and ensure it has the correct action and extras, but cannot seem to get it to work.

Code

Here is the code when creating the intent:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(MediaType.PNG.toString());
startActivity(Intent.createChooser(intent, "send");

and here is my attempt to match the Intent in my test, but fails to find a match:

Intents.init();
launchActivity(MyFragment.newIntent(getTargetContext());

Matcher<Intent> expectedIntent = allOf(
    hasAction(Intent.ACTION_CHOOSER),
    hasExtra(
        Intent.ACTION_SEND,
        hasExtra(Intent.EXTRA_STREAM, EXPECTED_SHARE_URI) // Expected URI has been copied from the extras 'uriString' value when debugging
    )
);
intending(expectedIntent).respondWith(new Instrumentation.ActivityResult(0, null));
MyScreen.clickShareButton(); // performs click on the share button
intended(expectedIntent);
Intents.release();

Error

IntentMatcher: (has action: is "android.intent.action.CHOOSER" and has extras: has bundle with: key: is "android.intent.extra.STREAM" value: is "[my uri appears here]")

Additional info

When debugging, the intent that is created results in an intent with action "android.intent.action.CHOOSER", and has an extra of type Intent, with action "android.intent.action.SEND" and type "image/png", and in turn has an extra, a HierarchicalUri with a uriString.

Summary

Anyone know what I am doing wrong? I can't find a way to tie all this together and create a matcher for this intent. Any help would be greatly appreciated!


回答1:


If you're getting an error on this line intended(expectedIntent) because the intent didn't match, it's probably because Intent.createChooser put your intent as an extra data with the key Intent.EXTRA_INTENT. In your case, you only need an extra intent matcher for the chooser:

Matcher<Intent> intent = allOf(
    hasAction(Intent.ACTION_SEND),
    hasExtra(Intent.EXTRA_STREAM, EXPECTED_SHARE_URI)
);

Matcher<Intent> expectedIntent = allOf(
    hasAction(Intent.ACTION_CHOOSER),
    // Intent.createChooser put your intent with the key EXTRA_INTENT
    hasExtra(Intent.EXTRA_INTENT, intent)
);

intending(anyIntent()).respondWith(new Instrumentation.ActivityResult(0, null));

MyScreen.clickShareButton();

intended(expectedIntent);


来源:https://stackoverflow.com/questions/60628692/how-to-stub-intent-createchooser-intent-using-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!