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