下面列出了android.support.test.espresso.action.GeneralSwipeAction#android.support.test.espresso.action.CoordinatesProvider 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Custom ViewAction to click on dedicated coordinates
* @param x -
* @param y -
* @return ViewAction -
*/
private ViewAction clickXY( final int x, final int y ){
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates( View view ){
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final float screenX = screenPos[0] + x;
final float screenY = screenPos[1] + y;
return new float[]{screenX, screenY};
}
},
Press.FINGER);
}
/**
* Custom ViewAction to click on dedicated coordinates
* @param x -
* @param y -
* @return ViewAction -
*/
private ViewAction clickXY( final int x, final int y ){
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates( View view ){
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final float screenX = screenPos[0] + x;
final float screenY = screenPos[1] + y;
return new float[]{screenX, screenY};
}
},
Press.FINGER);
}
public ViewAction clickXY(final float x, final float y){
final DisplayMetrics dm = activityRule.getActivity().getResources().getDisplayMetrics() ;
final float spX = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, x, dm);
final float spY = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, y, dm);
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final float screenX = screenPos[0] + spX;
final float screenY = screenPos[1] + spY;
float[] coordinates = {screenX, screenY};
return coordinates;
}
},
Press.FINGER);
}
public CustomClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider,
PrecisionDescriber precisionDescriber, ViewAction rollbackAction) {
this.coordinatesProvider = coordinatesProvider;
this.tapper = tapper;
this.precisionDescriber = precisionDescriber;
this.rollbackAction = Optional.fromNullable(rollbackAction);
}
/**
* Translates the given coordinates by the given distances. The distances are given in term
* of the view's size -- 1.0 means to translate by an amount equivalent to the view's length.
*/
private static CoordinatesProvider translate(final CoordinatesProvider coords,
final float dx, final float dy) {
return new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
float xy[] = coords.calculateCoordinates(view);
xy[0] += dx * view.getWidth();
xy[1] += dy * view.getHeight();
return xy;
}
};
}
static ViewAction swipeRightNotReachingThreshold(Context context) {
final float x = getWidthScreen(context) * 0.3f;
return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() {
@Override public float[] calculateCoordinates(View view) {
return new float[] {x, 0f};
}
}, Press.FINGER);
}
static ViewAction swipeRightReachingThreshold(Context context) {
final float x = getWidthScreen(context) * 0.8f;
return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() {
@Override public float[] calculateCoordinates(View view) {
return new float[] {x, 0f};
}
}, Press.FINGER);
}
static ViewAction swipeDownNotReachingThreshold(Context context) {
final float y = getHeightScreen(context) * 0.3f;
return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() {
@Override public float[] calculateCoordinates(View view) {
return new float[] {0f, y};
}
}, Press.FINGER);
}
static ViewAction swipeDownReachingThreshold(Context context) {
final float y = getHeightScreen(context) * 0.8f;
return new GeneralSwipeAction(Swipe.SLOW, GeneralLocation.TOP_LEFT, new CoordinatesProvider() {
@Override public float[] calculateCoordinates(View view) {
return new float[] {0f, y};
}
}, Press.FINGER);
}
public NoConstraintsSwipeAction(Swiper swiper, CoordinatesProvider startCoordinatesProvider,
CoordinatesProvider endCoordinatesProvider, PrecisionDescriber precisionDescriber) {
this.swiper = swiper;
this.startCoordinatesProvider = startCoordinatesProvider;
this.endCoordinatesProvider = endCoordinatesProvider;
this.precisionDescriber = precisionDescriber;
}
public CustomClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider,
PrecisionDescriber precisionDescriber) {
this(tapper, coordinatesProvider, precisionDescriber, null);
}
private static ViewAction swipe() {
CoordinatesProvider start = GeneralLocation.TOP_CENTER;
CoordinatesProvider end = GeneralLocation.BOTTOM_CENTER;
return new GeneralSwipeAction(Swipe.SLOW, start, end, Press.FINGER);
}