Testing Fragment.startActivityForResult

304 views
Skip to first unread message

Daniel x

unread,
Jan 22, 2014, 5:55:05 PM1/22/14
to robol...@googlegroups.com
I have a Fragment (support.v4 to be precise) that calls startActivityForResult and I want to test the request code it is called with.

The only way I can see to test this is by inspecting the parent shadow activity:
IntentForResult intentForResult = shadowActivity.getNextStartedActivityForResult();
assertEquals(requestCode, intentForResult.requestCode);

Unfortunately, the request codes used in fragments are not what the activity sees, so this results in not very nice reverse engineering:
int activityRequestCode = (fragmentIndex<<16) + (fragRequestCode&0xffff);
assertEquals(activityRequestCode , intentForResult.requestCode);

I thought that I could call 
Robolectric.shadowOf(fragment).getNextStartedActivityForResult();

but unfortunately there isn't a ShadowFragment class in the shadows.

So two answers I'm looking for:
- Am I missing some reason why there isn't an existing ShadowFragment class?
- Is there a better way for me to test this?

Thanks

Erich Douglass

unread,
Jan 24, 2014, 11:34:58 AM1/24/14
to robol...@googlegroups.com
That sounds like a reasonable test to write.

The reason that ShadowFragment doesn't exist is that no one has needed one yet. We typically only write shadow classes when needed and no one has contributed one yet. If you find that it's something useful to have, submit a pull request. I'd be happy to look at it.


--
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.

Reply all
Reply to author
Forward
0 new messages