Home >

Testing on Android

6. January 2010

androidFor the last couple of months, I have been working on Android platform as part of Software Engineering course(more on that later, i hope). Even though there are a lot of things I don’t like about android (like XML layouts and the ids of widgets being held in some other class etc but perhaps this is just me), I overall find it a good platform to work with.

What I really liked in Android is that it provides an Out-of-the-box environment for easy testing android applications. I don’t know how Windows Mobile projects handle this situation, but what android does is that it executes the test in the context of virtual machine. Android integrates nicely with JUnit framework, which is the java brother of NUnit.

What is more interesting than this is that Android also has UI testing framework. Being a person who hasn’t written UI tests much (as in none), I can’t say if it is a good one or not. I just liked the feature that came out of the box.

I have created a simple Activity (you can think of it as a window, but not exactly)

public class SampleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button b=(Button)findViewById(R.id.Button01);
        final EditText t=(EditText)findViewById(R.id.EditText01);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                t.setText(t.getText().toString()+"hello");
            }
        });
    }
}

And created the following test.

public class SampleActivityInstrumentation extends
		android.test.ActivityInstrumentationTestCase2 {

	public SampleActivityInstrumentation() {
		super("com.myandroid", SampleActivity.class);
		// TODO Auto-generated constructor stub
	}

	public void test_OK_button_appends_hello() throws Throwable {
		SampleActivity tne = (SampleActivity) getActivity();

		final Button btn=(Button) tne.findViewById(com.myandroid.R.id.Button01);
		final EditText titleEdit = (EditText) tne.findViewById(com.myandroid.R.id.EditText01);
		runTestOnUiThread(new Runnable() {
			public void run() {
				titleEdit.performClick();
			}
		});
		sendKeys("H E L L O");
		runTestOnUiThread(new Runnable() {
			public void run() {
				btn.performClick();
			}
		});
		assertEquals("hellohello",titleEdit.getText().toString());
		
	}

}

I found it very cool to have such a nice integration. More than that is that they use proven testing framework out of the box.

Comments

1/6/2010 3:56:33 PM #
Nice!  I didn't know you've been working with Android.
1/6/2010 8:17:10 PM #
I have been working with, because of the software engineering class. I am liking most of the development time I spent on Android, though.
9/12/2010 1:17:32 AM #
Pingback from musicsnmore.info

Halle Berry Does Some Hand-Holdin? With Olivier Martinez
9/12/2010 3:10:16 AM #
Pingback from celebschats.info

Viva! Las Vegas
10/15/2010 9:31:53 AM #
I had no clue why people go mad over android. Now i understand it why !!!!!
10/27/2010 1:49:31 PM #
I have an Android Phone and Ipad.. They are both cool and awesome.
10/27/2010 6:32:29 PM #
The products of Android is awesome especially Phone.
11/2/2010 7:52:28 AM #
I must say that overall I am really impressed with this blog.It is easy to see that you are passionate about your writing. If only I had your writing ability I look forward to more updates and will be returning
11/2/2010 7:55:15 AM #
This post was very well written, and it also contains many useful facts. I appreciated your professional way of writing the post. Thanks, you have made it easy for me to understand.
11/2/2010 7:58:19 AM #
After reading this I thought it was very informative. I appreciate you taking the time to put this blog piece together.
11/2/2010 8:00:57 AM #
I harmonize with your conclusions and will thirstily look forward to your approaching updates.
12/26/2010 10:44:33 PM #
If only I had your writing ability I look forward to more updates and will be returning. I didn't know you've been working with Android.
1/13/2011 10:32:00 AM #

This was a really helpful article.
2/26/2011 6:31:58 PM #
You are absolutely right that Android provides much flexibility for testing it's apps. And your testing was quite nice.
3/23/2011 5:07:07 AM #
Gratitude for another profound post. I am pretty sure this article has helped me save many hours of evaluation other similar posts just near uncover what I was wanting for
3/23/2011 4:24:08 PM #
Interesting article. Were did you got all the information from... Smile
Comments are closed