FileDocCategorySizeDatePackage
SimpleCursorAdapterTest.javaAPI DocAndroid 1.5 API9047Wed May 06 22:42:02 BST 2009android.widget

SimpleCursorAdapterTest

public class SimpleCursorAdapterTest extends android.test.AndroidTestCase
This is a series of tests of basic API contracts for SimpleCursorAdapter. It is incomplete and can use work. NOTE: This contract holds for underlying cursor types too and these should be extracted into a set of tests that can be run on any descendant of CursorAdapter.

Fields Summary
String[]
mFrom
int[]
mTo
int
mLayout
android.content.Context
mContext
ArrayList
mData2x2
android.database.Cursor
mCursor2x2
Constructors Summary
Methods Summary
private java.util.ArrayListcreateTestList(int rows, int cols)
Borrowed from CursorWindowTest.java

        ArrayList<ArrayList> list = Lists.newArrayList();
        Random generator = new Random();

        for (int i = 0; i < rows; i++) {
            ArrayList<Integer> col = Lists.newArrayList();
            list.add(col);
            for (int j = 0; j < cols; j++) {
                // generate random number
                Integer r = generator.nextInt();
                col.add(r);
            }
        }
        return list;
    
public voidsetUp()
Set up basic columns and cursor for the tests

        super.setUp();
        
        // all the pieces needed for the various tests
        mFrom = new String[]{"Column1", "Column2"};
        mTo = new int[]{com.android.internal.R.id.text1, com.android.internal.R.id.text2};
        mLayout = com.android.internal.R.layout.simple_list_item_2;
        mContext = getContext();

        // raw data for building a basic test cursor
        mData2x2 = createTestList(2, 2);
        mCursor2x2 = new ArrayListCursor(mFrom, mData2x2);
    
public voidtestChangeCursorColumns()
Test changeCursor() with differing column layout. This confirms that the Adapter can deal with cursors that have the same essential data (as defined by the original mFrom array) but it's OK if the physical structure of the cursor changes (columns rearranged).

        TestSimpleCursorAdapter ca = new TestSimpleCursorAdapter(mContext, mLayout, mCursor2x2, 
                mFrom, mTo);
        
        // check columns of original - mFrom and mTo should line up
        int[] columns = ca.getConvertedFrom();
        assertEquals(columns[0], 0);
        assertEquals(columns[1], 1);

        // Now make a new cursor with similar data but rearrange the columns
        String[] swappedFrom = new String[]{"Column2", "Column1"};
        Cursor c2 = new ArrayListCursor(swappedFrom, mData2x2);
        ca.changeCursor(c2);
        assertEquals(2, ca.getCount());

        // check columns to see if rearrangement tracked (should be swapped now)
        columns = ca.getConvertedFrom();
        assertEquals(columns[0], 1);
        assertEquals(columns[1], 0);
    
public voidtestChangeCursorLive()
Test changeCursor() with live cursor

        SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo);
        
        // Now see if we can pull 2 rows from the adapter
        assertEquals(2, ca.getCount());
        
        // now put in a different cursor (5 rows)
        ArrayList<ArrayList> data2 = createTestList(5, 2);
        Cursor c2 = new ArrayListCursor(mFrom, data2);
        ca.changeCursor(c2);
        
        // Now see if we can pull 5 rows from the adapter
        assertEquals(5, ca.getCount());
    
public voidtestChangeCursorNull()
Test changeCursor() with null cursor

        SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo);
        
        // Now see if we can pull 2 rows from the adapter
        assertEquals(2, ca.getCount());
        
        // now put in null
        ca.changeCursor(null);
        
        // The adapter should report zero rows
        assertEquals(0, ca.getCount());
    
public voidtestChangeMapping()
Test going from one mapping to a different mapping This is new functionality added in 12/2008.

        TestSimpleCursorAdapter ca = new TestSimpleCursorAdapter(mContext, mLayout, mCursor2x2, 
                mFrom, mTo);
        assertEquals(2, ca.getCount());

        // Now create a new configuration with same cursor and just one column mapped
        String[] singleFrom = new String[]{"Column1"};
        int[] singleTo = new int[]{com.android.internal.R.id.text1};
        ca.changeCursorAndColumns(mCursor2x2, singleFrom, singleTo);

        // And examine the results, make sure they're still consistent
        int[] columns = ca.getConvertedFrom();
        assertEquals(1, columns.length);
        assertEquals(0, columns[0]);
        int[] viewIds = ca.getTo();
        assertEquals(1, viewIds.length);
        assertEquals(com.android.internal.R.id.text1, viewIds[0]);
        
        // And again, same cursor, different map
        singleFrom = new String[]{"Column2"};
        singleTo = new int[]{com.android.internal.R.id.text2};
        ca.changeCursorAndColumns(mCursor2x2, singleFrom, singleTo);

        // And examine the results, make sure they're still consistent
        columns = ca.getConvertedFrom();
        assertEquals(1, columns.length);
        assertEquals(1, columns[0]);
        viewIds = ca.getTo();
        assertEquals(1, viewIds.length);
        assertEquals(com.android.internal.R.id.text2, viewIds[0]);
    
public voidtestChangeNullToMapped()
Test going from a null cursor to a non-null cursor *and* setting the to/from arrays This is new functionality added in 12/2008.

        TestSimpleCursorAdapter ca = new TestSimpleCursorAdapter(mContext, mLayout, null, null, null);
        assertEquals(0, ca.getCount());

        ca.changeCursorAndColumns(mCursor2x2, mFrom, mTo);
        assertEquals(2, ca.getCount());
        
        // check columns of original - mFrom and mTo should line up
        int[] columns = ca.getConvertedFrom();
        assertEquals(2, columns.length);
        assertEquals(0, columns[0]);
        assertEquals(1, columns[1]);
        int[] viewIds = ca.getTo();
        assertEquals(2, viewIds.length);
        assertEquals(com.android.internal.R.id.text1, viewIds[0]);
        assertEquals(com.android.internal.R.id.text2, viewIds[1]);
    
public voidtestCreateLive()
Test creating with a live cursor

        SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, mCursor2x2, mFrom, mTo);
        
        // Now see if we can pull 2 rows from the adapter
        assertEquals(2, ca.getCount());
    
public voidtestCreateNull()
Test creating with a null cursor

        SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, null, mFrom, mTo);
        
        // The adapter should report zero rows
        assertEquals(0, ca.getCount());
    
public voidtestNullConstructor()
Test that you can safely construct with a null cursor *and* null to/from arrays. This is new functionality added in 12/2008.

        SimpleCursorAdapter ca = new SimpleCursorAdapter(mContext, mLayout, null, null, null);
        assertEquals(0, ca.getCount());