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