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