How close toolbar Espresso

2019-07-10 16:18发布

问题:

I have layout with coordinatlayout, AppBarLayout and CollapsingToolbarLayout. I want to write a test that clicks on the last item in the list. I scroll through the list until the end, however, the desired item is hidden from the espresso.To scroll through the list, I use:
onView(withId(R.id.widget_recycler_view)).perform(RecyclerViewActions.scrollToPosition(3)); To hide, scroll through the toolbar, I tried the following code:

onView(withId(R.id.coordinator_layout)).perform(swipeUp());
onView(withId(R.id.toolbar)).perform(swipeUp());

But this code hides the toolbar only half, but it's not enough. Here is the code of the layout:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/coordinator_layout"
        >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:background="?attr/colorPrimary">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_scrollFlags="scroll|enterAlways"
                android:fitsSystemWindows="true">

                <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:id="@+id/toolbar"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    android:layout_gravity="top"
                    app:layout_collapseMode="parallax"
                    android:background="?attr/colorPrimary"
                    >
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <com.makeramen.RoundedImageView
                            style="@style/AvatarImage.MainActivity"
                            android:id="@+id/iv_avatar"
                            android:layout_marginRight="18dp"/>

                        <com.akbars.bankok.views.custom.TextViewFonted
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceSmallInverse"
                            android:layout_toRightOf="@id/iv_avatar"
                            android:id="@+id/name"
                            android:textSize="12sp"
                            app:customFont="Roboto-Medium.ttf"
                            android:textColor="@android:color/white"/>

                        <com.akbars.bankok.views.custom.TextViewFonted
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:textSize="16sp"
                            app:customFont="Roboto-Light.ttf"
                            android:id="@+id/tv_balance"
                            android:layout_toRightOf="@id/iv_avatar"
                            android:layout_below="@id/name"
                            android:textColor="@android:color/white"/>
                    </RelativeLayout>
                    <ProgressBar
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        style="?android:attr/progressBarStyleSmall"
                        android:id="@+id/progress_bar"
                        android:visibility="gone"
                        android:layout_marginLeft="20dp"/>
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
        <com.akbars.bankok.views.custom.SwipeEnableViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_view_pager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_marginBottom="@dimen/tab_bar_layout_height"/>

        <com.akbars.bankok.views.custom.CustomTabLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/bottom_navigation_tab"
            android:id="@+id/tabs"
            app:tabMode="fixed"
            app:tabIndicatorColor="@android:color/transparent"
            android:layout_gravity="center_horizontal|bottom"
            style="@style/MyCustomTabLayout"
            android:background="@android:color/white"
            android:elevation="10dp"
            />
    </android.support.design.widget.CoordinatorLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/shadow"
        android:background="@color/shadow"
        android:visibility="invisible"
        />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        style="@style/FABButton"
        android:src="@drawable/ic_import_export_white_24dp"
        app:backgroundTint="?attr/colorPrimary"
        />
    <include layout="@layout/fab_layout" />
</FrameLayout>

Just I attach my test. If you scroll through the hands, it works :) But it is necessary robotized process.

    @Test
    public void addNewPhone(){
        onView(withId(R.id.widget_recycler_view)).perform(RecyclerViewActions.scrollToPosition(3));
        onView(withId(R.id.coordinator_layout)).perform(swipeUp());
        putDelay(2500);
        onView(withId(R.id.layout_add_phone)).perform(click());
        onView(withId(R.id.edit_letay_phone)).perform(replaceText("9586222922"));
        closeSoftKeyboard();
        onView(withText(R.string.add)).perform(click());
        closeSoftKeyboard();
        onView(withId(R.id.edit_code_letay_phone)).perform(replaceText("52461"));
        onView(withId(R.id.bt_code_request)).perform(click());
    }

My question is: How do I hide the toolbar completely when scrolling the list or of the layout?

回答1:

I use the following custom view action in my tests:

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

        @Override
        public String getDescription() {
            return "Collapse App Bar Layout";
        }

        @Override
        public void perform(UiController uiController, View view) {
            AppBarLayout appBarLayout = (AppBarLayout) view;
            appBarLayout.setExpanded(false);
            uiController.loopMainThreadUntilIdle();
        }
    };
}

And the usage:

onView(withId(R.id.app_bar_layout)).perform(CustomViewActions.collapseAppBarLayout());

Keep it mind, you need to have animations disabled in the device settings, otherwise, Espresso will not wait for it to fully collapse before proceeding to the next step.