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