android - Start second Activity from Unittest -
i want test if sec activity started upon button click
public void testlanguaeselect() { startactivity(activitylanguageintent, null, null); textview textviewtitlebartext = (textview) getactivity().findviewbyid(r.id.titlebartext); assertequals(textviewtitlebartext.gettext(), "sprache"); final textview textviewtextmiddle = (textview) getactivity().findviewbyid(r.id.listitemtextmiddle); assertequals(textviewtextmiddle.gettext(), "deutsch"); ((relativelayout) textviewtextmiddle.getparent()).performclick(); // check if intend sent intent startedactivityintent = getstartedactivityintent(); assertnotnull(startedactivityintent); // wait mainactivity started activitymonitor mainactivitymonitor = new instrumentation.activitymonitor(mainactivity.class.getname(), null, false); getinstrumentation().addmonitor(mainactivitymonitor); activity activity = mainactivitymonitor.waitforactivitywithtimeout(10000); assertnotnull("activity not started", activity); }
the onlclick function
@override public void onclick(view v) { v.startanimation(animationutils.loadanimation(v.getcontext(), r.layout.animation_item_click)); intent intent = new intent(v.getcontext(), mainactivity.class); intent.putextra("language", locale); v.getcontext().startactivity(intent); }
the intend right , onclick function called waitformonitorwithtimeout runs in timeout , returns null.
my android versions are
<uses-sdk android:minsdkversion="17" android:targetsdkversion="21" />
mainactivity secondactivity = (mainactivity) getinstrumentation().waitformonitorwithtimeout(monitor, 3);
the parameter 3 mentioned in above answer's method in milliseconds not in seconds. if 3, testcase failed because in 5 milliseconds can't load next activity. 5000 or 10000 milliseconds work better. in documentation have given in seconds in fact in milliseconds. next method work improve above method
mainactivity secondactivity = (mainactivity) getinstrumentation().waitformonitorwithtimeout(monitor,10000);
ref : test if button starts new activity in android junit (pref without robotium)?
android unit-testing
No comments:
Post a Comment