【问题标题】:JUnit test on android app with cache and ArrayAdapter使用缓存和 ArrayAdapter 对 Android 应用进行 JUnit 测试
【发布时间】:2014-02-14 22:47:41
【问题描述】:
// 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));

        }
    });
}

我们正在使用测试驱动的开发风格来为我们的学校项目编写代码。在这个测试中,我试图证明在用户查看适配器列表中的评论后,该评论被缓存在历史缓存中。我对某些事情有点困惑,我需要一些帮助,因为如果我知道我的测试用例中没有明显的缺陷,那就太好了。这些是我的问题:

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

此行是否会返回与存储在数组适配器中的注释关联的视图?我的 ArrayAdapter 将遵循持有者模式,我不确定这是否是访问我需要模仿用户查看评论的按钮的正确方法。

CacheController cc = activity.getCacheController();

我有一个CacheController 对象,它在我们的主要活动中的onCreate() 方法上实例化。现在我想引用这个CacheController 来查看历史记录是否正确更新。我只是制作了一个新的CacheController 并在测试方法中模仿CacheController 的动作,但我想测试我在UIthread 上的数据会发生什么。那么,如何在 UI 线程中引用对象呢?

【问题讨论】:

    标签: java android junit android-arrayadapter ui-thread


    【解决方案1】:
    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)Singleton:将CacheController设为静态并放置一个访问器,例如:

    public class CacheController {
    
        private final CacheController instance = new CacheController();
    
        public static CacheController getCacheController() {
            return instance;
        }
    }
    

    在这两种情况下,您都应该注意潜在的多线程问题,因为您正在生成共享相同 CacheController 实例的新线程。

    【讨论】:

    • 好的,如果ListView 处理点击捕获并告诉我点击了哪个元素,那么我如何在测试中访问这个监听器?会是:listView.preformClick(),这将单击UI 显示上的第一个元素以显示ListView
    • 如果你想自动化用户界面点击,你应该检查类似Robotium的东西。否则手动执行。
    • 如果我有更多的代表,我会投票给你。感谢您的帮助。
    • 将问题标记为已回答 ;) 祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多