3

I'm trying to create a test method using Robotium to check if the Android application finishes after clicking on a button (in the code there is a call to finish() when the user taps on it).

public void test_onclickExit_finish() {
    String buttonText = resources.getString(R.string.exit);
    Button exitButton = solo.getButton(buttonText, true);
    solo.clickOnView(exitButton);
    // check here that the app has finished
    // wait for the activity to finish?
    assertTrue(solo.getCurrentActivity() == null);
}

But this test is failing. I don't know how can I indicate the test to wait till the activity has finished. Also I'm not sure if using getCurrentActivity() would be a good way to check if the app has finished.

How can I check that the application/ activity has finished?

Thanks.

Javi
  • 19,387
  • 30
  • 102
  • 135
  • What do you mean by finished?! No activities running? no services running? no process running? Typically exit buttons are not a thing in android, why would you want to exit something? just leave it backgrounded and the os will close it if it needs to and assuming you have programmed correctly with saving state when they reopen it will pop back open. – Paul Harris Mar 15 '13 at 09:09

3 Answers3

5

If it is your main activity use this:

assertTrue(solo.getCurrentActivity().isFinishing());
Drakkin
  • 878
  • 12
  • 27
  • 1. assertions are primarily for use during development and maintenance, so this check might be compiled out for production code and has no effect then, 2. this check is insufficient to either wait for the activity to finish or to check if it already has finished – Mobiletainment Nov 04 '13 at 15:00
  • I don't understand that comment. the assertTrue is in the test code, isn't it? – koljaTM Jan 21 '14 at 07:49
  • Calling solo.getCurrentActivity() is not reliable, as it may already be finished by the time you call it. I think @Filipe's answer might be more reliable. – Dan J Mar 05 '15 at 23:16
3

This question is old, but maybe my solution can help someone.

I found a way to wait/detect if an activity was finished when using Robotium.

  • Create a condition to detect when activity root view is detached from window: (I am using a helper method in my example)

    public static Condition activityWillClose(final Activity activity) {
    
        return new Condition() {
            boolean _detached = false;
    
            { // constructor
                View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
                rootView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
                    @Override
                    public void onViewAttachedToWindow(View view) {
                    }
    
                    @Override
                    public void onViewDetachedFromWindow(View view) {
                        _detached = true;
                    }
                });
            }
    
            @Override
            public boolean isSatisfied() {
                return _detached;
            }
        };
    }
    
  • wait for the condition in your test:

    solo.clickOnView(solo.getView(R.id.exitButton));
    
    Assert.assertTrue("should finish activity",
            solo.waitForCondition(activityWillClose(solo.getCurrentActivity()), 2000)
    );
    
Filipe Borges
  • 2,712
  • 20
  • 32
2

Application and instrumentation are running in the same process, if you finish your application, you cannot do anything more in instrumentation. It failed, because instrumentation was killed as well, and you tried to do something more. There is no way to check what you are trying to do with robotium.

maszter
  • 3,680
  • 6
  • 37
  • 53