Android Test Tutorial: ActivityInstrumentationTestCase2 sample

Source: Internet
Author: User

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

Related Article

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.