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