Espresso Android is very useful for a test case. But when I use IdlingResource , there are some problems.
I have a flag in my Activity and I will set it to true when every initial completion.
So my IdlingResource:
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 normal mode, the second log will be printed only when isIdleNow() return true .
But now my journal 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 was not in standby mode.
My english is bad, any problem please let me know! thanks!
Editorial: I already solved this problem:
I see that there is a comment on the IdlingResource class:
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 registering the Idling resource, just give any action of the form:
Espresso.registerIdlingResources(new WaitPingSuccessIdlingResource(activity)); Espresso.onView(ViewMatchers.withId(R.id.list_view)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));