Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Espresso click on ImageButton clicks on wrong position

I have a problem with espresso UI tests on android when i try to click on an image button:

I have the following layout:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#FFFFFF">

        <ImageButton
                android:id="@+id/parent_task_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:rotation="90"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_subdirectory_arrow_left_black_24dp"
                android:paddingRight="10dp"
                android:paddingLeft="12dp"/>

        <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="@color/layout_divider"></View>

        <com.mypackage.ui.TasklistItem
                android:id="@+id/parent_task_item"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </LinearLayout>

in my app this looks like this:

enter image description here

in my espresso test i try clicking the button (the one with the arrow) in it with:

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

but the click does not work as expected. i stepped it through (with the android "Show Taps" and "Pointer location" developer setting enabled) and it turns out that the test does not click centered on the image button but clicks here:

enter image description here

...exactly on the divider line but not on the center of the ImageButton.

If i replace the ImageButton with a normal Button the click is correct and the tests work! Has anyone an idea what the problem is here or how to fix it?

like image 755
stamanuel Avatar asked Jan 19 '26 02:01

stamanuel


1 Answers

I found the line that causes the problem (in the ImageButton):

 android:rotation="90"

If i remove this rotation from the image button the click is correctly in the middle. So my temporary fix is to rotate the drawable itself and not the button.

I am also currently digging into the android espresso code and trying to find the code that's wrong here. It seems the calculation for the coordinates to click is wrong because it ignores the rotation and therefore calculates it wrong. Will update this post if i find anything.

like image 150
stamanuel Avatar answered Jan 20 '26 15:01

stamanuel