このようなものが機能することを示す多くの例がありますが、次のコードは失敗します。このコードは、実際のプロジェクトに関連付けられたテスト プロジェクトにあります。
public class MyTest extends ActivityInstrumentationTestCase2<MyActivity> {
public MyTest(String name)
{
super("com.mypackage.activities", MyActivity.class);
setName(name);
}
public void testTap() throws Throwable
{
//Required by MotionEvent.obtain according to JavaDocs
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
Instrumentation i = getInstrumentation();
//Setup the info needed for our down and up events to create a tap
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 300, 20, 0);
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 300, 20, 0);
//Send the down/up tap event
i.sendPointerSync(downEvent);
i.sendPointerSync(upEvent);
//Delay to see the results
Thread.currentThread().sleep(3000);
}
}
これにより、java.lang.SecurityException がスローされます。別のアプリケーションに注入するには、i.sendPointerSync() 呼び出しに対する INJECT_EVENTS 権限が必要です。view.onTouchEvent(event) と view.dispatchTouchEvent(event) も試しましたが成功しませんでした。
私が考えることができる唯一のことは、これが機能している例がテストされているプロジェクトで生きているかどうかです。テストを別のプロジェクトに分けて、次のようなビルドサーバーから実行できるようにすることをお勧めするため、これは悪いようです。
adb -e shell am instrument -w com.mypackage.activities.test/android.test.InstrumentationTestRunner