2017-04-01 92 views
0

我在使用Espresso進行測試時閒置資源時出現問題。IdlingResources不起作用Espresso Android

它不起作用。它只被調用兩次,即使返回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所有的時間,但它也不管用。

出了什麼問題?

+0

你似乎被變異什麼狀態通常是隻讀的getter方法isIdleNow。我建議你在明確的遞增/遞減調用中管理空閒狀態,就像Google的這個例子:https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/contrib/src/主/ JAVA /安卓/支持/測試/咖啡/的contrib/CountingIdlingResource.java –

回答

1

您在下面的代碼和平錯過return true;

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