Issue
does anybody know, how to set a seekBar to a specific value or just a click on that view on Espresso UI Testing ?
I just get an exception: Error performing 'single click' on view with id...
onView(withId(R.id.FilterPriceMax)).perform(click());
Solution
I use a swipe-action to perform an actual swipe on the SeekBar
. This makes sure the callback method (SeekBar.OnSeekBarChangeListener. onProgressChanged
) is called with fromUser
set to true. It is also more in the line of click testing.
public static ViewAction scrubSeekBarAction(int progress) {
return actionWithAssertions(new GeneralSwipeAction(
Swipe.SLOW,
new SeekBarThumbCoordinatesProvider(0),
new SeekBarThumbCoordinatesProvider(progress),
Press.PINPOINT));
}
private static class SeekBarThumbCoordinatesProvider implements CoordinatesProvider {
int mProgress;
public SeekBarThumbCoordinatesProvider(int progress) {
mProgress = progress;
}
private static float[] getVisibleLeftTop(View view) {
final int[] xy = new int[2];
view.getLocationOnScreen(xy);
return new float[]{ (float) xy[0], (float) xy[1] };
}
@Override
public float[] calculateCoordinates(View view) {
if (!(view instanceof SeekBar)) {
throw new PerformException.Builder()
.withViewDescription(HumanReadables.describe(view))
.withCause(new RuntimeException(String.format("SeekBar expected"))).build();
}
SeekBar seekBar = (SeekBar) view;
int width = seekBar.getWidth() - seekBar.getPaddingLeft() - seekBar.getPaddingRight();
double progress = mProgress == 0 ? seekBar.getProgress() : mProgress;
int xPosition = (int) (seekBar.getPaddingLeft() + width * progress / seekBar.getMax());
float[] xy = getVisibleLeftTop(seekBar);
return new float[]{ xy[0] + xPosition, xy[1] + 10 };
}
}
Answered By - Jakob C
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.