9

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()));
3
  • Check this library: github.com/metova/Cappuccino Jan 17, 2016 at 20:25
  • 1
    I had a similar problem. It's a bummer that IdlingResource will only wait for view actions, not all code.
    – tir38
    Apr 19, 2016 at 19:46
  • Also Cappuccino isn't going to help you there. IdlingResource still will only happen on view action
    – tir38
    Apr 19, 2016 at 19:51

1 Answer 1

5

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.
1
  • 2
    Note: in com.android.support.test.espresso:espresso-core:3.0.1 Espresso.onIdle() doesn't wait for just-registered Idling Resources (via IdlingRegistry.register), only onView/onData does, see sync call in the beginning of those methods.
    – TWiStErRob
    Apr 10, 2020 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.