Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a view with rotation perform(click()) doesn't work in espresso ui test

I write an espresso UI test for my application, and for some views (in this case ImageButton) perform(click()) doesn't work and also no error is displayed. this is the line that not worked:

   try {
        Thread.sleep(TIME_NORMAL_ELPASED);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
onView(allOf(withId(R.id.toolbar_navigation_btn)
            , isCompletelyDisplayed())).perform(click());

and this is my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="activities.DepartFlight.fragments.AvailableFragment">

<android.support.v7.widget.Toolbar
    android:id="@+id/available_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="@drawable/background_toolbar">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/toolbar_navigation_btn"
            android:layout_width="@dimen/icon_toolbar"
            android:layout_height="@dimen/icon_toolbar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_selector"
            android:rotation="180"
            android:src="@drawable/ic_back"
            android:tint="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/text_toolbar_flight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="2dp"
            android:textColor="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/toolbar_subtitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/text_toolbar_flight"
            android:layout_centerHorizontal="true"
            android:textColor="@color/white"
            android:textSize="@dimen/font_size" />

    </RelativeLayout>
</android.support.v7.widget.Toolbar>

<include
    android:id="@+id/zero_filtered_items"
    android:visibility="gone"
    layout="@layout/zero_filterd_itesm_row" />

<com.faranegar.bookflight.customView.CustomRecyclerView
    android:id="@+id/available_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/depart_flight_activity_footer"
    android:layout_below="@id/available_toolbar"
    android:background="@android:color/transparent"
    android:visibility="gone"
    />

<com.tuyenmonkey.mkloader.MKLoader
    android:id="@+id/progressBar"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:layout_centerInParent="true"
    android:visibility="gone"
    app:mk_color="@color/colorPrimary"
    app:mk_type="TwinFishesSpinner" />

<RelativeLayout
    android:id="@+id/depart_flight_activity_footer"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimary"
    android:visibility="visible">

    <LinearLayout
        android:id="@+id/view_sort"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="18dp"
        android:layout_marginStart="18dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/sort_icon"
            android:tint="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/sortText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:text="@string/sort_text"
            android:textColor="@color/white"
            android:textSize="@dimen/text_less_normal" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/view_filter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:layout_toRightOf="@id/view_sort"
        android:gravity="center"
        android:orientation="horizontal"
        android:visibility="visible">

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/ic_filter"
            android:tint="@color/white" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/filterText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginStart="5dp"
            android:text="@string/filter_text"
            android:textColor="@color/white"
            android:textSize="@dimen/text_less_normal" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/next_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/back_day">

        <Button
            android:id="@+id/next_day_available_btn"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_selector"
            android:textColor="@color/white"
            android:textSize="@dimen/icon_btn" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/next_day_available_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/next_day_available_btn"
            android:text="@string/next_day"
            android:textColor="@color/white"
            android:visibility="visible" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/back_day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp">

        <Button
            android:id="@+id/prev_day_available_btn"
            android:layout_width="20dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/prev_day_available_text"
            android:background="@drawable/button_selector"
            android:textColor="@color/white"
            android:textSize="@dimen/icon_btn" />

        <com.faranegar.bookflight.customView.CustomTextView
            android:id="@+id/prev_day_available_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="@string/back_day"
            android:textColor="@color/white" />
    </RelativeLayout>

</RelativeLayout>

<com.faranegar.bookflight.customView.CustomTextView
    android:id="@+id/warning"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/not_available_flights"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    android:visibility="gone" />

<com.faranegar.bookflight.customView.CustomTextView
    android:id="@+id/ads_motto_captan"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/progressBar"
    android:layout_centerInParent="true"
    android:padding="15dp"
    android:text="@string/captan_ads_motto"
    android:textColor="@color/colorPrimary"
    android:textSize="16sp"
    android:visibility="gone" />

<WebView
    android:id="@+id/arzanBelitBanner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/depart_flight_activity_footer"
    android:visibility="gone" />


<include
    android:id="@+id/failedLayout"
    layout="@layout/failed_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone"
    />

<include
    android:id="@+id/saatraapProgressBarLayout"
    layout="@layout/saatraap_brogressbar_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:visibility="gone" />

and a fragment shows the above XML.

what's wrong?

like image 510
Hamidreza Shokouhi Avatar asked Mar 05 '19 10:03

Hamidreza Shokouhi


1 Answers

I think setRotation may have confused click() and coordinates calculation. If that's the case, you could either remove android:rotation then rotate the image physically, or try to create a custom click action:

public static ViewAction forceClick() {
    return new ViewAction() {
        @Override public Matcher<View> getConstraints() {
            return allOf(isClickable(), isEnabled());
        }

        @Override public String getDescription() {
            return "force click";
        }

        @Override public void perform(UiController uiController, View view) {
            view.performClick();
            uiController.loopMainThreadUntilIdle();
        }
    };
}

This action performs click on the view by invoking performClick() without finding its coordinates. But make sure the view has the click listener attached in the app:

onView(withId(R.id.toolbar_navigation_btn)).perform(forceClick());
like image 71
Aaron Avatar answered Sep 22 '22 07:09

Aaron