Home | History | Annotate | Download | only in cts
      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 
     22 import android.content.Context;
     23 import android.database.Cursor;
     24 import android.database.MatrixCursor;
     25 import android.support.test.InstrumentationRegistry;
     26 import android.support.test.annotation.UiThreadTest;
     27 import android.support.test.filters.MediumTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.ViewGroup;
     32 import android.widget.ResourceCursorTreeAdapter;
     33 
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 
     38 /**
     39  * Test {@link ResourceCursorTreeAdapter}.
     40  */
     41 @MediumTest
     42 @RunWith(AndroidJUnit4.class)
     43 public class ResourceCursorTreeAdapterTest {
     44     private ResourceCursorTreeAdapter mResourceCursorTreeAdapter;
     45 
     46     private Context mContext;
     47 
     48     private ViewGroup mParent;
     49 
     50     private int mCollapsedGroupLayout = R.layout.cursoradapter_group0;
     51 
     52     private int mGroupLayout = mCollapsedGroupLayout;
     53 
     54     private int mExpandedGroupLayout = R.layout.cursoradapter_group1;
     55 
     56     private int mNormalChildLayout = R.layout.cursoradapter_item0;
     57 
     58     private int mChildLayout = mNormalChildLayout;
     59 
     60     private int mLastChildLayout = R.layout.cursoradapter_item1;
     61 
     62     private int mCollapsedGroupId = R.id.cursorAdapter_group0;
     63 
     64     private int mGroupId = mCollapsedGroupId;
     65 
     66     private int mExpandedGroupId = R.id.cursorAdapter_group1;
     67 
     68     private int mNormalChildId = R.id.cursorAdapter_item0;
     69 
     70     private int mChildId = mNormalChildId;
     71 
     72     private int mLastChildId = R.id.cursorAdapter_item1;
     73 
     74     @Before
     75     public void setup() {
     76         mContext = InstrumentationRegistry.getTargetContext();
     77         LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(
     78                 Context.LAYOUT_INFLATER_SERVICE);
     79         mParent = (ViewGroup) layoutInflater.inflate(R.layout.cursoradapter_host, null);
     80     }
     81 
     82     @UiThreadTest
     83     @Test
     84     public void testConstructor() {
     85         mResourceCursorTreeAdapter = new MockResourceCursorTreeAdapter(mContext, null,
     86                 mGroupLayout, mChildLayout);
     87         assertNull(mResourceCursorTreeAdapter.getCursor());
     88 
     89         Cursor cursor = createTestCursor(3, 3);
     90         mResourceCursorTreeAdapter = new MockResourceCursorTreeAdapter(mContext, cursor,
     91                 mGroupLayout, mChildLayout);
     92         assertEquals(cursor, mResourceCursorTreeAdapter.getCursor());
     93 
     94         new MockResourceCursorTreeAdapter(mContext, null, mCollapsedGroupLayout,
     95                 mExpandedGroupLayout, mChildLayout);
     96 
     97         new MockResourceCursorTreeAdapter(mContext, null, mCollapsedGroupLayout,
     98                 mExpandedGroupLayout, mNormalChildLayout, mLastChildLayout);
     99 
    100         new MockResourceCursorTreeAdapter(mContext, null, -1, -1);
    101 
    102         new MockResourceCursorTreeAdapter(mContext, null, -1, -1, -1);
    103 
    104         new MockResourceCursorTreeAdapter(mContext, null, -1, -1, -1, -1);
    105     }
    106 
    107     // The parameters Context and Cursor are never read in the method
    108     @UiThreadTest
    109     @Test
    110     public void testNewChildView() {
    111         mResourceCursorTreeAdapter = new MockResourceCursorTreeAdapter(mContext, null,
    112                 mGroupLayout, mChildLayout);
    113 
    114         View result = mResourceCursorTreeAdapter.newChildView(null, null, true, mParent);
    115         assertEquals(mChildId, result.getId());
    116 
    117         result = mResourceCursorTreeAdapter.newChildView(null, null, false, mParent);
    118         assertEquals(mChildId, result.getId());
    119 
    120         mResourceCursorTreeAdapter = new MockResourceCursorTreeAdapter(mContext, null,
    121                 mGroupLayout, mGroupLayout, mNormalChildLayout, mLastChildLayout);
    122 
    123         result = mResourceCursorTreeAdapter.newChildView(null, null, true, mParent);
    124         assertEquals(mLastChildId, result.getId());
    125 
    126         result = mResourceCursorTreeAdapter.newChildView(null, null, false, mParent);
    127         assertEquals(mNormalChildId, result.getId());
    128     }
    129 
    130     // The parameters Context and Cursor are never read in the method
    131     @UiThreadTest
    132     @Test
    133     public void testNewGroupView() {
    134         mResourceCursorTreeAdapter = new MockResourceCursorTreeAdapter(mContext, null,
    135                 mGroupLayout, mChildLayout);
    136 
    137         View result = mResourceCursorTreeAdapter.newGroupView(null, null, true, mParent);
    138         assertEquals(mGroupId, result.getId());
    139 
    140         result = mResourceCursorTreeAdapter.newGroupView(null, null, false, mParent);
    141         assertEquals(mGroupId, result.getId());
    142 
    143         mResourceCursorTreeAdapter = new MockResourceCursorTreeAdapter(mContext, null,
    144                 mCollapsedGroupLayout, mExpandedGroupLayout, mChildLayout);
    145 
    146         result = mResourceCursorTreeAdapter.newGroupView(null, null, true, mParent);
    147         assertEquals(mExpandedGroupId, result.getId());
    148 
    149         result = mResourceCursorTreeAdapter.newGroupView(null, null, false, mParent);
    150         assertEquals(mCollapsedGroupId, result.getId());
    151     }
    152 
    153     /**
    154      * Creates the test cursor.
    155      *
    156      * @param colCount the column count
    157      * @param rowCount the row count
    158      * @return the cursor
    159      */
    160     @SuppressWarnings("unchecked")
    161     private Cursor createTestCursor(int colCount, int rowCount) {
    162         String[] columns = new String[colCount + 1];
    163         for (int i = 0; i < colCount; i++) {
    164             columns[i] = "column" + i;
    165         }
    166         columns[colCount] = "_id";
    167 
    168         MatrixCursor cursor = new MatrixCursor(columns, rowCount);
    169         Object[] row = new Object[colCount + 1];
    170         for (int i = 0; i < rowCount; i++) {
    171             for (int j = 0; j < colCount; j++) {
    172                 row[j] = "" + rowCount + "" + colCount;
    173             }
    174             row[colCount] = i;
    175             cursor.addRow(row);
    176         }
    177         return cursor;
    178     }
    179 
    180     private class MockResourceCursorTreeAdapter extends ResourceCursorTreeAdapter {
    181         public MockResourceCursorTreeAdapter(Context context, Cursor cursor,
    182                 int collapsedGroupLayout, int expandedGroupLayout, int childLayout,
    183                 int lastChildLayout) {
    184             super(context, cursor, collapsedGroupLayout, expandedGroupLayout, childLayout,
    185                     lastChildLayout);
    186         }
    187 
    188         public MockResourceCursorTreeAdapter(Context context, Cursor cursor,
    189                 int collapsedGroupLayout, int expandedGroupLayout, int childLayout) {
    190             super(context, cursor, collapsedGroupLayout, expandedGroupLayout, childLayout);
    191         }
    192 
    193         public MockResourceCursorTreeAdapter(Context context, Cursor cursor, int groupLayout,
    194                 int childLayout) {
    195             super(context, cursor, groupLayout, childLayout);
    196         }
    197 
    198         @Override
    199         protected void bindChildView(View view, Context context, Cursor cursor,
    200                 boolean isLastChild) {
    201         }
    202 
    203         @Override
    204         protected void bindGroupView(View view, Context context, Cursor cursor,
    205                 boolean isExpanded) {
    206         }
    207 
    208         @Override
    209         protected Cursor getChildrenCursor(Cursor groupCursor) {
    210             return null;
    211         }
    212     }
    213 }
    214