IdlingResources不适用于Espresso Android

时间:2017-04-01 06:17:25

标签: android android-studio testing android-espresso

使用Espresso进行测试时,我遇到了闲置资源的问题。

它不起作用。它只被调用两次,即使返回false也是如此。

public class MyIdlingResource implements IdlingResource {

    private boolean mIdle;
    private ResourceCallback mResourceCallback;

    public MyIdlingResource () {
        this.mIdle = false;
        this.mResourceCallback = null;
    }

    @Override
    public final String getName() {
        return MyIdlingResource .class.getSimpleName();
    }

    @Override
    public final boolean isIdleNow() {
        ArrayList<View> views = doStuff();
        mIdle =  views != null && !views.isEmpty();

        if (mIdle) {
            if (mResourceCallback != null) {
                mResourceCallback.onTransitionToIdle();
            }
        }

        return false;
    }

    @Override
    public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
        mResourceCallback = resourceCallback;
    }

}

所以在这种情况下我会一直返回false,但它也不起作用。

有什么问题?

1 个答案:

答案 0 :(得分:1)

你错过了return true;以下代码的和平:

if (mIdle) {
    if (mResourceCallback != null) {
        mResourceCallback.onTransitionToIdle();
        return true; // this one is missed
    }
}