Androidアプリケーションの単体テストを作成しようとしています。私が問題を抱えている特定のテストは、ユーザーがMapViewのどこかをタップすると、アプリケーションがタップした場所のマーカーをドロップすることを確認することです。しかし、私が試してみるすべてのものは何らかのエラーメッセージを私に残す。誰もこのようなものを書く方法を知っていますか?Androidのアクティビティ/ビューイベントをデバッグする方法
public class MyMapActivityTest extends ActivityInstrumentationTestCase2<MyMapActivity> {
public MyMapActivityTest() {
super("com.example.blah", MyMapActivity.class);
}
public void testPreconditions() {
assertNotNull(getActivity().findViewById(com.example.blah.R.id.mapview));
}
// This method gives me the following error message: java.lang.SecurityException: Injecting to another application requires INJECT_EVENT permission
public void testTap1() {
final MyMapActivity m = getActivity();
final MapView mapView = (MapView) m.findViewById(com.example.blah.R.id.mapview);
getInstrumentation().waitForIdleSync();
getInstrumentation().sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 1, 0, 0));
assertTrue(true); // this doesn't execute
}
// junit.framework.AssertionFailedError: Click can not be completed! Something is in the way e.g. the keyboard
public void testTap2() {
final MyMapActivity m = getActivity();
final MapView mapView = (MapView) m.findViewById(com.example.blah.R.id.mapview);
// Robotium
Solo solo = new Solo(getInstrumentation(), m);
solo.clickOnScreen(1,1);
assertTrue(true); // this doesn't execute
}
// This method gives me the following error message: java.lang.SecurityException: Injecting to another application requires INJECT_EVENT permission
public void testTap3() {
final MyMapActivity m = getActivity();
final MapView mapView = (MapView) m.findViewById(com.example.blah.R.id.mapview);
TouchUtils.tapView(this, mapView);
assertTrue(true); // this doesn't execute
}
}
MyMapActivityTestのAndroidManifest.xmlファイルには、 があります。テスト対象のクラス
MyMapActivity
は、 "package com.example.blah"で始まります。ただし、MapView自体はcom.google.android.mapsパッケージの一部です。 – jayまた、違いがあるかどうかはわかりませんが、 "android.app.Application"のサブクラスをテストしようとしていません。私のプログラムは、 "android.app.Activity"を拡張したクラスです。ありがとう。 – jay
それはまさに問題です。 com.google.android.mapsを他のアプリケーションとして使用することは許可されていません。そのため、セキュリティの例外が発生しています。その周りに道はない。マップアプリケーションを証明書で辞任し、そのアプリケーション用の新しいテストプロジェクトを作成することはできますが、com.example.blahをターゲットパッケージとして指定したので、既存のテストプロジェクトと一緒に使用することはできません。 – Renas