Testing on Android

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) {
				// TODO Auto-generated method stub
				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.


Posted 01-06-2010 2:30 AM by Tuna Toksoz
Filed under: , ,

[Advertisement]

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)