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 17package android.widget.listview; 18 19import android.test.ActivityInstrumentationTestCase2; 20import android.test.UiThreadTest; 21import android.test.suitebuilder.annotation.MediumTest; 22import android.widget.ListView; 23 24/** 25 * Basic tests of setting & clearing the selection 26 */ 27public class ListSetSelectionTest extends ActivityInstrumentationTestCase2<ListSimple> { 28 private ListSimple mActivity; 29 private ListView mListView; 30 31 public ListSetSelectionTest() { 32 super("com.android.frameworks.coretests", ListSimple.class); 33 } 34 35 @Override 36 protected void setUp() throws Exception { 37 super.setUp(); 38 39 mActivity = getActivity(); 40 mListView = getActivity().getListView(); 41 } 42 43 @MediumTest 44 public void testPreconditions() { 45 assertNotNull(mActivity); 46 assertNotNull(mListView); 47 } 48 49 /** Confirm that we can set the selection to each specific position */ 50 @MediumTest 51 @UiThreadTest 52 public void testSetSelection() { 53 // Set the selection to each position 54 int childCount = mListView.getChildCount(); 55 for (int i=0; i<childCount; i++) { 56 mListView.setSelection(i); 57 assertEquals("Set selection", i, mListView.getSelectedItemPosition()); 58 } 59 } 60 61 /** Confirm that you cannot unset the selection using the same API */ 62 @MediumTest 63 @UiThreadTest 64 public void testClearSelection() { 65 // Set the selection to first position 66 mListView.setSelection(0); 67 assertEquals("Set selection", 0, mListView.getSelectedItemPosition()); 68 69 // Clear the selection 70 mListView.setSelection(ListView.INVALID_POSITION); 71 assertEquals("Set selection", 0, mListView.getSelectedItemPosition()); 72 } 73} 74