How to send key events to a headless emulator in an instrumentation test?


We are currently working on an instrumentation test suite which runs on our build server, but while the tests pass on a dev machine using a normal Android emulator, the builds fail on the build server since there we only run a headless emulator with the -no-window flag.

The failure occurs when trying to invoke the InstrumentationTestCase.sendKeys() method to programmatically open the options menu. The error is:

Permission denied: injecting key event from pid 646 uid 10026 to window Window{43d55100 paused=false} owned by uid 1000

We then found out that there's a INJECT_EVENTS permission, but setting it in the manifest had no effect. In fact in the log we saw this output:

Not granting permission android.permission.INJECT_EVENTS to package com.qype.radar (protectionLevel=2 flags=0x6644)

Does that mean this permission is useless?

We also tried to let the instrumentation test app and the app under test share the same Linux user ID using android:sharedUserId and run in the same process (android:process -- we weren't sure if that was already the case), but still no luck.

Does this mean it's currently impossible to run instrumentations which contain key events on a headless emulator, or are we missing something?

I had similar problem with my test on the Hudson server. In my case the problem I solved by suggestion from Android SDK:

Important was that I had to enable permissions for main application too.