Thursday, May 19, 2011

The nag

Instead of blogging as I post, this time I'm doing as an after the fact, just for a change of pace.

In this case, I wanted to institute a nag message in the settings validation, in case the potential customer tries to enter a number which is greater than the actual range of words available in the free edition.

A way to determine if it's paid or not is to simply check the total number of words against a "nag" number of words available in resources. The number of words will always be greater then the nag number in the free version. If the customer tries to enter a greater number, he will not only get the error message stating the number is too high, he will also get another message inviting him (or her) to purchase the paid app.

The problem turned out to be that I was unable to cover both conditions in a single test run. I did try to set an attribute on the activity, retrieved from robotium's "getCurrentActivity" message, but that invariably causes the test to fail regardless.

However, the good news is I was able to obtain the row count (number of words) and the nag number using standard api's from robotium, like so:


private int getRowNumberInfo() {
Activity a = solo.getCurrentActivity();

DataBaseHelper dbHelper = DataBaseHelper.createDB(a);

int lastNum = dbHelper.getLevelCount(5);

dbHelper.close();

int compNum = lastNum + 1;

/* get max Number */
int nagNumber = InitUtils.getNagQuestionNumber(a);

if (nagNumber < compNum) {
compNum = nagNumber;
}
return compNum;
}



Here's the production code:

if (quizEndNum > compNum ){

valid = false;

String message1 = "Quiz end number must be less than or equal to " + compNum;
String message2 = "Please check out our paid version on the Android Market place for the all the Vocabulary";

Toast.makeText(getBaseContext(),
message1,
Toast.LENGTH_LONG).show();

if (!paid) {

Toast.makeText(getBaseContext(),
message2,
Toast.LENGTH_LONG).show();

}

}

And finally, the test:

public void testEndQuizNumTooHigh() throws Exception {

int compNum = getRowNumberInfo();
SettingsActivity a = (SettingsActivity) solo.getCurrentActivity();
int nagNumber = a.getmNagNumber();

boolean paid = true;
if (nagNumber < compNum) {
paid = false;
}

solo.clearEditText(0);
solo.enterText(0, "5");

solo.clearEditText(1);
solo.enterText(1, "1");

solo.clearEditText(2);
solo.enterText(2, "1000");
solo.clickOnButton(0);
assertTrue(this.solo
.waitForText("Quiz end number must be less than or equal to "
+ compNum));
assertTrue(this.solo
.searchText("Quiz end number must be less than or equal to "
+ compNum));


if (!paid) {
assertTrue(this.solo
.waitForText("Please check out our paid version on the Android Market place for the all the Vocabulary"));
assertTrue(this.solo
.searchText("Please check out our paid version on the Android Market place for the all the Vocabulary"));
}


}

One final note - I changed the starting activity from StartActivity to SettingsActivity, so getCurrentActivity would return the current activity.

No comments:

Post a Comment