Androidtestcase is a generic test class under an Android platform that supports all JUnit assert methods and standard setup and Teardown methods. If your test requires access to the resources of the application or the test method relies on the context, you can use Androidtestcase as the base class.
Its class inheritance relationship is shown in the following illustration:
Focus2androidtest test is also the Android Apidemos sample resolution (116): Views->focus->2. Horizontal
But the focus of the test is different, the content of the Focus2androidtest test does not need to start the activity, but rather the test r.layout.focus_2 's Layout (Resource) focuses In order to conform to the pre design (which can be seen as some static performance of the activity), you can test some of the static properties of the focus by using the Focusfinder method, which has the following code:
public class Focus2androidtest extends Androidtestcase {private Focusfinder mfocusfinder;
Private ViewGroup Mroot;
Private Button Mleftbutton;
Private Button Mcenterbutton;
Private Button Mrightbutton;
@Override protected void SetUp () throws Exception {Super.setup ();
Mfocusfinder = Focusfinder.getinstance ();
Inflate the layout final context = GetContext ();
Final Layoutinflater Inflater = layoutinflater.from (context);
Mroot = (viewgroup) inflater.inflate (r.layout.focus_2, NULL);
Manually measure it, and lay it out mroot.measure (500, 500);
Mroot.layout (0, 0, 500, 500);
Mleftbutton = (Button) Mroot.findviewbyid (R.id.leftbutton);
Mcenterbutton = (Button) Mroot.findviewbyid (R.id.centerbutton);
Mrightbutton = (Button) Mroot.findviewbyid (R.id.rightbutton); } @SmalLtest public void Testpreconditions () {assertnotnull (Mleftbutton);
Asserttrue ("center button should is right of the left button", Mleftbutton.getright () < Mcenterbutton.getleft ()); Asserttrue ("Right button should is right of center button", Mcenterbutton.getright () < Mrightbutton.getleft ()
); @SmallTest public void Testgoingrightfromleftbuttonjumpsovercentertoright () {assertequals ("rig HT should is next focus from left, Mrightbutton, Mfocusfinder.findnextfocus (Mroot, Mleftbutton, View.fo
Cus_right)); @SmallTest public void Testgoingleftfromrightbuttongoestocenter () {assertequals ("center should Be next focus from right, Mcenterbutton, Mfocusfinder.findnextfocus (Mroot, Mrightbutton, View.focus_le
FT)); }
}
Testgoingrightfromleftbuttonjumpsovercentertoright and Testgoingleftfromrightbuttongoestocenter
The Mfocusfinder Findnextfocus is used to test whether the next control that can gain focus in Mleftbutton,mrightbutton conforms to the prior design.
See a full set of tutorials: http://www.bianceng.cn/OS/extra/201301/35252.htm