2010-12-07 22 views
0

我正在嘗試爲我的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 
} 

}

回答

0

你得到安全例外,通常當您嘗試與你不能與之交互的應用程序進行交互發生。一個典型的場景是當被測試的應用程序打開一個屬於另一個應用程序的Activity時。在您的Android測試項目(AndroidManifest.xml)中,您可以定義要測試的包。 MapView是該軟件包的一部分嗎?

/Renas

+0

在MyMapActivityTest的AndroidManifest.xml文件中,我有。然後,被測試的課程MyMapActivity以「package com.example.blah」開頭。但是,MapView本身是com.google.android.maps包的一部分。 – jay 2010-12-08 14:13:09

0

當您嘗試和「觸摸」比被測另一個進程的孩子會出現此錯誤。

在您的示例中,我會想象您的obtain()方法正在創建一個觸摸事件,它實際上會與通知欄進行交互 - 而不是由您的應用程序擁有。

如果可能,更安全的交互方式是使用TouchUtils.clickView()

我想在你的實例中,你正在測試單個視圖的觸摸,所以在你發送觸摸事件之前,你可能需要在你的TestCase中查找視圖的頂部,左側,寬度和高度。