kwi wi
kwi wi

Reputation: 103

TouchUtils with Android Emulator

Has anyone been able to successfully perform unit testing on the Android Emulator using methods offered by the TouchUtils class?
I'm able to get the test(s) to pass on my device but when I run the exact same test(s) (and test suite) on an emulator, any test using a TouchUtils methods always throws the following exception:

java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
at android.os.Parcel.readException(Parcel.java:1327)
at android.os.Parcel.readException(Parcel.java:1281)
at android.view.IWindowManager$Stub$Proxy.injectPointerEvent(IWindowManager.java:1196)
at android.app.Instrumentation.sendPointerSync(Instrumentation.java:902)
at android.test.TouchUtils.drag(TouchUtils.java:786)
at android.test.TouchUtils.dragViewTo(TouchUtils.java:633)
...

I've unlocked the keyguard and even ran other (non TouchUtils) test cases which have passed.
I do not have the @UiThreadTest applied nor am I running anything that requires code to run on the UI thread.
When I comment out the line that employs TouchUtils.dragViewTo(...) and put in a simple assert(true), the test runs and passes.

Any ideas?

Upvotes: 7

Views: 1281

Answers (2)

Tyler
Tyler

Reputation: 19848

The emulator is so slow, that when you emulate a UI interaction after you dismiss the virtual keyboard, there isn't enough time for the virtual keyboard to dismiss and so you are actually injecting events into the virtual keyboard and not your application.

Simply sleep on your thread for about 500-1000ms before you attempt to do any UI interactions after hiding the soft keyboard.

try {
    Thread.sleep(500);
} 
catch (InterruptedException e) {
    e.printStackTrace();
}

Upvotes: 0

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

It happens if your phone is locked or there is some other Activity on the HomeScreen.

Upvotes: 2

Related Questions