// 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線程中引用對象呢?
好吧,如果'ListView'處理點擊捕獲並告訴我哪個元素被點擊了,那麼我如何在測試中訪問這個監聽器呢?會是:'listView。preformClick()',然後點擊'ListView'的'UI'顯示的第一個元素。 – user3311776
如果你想自動化UI點擊,你應該檢查類似[Robotium](https://code.google.com/p/robotium /)。否則手動。 – m0skit0
如果我有更多的代表,我會爲你投票。謝謝您的幫助。 – user3311776