FileDocCategorySizeDatePackage
ListGetCheckItemIdsTest.javaAPI DocAndroid 5.1 API4945Thu Mar 12 22:22:12 GMT 2015android.widget.listview

ListGetCheckItemIdsTest

public class ListGetCheckItemIdsTest extends android.test.ActivityInstrumentationTestCase2
Testing the ListView getCheckItemIds() method in different situations.

Fields Summary
private android.widget.ListView
mListView
Constructors Summary
public ListGetCheckItemIdsTest()

        super(ListSimple.class);
    
Methods Summary
private voidassertChecked(java.lang.String message, long expectedIds)

        // Sort the two arrays since we are actually doing a set equality.
        long[] checkItemIds = mListView.getCheckItemIds();
        long[] sortedCheckItemsIds = new long[checkItemIds.length];
        System.arraycopy(checkItemIds, 0, sortedCheckItemsIds, 0, checkItemIds.length);
        Arrays.sort(sortedCheckItemsIds);

        long[] sortedExpectedIds = new long[expectedIds.length];
        System.arraycopy(expectedIds, 0, sortedExpectedIds, 0, expectedIds.length);
        Arrays.sort(sortedExpectedIds);

        assertTrue(message, Arrays.equals(sortedExpectedIds, sortedCheckItemsIds));
    
protected voidsetUp()

        super.setUp();

        mListView = getActivity().getListView();
    
public voidtestClearChoices()

        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mListView.setItemChecked(0, true);
        mListView.clearChoices();
        assertChecked("Item checked after SINGLE clear choice");

        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        int childCount = mListView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            mListView.setItemChecked(0, i % 3 == 0);
        }
        mListView.clearChoices();
        assertChecked("Item checked after MULTIPLE clear choice");
    
public voidtestMultipleCheck()

        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        assertChecked("Item checked when setting Multiple mode");

        int childCount = mListView.getChildCount();
        assertTrue("Tests requires at least 4 items", childCount >= 4);

        mListView.setItemChecked(1, true);
        assertChecked("First element non checked", 1);

        mListView.setItemChecked(3, true);
        assertChecked("Second element not checked", 1, 3);

        mListView.setItemChecked(0, true);
        assertChecked("Third element not checked", 0, 1, 3);

        mListView.setItemChecked(2, false);
        assertChecked("Unchecked element appears checked", 0, 1, 3);

        mListView.setItemChecked(1, false);
        assertChecked("Unchecked element remains", 0, 3);

        mListView.setItemChecked(2, false);
        assertChecked("Already unchecked element appears", 0, 3);

        mListView.setItemChecked(3, false);
        assertChecked("Unchecked 3 remains", 0);

        mListView.setItemChecked(3, false);
        assertChecked("Twice unchecked 3 remains", 0);

        mListView.setItemChecked(0, false);
        assertChecked("Checked items after last element unchecked");
    
public voidtestNoneCheck()

        mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);

        mListView.setItemChecked(0, true);
        assertChecked("None check choice has item checked");
    
public voidtestSimpleCheck()

        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        assertChecked("Item checked when setting Single mode");

        // Test a check at each position
        int childCount = mListView.getChildCount();
        for (int i=0; i<childCount; i++) {
            mListView.setItemChecked(i, true);
            assertChecked("Only element " + i + " should be checked", i);
        }

        // Check an element and uncheck some others
        for (int i = 0; i < childCount; i++) {
            mListView.setItemChecked(i, true);
            mListView.setItemChecked((i - 3 + childCount) % childCount, false);
            mListView.setItemChecked((i + 1) % childCount, false);
            assertChecked("Only element " + i + " should be checked", i);
        }