androidx.test.espresso.IdlingResource Java Examples

The following examples show how to use androidx.test.espresso.IdlingResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void testSync_withDuplicateRegisterAndUnregister() {
  IdlingResource toUnReg = new OnDemandIdlingResource("toUnReg");
  IdlingResource alreadyReg = new OnDemandIdlingResource("alreadyReg");
  IdlingResource newReg = new OnDemandIdlingResource("newReg");

  // Register "toUnReg" and "alreadyReg"
  assertTrue(registry.registerResources(Lists.newArrayList(toUnReg)));
  assertTrue(registry.registerResources(Lists.newArrayList(alreadyReg)));

  // Sync with second "alreadyReg" and "newReg"
  registry.sync(Sets.newHashSet(alreadyReg, newReg), Sets.<Looper>newHashSet());
  // Verify toUnReg is unregistered
  // Verify alreadyReg stayed registered
  // verify newReg registered
  assertEquals(2, registry.getResources().size());
  assertTrue(registry.getResources().contains(alreadyReg));
  assertTrue(registry.getResources().contains(newReg));
}
 
Example #2
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void testSync_withMultiRegisterAndUnregister() {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  IdlingResource r2 = new OnDemandIdlingResource("r2");

  // Register r1 directly
  assertTrue(registry.registerResources(Lists.newArrayList(r1)));
  // Sync with second r2 and a looper
  registry.sync(Sets.newHashSet(r2), Sets.newHashSet(Looper.getMainLooper()));
  // Verify r2 and looper registered
  // Verify r1 is unregister
  assertEquals(2, registry.getResources().size());
  assertTrue(
      registry
          .getResources()
          .contains(LooperIdlingResourceInterrogationHandler.forLooper(handler.getLooper())));
  assertTrue(registry.getResources().contains(r2));
}
 
Example #3
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void allResourcesAreIdle() throws Exception {
  OnDemandIdlingResource r1 = new OnDemandIdlingResource("r1");
  OnDemandIdlingResource r2 = new OnDemandIdlingResource("r2");
  IdlingResource r3 = new OnDemandIdlingResource("r3");
  r1.forceIdleNow();
  r2.forceIdleNow();
  registry.registerResources(Lists.newArrayList(r1, r2));
  FutureTask<Boolean> resourcesIdle = createIdleCheckTask(registry);
  handler.post(resourcesIdle);
  assertTrue(resourcesIdle.get());

  registry.registerResources(Lists.newArrayList(r3));

  resourcesIdle = createIdleCheckTask(registry);
  handler.post(resourcesIdle);
  assertFalse(resourcesIdle.get());
}
 
Example #4
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void registerAndUnregisterReturnValue() {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  IdlingResource r2 = new OnDemandIdlingResource("r2");

  assertTrue(registry.registerResources(Lists.newArrayList(r1)));
  assertFalse(registry.registerResources(Lists.newArrayList(r1)));
  assertTrue(registry.registerResources(Lists.newArrayList(r2)));

  IdlingResource r3 = new OnDemandIdlingResource("r3");
  assertFalse(registry.registerResources(Lists.newArrayList(r3, r3)));

  IdlingResource r4 = new OnDemandIdlingResource("r4");
  assertFalse(registry.unregisterResources(Lists.newArrayList(r4)));

  assertTrue(registry.unregisterResources(Lists.newArrayList(r1)));
  assertFalse(registry.unregisterResources(Lists.newArrayList(r1)));
  assertTrue(registry.unregisterResources(Lists.newArrayList(r2)));

  assertFalse(registry.unregisterResources(Lists.newArrayList(r3, r3)));
}
 
Example #5
Source File: IdlingResourceRegistry.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a list of all currently registered {@link IdlingResource}s. This method is safe to call
 * from any thread.
 *
 * @return an ImmutableList of {@link IdlingResource}s.
 */
public List<IdlingResource> getResources() {
  if (Looper.myLooper() != looper) {
    return runSynchronouslyOnMainThread(
        new Callable<List<IdlingResource>>() {
          @Override
          public List<IdlingResource> call() {
            return getResources();
          }
        });
  } else {
    ImmutableList.Builder<IdlingResource> irs = ImmutableList.builder();
    for (IdlingState is : idlingStates) {
      irs.add(is.resource);
    }
    return irs.build();
  }
}
 
Example #6
Source File: IdlingResourceRegistry.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Unregisters the given resources. If any of the given resources are not already registered, a
 * warning is logged.
 *
 * @return {@code true} if all resources were successfully unregistered
 */
public boolean unregisterResources(final List<? extends IdlingResource> resourceList) {
  if (Looper.myLooper() != looper) {
    return runSynchronouslyOnMainThread(
        new Callable<Boolean>() {
          @Override
          public Boolean call() {
            return unregisterResources(resourceList);
          }
        });
  } else {
    boolean allUnregisteredSuccessfully = true;
    for (IdlingResource resource : resourceList) {
      boolean found = false;
      for (int i = 0; i < idlingStates.size(); i++) {
        if (idlingStates.get(i).resource.getName().equals(resource.getName())) {
          idlingStates.remove(i);
          found = true;
          break;
        }
      }

      if (!found) {
        allUnregisteredSuccessfully = false;
        Log.e(
            TAG,
            String.format(
                Locale.ROOT,
                "Attempted to unregister resource that is not registered: "
                    + "'%s'. Resource list: %s",
                resource.getName(),
                getResources()));
      }
    }
    return allUnregisteredSuccessfully;
  }
}
 
