CountingIdlingResource

public final class CountingIdlingResource
extends Object implements IdlingResource

java.lang.Object
   ↳ android.support.test.espresso.idling.CountingIdlingResource


一个实现IdlingResource ,通过维护一个内部计数器来确定闲置。 当计数器为0时 - 它被认为是空闲的,当它不是零时,它不是空闲的。 这与Semaphore行为方式非常相似。

计数器可以从任何线程增加或减少。 如果它达到不合逻辑的状态(如计数器小于零),它将抛出IllegalStateException异常。

然后可以使用这个类来包装正在进行的操作,并阻止测试访问UI。

 public interface FooServer {
     public Foo newFoo();
     public void updateFoo(Foo foo);
   }

   public DecoratedFooServer implements FooServer {
     private final FooServer realFooServer;
     private final CountingIdlingResource fooServerIdlingResource;

     public DecoratedFooServer(FooServer realFooServer,
         CountingIdlingResource fooServerIdlingResource) {
       this.realFooServer = checkNotNull(realFooServer);
       this.fooServerIdlingResource = checkNotNull(fooServerIdlingResource);
     }

     public Foo newFoo() {
       fooServerIdlingResource.increment();
       try {
         return realFooServer.newFoo();
       } finally {
         fooServerIdlingResource.decrement();
       }
     }

     public void updateFoo(Foo foo) {
       fooServerIdlingResource.increment();
       try {
         realFooServer.updateFoo(foo);
       } finally {
         fooServerIdlingResource.decrement();
       }
     }
   }
   
   
Then in your test setup:
   public void setUp() throws Exception {
       super.setUp();
       FooServer realServer = FooApplication.getFooServer();
       CountingIdlingResource countingResource = new CountingIdlingResource("FooServerCalls");
       FooApplication.setFooServer(new DecoratedFooServer(realServer, countingResource));
       Espresso.registerIdlingResource(countingResource);
     }
   
   

Summary

Public constructors

CountingIdlingResource(String resourceName)

无调试跟踪创建CountingIdlingResource。

CountingIdlingResource(String resourceName, boolean debugCounting)

创建一个CountingIdlingResource。

Public methods

void decrement()

将正在进行的交易的计数减少到被监视的资源。

void dumpStateToLogs()

将此资源的当前状态打印到信息级别的logcat。

String getName()

返回资源的名称(用于记录和注册幂等性)。

void increment()

将正在进行的交易计数增加到被监视的资源。

boolean isIdleNow()

如果资源当前空闲,则返回 true

void registerIdleTransitionCallback(IdlingResource.ResourceCallback resourceCallback)

注册给定的 IdlingResource.ResourceCallback的资源。

Inherited methods

From class java.lang.Object
From interface android.support.test.espresso.IdlingResource

Public constructors

CountingIdlingResource

CountingIdlingResource (String resourceName)

无调试跟踪创建CountingIdlingResource。

Parameters
resourceName String: the resource name this resource should report to Espresso.

CountingIdlingResource

CountingIdlingResource (String resourceName, 
                boolean debugCounting)

创建一个CountingIdlingResource。

Parameters
resourceName String: the resource name this resource should report to Espresso.
debugCounting boolean: if true increment & decrement calls will print trace information to logs.

Public methods

decrement

void decrement ()

将正在进行的交易的计数减少到被监视的资源。 如果此操作导致计数器下降到0以下,则会引发异常。

Throws
IllegalStateException if the counter is below 0.

dumpStateToLogs

void dumpStateToLogs ()

将此资源的当前状态打印到信息级别的logcat。

getName

String getName ()

返回资源的名称(用于记录和注册幂等性)。

Returns
String

increment

void increment ()

将正在进行的交易计数增加到被监视的资源。 这个方法可以从任何线程调用。

isIdleNow

boolean isIdleNow ()

如果资源当前空闲,则返回true Espresso将始终从主线程调用此方法,因此它应该是非阻塞的并立即返回。

Returns
boolean

registerIdleTransitionCallback

void registerIdleTransitionCallback (IdlingResource.ResourceCallback resourceCallback)

注册给定的IdlingResource.ResourceCallback与资源。 Espresso会调用这个方法:


You only need to call this upon transition from busy to idle - if the resource is already idle when the method is called invoking the call back is optional and has no significant impact.

Parameters
resourceCallback IdlingResource.ResourceCallback