Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso - Perform action fails on fullscreen activity - InjectEventSecurityException

I wasted two days trying to perform click action on a fullscreen activity.

Steps to reproduce

  1. Create a new project in android studio using fullscreen activity code template
  2. Write an espresso test for the activity to perform click

    public class FullscreenActivityTest 
                  extends ActivityInstrumentationTestCase2<FullscreenActivity> {
    public FullscreenActivityTest() {
        super(FullscreenActivity.class);
    }
    
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }
    
    public void testClickingOnLayout() {
        onView(withId(R.id.fullscreen_content)).perform(click());
    }
    

    }

  3. Run this test on emulator (any android version from ginger bread to lollipop).

Failure

android.support.test.espresso.PerformException: 
Error performing 'click' on view 'with id: com.example.espressodefect:id/fullscreen_content'
...
Caused by: android.support.test.espresso.PerformException: 
Error performing 'Send down montion event' on view 'unknown'.
...
Caused by: android.support.test.espresso.InjectEventSecurityException: java.lang.SecurityException: 
Injecting to another application requires INJECT_EVENTS permission

Full stack is here.

Tried

I have tried the following with no luck:

  • No lock screen on the emulator. (also updated activity using WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD)
  • All animations turned off
  • No on-screen keyboard, or anything overlayed on top of the app.
  • Also followed instructions here at developer.android.com
  • JUnit3 and JUnit4 styles (AndroidJUnitRunner)
  • <uses-permission> for INJECT_EVENTS

TL;DR

In espresso test, performing actions work fine if I use non-fullscreen activity like the one created with blank activity. Same action on full screen fails.

I am using Espresso 2.0 and support libraries.

What am I doing wrong here?

Update

Espresso dev confirmed this as defect. Please see issue 140.

like image 396
rpattabi Avatar asked Mar 29 '15 13:03

rpattabi


1 Answers

This bug was solved in the last release of Espresso:

Espresso 2.2.2, Runner/Rules 0.5 (2016-02-22, silent release) - https://google.github.io/android-testing-support-library/downloads/release-notes/index.html

In my project, i've changed the dependency in the /app/build.grandle from:

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'

to

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

In AndroidStudio: Grandle Scripts => build.gradle file

like image 166
tam.teixeira Avatar answered Sep 20 '22 21:09

tam.teixeira