1 /* 2 * Copyright (C) 2008 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 android.widget.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNull; 21 import static org.junit.Assert.fail; 22 23 import android.content.Context; 24 import android.database.Cursor; 25 import android.database.MatrixCursor; 26 import android.graphics.Bitmap; 27 import android.graphics.drawable.BitmapDrawable; 28 import android.support.test.InstrumentationRegistry; 29 import android.support.test.annotation.UiThreadTest; 30 import android.support.test.filters.SmallTest; 31 import android.support.test.runner.AndroidJUnit4; 32 import android.view.View; 33 import android.widget.ImageView; 34 import android.widget.SimpleCursorTreeAdapter; 35 import android.widget.TextView; 36 37 import com.android.compatibility.common.util.WidgetTestUtils; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 /** 44 * Test {@link SimpleCursorTreeAdapter}. 45 */ 46 @SmallTest 47 @RunWith(AndroidJUnit4.class) 48 public class SimpleCursorTreeAdapterTest { 49 private static final int GROUP_LAYOUT = R.layout.cursoradapter_group0; 50 51 private static final int CHILD_LAYOUT = R.layout.cursoradapter_item0; 52 53 private static final String[] COLUMNS_CHILD_FROM = new String[] { 54 "column2" 55 }; 56 57 private static final String[] COLUMNS_GROUP_FROM = new String[] { 58 "column1" 59 }; 60 61 private static final int[] VIEWS_GROUP_TO = new int[] { 62 R.id.cursorAdapter_group0 63 }; 64 65 private static final int[] VIEWS_CHILD_TO = new int[] { 66 R.id.cursorAdapter_item0 67 }; 68 69 private static final String SAMPLE_IMAGE_NAME = "testimage.jpg"; 70 71 private MockSimpleCursorTreeAdapter mSimpleCursorTreeAdapter; 72 73 private Context mContext; 74 75 private Cursor mGroupCursor; 76 77 private Cursor mChildCursor; 78 79 @Before 80 public void setup() { 81 mContext = InstrumentationRegistry.getTargetContext(); 82 } 83 84 @UiThreadTest 85 @Test 86 public void testConstructor() { 87 mGroupCursor = createTestCursor(2, 20, "group"); 88 new MockSimpleCursorTreeAdapter(mContext, mGroupCursor, 89 GROUP_LAYOUT, COLUMNS_GROUP_FROM, VIEWS_GROUP_TO, 90 CHILD_LAYOUT, COLUMNS_CHILD_FROM, VIEWS_CHILD_TO); 91 92 new MockSimpleCursorTreeAdapter(mContext, mGroupCursor, 93 GROUP_LAYOUT, GROUP_LAYOUT, COLUMNS_GROUP_FROM, 94 VIEWS_GROUP_TO, CHILD_LAYOUT, COLUMNS_CHILD_FROM, VIEWS_CHILD_TO); 95 96 new MockSimpleCursorTreeAdapter(mContext, mGroupCursor, 97 GROUP_LAYOUT, GROUP_LAYOUT, COLUMNS_GROUP_FROM, VIEWS_GROUP_TO, 98 CHILD_LAYOUT, CHILD_LAYOUT, COLUMNS_CHILD_FROM, VIEWS_CHILD_TO); 99 } 100 101 @UiThreadTest 102 @Test 103 public void testBindChildView() { 104 mGroupCursor = createTestCursor(2, 20, "group"); 105 mChildCursor = createTestCursor(3, 4, "child"); 106 mChildCursor.moveToFirst(); 107 mSimpleCursorTreeAdapter = new MockSimpleCursorTreeAdapter(mContext, mGroupCursor, 108 GROUP_LAYOUT, COLUMNS_GROUP_FROM, VIEWS_GROUP_TO, 109 CHILD_LAYOUT, COLUMNS_CHILD_FROM, VIEWS_CHILD_TO); 110 111 TextView view = new TextView(mContext); 112 view.setId(R.id.cursorAdapter_item0); 113 mSimpleCursorTreeAdapter.bindChildView(view, null, mChildCursor, true); 114 assertEquals("child02", view.getText().toString()); 115 116 mChildCursor.moveToNext(); 117 mSimpleCursorTreeAdapter.bindChildView(view, null, mChildCursor, false); 118 assertEquals("child12", view.getText().toString()); 119 } 120 121 // The param context and isExpanded is never read. 122 @UiThreadTest 123 @Test 124 public void testBindGroupView() { 125 mGroupCursor = createTestCursor(2, 20, "group"); 126 mGroupCursor.moveToFirst(); 127 mSimpleCursorTreeAdapter = new MockSimpleCursorTreeAdapter(mContext, mGroupCursor, 128 GROUP_LAYOUT, COLUMNS_GROUP_FROM, VIEWS_GROUP_TO, 129 CHILD_LAYOUT, COLUMNS_CHILD_FROM, VIEWS_CHILD_TO); 130 TextView view = new TextView(mContext); 131 view.setId(R.id.cursorAdapter_group0); 132 mSimpleCursorTreeAdapter.bindGroupView(view, null, mGroupCursor, true); 133 assertEquals("group01", view.getText().toString()); 134 135 mGroupCursor.moveToNext(); 136 mSimpleCursorTreeAdapter.bindGroupView(view, null, mGroupCursor, false); 137 assertEquals("group11", view.getText().toString()); 138 } 139 140 @UiThreadTest 141 @Test 142 public void testSetViewImage() { 143 mGroupCursor = createTestCursor(2, 20, "group"); 144 mSimpleCursorTreeAdapter = new MockSimpleCursorTreeAdapter(mContext, mGroupCursor, 145 GROUP_LAYOUT, COLUMNS_GROUP_FROM, VIEWS_GROUP_TO, 146 CHILD_LAYOUT, COLUMNS_CHILD_FROM, VIEWS_CHILD_TO); 147 148 // color drawable 149 ImageView view = new ImageView(mContext); 150 assertNull(view.getDrawable()); 151 mSimpleCursorTreeAdapter.setViewImage(view, 152 String.valueOf(android.widget.cts.R.drawable.scenery)); 153 BitmapDrawable d = (BitmapDrawable) mContext.getResources().getDrawable( 154 android.widget.cts.R.drawable.scenery); 155 WidgetTestUtils.assertEquals(d.getBitmap(), 156 ((BitmapDrawable) view.getDrawable()).getBitmap()); 157 158 // blank 159 view = new ImageView(mContext); 160 assertNull(view.getDrawable()); 161 mSimpleCursorTreeAdapter.setViewImage(view, ""); 162 assertNull(view.getDrawable()); 163 164 // null 165 view = new ImageView(mContext); 166 assertNull(view.getDrawable()); 167 try { 168 // Should declare NullPoinertException if the uri or value is null 169 mSimpleCursorTreeAdapter.setViewImage(view, null); 170 fail("Should throw NullPointerException if the uri or value is null"); 171 } catch (NullPointerException e) { 172 } 173 174 // uri 175 view = new ImageView(mContext); 176 assertNull(view.getDrawable()); 177 try { 178 mSimpleCursorTreeAdapter.setViewImage(view, 179 SimpleCursorAdapterTest.createTestImage(mContext, SAMPLE_IMAGE_NAME, 180 android.widget.cts.R.raw.testimage)); 181 Bitmap actualBitmap = ((BitmapDrawable) view.getDrawable()).getBitmap(); 182 Bitmap test = WidgetTestUtils.getUnscaledAndDitheredBitmap(mContext.getResources(), 183 android.widget.cts.R.raw.testimage, actualBitmap.getConfig()); 184 WidgetTestUtils.assertEquals(test, actualBitmap); 185 } finally { 186 SimpleCursorAdapterTest.destroyTestImage(mContext, SAMPLE_IMAGE_NAME); 187 } 188 } 189 190 /** 191 * Creates the test cursor. 192 * 193 * @param colCount the column count 194 * @param rowCount the row count 195 * @param prefix the prefix of each cell 196 * @return the cursor 197 */ 198 @SuppressWarnings("unchecked") 199 private Cursor createTestCursor(int colCount, int rowCount, String prefix) { 200 String[] columns = new String[colCount + 1]; 201 for (int i = 0; i < colCount; i++) { 202 columns[i] = "column" + i; 203 } 204 columns[colCount] = "_id"; 205 206 MatrixCursor cursor = new MatrixCursor(columns, rowCount); 207 Object[] row = new Object[colCount + 1]; 208 for (int i = 0; i < rowCount; i++) { 209 for (int j = 0; j < colCount; j++) { 210 row[j] = prefix + i + "" + j; 211 } 212 row[colCount] = i; 213 cursor.addRow(row); 214 } 215 return cursor; 216 } 217 218 private class MockSimpleCursorTreeAdapter extends SimpleCursorTreeAdapter { 219 public MockSimpleCursorTreeAdapter(Context context, Cursor cursor, 220 int collapsedGroupLayout, int expandedGroupLayout, String[] groupFrom, 221 int[] groupTo, int childLayout, int lastChildLayout, String[] childFrom, 222 int[] childTo) { 223 super(context, cursor, collapsedGroupLayout, expandedGroupLayout, groupFrom, groupTo, 224 childLayout, lastChildLayout, childFrom, childTo); 225 } 226 227 public MockSimpleCursorTreeAdapter(Context context, Cursor cursor, 228 int collapsedGroupLayout, int expandedGroupLayout, String[] groupFrom, 229 int[] groupTo, int childLayout, String[] childFrom, int[] childTo) { 230 super(context, cursor, collapsedGroupLayout, expandedGroupLayout, groupFrom, groupTo, 231 childLayout, childFrom, childTo); 232 } 233 234 public MockSimpleCursorTreeAdapter(Context c, Cursor cursor, int groupLayout, 235 String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, 236 int[] childTo) { 237 super(c, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); 238 } 239 240 @Override 241 protected Cursor getChildrenCursor(Cursor groupCursor) { 242 return createTestCursor(3, 4, "child"); 243 } 244 245 @Override 246 protected void bindChildView(View v, Context context, Cursor cursor, boolean isLastChild) { 247 super.bindChildView(v, context, cursor, isLastChild); 248 } 249 250 protected void bindGroupView(View v, Context context, Cursor cursor, boolean isExpanded) { 251 super.bindGroupView(v, context, cursor, isExpanded); 252 } 253 254 @Override 255 protected void setViewImage(ImageView v, String value) { 256 super.setViewImage(v, value); 257 } 258 } 259 } 260