Example #7
Source File: MainActivity.java    From testing-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Only called from test, creates and returns a new {@link SimpleIdlingResource}.
 */
@VisibleForTesting
@NonNull
public IdlingResource getIdlingResource() {
    if (mIdlingResource == null) {
        mIdlingResource = new SimpleIdlingResource();
    }
    return mIdlingResource;
}
 
Example #8
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testSync_ofLooperAsIdlingResource() {
  IdlingResource r1 = LooperIdlingResourceInterrogationHandler.forLooper(handler.getLooper());
  registry.sync(Sets.newHashSet(r1), Sets.newHashSet(handler.getLooper()));
  assertEquals(1, registry.getResources().size());
  String expectedLooperResourceName =
      LooperIdlingResourceInterrogationHandler.forLooper(handler.getLooper()).getName();
  assertEquals(expectedLooperResourceName, registry.getResources().get(0).getName());
}
 
Example #9
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void testSync_ofIdlingResource() {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  registry.sync(Sets.newHashSet(r1), Sets.<Looper>newHashSet());
  assertEquals(1, registry.getResources().size());
  assertTrue(registry.getResources().contains(r1));
}
 
Example #10
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void getResources() {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  IdlingResource r2 = new OnDemandIdlingResource("r2");

  assertEquals(0, registry.getResources().size());

  registry.registerResources(Lists.newArrayList(r1, r2));
  assertEquals(2, registry.getResources().size());

  registry.unregisterResources(Lists.newArrayList(r1, r2));
  assertEquals(0, registry.getResources().size());
}
 
Example #11
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void registerAndUnregisterNeverIdling() throws Exception {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  registry.registerResources(Lists.newArrayList(r1));

  FutureTask<Boolean> resourcesIdle = createIdleCheckTask(registry);
  handler.post(resourcesIdle);
  assertFalse(resourcesIdle.get());

  registry.unregisterResources(Lists.newArrayList(r1));

  resourcesIdle = createIdleCheckTask(registry);
  handler.post(resourcesIdle);
  assertTrue(resourcesIdle.get());
}
 
Example #12
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void unregisterNeverRegistered() throws Exception {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  IdlingResource r2 = new OnDemandIdlingResource("r2");

  assertTrue(registry.registerResources(Lists.newArrayList(r1)));
  assertFalse(registry.unregisterResources(Lists.newArrayList(r2)));

  FutureTask<Boolean> resourcesIdle = createIdleCheckTask(registry);
  handler.post(resourcesIdle);

  // r1 should still be registered
  assertFalse(resourcesIdle.get());
}
 
Example #13
Source File: IdlingResourceRegistryTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void registerDuplicates() {
  IdlingResource r1 = new OnDemandIdlingResource("r1");
  IdlingResource r1dup = new OnDemandIdlingResource("r1");
  assertTrue(registry.registerResources(Lists.newArrayList(r1)));
  assertFalse(registry.registerResources(Lists.newArrayList(r1)));
  assertFalse(registry.registerResources(Lists.newArrayList(r1dup)));
  assertEquals(1, registry.getResources().size());
}
 
Example #14
Source File: IdlingResourceRegistry.java    From android-test with Apache License 2.0 5 votes vote down vote up
private void logDuplicateRegistrationError(
    IdlingResource newResource, IdlingResource oldResource) {
  Log.e(
      TAG,
      String.format(
          Locale.ROOT,
          "Attempted to register resource with same names:"
              + " %s. R1: %s R2: %s.\nDuplicate resource registration will be ignored.",
          newResource.getName(),
          newResource,
          oldResource));
}
 
Example #15
Source File: IdlingResourceRegistry.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Registers the given resources. If any of the given resources are already registered, a warning
 * is logged.
 *
 * @return {@code true} if all resources were successfully registered
 */
public boolean registerResources(final List<? extends IdlingResource> resourceList) {
  if (Looper.myLooper() != looper) {
    return runSynchronouslyOnMainThread(
        new Callable<Boolean>() {
          @Override
          public Boolean call() {
            return registerResources(resourceList);
          }
        });
  } else {
    boolean allRegisteredSuccessfully = true;
    for (IdlingResource resource : resourceList) {
      checkNotNull(resource.getName(), "IdlingResource.getName() should not be null");

      boolean duplicate = false;
      for (IdlingState oldState : idlingStates) {
        if (resource.getName().equals(oldState.resource.getName())) {
          // This does not throw an error to avoid leaving tests that register resource in test
          // setup in an undeterministic state (we cannot assume that everyone clears vm state
          // between each test run)
          logDuplicateRegistrationError(resource, oldState.resource);
          duplicate = true;
          break;
        }
      }

      if (!duplicate) {
        IdlingState is = new IdlingState(resource, handler);
        idlingStates.add(is);
        is.registerSelf();
      } else {
        allRegisteredSuccessfully = false;
      }
    }
    return allRegisteredSuccessfully;
  }
}
 
