我想檢查一下,當它運行的設備沒有攝像頭時,我的應用程序會顯示一條錯誤消息。我嘗試傳遞一個模擬上下文,但當我試圖模擬CameraManager類時,mockito給出了一個錯誤,因爲它聲明爲final。當然,android有一個簡單的解決方案呢?這是我的嘗試:Android - 測試我的應用程序時,如何模擬缺相機?
public class CreateNewIdentityActivityUnitTest extends ActivityUnitTestCase<CreateNewIdentityActivity> {
public CreateNewIdentityActivityUnitTest() {
super(CreateNewIdentityActivity.class);
}
public void testErrorMessageDisplayedWhenNoCamerasExist() throws Exception {
// Create the mock cameramanager
// THIS LINE FAILS BECAUSE CAMERAMANAGER IS FINAL
CameraManager mockCameraManager = mock(CameraManager.class);
String[] cameraIdList = {};
when(mockCameraManager.getCameraIdList()).thenReturn(cameraIdList);
// Create the mock context
Context mockContext = mock(Context.class);
when(mockContext.getSystemService(Context.CAMERA_SERVICE)).thenReturn(mockCameraManager);
// Start the activity
Intent intent = new Intent(mockContext, CreateNewIdentityActivity.class);
Activity activity = startActivity(intent, null, null);
// Verify that the error message was made visible
TextView errorTextView = (TextView)activity.findViewById(R.id.ErrorTextView);
assertNotNull(errorTextView);
assertEquals(View.VISIBLE, errorTextView.getVisibility());
}
}
這是否適用於靜態圖片?我嘗試了一個PNG文件,但它不起作用。 –
您可以將位圖轉換爲紋理並加載到GLSurfaceView。 – muneikh
謝謝。我應該得到紋理,加載它在CameraActivity.java,我改變this.videoRenderer =新的VideoRenderer(CameraActivity.this,this.textureVideoInputSource); to this.videoRenderer = new VideoRenderer(CameraActivity.this,this.texture);假設我有一個名爲text的私人類成員。我知道這似乎並不正確。 –