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