public final class

IdlingRegistry

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.IdlingRegistry

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-idling-resource', version: '3.6.1'

  • groupId: androidx.test.espresso
  • artifactId: espresso-idling-resource
  • version: 3.6.1

Artifact androidx.test.espresso:espresso-idling-resource:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-idling-resource com.android.support.test.espresso:espresso-idling-resource

Androidx class mapping:

androidx.test.espresso.IdlingRegistry android.support.test.espresso.IdlingRegistry

Overview

Handles registering and unregistering of IdlingResources with Espresso from within your application code.

These resources are required by Espresso to provide synchronisation against your application code. All registered resources with this registry will be automatically synchronized against for each Espresso interaction.

This registry along with IdlingResource interface are bundled together in a small light weight module so that it can be pulled in as a dependency of the App under test with close to no overhead.

Summary

Methods
public static IdlingRegistrygetInstance()

Returns a singleton instance of this IdlingRegistry that should be globally used for registering and unregistering IdlingResources

public java.util.Collection<Looper>getLoopers()

Returns a set of all currently registered s.

public java.util.Collection<IdlingResource>getResources()

Returns a set of all currently registered IdlingResources.

public booleanregister(IdlingResource idlingResources[])

Registers one or more IdlingResources.

public voidregisterLooperAsIdlingResource(Looper looper)

Registers a for idle checking with the framework.

public booleanunregister(IdlingResource idlingResources[])

Unregisters one or more IdlingResources.

public booleanunregisterLooperAsIdlingResource(Looper looper)

Unregisters a .

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static IdlingRegistry getInstance()

Returns a singleton instance of this IdlingRegistry that should be globally used for registering and unregistering IdlingResources

public boolean register(IdlingResource idlingResources[])

Registers one or more IdlingResources. When registering more than one resource, ensure that each has a unique name returned from IdlingResource.getName()

Returns:

true if at least one resource was successfully added to the registry

public boolean unregister(IdlingResource idlingResources[])

Unregisters one or more IdlingResources.

Returns:

true if at least one resource was successfully removed from the registry

public void registerLooperAsIdlingResource(Looper looper)

Registers a for idle checking with the framework. This is intended for use with non-UI thread s only.

public boolean unregisterLooperAsIdlingResource(Looper looper)

Unregisters a .

Attempting to unregister a looper that is not registered is a no-op.

Returns:

true if the looper was successfully removed from the registry

public java.util.Collection<IdlingResource> getResources()

Returns a set of all currently registered IdlingResources.

public java.util.Collection<Looper> getLoopers()

Returns a set of all currently registered s.

Source

/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.espresso;

import static java.util.Collections.synchronizedSet;

import android.os.Looper;
import android.util.Log;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Handles registering and unregistering of {@link IdlingResource}s with Espresso from within your
 * application code.
 *
 * <p>These resources are required by Espresso to provide synchronisation against your application
 * code. All registered resources with this registry will be automatically synchronized against for
 * each Espresso interaction.
 *
 * <p>This registry along with {@link IdlingResource} interface are bundled together in a small
 * light weight module so that it can be pulled in as a dependency of the App under test with close
 * to no overhead.
 */
public final class IdlingRegistry {

  private static final String TAG = IdlingRegistry.class.getSimpleName();
  private static final IdlingRegistry instance = new IdlingRegistry();
  private final Set<IdlingResource> resources = synchronizedSet(new HashSet<IdlingResource>());
  private final Set<Looper> loopers = synchronizedSet(new HashSet<Looper>());

  // VisibleForTesting
  IdlingRegistry() {}

  /**
   * Returns a singleton instance of this {@link IdlingRegistry} that should be globally used for
   * registering and unregistering {@link IdlingResource}s
   */
  public static IdlingRegistry getInstance() {
    return instance;
  }

  /**
   * Registers one or more {@link IdlingResource}s. When registering more than one resource, ensure
   * that each has a unique name returned from {@link IdlingResource#getName()}
   *
   * @return {@code true} if at least one resource was successfully added to the registry
   */
  public boolean register(IdlingResource... idlingResources) {
    if (null == idlingResources) {
      throw new NullPointerException("idlingResources cannot be null!");
    }
    Log.d(TAG, "Registering idling resources: " + Arrays.toString(idlingResources));
    return resources.addAll(Arrays.asList(idlingResources));
  }

  /**
   * Unregisters one or more {@link IdlingResource}s.
   *
   * @return {@code true} if at least one resource was successfully removed from the registry
   */
  public boolean unregister(IdlingResource... idlingResources) {
    if (null == idlingResources) {
      throw new NullPointerException("idlingResources cannot be null!");
    }
    Log.d(TAG, "Unregistering idling resources: " + Arrays.toString(idlingResources));
    return resources.removeAll(Arrays.asList(idlingResources));
  }

  /**
   * Registers a {@link Looper} for idle checking with the framework. This is intended for use with
   * non-UI thread {@link Looper}s only.
   *
   * @throws IllegalArgumentException if looper is the main looper.
   * @throws NullPointerException if looper is null.
   */
  public void registerLooperAsIdlingResource(Looper looper) {
    if (null == looper) {
      throw new NullPointerException("looper cannot be null!");
    }
    if (Looper.getMainLooper() == looper) {
      throw new IllegalArgumentException("Not intended for use with main looper!");
    }

    loopers.add(looper);
  }

  /**
   * Unregisters a {@link Looper}.
   *
   * <p>Attempting to unregister a looper that is not registered is a no-op.
   *
   * @return {@code true} if the looper was successfully removed from the registry
   */
  public boolean unregisterLooperAsIdlingResource(Looper looper) {
    if (null == looper) {
      throw new NullPointerException("looper cannot be null!");
    }
    return loopers.remove(looper);
  }

  /** Returns a set of all currently registered {@link IdlingResource}s. */
  public Collection<IdlingResource> getResources() {
    synchronized (resources) {
      return new HashSet<>(resources);
    }
  }

  /** Returns a set of all currently registered {@link Looper}s. */
  public Collection<Looper> getLoopers() {
    synchronized (loopers) {
      return new HashSet<>(loopers);
    }
  }
}