StreamItemAdapterTest.java revision d78ee90c0d4d93b82ad440396e87ae4f47f53e93
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.contacts.detail;
18
19import com.android.contacts.util.StreamItemEntry;
20import com.android.contacts.util.StreamItemEntryBuilder;
21import com.google.common.collect.Lists;
22
23import android.test.AndroidTestCase;
24import android.view.View;
25
26import java.util.ArrayList;
27
28// TODO: We should have tests for action, but that requires a mock sync-adapter that specifies
29// an action or doesn't
30
31/**
32 * Unit tests for {@link StreamItemAdapter}.
33 */
34public class StreamItemAdapterTest extends AndroidTestCase {
35    private StreamItemAdapter mAdapter;
36    private FakeOnClickListener mListener;
37    private View mView;
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42        mListener = new FakeOnClickListener();
43        mAdapter = new StreamItemAdapter(getContext(), mListener);
44    }
45
46    @Override
47    protected void tearDown() throws Exception {
48        mAdapter = null;
49        mListener = null;
50        super.tearDown();
51    }
52
53    public void testGetCount_Empty() {
54        mAdapter.setStreamItems(createStreamItemList(0));
55        // There is actually one view: the header.
56        assertEquals(1, mAdapter.getCount());
57    }
58
59    public void testGetCount_NonEmpty() {
60        mAdapter.setStreamItems(createStreamItemList(3));
61        // There is one extra view: the header.
62        assertEquals(4, mAdapter.getCount());
63    }
64
65    public void testGetView_Header() {
66        // Just check that we can inflate it correctly.
67        mView = mAdapter.getView(0, null, null);
68    }
69
70    /** Counter used by {@link #createStreamItemEntryBuilder()} to create unique builders. */
71    private int mCreateStreamItemEntryBuilderCounter = 0;
72
73    /** Returns a stream item builder with basic information in it. */
74    private StreamItemEntryBuilder createStreamItemEntryBuilder() {
75        return new StreamItemEntryBuilder().setText(
76                "text #" + mCreateStreamItemEntryBuilderCounter++);
77    }
78
79    /** Creates a list containing the given number of {@link StreamItemEntry}s. */
80    private ArrayList<StreamItemEntry> createStreamItemList(int count) {
81        ArrayList<StreamItemEntry> list = Lists.newArrayList();
82        for (int index = 0; index < count; ++index) {
83            list.add(createStreamItemEntryBuilder().build());
84        }
85        return list;
86    }
87
88    /** Checks that the stream item view has a click listener. */
89    private void assertStreamItemViewHasOnClickListener() {
90        assertFalse("listener should have not been invoked yet", mListener.clicked);
91        mView.performClick();
92        assertTrue("listener should have been invoked", mListener.clicked);
93    }
94
95    /** Checks that the stream item view does not have a click listener. */
96    private void assertStreamItemViewHasNoOnClickListener() {
97        assertFalse("listener should have not been invoked yet", mListener.clicked);
98        mView.performClick();
99        assertFalse("listener should have not been invoked", mListener.clicked);
100    }
101
102    /** Checks that the stream item view is clickable. */
103    private void assertStreamItemViewFocusable() {
104        assertNotNull("should have a stream item", mView);
105        assertTrue("should be focusable", mView.isFocusable());
106    }
107
108    /** Asserts that there is a stream item but it is not clickable. */
109    private void assertStreamItemViewNotFocusable() {
110        assertNotNull("should have a stream item", mView);
111        assertFalse("should not be focusable", mView.isFocusable());
112    }
113
114    /** Checks that the stream item view has the given stream item as its tag. */
115    private void assertStreamItemViewHasTag(StreamItemEntry streamItem) {
116        Object tag = mView.getTag();
117        assertNotNull("should have a tag", tag);
118        assertTrue("should be a StreamItemEntry", tag instanceof StreamItemEntry);
119        StreamItemEntry streamItemTag = (StreamItemEntry) tag;
120        // The streamItem itself should be in the tag.
121        assertSame(streamItem, streamItemTag);
122    }
123
124    /** Checks that the stream item view has the given stream item as its tag. */
125    private void assertStreamItemViewHasNoTag() {
126        Object tag = mView.getTag();
127        assertNull("should not have a tag", tag);
128    }
129
130    /**
131     * Simple fake implementation of {@link View.OnClickListener} which sets a member variable to
132     * true when clicked.
133     */
134    private final class FakeOnClickListener implements View.OnClickListener {
135        public boolean clicked = false;
136
137        @Override
138        public void onClick(View view) {
139            clicked = true;
140        }
141    }
142}
143