2013-05-21 81 views
1

在我的單元測試過程中,我想基本測試一個活動是否由一個按鈕上的簡單按壓啓動,即在HorizontalListView中。在之前的單元測試中,我已經成功地開始了新的活動,但是這個活動並不像通緝那樣。我有斷言失敗告訴我,我想要啓動的活動仍然爲空。Android JUnit Horizo​​ntalListView performItemClick()不起作用

其實,當我啓動我的單元測試時,它似乎沒有執行點擊。我必須在測試過程中使用我的小指執行測試我自己

要查看我是否點擊了正確的視圖,我使用了日誌來查看我的測試點擊了哪個視圖以及我實際想要點擊的視圖:它似乎是正確的視圖(正確的ID,正確的位置和正確的看法)。所以我認爲問題不在於我點擊的地方。

我正在測試的Activity稱爲StudioActivity,我想要啓動的Activity稱爲AddBoasterActivity。

我的單元測試方法:

public void testSwitchToAddBoaster(){ 
    assertTrue(mBoasterPreviewFragment.getWithAddButton()); 
    ActivityMonitor activityMonitor = getInstrumentation().addMonitor(AddBoasterActivity.class.getName(), null, false); 
    mStudioActivity.runOnUiThread(new Runnable() { 
     @Override 
     public void run(){ 
      int lPosition = mBoasterPreviewFragment.getAdapter().getCount()-1; 
      View lView = mBoasterPreviewFragment.getBoaster().getChildAt(lPosition); 

      //HERE ARE LOGS TO SEE IF MY TEST IS PERFORMING 
      //THE CLICK ON THE RIGHT VIEW AT THE GOOD POSITION 
      Log.i("TEST POSITION:", ""+lPosition); 
      Log.i("TEST ID: ",""+lView.getId()); 
      Log.i("TEST VIEW: ", ""+lView); 
      //AND IT'S THE GOOD VIEW! 

      mBoasterPreviewFragment.getBoaster().performItemClick(lView,lPosition,lView.getId()); 
     } 
     });  
    AddBoasterActivity lAddBoasterActivity = (AddBoasterActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor,5000);   
    assertNotNull(lAddBoasterActivity); //HERE IS THE FAILURE, THE ACTIVITY HASN'T BEEN STARTED 

    mWFBoasterPreviewFragment = new WeakReference<BoasterPreviewFragment>((BoasterPreviewFragment) lAddBoasterActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_preview)); 
    mBoasterPreviewFragment = mWFBoasterPreviewFragment.get();  
    assertNotNull(mBoasterPreviewFragment); 

    lAddBoasterActivity.finish(); 
}  

的OnItemClickListener:

mBoaster.setAdapter(mAdapter); 
    if (withAddBoasterButton) { 
     mBoaster.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       if (position == mAdapter.getCount() - 1) { 
        //HERE IS THE CURRENT VIEW 
        Log.i("POSITION:", ""+position); 
        Log.i("ID: ",""+id); 
        Log.i("VIEW: ", ""+view); 
        Intent intent = new Intent(getActivity().getApplicationContext(), AddBoasterActivity.class); 
        intent.putExtra(AddBoasterActivity.EXTRA_BOASTER_SELECTED, mSelectedUsers); 
        intent.putExtra(AddBoasterActivity.EXTRA_IS_STREAM, isStream); 
        startActivityForResult(intent, AddBoasterActivity.REQUEST_CODE_ACTIVITY); 
      } 
     }}); 
    } 

手動執行點擊是用於具有要被自動化測試相當尷尬。所以如果有人知道它爲什麼這樣做。

感謝您的幫助!

回答

0

嗯,我終於找到了......只是一個晚上睡覺,這是好的。我應該早點考慮一下,但Robotium已經幫我幾次了。

檢查出來,如果你還沒有測試它尚未:https://code.google.com/p/robotium/

因此,這裏是它的外觀像Robotium:

public void testSwitchToAddBoaster(){ 
    Solo solo = new Solo(getInstrumentation(), getActivity()); 
    getInstrumentation().waitForIdleSync(); 
    ActivityMonitor activityMonitor = getInstrumentation().addMonitor(AddBoasterActivity.class.getName(), null, false); 
    assertTrue(mBoasterPreviewFragment.getWithAddButton()); 

    int lPosition = mBoasterPreviewFragment.getAdapter().getCount()-1; 
    View lView = mBoasterPreviewFragment.getBoaster().getChildAt(lPosition); 

    solo.clickOnView(lView); //SIMPLE CLICK 

    AddBoasterActivity lAddBoasterActivity = (AddBoasterActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor,5000);   
    assertNotNull(lAddBoasterActivity); //NOW THE ACTIVITY IS STARTED 

    mWFBoasterPreviewFragment = new WeakReference<BoasterPreviewFragment>((BoasterPreviewFragment) lAddBoasterActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_preview)); 
    mBoasterPreviewFragment = mWFBoasterPreviewFragment.get();  
    assertNotNull(mBoasterPreviewFragment); 

    lAddBoasterActivity.finish(); 
} 
相關問題