Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout

It looks like CoordinatorLayout breaks the behaviour of Espresso actions such as scrollTo() or RecyclerViewActions.scrollToPosition().

Issue with NestedScrollView

For a layout like this one:

<android.support.design.widget.CoordinatorLayout     android:layout_width="match_parent"     android:layout_height="match_parent">      <android.support.v4.widget.NestedScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content"         app:layout_behavior="@string/appbar_scrolling_view_behavior">          ...      </android.support.v4.widget.NestedScrollView>      <android.support.design.widget.AppBarLayout         android:layout_width="match_parent"         android:layout_height="wrap_content" >          ...      </android.support.design.widget.AppBarLayout>  </android.support.design.widget.CoordinatorLayout> 

If I try to scroll to any view inside the NestedScrollView using ViewActions.scrollTo() the first problem I find is that I get a PerformException. This is because this action only supports ScrollView and NestedScrollView doesn't extend it. A workaround for this problem is explained here, basically we can copy the code in scrollTo() and change the constrains to support NestedScrollView. This seems to work if the NestedScrollView is not in a CoordinatorLayout but as soon as you put it inside a the CoordinatorLayout the scrolling action fails.

Issue with RecyclerView

For the same layout, if I replace the NestedScrollView with a RecyclerView there is also problems with the the scrolling.

In this case I'm using RecyclerViewAction.scrollToPosition(position). Unlike the NestedScrollView, here I can see some scrolling happening. However, it looks like it scrolls to the wrong position. For example, if I scroll to the last position, it makes visible the second to last but not the last one. When I move the RecyclerView out of the CoordinatorLayout the scrolling works as it should.

At the moment we can't write any Espresso test for the screens that use CoordinatorLayout due to this issues. Anyone experiencing the same problems or knows a workaround?

like image 352
ivacf Avatar asked Feb 08 '16 15:02

ivacf


2 Answers

This is happening because the Espresso scrollTo() method explicitly checks the layout class and only works for ScrollView & HorizontalScrollView. Internally it's using View.requestRectangleOnScreen(...) so I'd expect it to actually work fine for many layouts.

My workaround for NestedScrollView was to take ScrollToAction and modify that constraint. The modified action worked fine for NestedScrollView with that change.

Changed method in ScrollToAction class:

public Matcher<View> getConstraints() {     return allOf(withEffectiveVisibility(Visibility.VISIBLE), isDescendantOfA(anyOf(             isAssignableFrom(ScrollView.class), isAssignableFrom(HorizontalScrollView.class), isAssignableFrom(NestedScrollView.class)))); } 

Convenience method:

public static ViewAction betterScrollTo() {     return ViewActions.actionWithAssertions(new NestedScrollToAction()); } 
like image 200
Turnsole Avatar answered Oct 17 '22 03:10

Turnsole


Here is how I did the same thing that @miszmaniac did in Kotlin. With delegation in Kotlin, it is much cleaner and easier because I don't have to override the methods I don't need to.

class ScrollToAction(     private val original: android.support.test.espresso.action.ScrollToAction = android.support.test.espresso.action.ScrollToAction() ) : ViewAction by original {    override fun getConstraints(): Matcher<View> = anyOf(       allOf(           withEffectiveVisibility(Visibility.VISIBLE),           isDescendantOfA(isAssignableFrom(NestedScrollView::class.java))),       original.constraints   ) } 
like image 33
tasomaniac Avatar answered Oct 17 '22 05:10

tasomaniac