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));

        }
    });
}

We are using a test driven development style in order to code our project for school. In this test I am trying to prove that after a user views a comment from the list in the adapter, that this comment is cached in a history cache. I'm a little confused about some things and I would like some help, because it would be great if I knew there were no obvious flaws in my test case. these are my questions:

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

Will this line return the view that is associated with the comment stored in the array adapter? My ArrayAdapter is going to follow a holder patter and I'm not sure if this is the proper way to get access to the buttons I need to mimic the user viewing the comment.

CacheController cc = activity.getCacheController();

I have a CacheController object that is instantiated upon the onCreate() method in our main activity. Now I want to reference this CacheController to see if the history is updated properly. I was just making a new CacheController and mimicking the actions of the CacheController in the test method, but I want to test what happens to my data on the UIthread. So, how do I reference objects in the UI thread?

Joseph Boyle
  • 560
  • 5
  • 15

1 Answers1

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

Will this line return the view that is associated with the comment stored in the array adapter?

I think it should work, but I don't understand why would you want to access the View.

My ArrayAdapter is going to follow a holder patter and I'm not sure if this is the proper way to get access to the buttons I need to mimic the user viewing the comment.

The ArrayAdapter is usually used for a ListView. You should just let ListView handle the click capturing and tell you which element was clicked.

So, how do I reference objects in the UI thread?

You have 2 solutions for this that come to my mind right now:

1) Pass the CacheController instance, for example:

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: make the CacheController static and put an accessor, for example:

public class CacheController {

    private final CacheController instance = new CacheController();

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

In both cases you should be aware about potential multi-threading issues because you're spawning new threads that all share same CacheController instance.

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • Ok, if the `ListView` handles the click capturing and tell me which element was clicked, then how do I access this listener in the test? Would it be: `listView.preformClick()`, and that would click the first element on the `UI` display for the `ListView` – user3311776 Feb 14 '14 at 23:37
  • If you want to automate UI clicks you should check for something like [Robotium](https://code.google.com/p/robotium/). Otherwise do manually. – m0skit0 Feb 14 '14 at 23:43
  • I'd vote you up if I had more rep. Thanks for the help. – user3311776 Feb 14 '14 at 23:52
  • Mark the question as anwered ;) Good luck! – m0skit0 Feb 15 '14 at 00:27