Issue
I have an android app. When the user clicks button A and intent is fired like this android-presudocode :)
//inside FirstActivity
@Override
public void onClick(View view) {
startActivity(new Intent(this, AnotherActivity.class));
}
So if I'm not mistaken, the onResume method in AnotherActivity should be called, right?
I use ActivityInstrumentationTestCase2<FirstActivity> to test my activity but I'm unable to instantiate AnotherActivity.
So the question is, how can I test this: 'When a button is pressed, the correct activity is resumed and the correct extras are passed to the intent'.
Solution
You can use the instrumentation to make an ActivityMonitor. This will monitor if a new activity has been started.
ActivityMonitor am = getInstrumentation().addMonitor(Activity3.class.getName(), null, true;
Then you want to use button.performClick() to "press the button". Finally, you check if the activity monitor has been hit.
am.waitForActivitywithTimeout(timeout);
assertEquals(1, am.getHits());
I haven't used ActivityInstrumentationTestCase2 in quite a while so I don't guarantee these steps are exactly right. In any case, I recommend that you take a look at Robolectric: a wonderful unit testing framework for Android that will change your life. It will help you overcome many situations that are difficult or impossible to test with any of the default instrumentation tests.
Answered By - aleph_null
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.