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