2012-03-07 7 views
1

Instrumentation을 사용하여 TestCase에서 Android 활동의 라이프 사이클을 제어하는 ​​방법은 무엇입니까?장비 : 컨트롤 라이프 사이클

official documentation에서 "라이프 사이클 제어 : 계측을 사용하면 테스트 사례 클래스에서 제공하는 방법을 사용하여 테스트중인 활동을 시작하고 일시 중지하고 파괴 할 수 있습니다."라고 명시되어 있습니다. 물론이 테스트 케이스를 사용하면 getActivity()를 호출 할 때 자동으로 액티비티가 생성되고 각 테스트 케이스 후에 중지됩니다. 그러나 모든 라이프 사이클 메소드가 올바르게 구현되었는지 확인하기 위해 라이프 사이클을 외부 적으로 제어하는 ​​방법은 무엇입니까?

onactivityXXX의 라이프 사이클 메소드는 각 메소드를 호출하는 데 도움이되지만 활동을 일시 중지하거나 중지시키지 않습니다. 아무도 도움을 요청하고 사용할 방법을 말해 줄 수 있습니까?

Android 애플리케이션의 라이프 사이클 구현을 테스트 할 수있는 방법이 있습니까?

답변

1

이 당신에게 수명주기를 완벽하게 제어를 제공하지 않습니다하지만 그것은 예 here을 찾을 수 있습니다 : 당신이 주요 라이프 사이클 이벤트 일부 제어를 제공

// Start the main activity of the application under test 
    mActivity = getActivity(); 

    // Get a handle to the Activity object's main UI widget, a Spinner 
    mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01); 

    // Set the Spinner to a known position 
    mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION); 

    // Stop the activity - The onDestroy() method should save the state of the Spinner 
    mActivity.finish(); 

    // Re-start the Activity - the onResume() method should restore the state of the Spinner 
    mActivity = getActivity(); 

    // Get the Spinner's current position 
    int currentPosition = mActivity.getSpinnerPosition(); 

    // Assert that the current position is the same as the starting position 
    assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition); 

. 나는 현재 같은 문제를 다루고있는 메신저를 가지고 있는데, 도와야 할 로보슘을 찾고있다.