How to check for text in a ListView string that uses an adapter that extends BaseAdapter

I have an Activity that contains a ListView that I am trying to check for the existence of a specific record using Espresso. ListView data is displayed using an adapter that extends BaseAdapter.

public class CommentListAdapter extends BaseAdapter {
    private static LayoutInflater inflater = null;
    Context context;
    List<Comment> comments;

    public CommentListAdapter(Context context, List<Comment> comments) {
        this.context = context;
        this.comments = comments;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return comments.size();
    }

    @Override
    public Object getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.comment_row, null);
        }

        TextView timestamp = (TextView) view.findViewById(R.id.commentTimestamp);
        timestamp.setText(new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").format(comments.get(position).getTimeStamp()));

        TextView commentText = (TextView) view.findViewById(R.id.commentText);
        commentText.setText(comments.get(position).getComment());

        return view;
    }
}

      

The field I need to interrogate is the R.id.commentText field. Looking at the Espresso documentation I saw an example for the SimpleAdapter, but can't figure out how to refactor it to work with my CommentAdapter.

I really wonder if I should use SimpleAdapter or ArrayAdapter, however I went down the path of extending BaseAdapter because another article recommended it. This incorrectly changes my implementation, just to run the Espresso test because it is not clear how to write the test code.

Please can anyone help?

Mark

The solution I came across was to use ArrayAdapter instead of extending BaseAdapter and creating the following Matcher

public class CommentListAdapterMatchers {
    public static Matcher<Object> withContent(String expectedText) {
        return withContent(equalTo(expectedText));
    }

    public static Matcher<Object> withContent(final Matcher<String> expectedObject) {
        return new BoundedMatcher<Object, Comment>(Comment.class) {
            @Override
            public boolean matchesSafely(Comment actualObject) {
                return expectedObject.matches(actualObject.getComment());
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("with content: " + description);
            }
        };
    }
}

      

Finally, I used the following call onData p>

onData(allOf(is(Comment.class), CommentListAdapterMatchers.withContent("My Comment4"))).check(matches(isDisplayed()));

      

This worked on my test project, but then once I integrated it into a more complex layout I needed to tell the ListView I was interested in using inAdapterView

onData(allOf(is(Comment.class), CommentListAdapterMatchers.withContent("My Comment4"))).inAdapterView(withId(R.id.commentListView)).check(matches(isDisplayed()));

      

+3


source to share





All Articles