2013-02-17 44 views
4

我正在使用Robotium在功能上測試Android應用程序。我想在onDestroy被調用後測試onResume行爲。 This post關於使用Instrumentation的提示,但我無法讓它工作。 我已經包含了以下內容,但是這會失敗並出現IllegalStateException異常。 是否有可能銷燬應用程序並重新啓動它?如何使用Robotium調用onDestroy來自動測試onResume行爲?

public class MainActivityFunctionalTest extends ActivityInstrumentationTestCase2<MainActivity> { 
private Solo solo; 

public MainActivityFunctionalTest() { 
    super(MainActivity.class); 
} 

public void testActionList() { 
    getInstrumentation().callActivityOnDestroy(solo.getCurrentActivity()); 
    ... 
} 

導致以下異常:

java.lang.IllegalStateException: Must be called from main thread of process at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1373) at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1825) at android.app.Activity.performDestroy(Activity.java:5171) at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109) at nl.handypages.trviewer.test.MainActivityFunctionalTest.testActionList(MainActivityFunctionalTest.java:81) at java.lang.reflect.Method.invokeNative(Native Method) at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214) at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199) at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584) 
+0

要擺脫這種例外的,儘量使用runOnMainSync儀表對象 – maszter 2013-02-17 21:40:07

回答

1

那麼你這裏有兩個問題,第一個問題是,儀表callactivityondestroy方法需要你從主線程中調用它。有關如何執行此操作的詳細信息,請參閱runOnUiThread usage tips

第二個問題是,(我可能在這裏錯了)是從來沒有想過從onDestroy去onResume http://developer.android.com/reference/android/app/Activity.html說,在ondestroy後,活動是從字面上銷燬。你將不得不創建一個新的活動實例來獲得另一個onResume。與您可以通過再次啓動您的活動。我想你可以做你的方式,但林不知道這將是多麼有效的是,如果你想要做這樣的方式,只需調用中的onResume一樣的onDestroy爲我聯繫到的問題指出。

1

謝謝。我添加並稱這個。

public void callActivityOnDestroy(final Activity activity){ 
    getInstrumentation().runOnMainSync(new Runnable() { 
      public void run() { 
       activity.finish(); 
      } 
     }); 
} 
public void callActivityStart(final Activity activity){ 
    getInstrumentation().runOnMainSync(new Runnable() { 
      public void run() { 
       activity.startActivity(new Intent(activity, MainActivity.class)); 
      } 
     }); 
} 

還或者(在我的情況)我就可以撥打電話(如果不希望完全殺滅過程我):

getInstrumentation().callActivityOnStop(getActivity()); 
getInstrumentation().callActivityOnRestart(getActivity()); 
2

我不認爲你可以重新開始一個活動,已經被摧毀。如果你的活動不被破壞,但只停了下來,然後就可以重新啓動並恢復它。的onResume需要在UI線程中調用,所以假設你想測試後,該活動已經在運行恢復狀態,你可以這樣做:

@UiThreadTest 
public void testResumeAfterStop() { 
    Instrumentation instr = this.getInstrumentation(); 

    // here, test or record down whatever should be tested 
    // when the activity is in resume state the first time 

    instr.callActivityOnPause(getActivity()); 
    instr.callActivityOnStop(getActivity()); 
    instr.callActivityOnRestart(getActivity()); 
    instr.callActivityOnStart(getActivity()); 
    instr.callActivityOnResume(getActivity()); 

    // Now you are in the resume state again. 
    // Test whatever you need here. 

} 
2

您可以模擬這樣的配置變化:

getInstrumentation().runOnMainSync(new Runnable() { 
     @Override 
     public void run() { 
      activity.recreate(); 
     } 
    }); 
    setActivity(null); 
    activity = getActivity(); 

這將導致創建活動的新實例,應該正確地去整個生命週期(包括通過捆綁恢復從到新的活動)。請注意,這隻適用於Honeycomb(API等級11)及更高版本。