我正在嘗試爲我的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 2010-12-08 14:13:09