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