Android Test Framework Preliminary

Source: Internet
Author: User

First, the purpose of the experiment

1. Mastering the setup of Android test projects

2. Master the basics of the Android test framework

3. Write run Android Test

Second, the contents and steps of the experiment

To build the Android project MyProject, run as follows:

L Click the OK button to capitalize the letters in EditText

Click the hyperlink to open the Internet browser

Please use the knowledge to test this project, require:

1. Alignment test of components (Assertonscreen and assertrightaligned methods)

2, the EditText test (using SendKeys and Sendrepeatedkeys two methods)

3. Function test on button (PerformClick and SendKeys method)

4. Test the hyperlink (Activitymonitor inner Class)

5, in order to ensure the integrity and accuracy of testing, please add the necessary functions (such as prerequisites, various methods, etc.)

Note: The process for building an Android test project is as follows

1. New Android Test Project

2, after the establishment of the test project, in the test project in the SRC directory, right click on the package you created, select the new->junit test case, pop up the following interface:

Code
Package Com.sise.zhw;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;public Class Myprojectactivity extends Activity {    /** called when the activity is first created. */private EditText Mmessage;privat e Button mok;    @Override public    void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main);        Mmessage= (EditText) Findviewbyid (r.id.message);        mok= (Button) Findviewbyid (R.id.ok);        Mok.setonclicklistener (New Onclicklistener () {public void OnClick (View v) {//TODO auto-generated method Stubmmessage.settext (Mmessage.gettext (). toString (). toUpperCase ());}});}}    

Layout file

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G T;<imageview android:layout_width= "wrap_content" android:id= "@+id/imageview1" android:layout_height= "Wrap_con Tent "android:src=" @drawable/ic_launcher "android:layout_marginbottom=" 6dip "android:layout_gravity=" Center_horiz Ontal "android:layout_margintop=" 20dip "/><textview android:layout_width=" Wrap_content "Android:layout_ height= "Wrap_content" android:text= "myfirstprojecttest" android:layout_gravity= "center"/><textview and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "www.sise.com.cn" Android:layo     ut_gravity= "Center" android:autolink= "web" android:id= "@+id/link" android:textsize= "18SP"/><edittext Android:layout_height= "Wrap_contEnt "android:layout_width=" fill_parent "android:layout_marginbottom=" 0dip "android:layout_marginleft=" 6dip "an    droid:layout_marginright= "6dip" android:layout_margintop= "24dip" android:hint= "Sise" android:id= "@+id/message"    android:maxlines= "1"/><button android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "Right" android:layout_margin= "6dip" android:paddingleft= "24dip" android:paddingright= "2 4dip "android:text=" OK "android:id=" @+id/ok "/></linearlayout>

  

Test code
Package Com.sise.zhw.test;import static Android.test.moreasserts.assertnotcontainsregex;import static Android.test.viewasserts.assertonscreen;import Static Android.test.viewasserts.assertrightaligned;import Com.sise.zhw.myprojectactivity;import Android.app.instrumentation;import Android.app.instrumentation.activitymonitor;import Android.content.intent;import Android.content.IntentFilter; Import Android.test.activityinstrumentationtestcase2;import Android.test.touchutils;import Android.test.uithreadtest;import Android.view.keyevent;import Android.view.view;import Android.widget.Button; Import Android.widget.edittext;import Android.widget.textview;public class myfirstprojectactivitytests extendsactivityinstrumentationtestcase2<myprojectactivity> {private myprojectactivity mActivity;private EditText mmessage;private Button mcapitalize;private TextView mlink;private Instrumentation Minstrumentation;public Myfirstprojectactivitytests () {This ("myfirstprojectactivitytests");} Public MyfirsTprojectactivitytests (String name) {super (Myprojectactivity.class); SetName (name);} protected void SetUp () throws Exception {Super.setup (); Setactivityinitialtouchmode (false); mactivity = Getactivity (); Minstrumentation = Getinstrumentation (); mLink = (TextView) Mactivity.findviewbyid (com.sise.zhw.r.id.link); mMessage = (EditText) Mactivity.findviewbyid (com.sise.zhw.r.id.message); mcapitalize = (Button) Mactivity.findviewbyid ( Com.sise.zhw.r.id.ok);} protected void TearDown () throws Exception {Super.teardown ();} public void Testpreconditions () {assertnotnull (mactivity); Assertnotnull (minstrumentation); Assertnotnull (MLink); Assertnotnull (Mmessage); Assertnotnull (mcapitalize);} public void Testalignment () {final int margin = 0;assertrightaligned (mmessage, mcapitalize, margin);} public void Testuserinterfacelayout () {final int margin = 0;final View origin = Mactivity.getwindow (). Getdecorview (); Asse Rtonscreen (origin, Mmessage); Assertonscreen (origin, Mcapitalize); assertrightaligned (Mmessage, Mcapitalize, Margin);} public void Testuserinterfacelayoutwithotherorigin () {final int margin = 0; View origin = Mmessage.getrootview (); Assertonscreen (origin, mmessage); origin = Mcapitalize.getrootview (); Assertonscreen (origin, Mcapitalize); assertrightaligned (Mmessage, mcapitalize, margin);} @UiThreadTestpublic void Testnoerrorincapitalization () {final String msg = "This is a sample"; Mmessage.settext (msg); Mcapitalize.performclick (); final string actual = Mmessage.gettext (). toString (); final string notexpectedregexp = "(? I: Error) "; Assertnotcontainsregex (" Capitalization found error: ", Notexpectedregexp, Actual);} public void Testfollowlink () {final Instrumentation inst = getinstrumentation (); Intentfilter intentfilter = new Intentfil ter (Intent.action_view); Intentfilter.adddatascheme ("http"); Intentfilter.addcategory (intent.category_browsable) ; Activitymonitor monitor = Inst.addmonitor (Intentfilter, NULL, FALSE); Assertequals (0, Monitor.gethits ()); Touchutils.clickview (this, mLink); Monitor.waitforactivItywithtimeout (Assertequals) (1, monitor.gethits ()); Inst.removemonitor (monitor); private void Requestmessagefocus () {try {runtestonuithread (new Runnable () {public void run () {Mmessage.requestfocus ()}) ;} catch (Throwable e) {fail ("couldn ' t set focus");} Minstrumentation.waitforidlesync ();} public void Testsendkeyints () {requestmessagefocus (); SendKeys (Keyevent.keycode_h,keyevent.keycode_e, Keyevent.keycode_e,keyevent.keycode_e,keyevent.keycode_y,keyevent.keycode_alt_left,keyevent.keycode_1, Keyevent.keycode_dpad_down,keyevent.keycode_enter); final String expected = "heeey!"; Final String actual = Mmessage.gettext (). toString (); Assertequals (expected, actual);} public void testsendkeystring () {requestmessagefocus (); SendKeys ("H 3*e Y alt_left 1 dpad_down ENTER"); final String expect ed = "heeey!"; Final String actual = Mmessage.gettext (). toString (); Assertequals (expected, actual);} public void Testsendrepeatedkeys () {requestmessagefocus (); Sendrepeatedkeys (1, keyevent.keycode_h,3, Keyevent.keycode_e,1, keyevent.keycode_y,1, keyevent.keycode_alt_left,1, keyevent.keycode_1,1, keyevent.keycode_dpad_down,1, Keyevent.keycode_enter); final String expected = "heeey!"; Final String actual = Mmessage.gettext (). toString (); Assertequals (expected, actual);} public void Testcapitalizationsendingkeys () {final String keyssequence = "T e S t SPACE M E"; Requestmessagefocus (); Sendkey S (keyssequence); Touchutils.clickview (this, mcapitalize); final string expected = "Test Me". toUpperCase (); final string actual = Mmessage.gettext (). toString (); Assertequals (expected, actual);} public void Testactivitypermission () {final string pkg= "COM.SISE.ZHW"; final string activity=pkg+ ". Mycontactsactivity "; final String permission=" Android. MainFest.permission.CALL_PHONE ";//assertactivityrequirespermission (Pkg,activity,permission);}}

  

Android Test Framework Preliminary

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.