How can i test share intent with espresso-intents?

3,897 views
Skip to first unread message

Igor Matos

unread,
Oct 29, 2015, 2:12:28 PM10/29/15
to Android Testing Support Library
Hello, i've searched a lot and still didnt found a way to do this, probably i am loosing something.

The case:
I have one share button wich create a intent createChooser, and i want to test if the expected url is being shared.

The problem is that the share intent is encapsulated by chooser. 

At the test i can match the action_chooser but not action_send, wich is the intent that has the url that i want to check.


THE RECORDED INTENT
Recorded intents:

-Intent { act=android.intent.action.CHOOSER (has extras) } handling packages:[[android]],
extras
:[Bundle[{android.intent.extra.INTENT=Intent { act=android.intent.action.SEND typ=text/plain (has extras) }, android.intent.extra.TITLE=Share post link}]])


Thanks in advance.

Igor Matos

unread,
Nov 5, 2015, 3:19:22 PM11/5/15
to Android Testing Support Library
I misunderstood the stubbing, and i was stubbing the chooser but i shouldn't. I dont stub anymore and i fixed my code with Luis Ramirez help.

intended(allOf(hasAction(Intent.ACTION_CHOOSER),
 hasExtra
(is(Intent.EXTRA_INTENT),
 allOf
( hasAction(Intent.ACTION_SEND),
 hasExtra
(Intent.EXTRA_TEXT, "Expected url") ))));

It worked =))

papp.r...@gmail.com

unread,
Aug 26, 2016, 6:49:54 AM8/26/16
to Android Testing Support Library
Awesome! Thank you for the example.

Here's a reusable method to make it easier to read:
public static Matcher<Intent> chooser(Matcher<Intent> matcher) {
return allOf(hasAction(Intent.ACTION_CHOOSER), hasExtra(is(Intent.EXTRA_INTENT), matcher));
}

intended(chooser(allOf(
      hasAction(Intent.ACTION_SEND),
hasExtra(Intent.EXTRA_TEXT, "Expected url")
)));
Reply all
Reply to author
Forward
0 new messages