Dan_Dan_Man
Dan_Dan_Man

Reputation: 504

Robotium - Waiting for an Activity's tasks to complete

I've just started learning to use Robotium for testing my app. I have written a test case that resets a list of stats, and then checks if the values are equal to 0. The code is below:

public void testClearStats() {
    solo.clickOnButton("Clear Stats");
    solo.clickOnButton("Yes");
    TextView views = (TextView) solo.getView(R.id.textViewsNum);
    TextView prompts = (TextView) solo.getView(R.id.textPromptsNum);
    TextView completions = (TextView) solo.getView(R.id.textCompleteNum);
    assertEquals("0", views.getText().toString());
    assertEquals("0", prompts.getText().toString());
    assertEquals("0", completions.getText().toString());
}

The test was failing when it shouldn't because it was checking the values of the TextViews before their results were reset. To get around this, I added this line:

solo.waitForActivity(solo.getCurrentActivity().toString());

With this statement the test passes but it seems to take an unnecessary long time to complete. I was wondering if there was a better/correct way of way doing this, or is this the best way of doing it?

Thanks

Upvotes: 1

Views: 2735

Answers (2)

moud
moud

Reputation: 749

You can always use waitForActivity and choose a specific timeout.solo.waitForActivity(YourActivity.class, timeout);

Upvotes: -1

Paul Harris
Paul Harris

Reputation: 5809

You will have to wait on something, what you choose will depend on your application and without looking at it i cannot answer what will be the best thing to wait for.

What visual indicators do you have for the reset to occur? do you have a new activity open? Is there text telling it has complete? if it is literally just the three textfields. if it is then you might manage to use solo.waitfortext("0") although the better way will be to use the new concept of conditions and use the solo.waitForCondition(method) (the condition will probably be to wait for the text to be 0, but you would have put the condition into one place and then if you later find a better way then you only have to change it once).

public class WaitForReset implements Condition
{

    public boolean isSatisfied()
    {
        TextView views = (TextView) solo.getView(R.id.textViewsNum);
        TextView prompts = (TextView) solo.getView(R.id.textPromptsNum);
        TextView completions = (TextView) solo.getView(R.id.textCompleteNum);
        if(isViewZero(views) && isViewZero(prompts) && isViewZero(completions))
        {
            return true
        }
        else
        {
            return false;
        }
    }

    private isViewZero(TextView textView)
    {
        if((textView!=null) && (textView.getText().toString() ==0))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

You can then assert on the value of the waitforcondition being true!

Upvotes: 4

Related Questions