Created
October 19, 2017 13:29
-
-
Save anonymous/d64f111e18e45aff272857da0f787060 to your computer and use it in GitHub Desktop.
Espresso Matcher supporting NestedScrollView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.capitalone.mobile.wallet.androidtest; | |
import android.graphics.Rect; | |
import android.support.test.espresso.PerformException; | |
import android.support.test.espresso.UiController; | |
import android.support.test.espresso.ViewAction; | |
import android.support.test.espresso.matcher.ViewMatchers.Visibility; | |
import android.support.test.espresso.util.HumanReadables; | |
import android.support.v4.widget.NestedScrollView; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.HorizontalScrollView; | |
import android.widget.ScrollView; | |
import org.hamcrest.Matcher; | |
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast; | |
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; | |
import static org.hamcrest.Matchers.allOf; | |
import static org.hamcrest.Matchers.anyOf; | |
/** | |
* Enables scrolling to the given view. View must be a descendant of a ScrollView, HorizontalScrollView or | |
* NestedScrollView. This is a copy of android.support.test.espresso.action.ScrollToAction because some of our views | |
* have nested scroll views which can't be interacted with the default action. | |
* | |
* Created BetterScrollToAction because normal ScrollToAction doesn't support android.support.v4.widget.NestedScrollView | |
* | |
* @see android.support.test.espresso.action.ScrollToAction | |
*/ | |
public class BetterScrollToAction implements ViewAction { | |
private static final String TAG = BetterScrollToAction.class.getSimpleName(); | |
protected int percentVisible = 90; | |
@SuppressWarnings("unchecked") | |
@Override | |
public Matcher<View> getConstraints() { | |
return allOf( | |
withEffectiveVisibility(Visibility.VISIBLE), | |
isDescendantOfA(anyOf(isAssignableFrom(ScrollView.class), isAssignableFrom(HorizontalScrollView.class), | |
isAssignableFrom(NestedScrollView.class)))); | |
} | |
@Override | |
public void perform(UiController uiController, View view) { | |
if (isDisplayingAtLeast(percentVisible).matches(view)) { | |
Log.i(TAG, "View is already displayed. Returning."); | |
return; | |
} | |
Rect rect = new Rect(); | |
view.getDrawingRect(rect); | |
if (!view.requestRectangleOnScreen(rect, true /* immediate */)) { | |
Log.w(TAG, "Scrolling to view was requested, but none of the parents scrolled."); | |
} | |
uiController.loopMainThreadUntilIdle(); | |
if (!isDisplayingAtLeast(percentVisible).matches(view)) { | |
throw new PerformException.Builder().withActionDescription(this.getDescription()) | |
.withViewDescription(HumanReadables.describe(view)) | |
.withCause(new RuntimeException("Scrolling to view was attempted, but the view is not displayed")) | |
.build(); | |
} | |
} | |
@Override | |
public String getDescription() { | |
return "scroll to"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment