Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso Idling Resource doesn't work

The Android espresso is very useful for test case. But when I use IdlingResource there is some trouble.

I have a flag in my Activity, and I will set it to true when every initial complete.

So my IdlingResource is this:

/**
 * 等待所有初始化工作完成
 */
private class WaitPingSuccessIdlingResource implements IdlingResource {
    private ChoiceServerActivity choiceServerActivity;
    private ResourceCallback mResourceCallback;

    public WaitPingSuccessIdlingResource(ChoiceServerActivity choiceServerActivity) {
        this.choiceServerActivity = choiceServerActivity;
    }

    @Override
    public String getName() {
        return String.valueOf(hashCode());
    }

    @Override
    public boolean isIdleNow() {
        if (mResourceCallback != null && choiceServerActivity.isAllDataInited()) {
            mResourceCallback.onTransitionToIdle();
        }
        boolean rst = choiceServerActivity.isAllDataInited();
        Log.i("tonghu","WaitPingSuccessIdlingResource, isIdleNow(L94): rst " + rst);
        return rst;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback callback) {
        this.mResourceCallback = callback;
    }
}

And I register like this:

Espresso.registerIdlingResources(new WaitPingSuccessIdlingResource(activity));
Log.i("tonghu", "ChoiceServerActivityTest, testPingSuccess(L42): 2222");

In normally, the second log will print only when isIdleNow() return true.

But now my log is:

I/tonghu  (23470): WaitPingSuccessIdlingResource, isIdleNow(L94): rst false
I/tonghu  (23470): ChoiceServerActivityTest, testPingSuccess(L42): 2222

Why the second log can print when my IdlingResource wasn't idle.

My English is poor, any problem, please let me know! Thx!


EDITED: I have already solve this problem:

I see there is a comment on class IdlingResource:

In such cases, test authors can register the custom resource and 
{@link    Espresso} will wait for the resource to become idle prior 
to   executing a view operation.

So after register Idling resource, just give any a view action:

Espresso.registerIdlingResources(new  WaitPingSuccessIdlingResource(activity));
Espresso.onView(ViewMatchers.withId(R.id.list_view)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
like image 319
user3875388 Avatar asked Oct 14 '15 08:10

user3875388


People also ask

What is espresso idling resource?

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.


1 Answers

Same problem here, found that registering idlingResources won't cause Espresso to wait, but besides Espresso.onView, you still can use Espresso.onIdle() to wait for registered idlingResources to turn idle.

Finally, I found the official document, quote from here:

Register idling resources before you need them.

The synchronization benefits associated with idling resources only take effect following Espresso's first invocation of that resource's isIdleNow() method.

The following list shows several examples of this property:

  1. If you register an idling resource in a method annotated with @Before, the idling resource takes effect in the first line of each test.
  2. If you register an idling resource inside a test, the idling resource takes effect during the next Espresso-based action. This behavior still occurs even if the next action is in the same test as the statement that registers the idling resource.
like image 79
xfdai Avatar answered Oct 25 '22 20:10

xfdai