2014-02-14 25 views
2
// use case 10b alternate version 
// caches a read comment temporarily 
public void testCacheReadComment2() throws Throwable{ 
    runTestOnUiThread(new Runnable() 
    { 
     @Override 
     public void run(){ 
      CommentBuilder commentBuilder = new commentBuilder(); 
      Comment comment = commentBuilder.createTopTestComment(); 
      //browse button on main screen 
      ((Button)activity.findViewById(ca.ualberta.cs.team5geotopics.browseButton)).performClick(); 
      //the ListView for the custom adapter 
      ListView listView = (ListView) activity.findViewById(ca.ualberta.cs.team5geotopics.commentList); 
      //the custom adapter on the physical screen 
      ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) listView.getAdapter(); 
      adapter.add(comment); 
      adapter.notifyDataSetChanged(); 

      View view = adapter.getView(adapter.getPosition(comment), null, null); 
      ViewAsserts.assertOnScreen(listView, view); 
      //this is the button to view the Top Level comment in the list 
      ViewAsserts.assertOnScreen(view, (Button) view.viewTopLevelComment); 
      ((Button)view.viewTopLevelComment).performClick(); 

      // is there a way I can get references to the objects 
      // already instantiated in the test thread? 
      CacheController cc = activity.getCacheController(); 
      assertTrue(cc.getHistory().contains(comment)); 

     } 
    }); 
} 

我們正在使用測試驅動的開發風格來編寫我們的學校項目。在此測試中,我試圖證明用戶在適配器中查看來自列表的註釋之後,該註釋被緩存在歷史緩存中。我對某些事情有點困惑,我希望有一些幫助,因爲如果我知道我的測試案例中沒有明顯的缺陷,那將會很棒。這些是我的問題:對具有緩存和ArrayAdapter的Android應用程序進行JUnit測試

View view = adapter.getView(adapter.getPosition(comment), null, null); 

此行是否會返回與存儲在陣列適配器中的註釋相關聯的視圖?我的ArrayAdapter將遵循持有者模式,我不確定這是否是訪問我需要模仿查看評論的用戶的按鈕的正確方法。

CacheController cc = activity.getCacheController(); 

我有一個在我們的主要活動的onCreate()方法實例化一個CacheController對象。現在我想引用這個CacheController來查看歷史是否正確更新。我只是在製作新的CacheController並模仿測試方法中的CacheController的操作,但我想測試我的數據在UIthread上發生了什麼。那麼,我如何在UI線程中引用對象呢?

回答

0
View view = adapter.getView(adapter.getPosition(comment), null, null); 

將這一行返回與存儲在陣列適配器的評論 相關的看法?

我認爲它應該工作,但我不明白你爲什麼要訪問視圖。

我ArrayAdapter是要遵循一個支架圖案,我不知道,如果 這是可以訪問我需要模仿 用戶查看註釋的按鈕的正確方法。

ArrayAdapter通常用於ListView。你應該只是let ListView handle the click capturing and tell you which element was clicked

那麼,我如何在UI線程中引用對象呢?

您有這2個解決方案,我的腦海裏來,現在:

1)傳遞CacheController實例,例如:

public class YourClass { 

    private final CacheController cacheController; 

    public YourClass(final CacheController cacheController) { 
     this.cacheController = cacheController; 
    } 

    public void testCacheReadComment2() throws Throwable { 
     CacheController cc = this.cacheController; 
    } 
} 

2)單件:使CacheController靜把一個訪問器,例如:

public class CacheController { 

    private final CacheController instance = new CacheController(); 

    public static CacheController getCacheController() { 
     return instance; 
    } 
} 

在這兩種情況下,你應該知道鍋ential多線程問題,因爲你正在產生所有共享同一個CacheController實例的新線程。

+0

好吧,如果'ListView'處理點擊捕獲並告訴我哪個元素被點擊了,那麼我如何在測試中訪問這個監聽器呢?會是:'listView。preformClick()',然後點擊'ListView'的'UI'顯示的第一個元素。 – user3311776

+0

如果你想自動化UI點擊,你應該檢查類似[Robotium](https://code.google.com/p/robotium /)。否則手動。 – m0skit0

+0

如果我有更多的代表,我會爲你投票。謝謝您的幫助。 – user3311776

相關問題