StreamItemAdapterTest.java revision d78ee90c0d4d93b82ad440396e87ae4f47f53e93
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.test.AndroidTestCase;
24a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerdaimport android.view.View;
25a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
26a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerdaimport java.util.ArrayList;
27a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
28d78ee90c0d4d93b82ad440396e87ae4f47f53e93Daniel Lehmann// TODO: We should have tests for action, but that requires a mock sync-adapter that specifies
29d78ee90c0d4d93b82ad440396e87ae4f47f53e93Daniel Lehmann// an action or doesn't
30d78ee90c0d4d93b82ad440396e87ae4f47f53e93Daniel Lehmann
31a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda/**
32a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda * Unit tests for {@link StreamItemAdapter}.
33a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda */
34a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerdapublic class StreamItemAdapterTest extends AndroidTestCase {
35a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private StreamItemAdapter mAdapter;
36a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private FakeOnClickListener mListener;
37a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private View mView;
38a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
39a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    @Override
40a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    protected void setUp() throws Exception {
41a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        super.setUp();
42a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mListener = new FakeOnClickListener();
43a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mAdapter = new StreamItemAdapter(getContext(), mListener);
44a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
45a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
46a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    @Override
47a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    protected void tearDown() throws Exception {
48a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mAdapter = null;
49a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mListener = null;
50a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        super.tearDown();
51a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
52a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
53a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    public void testGetCount_Empty() {
54a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mAdapter.setStreamItems(createStreamItemList(0));
55a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        // There is actually one view: the header.
56a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertEquals(1, mAdapter.getCount());
57a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
58a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
59a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    public void testGetCount_NonEmpty() {
60a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mAdapter.setStreamItems(createStreamItemList(3));
61a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        // There is one extra view: the header.
62a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertEquals(4, mAdapter.getCount());
63a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
64a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
65a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    public void testGetView_Header() {
66a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        // Just check that we can inflate it correctly.
67a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mView = mAdapter.getView(0, null, null);
68a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
69a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
70a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Counter used by {@link #createStreamItemEntryBuilder()} to create unique builders. */
71a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private int mCreateStreamItemEntryBuilderCounter = 0;
72a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
73a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Returns a stream item builder with basic information in it. */
74a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private StreamItemEntryBuilder createStreamItemEntryBuilder() {
75a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        return new StreamItemEntryBuilder().setText(
76a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda                "text #" + mCreateStreamItemEntryBuilderCounter++);
77a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
78a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
79a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Creates a list containing the given number of {@link StreamItemEntry}s. */
80a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private ArrayList<StreamItemEntry> createStreamItemList(int count) {
81a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        ArrayList<StreamItemEntry> list = Lists.newArrayList();
82a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        for (int index = 0; index < count; ++index) {
83a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda            list.add(createStreamItemEntryBuilder().build());
84a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        }
85a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        return list;
86a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
87a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
88a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Checks that the stream item view has a click listener. */
89a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private void assertStreamItemViewHasOnClickListener() {
90a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertFalse("listener should have not been invoked yet", mListener.clicked);
91a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mView.performClick();
92a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertTrue("listener should have been invoked", mListener.clicked);
93a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
94a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
95a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Checks that the stream item view does not have a click listener. */
96a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private void assertStreamItemViewHasNoOnClickListener() {
97a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertFalse("listener should have not been invoked yet", mListener.clicked);
98a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        mView.performClick();
99a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertFalse("listener should have not been invoked", mListener.clicked);
100a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
101a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
102a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Checks that the stream item view is clickable. */
103a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private void assertStreamItemViewFocusable() {
104a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertNotNull("should have a stream item", mView);
105a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertTrue("should be focusable", mView.isFocusable());
106a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
107a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
108a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Asserts that there is a stream item but it is not clickable. */
109a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private void assertStreamItemViewNotFocusable() {
110a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertNotNull("should have a stream item", mView);
111a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertFalse("should not be focusable", mView.isFocusable());
112a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
113a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
114a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Checks that the stream item view has the given stream item as its tag. */
115a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private void assertStreamItemViewHasTag(StreamItemEntry streamItem) {
116a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        Object tag = mView.getTag();
117a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertNotNull("should have a tag", tag);
118a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertTrue("should be a StreamItemEntry", tag instanceof StreamItemEntry);
119a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        StreamItemEntry streamItemTag = (StreamItemEntry) tag;
120a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        // The streamItem itself should be in the tag.
121a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertSame(streamItem, streamItemTag);
122a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
123a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
124a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /** Checks that the stream item view has the given stream item as its tag. */
125a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private void assertStreamItemViewHasNoTag() {
126a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        Object tag = mView.getTag();
127a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        assertNull("should not have a tag", tag);
128a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
129a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
130a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    /**
131a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda     * Simple fake implementation of {@link View.OnClickListener} which sets a member variable to
132a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda     * true when clicked.
133a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda     */
134a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    private final class FakeOnClickListener implements View.OnClickListener {
135a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        public boolean clicked = false;
136a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda
137a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        @Override
138a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        public void onClick(View view) {
139a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda            clicked = true;
140a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda        }
141a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda    }
142a21993720988a5fc0b48594d1ff0ce6f932780b7Flavio Lerda}
143