For 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.
d275b7a7-75cb-47c7-92a4-fa9316b50459|1|1.0