Testing intent resolution

466 views
Skip to first unread message

Jonathan Gerrish

unread,
Oct 2, 2013, 12:29:37 PM10/2/13
to robol...@googlegroups.com
Hi,

Is there a way to effectively test Android intent resolution using Robolectric? More specifically, is there a way to test how a particular intents action, category and data fields resolve to an activity as defined by that activities intent filter in the AndroidManifest.xml.

I know I can call

Robolectric.shadowOf(myActivity).peekNextStartedActivity() but this really just returns the captured intent, so I can only just test the values that I expected the intent to have but what I actually want to do is test how android would resolve those values against the activity filters defined in my AndroidManifest.xml so I can have confidence they would resolve at run time. The alternative would be an integration test, but I'd prefer to avoid this if possible.

@see http://developer.android.com/guide/components/intents-filters.html#ires

Thanks, Jonathan.

Mike Grafton

unread,
Oct 2, 2013, 12:47:26 PM10/2/13
to robol...@googlegroups.com
Hi Jonthan,

Great question. This is not currently possible but I know it's on the List of Awesome Things For the Future Robolectric. I've been told by Christian Williams that he doesn't think this would be particularly difficult. 

I'd like to see this as well. If you have any ideas on how it would actually look to the test writer, or how to implement this, please do post. We'll see if we can get this into our planning for 2.4 or so.

Mike


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jonathan Gerrish

unread,
Oct 2, 2013, 1:22:14 PM10/2/13
to robol...@googlegroups.com
Hi Mike,

Good to hear :-)

I was trying this, hoping it would work:-

ComponentName resolvedComponent = Robolectric.shadowOf(myActivity)
.peekNextStartedActivity()
.resolveActivity(Robolectric.packageManager);
assertThat(resolvedComponent.getClassName(),
is(ExpectedActivity.class.getName());

but resolvedComponent == null in this case.

What do you think?

Jonathan

Christian Williams

unread,
Oct 3, 2013, 7:09:44 PM10/3/13
to robol...@googlegroups.com
Hey Jonathan! Yay, you're using Robolectric!

Here's the totally wrong way to do this (that may or may not work for you): https://github.com/robolectric/robolectric/pull/733

It misses lots of subtleties of intent resolution. A much better way to handle this would involve pulling in the PackageManagerService class, shadowing away lots of its ickier bits, and using its native code to process AndroidManifest.xml files.

Worth trying, I think, but not a quickie.

--X

Jonathan Gerrish

unread,
Oct 8, 2013, 10:37:12 AM10/8/13
to robol...@googlegroups.com
Hey Christian,

Yeah, something like that would work as a starting point, I guess we'd
need to add some of the complexities around matching between action,
category and data vs the intent-filter as well to support our needs.

Robolectric has been working well for us, converted > 3000 android
tests, now TDD is a reality for our team - Thank you! :-)

Jonathan

Christian Williams

unread,
Oct 8, 2013, 2:44:04 PM10/8/13
to robol...@googlegroups.com
Cool, sounds like we just need to pull in PackageManagerService. I think that can happen for 2.3.

Glad to hear Robolectric is making your life nicer!

--X
Reply all
Reply to author
Forward
0 new messages