FileDocCategorySizeDatePackage
GroupingListAdapterTests.javaAPI DocAndroid 5.1 API12357Thu Mar 12 22:22:48 GMT 2015com.android.common.widget

GroupingListAdapterTests

public class GroupingListAdapterTests extends android.test.AndroidTestCase
Tests for {@link GroupingListAdapter}. Running all tests: adb shell am instrument -e class com.android.common.widget.GroupingListAdapterTests \ -w com.android.common.tests/android.test.InstrumentationTestRunner

Fields Summary
private static final String[]
PROJECTION
private static final int
GROUPING_COLUMN_INDEX
private android.database.MatrixCursor
mCursor
private long
mNextId
private GroupingListAdapter
mAdapter
Constructors Summary
Methods Summary
private voidassertPositionMetadata(int position, int itemType, boolean isExpanded, int cursorPosition)

        GroupingListAdapter.PositionMetadata metadata = new GroupingListAdapter.PositionMetadata();
        mAdapter.obtainPositionMetadata(metadata, position);
        assertEquals(itemType, metadata.itemType);
        if (metadata.itemType == ITEM_TYPE_GROUP_HEADER) {
            assertEquals(isExpanded, metadata.isExpanded);
        }
        assertEquals(cursorPosition, metadata.cursorPosition);
    
private voidbuildCursor(java.lang.String numbers)


        
        mCursor = new MatrixCursor(PROJECTION);
        mNextId = 1;
        for (String number : numbers) {
            mCursor.addRow(new Object[]{mNextId, number});
            mNextId++;
        }
    
public voidtestGroupDescriptorArrayGrowth()

        String[] numbers = new String[500];
        for (int i = 0; i < numbers.length; i++) {

            // Make groups of 2
            numbers[i] = String.valueOf((i / 2) * 2);
        }

        buildCursor(numbers);
        mAdapter.changeCursor(mCursor);

        assertEquals(250, mAdapter.getCount());
    
public voidtestGroupingWithCollapsedGroupAtTheBeginning()

        buildCursor("1", "1", "2");
        mAdapter.changeCursor(mCursor);

        assertEquals(2, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_GROUP_HEADER, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_STANDALONE, false, 2);
    
public voidtestGroupingWithCollapsedGroupAtTheEnd()

        buildCursor("1", "2", "3", "3", "3");
        mAdapter.changeCursor(mCursor);

        assertEquals(3, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_STANDALONE, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_GROUP_HEADER, false, 2);
    
public voidtestGroupingWithCollapsedGroupInTheMiddle()

        buildCursor("1", "2", "2", "2", "3");
        mAdapter.changeCursor(mCursor);

        assertEquals(3, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_STANDALONE, false, 4);
    
public voidtestGroupingWithExpandCollapseCycleAtTheBeginning()

        buildCursor("1", "1", "2");
        mAdapter.changeCursor(mCursor);
        mAdapter.toggleGroup(0);
        mAdapter.toggleGroup(0);

        assertEquals(2, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_GROUP_HEADER, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_STANDALONE, false, 2);
    
public voidtestGroupingWithExpandedGroupAtTheBeginning()

        buildCursor("1", "1", "2");
        mAdapter.changeCursor(mCursor);
        mAdapter.toggleGroup(0);

        assertEquals(4, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_GROUP_HEADER, true, 0);
        assertPositionMetadata(1, ITEM_TYPE_IN_GROUP, false, 0);
        assertPositionMetadata(2, ITEM_TYPE_IN_GROUP, false, 1);
        assertPositionMetadata(3, ITEM_TYPE_STANDALONE, false, 2);
    
public voidtestGroupingWithExpandedGroupAtTheEnd()

        buildCursor("1", "2", "3", "3", "3");
        mAdapter.changeCursor(mCursor);
        mAdapter.toggleGroup(2);

        assertEquals(6, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_STANDALONE, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_GROUP_HEADER, true, 2);
        assertPositionMetadata(3, ITEM_TYPE_IN_GROUP, false, 2);
        assertPositionMetadata(4, ITEM_TYPE_IN_GROUP, false, 3);
        assertPositionMetadata(5, ITEM_TYPE_IN_GROUP, false, 4);
    
public voidtestGroupingWithExpandedGroupInTheMiddle()

        buildCursor("1", "2", "2", "2", "3");
        mAdapter.changeCursor(mCursor);
        mAdapter.toggleGroup(1);

        assertEquals(6, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, true, 1);
        assertPositionMetadata(2, ITEM_TYPE_IN_GROUP, false, 1);
        assertPositionMetadata(3, ITEM_TYPE_IN_GROUP, false, 2);
        assertPositionMetadata(4, ITEM_TYPE_IN_GROUP, false, 3);
        assertPositionMetadata(5, ITEM_TYPE_STANDALONE, false, 4);
    
