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 run the emulator without -no-window
on headless machines by first running an Xvnc instance (i.e. fake X server) then starting the emulator in that DISPLAY
.
More accurately, I get the Xvnc and Android Emulator Jenkins plugins to do this for me.
Unfortunately, unlocking the screen is still a concern before injecting UI events, but this is (hackily) resolved by automatically running a command like this (similar to this other answer you've seen):
echo "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" | nc -q1 localhost 5554
Edit:
I discovered that this method is far more reliable:
adb shell input keyevent 82
Some info about keycode 82.