Monday, May 25, 2009

 

I have been a good developer and deserve a cookie

...for I have spent the last couple of hours building Android JUnit test cases.

See my very first post to see how to run these kinds of tests. It's kinda cool; after triggering 'em from the command line, you can flip to the Android emulator, and see the windows and text appearing and disappearing...

A few interesting points here, anyways:



Feast thine eyes:


package com.rezendi.wtw.test;

import android.content.Intent;
import android.database.Cursor;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import android.widget.EditText;

import com.rezendi.wtw.R;
import com.rezendi.wtw.Settings;
import com.rezendi.wtw.TravelNoteEdit;
import com.rezendi.wtw.TravelNotesDbAdapter;

/**
* Test code for a travel-note lifecycle.
*
* @author Jon Evans
*
*/
public class NoteTest extends ActivityInstrumentationTestCase2 {

public NoteTest() {
super("com.rezendi.wtw", TravelNoteEdit.class);
}

/* (non-Javadoc)
* @see android.test.AndroidTestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
}

/* (non-Javadoc)
* @see android.test.AndroidTestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}

/**
* Open an empty TravelNoteEdit and check all is well.
*/
public void test1EmptyNote() {
TravelNoteEdit tne = (TravelNoteEdit) getActivity();

EditText titleEdit = (EditText) tne.findViewById(R.id.title);
String titleText = titleEdit.getText().toString();
assertEquals(0, titleText.length());

EditText bodyEdit = (EditText) tne.findViewById(R.id.body);
String bodyText = bodyEdit.getText().toString();
assertEquals(0, bodyText.length());

Button picButton = (Button) tne.findViewById(R.id.addPicture);
assertEquals(picButton.getText(), tne.getText(R.string.addPicture));
}

/**
* Open TravelNoteEdit, edit its contents, save its data, check saved data, delete.
*/
@UiThreadTest
public void test2DataSave() {
TravelNoteEdit tne = (TravelNoteEdit) getActivity();

tne.setLocationString("wtwTest"); // mark this as test data
EditText titleEdit = (EditText) tne.findViewById(R.id.title);
titleEdit.setText(titleEdit.getText().toString()+ "test edited");

EditText bodyEdit = (EditText) tne.findViewById(R.id.body);
bodyEdit.setText(bodyEdit.getText().toString()+ "test edited");

Button saveButton = (Button) tne.findViewById(R.id.save);
saveButton.performClick();

//OK, we're done, so;
TravelNotesDbAdapter dbAdapter = new TravelNotesDbAdapter(getInstrumentation().getTargetContext());
dbAdapter.open();
Cursor note = dbAdapter.fetchTestNotes();
String newTitleText = note.getString(
note.getColumnIndexOrThrow(TravelNotesDbAdapter.KEY_TITLE));
String newBodyText = note.getString(
note.getColumnIndexOrThrow(TravelNotesDbAdapter.KEY_BODY));
note.close();

assertEquals("test edited", newTitleText);
assertEquals("test edited", newBodyText);

dbAdapter.deleteTestData();
dbAdapter.close();
}

/**
* Create a note in the database, open TravelNoteEdit, check its contents.
*/
public void test3DataLoad() {
TravelNotesDbAdapter dbAdapter = new TravelNotesDbAdapter(getInstrumentation().getTargetContext());
dbAdapter.open();
long testId = dbAdapter.createNote(Settings.KEY_TEST, "test title", "test body", "test picture");

Intent intent = new Intent(getInstrumentation().getTargetContext(), TravelNoteEdit.class);
intent.putExtra(TravelNotesDbAdapter.KEY_ROWID, testId);
this.setActivityIntent(intent);

TravelNoteEdit tne = (TravelNoteEdit) getActivity();

EditText titleEdit = (EditText) tne.findViewById(R.id.title);
String titleText = titleEdit.getText().toString();
assertEquals("test title", titleText);

EditText bodyEdit = (EditText) tne.findViewById(R.id.body);
String bodyText = bodyEdit.getText().toString();
assertEquals("test body", bodyText);

Button picButton = (Button) tne.findViewById(R.id.addPicture);
assertEquals(picButton.getText(), tne.getText(R.string.seePicture));

dbAdapter.deleteTestData();
dbAdapter.close();
}
}

Labels: , , , ,


Comments:
Look who it is! “Why should you do my paper? What are some of the things I can expect when you do my assignment?” It would help if you had an idea of what you will get when you pay someone online to write your task. Once you hire https://essaysrescue.com/masterpapers-review/ for a term, thesis, or essay paper, you can expect writing that has the following characteristics.
 
Thanks for this amazing blog this is too much informative.
 
Thanks
 

Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]