Below an initial example of Activity testcase in Android. A first experiment after reading the android development page on Activity Testing.
The GuessPictureActivity is connected to a simple screen containing a TextView and ImageView. When the image is 'Clicked' a name will be displayed. On the next click a new photo will be loaded.
The activity under test is:
package com.ben.android.xmlweb;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class GuessPictureActivity extends Activity {
private TextView textFaceName;
private ImageView imageView;
private HashMap<String,Integer> drawingsByName;
private int counter;
static final String LOGID = "FaceMemory";
private String names[];
private Boolean guessed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
drawingsByName = new HashMap<String, Integer>();
drawingsByName.put("Pip", new Integer(R.drawable.pip));
drawingsByName.put("The Stig", new Integer(R.drawable.stig));
drawingsByName.put("Tux",new Integer(R.drawable.tux));
names = new String[drawingsByName.size()];
drawingsByName.keySet().toArray(names);
counter = 0;
guessed = false;
textFaceName = (TextView)findViewById(R.id.textFaceName);
imageView= (ImageView)findViewById(R.id.imageView);
imageView.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
if(!guessed) {
textFaceName.setText(names[counter]);
guessed = true;
} else {
counter++;
if(counter>=names.length) counter = 0;
imageView.setImageResource(drawingsByName.get(names[counter]));
textFaceName.setText(R.string.textGuessName);
guessed = false;
}
}
});
}
}
The activity test case looks like:
package com.ben.android.xmlweb.test;
import com.ben.android.xmlweb.GuessPictureActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.ImageView;
import android.widget.TextView;
public class GuessPictureActivityTest extends ActivityInstrumentationTestCase2<GuessPictureActivity> {
private GuessPictureActivity activity;
private ImageView imageView;
private TextView textView;
private String guessWhoString;
public GuessPictureActivityTest() {
super("com.ben.android.xmlweb.XmlGetter", GuessPictureActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
activity = getActivity();
imageView = (ImageView)getActivity().findViewById(com.ben.android.xmlweb.R.id.imageView);
textView = (TextView)getActivity().findViewById(com.ben.android.xmlweb.R.id.textFaceName);
guessWhoString = activity.getResources().getString(com.ben.android.xmlweb.R.string.textGuessName);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
@UiThreadTest
public void testAfterStartAQuestionIsAsked() {
assertNotNull(imageView);
assertNotNull(textView);
assertEquals(guessWhoString,textView.getText());
}
public void testOnClickTheNameOfPipIsDisplayed() throws InterruptedException{
Drawable initialDrawable = imageView.getDrawable();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.performClick();
}
});
getInstrumentation().waitForIdleSync();
String expectedText = "Pip";
assertEquals(expectedText,textView.getText());
assertSame(initialDrawable, imageView.getDrawable());
}
public void testOnTwoClicksADifferentImageIsDisplayed() {
Drawable initialDrawable = imageView.getDrawable();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.performClick();
imageView.performClick();
}
});
getInstrumentation().waitForIdleSync();
assertEquals(guessWhoString,textView.getText());
assertNotSame(initialDrawable, imageView.getDrawable());
}
}
If you like to see more, please drop a comment.
woensdag 30 maart 2011
Abonneren op:
Posts (Atom)