Thursday, May 19, 2011

Robotium - finish up the validation

I just realized - I'm converted to test-driven-development at this point. I like the fact that you bring scripted automation of tests into your work flow. I like the quality control that scripts provide. I the development technique of coding the test first and making it fail, and working from there. The simple-as-possible-test rule keeps you from getting too adventurous.

I just wish I spent less time debugging the tests, and more time working on the program itself. I'd say about 75 to 80 percent of my time is spent creating and especially debugging tests - mostly when the production code works fine. I'm hoping to bring this ratio down.

Anyway, back to the Robotium. The results yesterday were promising. Today, we're going to continue and hopefully complete validation of the Settings page. One validation I didn't do yesterday was ensuring that the starting quiz number is less than the ending quiz number. Let's get started.

Ok, first commit from last night.

Actually, I didn't test for the quiz num greater than words available, or even finish coding it, due to the db issues. Let's do that now:

dbHelper.openDataBase();
int lastNum = dbHelper.getLevelCount(inputLevel);
dbHelper.close();

if (quizStartNum > lastNum ){

valid = false;

Toast.makeText(getBaseContext(),
"Quiz start number must be less than or equal to " + lastNum,
Toast.LENGTH_LONG).show();

}



And the test - this should pass.

public void testStartQuizNumTooHigh() throws Exception {

solo.clickOnButton(0);
solo.clearEditText(0);
solo.enterText(0, "5");
solo.clearEditText(1);
solo.enterText(1, "1000");
solo.clickOnButton(0);
assertTrue(this.solo.waitForText("Quiz start number must be less than or equal to 669"));
assertTrue(this.solo.searchText("Quiz start number must be less than or equal to 669"));

}

Good. It passed. But, the log shows that ever-present database not closed exception. Sigh. It actually died on the open database. Did I forget to code the create database? No, it's there. What line does the trace show it called it from? Wait. Did I have the close in an if statement? Ok, right - the first test has an invalid level, so the close doesn't happen.

Does that mean all my open database are unnecessary? Let's check out the code.

public static DataBaseHelper createDB(Activity a) {

DataBaseHelper myDbHelper = new DataBaseHelper(a);

try {

myDbHelper.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

myDbHelper.openDataBase();

} catch (SQLException sqle) {

throw sqle;

}

return myDbHelper;
}


Read it and weep. This means I can get rid of all my database opens - they are redundant. I wan't sure, so I had left them in there. But it's obvious in retrospect. Where is that check for existing database I had put in there?

That was in the open database:

public void openDataBase() throws SQLException {

// Open the database
String myPath = DB_PATH + DB_NAME;

if (null == myDataBase) {

myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}

}

So, again, the open that's being called from the createDatbase.

Ok, well, at some point today, I'm going to go ahead and get rid of all the unnecessary open calls. But first, let's resolve this problem, which is branch logic which doesn't close the db the level is incorrect. Probably the simplest thing to do would be to call the createDB as if it were the open, just before it's read.


Like so:

dbHelper = DataBaseHelper.createDB(SettingsActivity.this);

int lastNum = dbHelper.getLevelCount(inputLevel);

dbHelper.close();

The dbHelper create creates a new instance of dbhelper when you call it. It check for the existence of the physical database, and copies it from the file if not there. then, opens the database and returns the new instance.


Let run the robotium tests. Well, they all failed on no button with 0 index. This happened yesterday, and was solved by a rerun. Let's try it.

Not this time. Why? I could clean the project, or try to get the button some other way. Let's clean, and also set the phone to it's normal starting state.

Ah. Great. this time it worked, We're back in business.

No comments:

Post a Comment