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