Java Code Examples for androidx.test.espresso.Espresso#registerIdlingResources()

The following examples show how to use androidx.test.espresso.Espresso#registerIdlingResources() . 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: TrafficPluginTest.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Before
public void beforeTest() {
  try {
    Timber.e("@Before: register idle resource");
    idlingResource = new OnMapReadyIdlingResource(rule.getActivity());
    Espresso.registerIdlingResources(idlingResource);
    onView(withId(android.R.id.content)).check(matches(isDisplayed()));
    mapboxMap = idlingResource.getMapboxMap();
    trafficPlugin = rule.getActivity().getTrafficPlugin();
  } catch (IdlingResourceTimeoutException idlingResourceTimeoutException) {
    Timber.e("Idling resource timed out. Couldn't not validate if map is ready.");
    throw new RuntimeException("Could not start executeTrafficTest for " + this.getClass().getSimpleName() + ".\n"
      + "The ViewHierarchy doesn't contain a view with resource id = R.id.mapView or \n"
      + "the Activity doesn't contain an instance variable with a name equal to mapboxMap.\n");
  }
}
 
Example 2
Source File: Sample2Tests.java    From PdfViewPager with Apache License 2.0 6 votes vote down vote up
@Test
public void swipesPdfToLastPageAndBackWithNoCrashesWhenDownloadIsCompleted() {
    startActivity();
    openActionBarMenu();

    onView(withText(R.string.menu_sample2_txt)).perform(click());
    onView(withId(R.id.btn_download)).perform(click());
    idlingResource = new WaitIdlingResource(System.currentTimeMillis(), 1500);
    Espresso.registerIdlingResources(idlingResource);
    try {
        swipeToLastPage();
        swipeToBeginning();
    } catch (Exception ex) {
        fail("Remote PDF crashes when paging");
    }
}
 
Example 3
Source File: EmailPasswordTest.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    if (mActivityResource != null) {
        Espresso.unregisterIdlingResources(mActivityResource);
    }

    // Register Activity as idling resource
    mActivityResource = new BaseActivityIdlingResource(mActivityTestRule.getActivity());
    Espresso.registerIdlingResources(mActivityResource);
}
 
Example 4
Source File: AnonymousTest.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    if (mActivityResource != null) {
        Espresso.unregisterIdlingResources(mActivityResource);
    }

    // Register Activity as idling resource
    mActivityResource = new BaseActivityIdlingResource(mActivityTestRule.getActivity());
    Espresso.registerIdlingResources(mActivityResource);
}
 
Example 5
Source File: MainActivityTest.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
@Before
public void before() {
    // Initialize intents
    Intents.init();

    // Idling resource
    mUploadIdlingResource = new ServiceIdlingResource(mActivityTestRule.getActivity(),
            MyUploadService.class);
    Espresso.registerIdlingResources(mUploadIdlingResource);
}
 
Example 6
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void withFabVisibilityChange(boolean shown, Runnable action) {
  OnVisibilityChangedListener listener = new OnVisibilityChangedListener(shown);
  CoordinatorLayout.LayoutParams lp =
      (CoordinatorLayout.LayoutParams) activityTestRule.getActivity().mFab.getLayoutParams();
  FloatingActionButton.Behavior behavior = (FloatingActionButton.Behavior) lp.getBehavior();
  behavior.setInternalAutoHideListener(listener);
  Espresso.registerIdlingResources(listener);
  try {
    action.run();
  } finally {
    Espresso.unregisterIdlingResources(listener);
  }
}
 
Example 7
Source File: Sample2Tests.java    From PdfViewPager with Apache License 2.0 5 votes vote down vote up
@Test
public void showsPdfAfterDownload() {
    startActivity();
    openActionBarMenu();

    onView(withText(R.string.menu_sample2_txt)).perform(click());
    onView(withId(R.id.btn_download)).perform(click());
    idlingResource = new WaitIdlingResource(System.currentTimeMillis(), 1500);
    Espresso.registerIdlingResources(idlingResource);
    onView(withId(R.id.btn_download)).check(matches(isDisplayed()));
    onView(withId(R.id.pdfViewPager)).check(matches(isDisplayed()));
}
 
Example 8
Source File: ViewPagerActions.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public final void perform(UiController uiController, View view) {
  final ViewPager viewPager = (ViewPager) view;
  // Add a custom tracker listener
  final CustomViewPagerListener customListener = new CustomViewPagerListener();
  viewPager.addOnPageChangeListener(customListener);

  // Note that we're running the following block in a try-finally construct. This
  // is needed since some of the actions are going to throw (expected) exceptions. If that
  // happens, we still need to clean up after ourselves to leave the system (Espresso) in a good
  // state.
  try {
    // Register our listener as idling resource so that Espresso waits until the
    // wrapped action results in the view pager getting to the STATE_IDLE state
    Espresso.registerIdlingResources(customListener);

    uiController.loopMainThreadUntilIdle();

    performScroll((ViewPager) view);

    uiController.loopMainThreadUntilIdle();

    customListener.mNeedsIdle = true;
    uiController.loopMainThreadUntilIdle();
    customListener.mNeedsIdle = false;
  } finally {
    // Unregister our idling resource
    Espresso.unregisterIdlingResources(customListener);
    // And remove our tracker listener from ViewPager
    viewPager.removeOnPageChangeListener(customListener);
  }
}
 
Example 9
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);
}
 
Example 10
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void registerIdlingResourceCallback() {
  // This cannot be done in setUp(), or swiping action cannot be executed.
  callback = new Callback(getBehavior());
  Espresso.registerIdlingResources(callback);
}
 
Example 11
Source File: TabLayoutTest.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void testSetScrollPosition(final boolean isLtr) throws Throwable {
  activityTestRule.runOnUiThread(
      () -> activityTestRule.getActivity().setContentView(R.layout.design_tabs_fixed_width));
  final TabLayout tabs = activityTestRule.getActivity().findViewById(R.id.tabs);
  assertEquals(TabLayout.MODE_SCROLLABLE, tabs.getTabMode());

  final TabLayoutScrollIdlingResource idler = new TabLayoutScrollIdlingResource(tabs);
  Espresso.registerIdlingResources(idler);

  // We're going to call setScrollPosition() incrementally, as if scrolling between one tab
  // and the next. Use the middle tab for best results. The positionOffsets should be in the
  // range [0, 1), so the final call will wrap to 0 but use the next tab's position.
  final int middleTab = tabs.getTabCount() / 2;
  final int[] positions = {middleTab, middleTab, middleTab, middleTab, middleTab + 1};
  final float[] positionOffsets = {0f, .25f, .5f, .75f, 0f};

  // Set layout direction
  onView(withId(R.id.tabs))
      .perform(
          setLayoutDirection(
              isLtr ? ViewCompat.LAYOUT_DIRECTION_LTR : ViewCompat.LAYOUT_DIRECTION_RTL));
  // Make sure it's scrolled all the way to the start
  onView(withId(R.id.tabs)).perform(selectTab(0));

  // Perform a series of setScrollPosition() calls
  final AtomicInteger lastScrollX = new AtomicInteger(tabs.getScrollX());
  for (int i = 0; i < positions.length; i++) {
    onView(withId(R.id.tabs))
        .perform(setScrollPosition(positions[i], positionOffsets[i]))
        .check(
            (view, notFoundException) -> {
              if (view == null) {
                throw notFoundException;
              }
              // Verify increasing or decreasing scroll X values
              int sx = view.getScrollX();
              assertTrue(isLtr ? sx > lastScrollX.get() : sx < lastScrollX.get());
              lastScrollX.set(sx);
            });
  }

  Espresso.unregisterIdlingResources(idler);
}
 
Example 12
Source File: MoviesListingActivityTest.java    From MovieGuide with MIT License 4 votes vote down vote up
@Before
public void registerIdlingResource() {
    idlingResource = activityTestRule.getActivity().getCountingIdlingResource();
    Espresso.registerIdlingResources(idlingResource);
}