public voidtestGroupingWithMultipleCollapsedGroups()

        buildCursor("1", "2", "2", "3", "4", "4", "5", "5", "6");
        mAdapter.changeCursor(mCursor);

        assertEquals(6, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_STANDALONE, false, 3);
        assertPositionMetadata(3, ITEM_TYPE_GROUP_HEADER, false, 4);
        assertPositionMetadata(4, ITEM_TYPE_GROUP_HEADER, false, 6);
        assertPositionMetadata(5, ITEM_TYPE_STANDALONE, false, 8);
    
public voidtestGroupingWithMultipleExpandedGroups()

        buildCursor("1", "2", "2", "3", "4", "4", "5", "5", "6");
        mAdapter.changeCursor(mCursor);
        mAdapter.toggleGroup(1);

        // Note that expanding the group of 2's shifted the group of 5's down from the
        // 4th to the 6th position
        mAdapter.toggleGroup(6);

        assertEquals(10, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, true, 1);
        assertPositionMetadata(2, ITEM_TYPE_IN_GROUP, false, 1);
        assertPositionMetadata(3, ITEM_TYPE_IN_GROUP, false, 2);
        assertPositionMetadata(4, ITEM_TYPE_STANDALONE, false, 3);
        assertPositionMetadata(5, ITEM_TYPE_GROUP_HEADER, false, 4);
        assertPositionMetadata(6, ITEM_TYPE_GROUP_HEADER, true, 6);
        assertPositionMetadata(7, ITEM_TYPE_IN_GROUP, false, 6);
        assertPositionMetadata(8, ITEM_TYPE_IN_GROUP, false, 7);
        assertPositionMetadata(9, ITEM_TYPE_STANDALONE, false, 8);
    
public voidtestGroupingWithoutGroups()

        buildCursor("1", "2", "3");
        mAdapter.changeCursor(mCursor);

        assertEquals(3, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_STANDALONE, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_STANDALONE, false, 2);
    
public voidtestPositionCache()

        buildCursor("1", "2", "2", "3", "4", "4", "5", "5", "6");
        mAdapter.changeCursor(mCursor);

        // First pass - building up cache
        assertEquals(6, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_STANDALONE, false, 3);
        assertPositionMetadata(3, ITEM_TYPE_GROUP_HEADER, false, 4);
        assertPositionMetadata(4, ITEM_TYPE_GROUP_HEADER, false, 6);
        assertPositionMetadata(5, ITEM_TYPE_STANDALONE, false, 8);

        // Second pass - using cache
        assertEquals(6, mAdapter.getCount());
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, false, 1);
        assertPositionMetadata(2, ITEM_TYPE_STANDALONE, false, 3);
        assertPositionMetadata(3, ITEM_TYPE_GROUP_HEADER, false, 4);
        assertPositionMetadata(4, ITEM_TYPE_GROUP_HEADER, false, 6);
        assertPositionMetadata(5, ITEM_TYPE_STANDALONE, false, 8);

        // Invalidate cache by expanding a group
        mAdapter.toggleGroup(1);

        // First pass - building up cache
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, true, 1);
        assertPositionMetadata(2, ITEM_TYPE_IN_GROUP, false, 1);
        assertPositionMetadata(3, ITEM_TYPE_IN_GROUP, false, 2);
        assertPositionMetadata(4, ITEM_TYPE_STANDALONE, false, 3);
        assertPositionMetadata(5, ITEM_TYPE_GROUP_HEADER, false, 4);
        assertPositionMetadata(6, ITEM_TYPE_GROUP_HEADER, false, 6);
        assertPositionMetadata(7, ITEM_TYPE_STANDALONE, false, 8);

        // Second pass - using cache
        assertPositionMetadata(0, ITEM_TYPE_STANDALONE, false, 0);
        assertPositionMetadata(1, ITEM_TYPE_GROUP_HEADER, true, 1);
        assertPositionMetadata(2, ITEM_TYPE_IN_GROUP, false, 1);
        assertPositionMetadata(3, ITEM_TYPE_IN_GROUP, false, 2);
        assertPositionMetadata(4, ITEM_TYPE_STANDALONE, false, 3);
        assertPositionMetadata(5, ITEM_TYPE_GROUP_HEADER, false, 4);
        assertPositionMetadata(6, ITEM_TYPE_GROUP_HEADER, false, 6);
        assertPositionMetadata(7, ITEM_TYPE_STANDALONE, false, 8);