Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Espresso: Wait for Activity to finish/start

Is there a canonical solution using Espresso to wait for a specific Activity to finish or start?

I have a SplashActivity that appears for a few seconds, then a MainActivity. I want Espresso to interact with the MainActivity, not the SplashActivity, but I can't seem to find any information about waiting for such a condition.

The closest thing I can find is a mention of idle resources but its not clear to me how I would use that here to wait for the Activity.

like image 337
SuperDeclarative Avatar asked Jun 10 '14 15:06

SuperDeclarative


People also ask

What is idling resource in Espresso?

An idling resource represents an asynchronous operation whose results affect subsequent operations in a UI test. By registering idling resources with Espresso, you can validate these asynchronous operations more reliably when testing your app.

What is IdlingResource?

An IdlingResource is an object that lets your app code tell your test code when the app is busy with some background work, so that the test code waits until that work completes. It's pretty easy to understand, but has some challenges to implement.


1 Answers

I guess your splash activity is performing some initialization.

If this is the case, my suggestion is to define some sort of listener pattern in order to be able to get a callback when the initialization is done. Then, you can make Espresso wait for the initialization with an IdlingResource.

NB: The following is NOT complete code, but it is meant to give you a hint in how to do so:

public class SplashIdlingResource implements IdlingResource, YourApplicationInitListener {

    // volatile because can be set by a different
    // thread than the test runner: the one calling back
    private volatile boolean mIsInitialized;

    private ResourceCallback mCallback;

    public SplashIdlingResource() {
        YourApplication application = // retrieve your Application object
        mIsInitialized = application.isInitialized();
        if (!mIsInitialized) {
            application.addInitListener(this);
        }
    }

    @Override
    public String getName() {
        return SplashIdlingResource.class.getName();
    }

    @Override
    public boolean isIdleNow() {
        return mIsInitialized;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback callback) {
        mCallback = callback;
    }

    @Override
    public void onApplicationInitCompleted() {
        m_isInitialized = true;
        if (m_callback != null) {
            m_callback.onTransitionToIdle();
        }
    }
}

Where onApplicationInitCompleted() is the callback you defined and which must be called when the Splash Activity, and so the initialization, is done.

Finally, register this new IdlingResource with Espresso by calling Espresso.registerIdlingResource in test setup.

like image 59
Luigi Massa Gallerano Avatar answered Sep 24 '22 16:09

Luigi Massa Gallerano