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 */
16package com.android.common.widget;
17
18import android.content.Context;
19import android.database.Cursor;
20import android.database.MatrixCursor;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.view.View;
24import android.view.ViewGroup;
25
26/**
27 * Tests for {@link CompositeCursorAdapter}.
28 *
29 * To execute, run:
30 * <pre>
31 *   adb shell am instrument -e class com.android.common.widget.CompositeCursorAdapterTest \
32 *      -w com.android.common.tests/android.test.InstrumentationTestRunner
33 * </pre>
34 */
35@SmallTest
36public class CompositeCursorAdapterTest extends AndroidTestCase {
37
38    public class TestCompositeCursorAdapter extends CompositeCursorAdapter {
39
40        public TestCompositeCursorAdapter() {
41            super(CompositeCursorAdapterTest.this.getContext());
42        }
43
44        private StringBuilder mRequests = new StringBuilder();
45
46        @Override
47        protected View newHeaderView(Context context, int partition, Cursor cursor, ViewGroup parent) {
48            return new View(context);
49        }
50
51        @Override
52        protected void bindHeaderView(View view, int partition, Cursor cursor) {
53            mRequests.append(partition + (cursor == null ? "" : cursor.getColumnNames()[0])
54                    + "[H] ");
55        }
56
57        @Override
58        protected View newView(Context context, int sectionIndex, Cursor cursor, int position,
59                ViewGroup parent) {
60            return new View(context);
61        }
62
63        @Override
64        protected void bindView(View v, int partition, Cursor cursor, int position) {
65            if (!cursor.moveToPosition(position)) {
66                fail("Invalid position:" + partition + " " + cursor.getColumnNames()[0] + " "
67                        + position);
68            }
69
70            mRequests.append(partition + cursor.getColumnNames()[0] + "["
71                    + cursor.getInt(0) + "] ");
72        }
73
74        @Override
75        public String toString() {
76            return mRequests.toString().trim();
77        }
78    }
79
80    public void testGetCountNoEmptySections() {
81        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
82        adapter.addPartition(false, false);
83        adapter.addPartition(false, false);
84
85        adapter.changeCursor(0, makeCursor("a", 2));
86        adapter.changeCursor(1, makeCursor("b", 3));
87
88        assertEquals(5, adapter.getCount());
89    }
90
91    public void testGetViewNoEmptySections() {
92        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
93        adapter.addPartition(false, false);
94        adapter.addPartition(false, false);
95
96        adapter.changeCursor(0, makeCursor("a", 1));
97        adapter.changeCursor(1, makeCursor("b", 2));
98
99        for (int i = 0; i < adapter.getCount(); i++) {
100            adapter.getView(i, null, null);
101        }
102
103        assertEquals("0a[0] 1b[0] 1b[1]", adapter.toString());
104    }
105
106    public void testGetCountWithHeadersAndNoEmptySections() {
107        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
108        adapter.addPartition(false, true);
109        adapter.addPartition(false, true);
110
111        adapter.changeCursor(0, makeCursor("a", 2));
112        adapter.changeCursor(1, makeCursor("b", 3));
113
114        assertEquals(7, adapter.getCount());
115    }
116
117    public void testGetViewWithHeadersNoEmptySections() {
118        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
119        adapter.addPartition(false, true);
120        adapter.addPartition(false, true);
121
122        adapter.changeCursor(0, makeCursor("a", 1));
123        adapter.changeCursor(1, makeCursor("b", 2));
124
125        for (int i = 0; i < adapter.getCount(); i++) {
126            adapter.getView(i, null, null);
127        }
128
129        assertEquals("0a[H] 0a[0] 1b[H] 1b[0] 1b[1]", adapter.toString());
130    }
131
132    public void testGetCountWithHiddenEmptySection() {
133        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
134        adapter.addPartition(false, true);
135        adapter.addPartition(false, true);
136
137        adapter.changeCursor(1, makeCursor("a", 2));
138
139        assertEquals(3, adapter.getCount());
140    }
141
142    public void testGetPartitionForPosition() {
143        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
144        adapter.addPartition(true, false);
145        adapter.addPartition(true, true);
146
147        adapter.changeCursor(0, makeCursor("a", 1));
148        adapter.changeCursor(1, makeCursor("b", 2));
149
150        assertEquals(0, adapter.getPartitionForPosition(0));
151        assertEquals(1, adapter.getPartitionForPosition(1));
152        assertEquals(1, adapter.getPartitionForPosition(2));
153        assertEquals(1, adapter.getPartitionForPosition(3));
154    }
155
156    public void testGetOffsetForPosition() {
157        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
158        adapter.addPartition(true, false);
159        adapter.addPartition(true, true);
160
161        adapter.changeCursor(0, makeCursor("a", 1));
162        adapter.changeCursor(1, makeCursor("b", 2));
163
164        assertEquals(0, adapter.getOffsetInPartition(0));
165        assertEquals(-1, adapter.getOffsetInPartition(1));
166        assertEquals(0, adapter.getOffsetInPartition(2));
167        assertEquals(1, adapter.getOffsetInPartition(3));
168    }
169
170    public void testGetPositionForPartition() {
171        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
172        adapter.addPartition(true, true);
173        adapter.addPartition(true, true);
174
175        adapter.changeCursor(0, makeCursor("a", 1));
176        adapter.changeCursor(1, makeCursor("b", 2));
177
178        assertEquals(0, adapter.getPositionForPartition(0));
179        assertEquals(2, adapter.getPositionForPartition(1));
180    }
181
182    public void testGetViewWithHiddenEmptySections() {
183        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
184        adapter.addPartition(false, false);
185        adapter.addPartition(false, false);
186
187        adapter.changeCursor(1, makeCursor("b", 2));
188
189        for (int i = 0; i < adapter.getCount(); i++) {
190            adapter.getView(i, null, null);
191        }
192
193        assertEquals("1b[0] 1b[1]", adapter.toString());
194    }
195
196    public void testGetCountWithShownEmptySection() {
197        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
198        adapter.addPartition(true, true);
199        adapter.addPartition(true, true);
200
201        adapter.changeCursor(1, makeCursor("a", 2));
202
203        assertEquals(4, adapter.getCount());
204    }
205
206    public void testGetViewWithShownEmptySections() {
207        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
208        adapter.addPartition(true, true);
209        adapter.addPartition(true, true);
210
211        adapter.changeCursor(1, makeCursor("b", 2));
212
213        for (int i = 0; i < adapter.getCount(); i++) {
214            adapter.getView(i, null, null);
215        }
216
217        assertEquals("0[H] 1b[H] 1b[0] 1b[1]", adapter.toString());
218    }
219
220    public void testAreAllItemsEnabledFalse() {
221        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
222        adapter.addPartition(true, false);
223        adapter.addPartition(true, true);
224
225        assertFalse(adapter.areAllItemsEnabled());
226    }
227
228    public void testAreAllItemsEnabledTrue() {
229        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
230        adapter.addPartition(true, false);
231        adapter.addPartition(true, false);
232
233        assertTrue(adapter.areAllItemsEnabled());
234    }
235
236    public void testIsEnabled() {
237        TestCompositeCursorAdapter adapter = new TestCompositeCursorAdapter();
238        adapter.addPartition(true, false);
239        adapter.addPartition(true, true);
240
241        adapter.changeCursor(0, makeCursor("a", 1));
242        adapter.changeCursor(1, makeCursor("b", 2));
243
244        assertTrue(adapter.isEnabled(0));
245        assertFalse(adapter.isEnabled(1));
246        assertTrue(adapter.isEnabled(2));
247        assertTrue(adapter.isEnabled(3));
248    }
249
250    private Cursor makeCursor(String name, int count) {
251        MatrixCursor cursor = new MatrixCursor(new String[]{name});
252        for (int i = 0; i < count; i++) {
253            cursor.addRow(new Object[]{i});
254        }
255        return cursor;
256    }
257}
258