Android :java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

前端 未结 9 1447
感动是毒
感动是毒 2020-12-01 11:48

Hi there I am new to Android Junit testing:

I have written some test code in MainActivityFunctionalTest.java file

MainActivityFunctionalTest.java:

         


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 12:35

    I was facing this very same problem myself, and here is what I found about this issue.

    1. Adding INJECT_EVENTS permission to your app, makes Android Studio point out that such permission "Is only granted to system apps". Moreover, Google's reference guide for manifest.permissions states that this permission is "Not for use by third-party applications."

      Now, chances are that your app, as mine, is not a system app. So adding this permission is definitely not a good thing to do, and luckily will not be apply on your 3rd party project. At least when developing on Android Studio.

    2. I can see that in your setUp method, you have called setActivityInitialTouchMode(false); As pointed out by Google's best practices for UI testing, when testing UI one must set Touch Mode to true. Otherwise, your test fixture will not be able to interact with UI elements.

    3. Just one more thing. This is an automated test that emulates user actions upon your app. If we interact with the device (real or virtual, doesn't matter), we will most likely make other things gain focus (even inside the app under test) and then there will be a conflict with the touch mode settings that the setUp method had performed.

    Ultimately, that is what was happening to me. I solved my problem simply by not clicking/touching/interacting with the device where the test was being ran.

提交回复
热议问题