5

my espresso instrumentation test can't click an ImageView:

Error performing 'single click' on view 'with id: com.example.test:id/btnNext'.

Caused by: android.support.test.espresso.PerformException: Error performing 'click (after 3 attempts)' on view 'unknown'.

Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

But what is strange that just three lines before it is able to click another ImageView looking quite similar to me. And using the app as human works as well.

Here is my minimal non-working example, do you see the error?

@RunWith(AndroidJUnit4.class)
@LargeTest
public class TestTest {

    @Rule
    public ActivityTestRule<TestActivity> _activityRule = new ActivityTestRule<TestActivity>(
            TestActivity.class);
    @Test
    public void testClicking() {
        onView(withId(R.id.btnPrevious)).perform(click());
        onView(withId(R.id.btnNext)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
        onView(withId(R.id.btnNext)).check(matches(isClickable()));
        onView(withId(R.id.btnNext)).perform(click()); // This line throws an exception
    }
}

The activity to test:

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);
    }
}

And the layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:id="@+id/lytFooter">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnPrevious"
            app:srcCompat="@drawable/left_arrow_shadow"
            android:layout_margin="1dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/lightbutton_selector"
            android:focusable="true"/>

        <TextView
            android:id="@+id/txtBottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/look_around"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:gravity="center_horizontal"
            style="@style/CameraText" />

        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/prgVideoMode"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:gravity="center_horizontal"
            android:max="100" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btnNext"
            app:srcCompat="@drawable/left_arrow_shadow"
            android:layout_margin="1dp"
            android:layout_gravity="center_vertical"
            android:background="@drawable/lightbutton_selector"
            android:focusable="true"
            android:clickable="true"/>

    </LinearLayout>

</RelativeLayout>

The full stracktrace is 80 lines long so I posted it here if you are interested: http://pastebin.com/RWa91G63

Community
  • 1
  • 1
PhilLab
  • 4,777
  • 1
  • 25
  • 77
  • provide your onCLickListener method... As I understand after clicking view, you go to another app like Googles Photos, right? – piotrek1543 Sep 08 '16 at 16:21
  • @piotrek: actually this is the whole code. There is no onClick set (and even if I do and just add a simple Log.v() ), it is not executed. – PhilLab Sep 08 '16 at 18:21
  • And please notice that the click on btnPrevious works - no listener there as well. – PhilLab Sep 08 '16 at 18:23
  • i don't understand why you use ImageViews instead of ImageButtons and why you're performing click() action when there's no listener. So what doeas this nextBtn do? – piotrek1543 Sep 08 '16 at 18:36
  • This is the minimal-non-working example I compiled from my otherwise functional (and much larger) application. To make sure I did not forget anything, I moved them to an extra activity&test and watched them fail :-) – PhilLab Sep 09 '16 at 07:44

0 Answers0