Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 415
Matthias Avatar asked Jul 09 '10 16:07

Matthias


2 Answers

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.

like image 159
Christopher Orr Avatar answered Oct 19 '22 03:10

Christopher Orr


I had similar problem with my test on the Hudson server. In my case the problem I solved by suggestion from Android SDK: http://developer.android.com/guide/topics/testing/testing_android.html#UITestTroubleshooting

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

like image 42
hsd Avatar answered Oct 19 '22 01:10

hsd