繁体   English   中英

如何在Espresso的RatingBar上设置特定评级?

[英]How to set a specific rating on RatingBar in Espresso?

我正在尝试编写一个测试RatingBar选择的Espresso测试。 如何使用Espresso设置特定评级? 我只看到click() ,它总是设置中等评级。

接受的答案帮助我编写了以下自定义ViewAction:

public final class SetRating implements ViewAction {

    @Override
    public Matcher<View> getConstraints() {
        Matcher <View> isRatingBarConstraint = ViewMatchers.isAssignableFrom(RatingBar.class);
        return isRatingBarConstraint;
    }

    @Override
    public String getDescription() {
        return "Custom view action to set rating.";
    }

    @Override
    public void perform(UiController uiController, View view) {
        RatingBar ratingBar = (RatingBar) view;
        ratingBar.setRating(3);
    }
}

您需要创建一个ViewAction的自定义子类,在视图上调用setRating,然后将自定义视图操作的实例传递给perform()方法。

您可以将tap事件发送到视图的正确部分,而不是在以编程方式设置评级的自定义View操作中调用setRating

这意味着您可以在生产代码中使用boolean fromUser参数,从而无需在设置评级之前将评级栏更改侦听器设置为null:

ratingBar.setRating(viewModel.rating());
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        if (fromUser) {
            viewModel.actions().onRate(rating);
        }
    }
});

以下操作对我来说已经足够了,因为我的评级栏设置为0.5步并且共有5颗星:

RateableMovieViewModel viewModel = viewModel(userActions).build();
rateableMovieViewHolder.bind(viewModel);

onView(withId(R.id.item_rateable_rating)).perform(setRating(4.5f));

verify(userActions).onRate(4.5f);

private static ViewAction setRating(final float rating) {
    if (rating % 0.5 != 0) {
        throw new IllegalArgumentException("Rating must be multiple of 0.5f");
    }

    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(RatingBar.class);
        }

        @Override
        public String getDescription() {
            return "Set rating on RatingBar in 0.5f increments";
        }

        @Override
        public void perform(UiController uiController, View view) {
            GeneralClickAction viewAction = new GeneralClickAction(
                    Tap.SINGLE,
                    new CoordinatesProvider() {
                        @Override
                        public float[] calculateCoordinates(View view) {
                            int[] locationOnScreen = new int[2];
                            v.getLocationOnScreen(locationOnScreen);
                            int screenX = locationOnScreen[0];
                            int screenY = locationOnScreen[1];
                            int numStars = ((RatingBar) view).getNumStars();
                            float widthPerStar = 1f * view.getWidth() / numStars;
                            float percent = rating / numStars;
                            float x = screenX + view.getWidth() * percent;
                            float y = screenY + view.getHeight() * 0.5f;
                            return new float[]{x - widthPerStar * 0.5f, y};
                        }
                    },
                    Press.FINGER,
                    InputDevice.SOURCE_UNKNOWN,
                    MotionEvent.BUTTON_PRIMARY
            );
            viewAction.perform(uiController, view);
        }
    };
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM