1 /* 2 * Copyright (C) 2010 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 package com.android.contacts.widget; 17 18 import android.content.Context; 19 import android.database.DataSetObserver; 20 import android.test.AndroidTestCase; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.ArrayAdapter; 24 25 import androidx.test.filters.SmallTest; 26 27 import com.google.common.collect.Lists; 28 29 import java.util.ArrayList; 30 import java.util.HashMap; 31 import java.util.HashSet; 32 import java.util.List; 33 34 /** 35 * Tests for {@link CompositeListAdapter}. 36 */ 37 @SmallTest 38 public class CompositeListAdapterTest extends AndroidTestCase { 39 40 private final class MockAdapter extends ArrayAdapter<String> { 41 boolean allItemsEnabled = true; 42 HashSet<Integer> enabledItems = new HashSet<Integer>(); 43 int viewTypeCount = 1; 44 HashMap<Integer, Integer> viewTypes = new HashMap<Integer, Integer>(); 45 MockAdapter(Context context, List<String> objects)46 private MockAdapter(Context context, List<String> objects) { 47 super(context, android.R.layout.simple_list_item_1, objects); 48 for (int i = 0; i < objects.size(); i++) { 49 viewTypes.put(i, 0); 50 } 51 } 52 53 @Override getView(int position, View convertView, ViewGroup parent)54 public View getView(int position, View convertView, ViewGroup parent) { 55 return new MockView(getContext(), position); 56 } 57 58 @Override areAllItemsEnabled()59 public boolean areAllItemsEnabled() { 60 return allItemsEnabled; 61 } 62 63 @Override isEnabled(int position)64 public boolean isEnabled(int position) { 65 return enabledItems.contains(position); 66 } 67 68 @Override getViewTypeCount()69 public int getViewTypeCount() { 70 return viewTypeCount; 71 } 72 73 @Override getItemViewType(int position)74 public int getItemViewType(int position) { 75 return viewTypes.get(position); 76 } 77 } 78 79 private final class MockView extends View { MockView(Context context, int position)80 public MockView(Context context, int position) { 81 super(context); 82 setTag(position); 83 } 84 } 85 86 private final class TestDataSetObserver extends DataSetObserver { 87 88 public int changeCount; 89 public int invalidationCount; 90 91 @Override onChanged()92 public void onChanged() { 93 changeCount++; 94 } 95 96 @Override onInvalidated()97 public void onInvalidated() { 98 invalidationCount++; 99 } 100 } 101 102 private MockAdapter mAdapter1; 103 private MockAdapter mAdapter2; 104 private MockAdapter mAdapter3; 105 106 @Override setUp()107 protected void setUp() throws Exception { 108 super.setUp(); 109 mAdapter1 = new MockAdapter(getContext(), Lists.newArrayList("A", "B")); 110 mAdapter2 = new MockAdapter(getContext(), new ArrayList<String>()); 111 mAdapter3 = new MockAdapter(getContext(), Lists.newArrayList("C", "D", "E")); 112 } 113 testGetCount()114 public void testGetCount() { 115 CompositeListAdapter adapter = new CompositeListAdapter(); 116 adapter.addAdapter(mAdapter1); 117 adapter.addAdapter(mAdapter2); 118 adapter.addAdapter(mAdapter3); 119 120 assertEquals(5, adapter.getCount()); 121 } 122 testGetCountWithInvalidation()123 public void testGetCountWithInvalidation() { 124 CompositeListAdapter adapter = new CompositeListAdapter(); 125 assertEquals(0, adapter.getCount()); 126 127 adapter.addAdapter(mAdapter1); 128 assertEquals(2, adapter.getCount()); 129 130 adapter.addAdapter(mAdapter2); 131 assertEquals(2, adapter.getCount()); 132 133 adapter.addAdapter(mAdapter3); 134 assertEquals(5, adapter.getCount()); 135 } 136 testGetItem()137 public void testGetItem() { 138 CompositeListAdapter adapter = new CompositeListAdapter(); 139 adapter.addAdapter(mAdapter1); 140 adapter.addAdapter(mAdapter2); 141 adapter.addAdapter(mAdapter3); 142 143 assertEquals("A", adapter.getItem(0)); 144 assertEquals("B", adapter.getItem(1)); 145 assertEquals("C", adapter.getItem(2)); 146 assertEquals("D", adapter.getItem(3)); 147 assertEquals("E", adapter.getItem(4)); 148 } 149 testGetItemId()150 public void testGetItemId() { 151 CompositeListAdapter adapter = new CompositeListAdapter(); 152 adapter.addAdapter(mAdapter1); 153 adapter.addAdapter(mAdapter2); 154 adapter.addAdapter(mAdapter3); 155 156 assertEquals(0, adapter.getItemId(0)); 157 assertEquals(1, adapter.getItemId(1)); 158 assertEquals(0, adapter.getItemId(2)); 159 assertEquals(1, adapter.getItemId(3)); 160 assertEquals(2, adapter.getItemId(4)); 161 } 162 testGetView()163 public void testGetView() { 164 CompositeListAdapter adapter = new CompositeListAdapter(); 165 adapter.addAdapter(mAdapter1); 166 adapter.addAdapter(mAdapter2); 167 adapter.addAdapter(mAdapter3); 168 169 assertEquals(0, adapter.getView(0, null, null).getTag()); 170 assertEquals(1, adapter.getView(1, null, null).getTag()); 171 assertEquals(0, adapter.getView(2, null, null).getTag()); 172 assertEquals(1, adapter.getView(3, null, null).getTag()); 173 assertEquals(2, adapter.getView(4, null, null).getTag()); 174 } 175 testGetViewTypeCount()176 public void testGetViewTypeCount() { 177 mAdapter1.viewTypeCount = 2; 178 mAdapter2.viewTypeCount = 3; 179 CompositeListAdapter adapter = new CompositeListAdapter(); 180 adapter.addAdapter(mAdapter1); 181 adapter.addAdapter(mAdapter2); 182 adapter.addAdapter(mAdapter3); 183 184 // Note that mAdapter2 adds an implicit +1 185 assertEquals(6, adapter.getViewTypeCount()); 186 } 187 testGetItemViewType()188 public void testGetItemViewType() { 189 mAdapter1.viewTypeCount = 2; 190 mAdapter1.viewTypes.put(0, 1); 191 mAdapter1.viewTypes.put(1, 0); 192 193 mAdapter3.viewTypeCount = 3; 194 mAdapter3.viewTypes.put(0, 1); 195 mAdapter3.viewTypes.put(1, 2); 196 mAdapter3.viewTypes.put(2, 0); 197 198 CompositeListAdapter adapter = new CompositeListAdapter(); 199 adapter.addAdapter(mAdapter1); 200 adapter.addAdapter(mAdapter2); 201 adapter.addAdapter(mAdapter3); 202 203 assertEquals(1, adapter.getItemViewType(0)); 204 assertEquals(0, adapter.getItemViewType(1)); 205 206 // Note: mAdapter2 throws in a +1 207 208 assertEquals(4, adapter.getItemViewType(2)); 209 assertEquals(5, adapter.getItemViewType(3)); 210 assertEquals(3, adapter.getItemViewType(4)); 211 } 212 testNotifyDataSetChangedPropagated()213 public void testNotifyDataSetChangedPropagated() { 214 CompositeListAdapter adapter = new CompositeListAdapter(); 215 adapter.addAdapter(mAdapter1); 216 adapter.addAdapter(mAdapter2); 217 218 TestDataSetObserver observer = new TestDataSetObserver(); 219 adapter.registerDataSetObserver(observer); 220 mAdapter1.add("X"); 221 222 assertEquals(1, observer.changeCount); 223 assertEquals(0, observer.invalidationCount); 224 assertEquals(3, adapter.getCount()); 225 assertEquals("A", adapter.getItem(0)); 226 assertEquals("B", adapter.getItem(1)); 227 assertEquals("X", adapter.getItem(2)); 228 229 mAdapter2.add("Y"); 230 assertEquals(2, observer.changeCount); 231 assertEquals(0, observer.invalidationCount); 232 assertEquals(4, adapter.getCount()); 233 assertEquals("A", adapter.getItem(0)); 234 assertEquals("B", adapter.getItem(1)); 235 assertEquals("X", adapter.getItem(2)); 236 assertEquals("Y", adapter.getItem(3)); 237 238 } 239 testNotifyDataSetChangedOnAddingAdapter()240 public void testNotifyDataSetChangedOnAddingAdapter() { 241 CompositeListAdapter adapter = new CompositeListAdapter(); 242 adapter.addAdapter(mAdapter1); 243 244 TestDataSetObserver observer = new TestDataSetObserver(); 245 adapter.registerDataSetObserver(observer); 246 adapter.addAdapter(mAdapter3); 247 248 assertEquals(1, observer.changeCount); 249 assertEquals(0, observer.invalidationCount); 250 assertEquals(5, adapter.getCount()); 251 assertEquals("A", adapter.getItem(0)); 252 assertEquals("B", adapter.getItem(1)); 253 assertEquals("C", adapter.getItem(2)); 254 assertEquals("D", adapter.getItem(3)); 255 assertEquals("E", adapter.getItem(4)); 256 } 257 testNotifyDataSetInvalidated()258 public void testNotifyDataSetInvalidated() { 259 CompositeListAdapter adapter = new CompositeListAdapter(); 260 adapter.addAdapter(mAdapter1); 261 262 TestDataSetObserver observer = new TestDataSetObserver(); 263 adapter.registerDataSetObserver(observer); 264 265 mAdapter1.remove("A"); 266 assertEquals(1, observer.changeCount); 267 assertEquals(0, observer.invalidationCount); 268 assertEquals(1, adapter.getCount()); 269 270 mAdapter1.remove("B"); 271 assertEquals(1, observer.changeCount); 272 assertEquals(1, observer.invalidationCount); 273 assertEquals(0, adapter.getCount()); 274 } 275 testAreAllItemsEnabled()276 public void testAreAllItemsEnabled() { 277 CompositeListAdapter adapter = new CompositeListAdapter(); 278 adapter.addAdapter(mAdapter1); 279 adapter.addAdapter(mAdapter3); 280 281 assertTrue(adapter.areAllItemsEnabled()); 282 } 283 testAreAllItemsEnabledWithInvalidation()284 public void testAreAllItemsEnabledWithInvalidation() { 285 CompositeListAdapter adapter = new CompositeListAdapter(); 286 adapter.addAdapter(mAdapter1); 287 assertTrue(adapter.areAllItemsEnabled()); 288 289 mAdapter3.allItemsEnabled = false; 290 adapter.addAdapter(mAdapter3); 291 292 assertFalse(adapter.areAllItemsEnabled()); 293 } 294 testIsEnabled()295 public void testIsEnabled() { 296 mAdapter1.allItemsEnabled = false; 297 mAdapter1.enabledItems.add(1); 298 299 CompositeListAdapter adapter = new CompositeListAdapter(); 300 adapter.addAdapter(mAdapter1); 301 adapter.addAdapter(mAdapter2); 302 adapter.addAdapter(mAdapter3); 303 304 assertFalse(adapter.isEnabled(0)); 305 assertTrue(adapter.isEnabled(1)); 306 assertTrue(adapter.isEnabled(2)); 307 assertTrue(adapter.isEnabled(3)); 308 assertTrue(adapter.isEnabled(4)); 309 } 310 testIsEnabledWhenAllEnabledAtLeastOneAdapter()311 public void testIsEnabledWhenAllEnabledAtLeastOneAdapter() { 312 mAdapter1.allItemsEnabled = false; 313 mAdapter1.enabledItems.add(1); 314 mAdapter3.allItemsEnabled = false; 315 mAdapter3.enabledItems.add(1); 316 317 CompositeListAdapter adapter = new CompositeListAdapter(); 318 adapter.addAdapter(mAdapter1); 319 adapter.addAdapter(mAdapter3); 320 321 assertFalse(adapter.isEnabled(0)); 322 assertTrue(adapter.isEnabled(1)); 323 assertFalse(adapter.isEnabled(2)); 324 assertTrue(adapter.isEnabled(3)); 325 assertFalse(adapter.isEnabled(4)); 326 } 327 } 328