I have developed an Android app that requires root access, and it works fine. I am trying to test the app using instrumentation tests with ActivityInstrumentationTestCase2
and cover the parts of the app that utilize root access. The tests should be fully automated as they will run on our CI.
The CI build creates a fresh emulator, and then it roots the emulator using the following commands:
adb -e install superuser.apk
adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
adb push su /system/xbin/su
adb shell chmod 06755 /system
adb shell chmod 06755 /system/xbin/su
It works OK, but at some point I have to click "Allow" button on the Superuser's root access prompt when the test requires it. My problem is how to execute those tests without manual clicking the "Allow" button and gain the root access.
If I try to click this button from instrumentation test using the following code
instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
I get
java.lang.SecurityException:
Injecting to another application requires INJECT_EVENTS permission
Apparently, this privilege is used only by system apps, and I cannot authorize my apps with this permission. Maybe I am doing something wrong here?
Another idea is to pre-authorize the app during the emulator boot from command line, but I haven't found the way for this.