Android Test Tutorial: Androidtestcase sample

Source: Internet
Author: User
Tags final

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.