Amir Jamak
Amir Jamak

Reputation: 351

Authorize Android Instrumentation Test for Root Access on Emulator

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.

Upvotes: 0

Views: 1273

Answers (1)

Paul Harris
Paul Harris

Reputation: 5809

You are going to have lots of issues with your approach, You could automate the button press using a different framework other than instrumentation (e.g. monkey/http://developer.android.com/tools/testing/testing_ui.html) but my vote for you would be to utilise snapshots/a custom disk image for your emulator, create your own snapshot/image that has the phone rooted and then you probably can should be able launch the emulator and tell it to use the snapshot that has root access already.

Upvotes: 1

Related Questions