意式浓缩咖啡-Set SeekBar [英] Espresso - Set SeekBar

查看:72
本文介绍了意式浓缩咖啡-Set SeekBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何将seekBar设置为特定值,或者只是单击Espresso UI Testing上的该视图吗?

does anybody know, how to set a seekBar to a specific value or just a click on that view on Espresso UI Testing ?

我只是遇到一个例外:对ID为ID的视图执行单击"时出错...

I just get an exception: Error performing 'single click' on view with id...

onView(withId(R.id.FilterPriceMax)).perform(click());

推荐答案

我使用划动动作对SeekBar进行实际划动.这样可以确保在fromUser设置为true的情况下调用回调方法(SeekBar.OnSeekBarChangeListener. onProgressChanged).点击测试中的功能也更多.

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 };
    }
}

这篇关于意式浓缩咖啡-Set SeekBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