Home | History | Annotate | Download | only in listview
      1 /*
      2  * Copyright (C) 2007 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.listview;
     18 
     19 import android.test.ActivityInstrumentationTestCase2;
     20 import android.test.UiThreadTest;
     21 import android.test.suitebuilder.annotation.MediumTest;
     22 import android.widget.ListView;
     23 
     24 import java.util.Arrays;
     25 
     26 /**
     27  * Testing the ListView getCheckItemIds() method in different situations.
     28  */
     29 public class ListGetCheckItemIdsTest extends ActivityInstrumentationTestCase2<ListSimple> {
     30     private ListView mListView;
     31 
     32     public ListGetCheckItemIdsTest() {
     33         super(ListSimple.class);
     34     }
     35 
     36     @Override
     37     protected void setUp() throws Exception {
     38         super.setUp();
     39 
     40         mListView = getActivity().getListView();
     41     }
     42 
     43     private void assertChecked(String message, long... expectedIds) {
     44         // Sort the two arrays since we are actually doing a set equality.
     45         long[] checkItemIds = mListView.getCheckItemIds();
     46         long[] sortedCheckItemsIds = new long[checkItemIds.length];
     47         System.arraycopy(checkItemIds, 0, sortedCheckItemsIds, 0, checkItemIds.length);
     48         Arrays.sort(sortedCheckItemsIds);
     49 
     50         long[] sortedExpectedIds = new long[expectedIds.length];
     51         System.arraycopy(expectedIds, 0, sortedExpectedIds, 0, expectedIds.length);
     52         Arrays.sort(sortedExpectedIds);
     53 
     54         assertTrue(message, Arrays.equals(sortedExpectedIds, sortedCheckItemsIds));
     55     }
     56 
     57     @MediumTest
     58     @UiThreadTest
     59     public void testNoneCheck() {
     60         mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
     61 
     62         mListView.setItemChecked(0, true);
     63         assertChecked("None check choice has item checked");
     64     }
     65 
     66     @MediumTest
     67     @UiThreadTest
     68     public void testSimpleCheck() {
     69         mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     70         assertChecked("Item checked when setting Single mode");
     71 
     72         // Test a check at each position
     73         int childCount = mListView.getChildCount();
     74         for (int i=0; i<childCount; i++) {
     75             mListView.setItemChecked(i, true);
     76             assertChecked("Only element " + i + " should be checked", i);
     77         }
     78 
     79         // Check an element and uncheck some others
     80         for (int i = 0; i < childCount; i++) {
     81             mListView.setItemChecked(i, true);
     82             mListView.setItemChecked((i - 3 + childCount) % childCount, false);
     83             mListView.setItemChecked((i + 1) % childCount, false);
     84             assertChecked("Only element " + i + " should be checked", i);
     85         }
     86     }
     87 
     88     @MediumTest
     89     @UiThreadTest
     90     public void testMultipleCheck() {
     91         mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
     92         assertChecked("Item checked when setting Multiple mode");
     93 
     94         int childCount = mListView.getChildCount();
     95         assertTrue("Tests requires at least 4 items", childCount >= 4);
     96 
     97         mListView.setItemChecked(1, true);
     98         assertChecked("First element non checked", 1);
     99 
    100         mListView.setItemChecked(3, true);
    101         assertChecked("Second element not checked", 1, 3);
    102 
    103         mListView.setItemChecked(0, true);
    104         assertChecked("Third element not checked", 0, 1, 3);
    105 
    106         mListView.setItemChecked(2, false);
    107         assertChecked("Unchecked element appears checked", 0, 1, 3);
    108 
    109         mListView.setItemChecked(1, false);
    110         assertChecked("Unchecked element remains", 0, 3);
    111 
    112         mListView.setItemChecked(2, false);
    113         assertChecked("Already unchecked element appears", 0, 3);
    114 
    115         mListView.setItemChecked(3, false);
    116         assertChecked("Unchecked 3 remains", 0);
    117 
    118         mListView.setItemChecked(3, false);
    119         assertChecked("Twice unchecked 3 remains", 0);
    120 
    121         mListView.setItemChecked(0, false);
    122         assertChecked("Checked items after last element unchecked");
    123     }
    124 
    125     @MediumTest
    126     @UiThreadTest
    127     public void testClearChoices() {
    128         mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    129         mListView.setItemChecked(0, true);
    130         mListView.clearChoices();
    131         assertChecked("Item checked after SINGLE clear choice");
    132 
    133         mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    134         int childCount = mListView.getChildCount();
    135         for (int i = 0; i < childCount; i++) {
    136             mListView.setItemChecked(0, i % 3 == 0);
    137         }
    138         mListView.clearChoices();
    139         assertChecked("Item checked after MULTIPLE clear choice");
    140     }
    141 }
    142