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

  3. Refreshing OAuth token using Retrofit without modifying all calls
  4. arm-linux-androideabi-gcc is unable to create an executable - compile ffmpeg for android armeabi devices
  5. How to develop Augmented Reality application for Android
  6. Android Error inflating class SwipeRefreshLayout
  7. Why don't I have permission to write to app dir on external storage?
  8. About Android image and asset sizes
  9. 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());
    }
    

    }

  10. 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.

  • Unable to find hprof-conv when launching Android Device Monitor
  • How to show toast in AsyncTask in doInBackground
  • Android Studio with gradle and Google Maps v2
  • How to make annotation like highlighting, strikethrough, underline, draw, add text, etc in android for a pdf viewer?
  • Android:How to programmatically set an Activity's theme to Theme.Dialog
  • NetBeans and stop/break on all Exceptions?
  • One Solution collect form web for “Espresso – Perform action fails on fullscreen activity – InjectEventSecurityException”

    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

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.