Example #16
Source File: Sample2Tests.java    From PdfViewPager with Apache License 2.0 5 votes vote down vote up
@After
public void tearDown() {
    List<IdlingResource> idlingResources = Espresso.getIdlingResources();
    for (IdlingResource resource : idlingResources) {
        Espresso.unregisterIdlingResources(resource);
    }
}
 
Example #17
Source File: ApolloIdlingResourceTest.java    From apollo-android with MIT License 5 votes vote down vote up
@Test
public void checkIdlingResourceTransition_whenCallIsQueued() throws ApolloException {
  server.enqueue(mockResponse());

  apolloClient = ApolloClient.builder()
      .okHttpClient(okHttpClient)
      .dispatcher(new Executor() {
        @Override public void execute(@NotNull Runnable command) {
          command.run();
        }
      })
      .serverUrl(server.url("/"))
      .build();

  final AtomicInteger counter = new AtomicInteger(1);
  idlingResource = ApolloIdlingResource.create(IDLING_RESOURCE_NAME, apolloClient);

  idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
    @Override public void onTransitionToIdle() {
      counter.decrementAndGet();
    }
  });

  assertThat(counter.get()).isEqualTo(1);
  Rx2Apollo.from(apolloClient.query(EMPTY_QUERY)).test().awaitTerminalEvent();
  assertThat(counter.get()).isEqualTo(0);
}
 
Example #18
Source File: MainActivityTest.java    From meat-grinder with MIT License 5 votes vote down vote up
@Test
public void testFabButtonAndList() {
    IdlingResource ir = new RecyclerViewScrollingIdlingResource((RecyclerView) activity.findViewById(R.id.list));
    IdlingRegistry.getInstance().register(ir);
    Matcher listMatcher = withId(R.id.list);
    onView(listMatcher).perform(smoothScrollTo(12));
    onView(withId(R.id.fab)).perform(click());
    onView(listMatcher).perform(smoothScrollTo(0));
    onView(withId(R.id.fab)).perform(click());
    IdlingRegistry.getInstance().unregister(ir);
}
 
Example #19
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() {
  scheduler.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
    @Override public void onTransitionToIdle() {
      idleCount.incrementAndGet();
    }
  });
}
 
Example #20
Source File: DelegatingIdlingResourceSchedulerTest.java    From RxIdler with Apache License 2.0 5 votes vote down vote up
@Before public void setUp() {
  scheduler.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
    @Override public void onTransitionToIdle() {
      idleCount.incrementAndGet();
    }
  });
}
 
Example #21
Source File: BaseTest.java    From adamant-android with GNU General Public License v3.0 5 votes vote down vote up
public void teardown() throws IOException {
    //if test fail, unregister all resources
    for (IdlingResource resource : registeredResources) {
        IdlingRegistry.getInstance().unregister(resource);
    }

    activityRule.finishActivity();
}
 
Example #22
Source File: BaseTest.java    From adamant-android with GNU General Public License v3.0 4 votes vote down vote up
protected void idlingBlock(IdlingResource idlingResource, Exceptionable runnable) throws Exception {
    IdlingRegistry.getInstance().register(idlingResource);
    registeredResources.add(idlingResource);
    runnable.run();
    IdlingRegistry.getInstance().unregister(idlingResource);
}
 
Example #23
Source File: IdlingResourceRegistry.java    From android-test with Apache License 2.0 4 votes vote down vote up
private IdlingState(IdlingResource resource, Handler handler) {
  this.resource = resource;
  this.handler = handler;
}
 
Example #24
Source File: LooperIdlingResourceInterrogationHandler.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public void registerIdleTransitionCallback(IdlingResource.ResourceCallback cb) {
  this.cb = cb;
}
 
Example #25
Source File: EspressoIdlingResource.java    From MovieGuide with MIT License 4 votes vote down vote up
public static IdlingResource getIdlingResource() {
    return mCountingIdlingResource;
}
 
Example #26
Source File: MoviesListingActivity.java    From MovieGuide with MIT License 4 votes vote down vote up
@VisibleForTesting
@NonNull
public IdlingResource getCountingIdlingResource() {
    return EspressoIdlingResource.getIdlingResource();
}
 
Example #27
Source File: MainActivityTest.java    From meat-grinder with MIT License 4 votes vote down vote up
@Override
public void registerIdleTransitionCallback(
        IdlingResource.ResourceCallback resourceCallback) {
    this.resourceCallback = resourceCallback;
}
 
Example #28
Source File: EspressoIdlingResource.java    From simple-stack with Apache License 2.0 4 votes vote down vote up
public static IdlingResource getIdlingResource() {
    return mCountingIdlingResource;
}
 
Example #29
Source File: MainActivityTest.java    From Barricade with Apache License 2.0 4 votes vote down vote up
@Before public void setup() {
  IdlingResource resource = OkHttp3IdlingResource.create("OkHttp", ApiUtils.getOkHttpClient());
  Espresso.registerIdlingResources(resource);
}