ACTIVITYINSTRUMENTATIONTESTCASE2 is used to test a single activity, the activity being tested can be started using instrumentationtestcase.launchactivity, Then you can directly manipulate the activity being tested.
ACTIVITYINSTRUMENTATIONTESTCASE2 also supports:
You can run test methods in the UI thread.
Can inject intent object into the activity being tested
ActivityInstrumentationTestCase2 replaces the previous activityinstrumentationtestcase, The new test should use ACTIVITYINSTRUMENTATIONTESTCASE2 as the base class.
Focus2activitytest's code is as follows for testing the Android Apidemos sample parsing (116): Views->focus->2. Horizontal
public class Focus2activitytest extends activityinstrumentationtestcase2<focus2> {private Button mleftbut
ton
Private Button Mcenterbutton;
Private Button Mrightbutton;
Public Focus2activitytest () {Super ("Com.example.android.apis", Focus2.class);
@Override protected void SetUp () throws Exception {Super.setup ();
Final Focus2 a = getactivity ();
Mleftbutton = (Button) A.findviewbyid (R.id.leftbutton);
Mcenterbutton = (Button) A.findviewbyid (R.id.centerbutton);
Mrightbutton = (Button) A.findviewbyid (R.id.rightbutton); @MediumTest public void Testpreconditions () {asserttrue ("center button should is right to left
Button ", Mleftbutton.getright () < Mcenterbutton.getleft ());
Asserttrue ("Right button should is right of center button", Mcenterbutton.getright () < Mrightbutton.getleft ()); Asserttrue ("Left button should is focused", Mleftbutton.isfocusEd ()); @MediumTest public void Testgoingrightfromleftbuttonjumpsovercentertoright () {SendKeys (Keyevent.keycode_
Dpad_right);
Asserttrue ("Right button should to be focused", mrightbutton.isfocused ()); @MediumTest public void Testgoingleftfromrightbuttongoestocenter () {getactivity (). Runonuithread () (New Ru
Nnable () {public void run () {mrightbutton.requestfocus ();
}
});
Wait for the request to go through Getinstrumentation (). Waitforidlesync ();
Asserttrue (mrightbutton.isfocused ());
SendKeys (Keyevent.keycode_dpad_left);
Asserttrue ("center button should be focused", mcenterbutton.isfocused ()); }
}
See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm