package de.dennisguse.opentracks.util; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.widget.ListView; import androidx.test.espresso.UiController; import androidx.test.espresso.ViewAction; import com.google.android.material.tabs.TabLayout; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; public class EspressoUtils { public static Matcher childAtPosition( final Matcher parentMatcher, final int position) { return new TypeSafeMatcher<>() { @Override public void describeTo(Description description) { description.appendText("Child at position " + position + " in parent "); parentMatcher.describeTo(description); } @Override public boolean matchesSafely(View view) { ViewParent parent = view.getParent(); return parent instanceof ViewGroup && parentMatcher.matches(parent) && view.equals(((ViewGroup) parent).getChildAt(position)); } }; } public static Matcher withListSize (final int size) { return new TypeSafeMatcher<>() { @Override public boolean matchesSafely(final View view) { return ((ListView) view).getCount() == size; } @Override public void describeTo(final Description description) { description.appendText("ListView should have " + size + " items"); } }; } public static ViewAction waitFor(final long duration_ms) { return new ViewAction() { @Override public String getDescription() { return "Wait for milliseconds."; } @Override public Matcher getConstraints() { return isDisplayed(); } @Override public void perform(UiController uiController, final View view) { uiController.loopMainThreadForAtLeast(duration_ms); } }; } public static ViewAction selectTabAtIndex(final int index) { return new ViewAction() { @Override public String getDescription() { return "Selecting tab."; } @Override public Matcher getConstraints() { return isDisplayed(); } @Override public void perform(UiController uiController, View view) { TabLayout tabLayout = (TabLayout) view; tabLayout.getTabAt(index).select(); } }; } }