1 /* 2 * Copyright (C) 2008 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.focus; 18 19 import android.test.ActivityInstrumentationTestCase2; 20 import android.test.FlakyTest; 21 import android.test.TouchUtils; 22 import android.test.suitebuilder.annotation.LargeTest; 23 import android.test.suitebuilder.annotation.MediumTest; 24 import android.test.suitebuilder.annotation.Suppress; 25 import android.view.KeyEvent; 26 import android.view.View; 27 import android.widget.AbsListView; 28 import android.widget.ListView; 29 import android.widget.listview.ListWithEditTextHeader; 30 31 public class ListWithEditTextHeaderTest extends ActivityInstrumentationTestCase2<ListWithEditTextHeader> { 32 private ListView mListView; 33 34 public ListWithEditTextHeaderTest() { 35 super(ListWithEditTextHeader.class); 36 } 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 mListView = getActivity().getListView(); 42 } 43 44 @MediumTest 45 public void testPreconditions() { 46 assertTrue("listview.getItemsCanFocus()", mListView.getItemsCanFocus()); 47 assertFalse("out of touch-mode", mListView.isInTouchMode()); 48 assertEquals("header view count", 1, mListView.getHeaderViewsCount()); 49 assertTrue("header does not have focus", mListView.getChildAt(0).isFocused()); 50 } 51 52 @FlakyTest(tolerance=2) 53 @LargeTest 54 public void testClickingHeaderKeepsFocus() { 55 TouchUtils.clickView(this, mListView.getChildAt(0)); 56 assertTrue("header does not have focus", mListView.getChildAt(0).isFocused()); 57 assertEquals("something is selected", AbsListView.INVALID_POSITION, mListView.getSelectedItemPosition()); 58 } 59 60 @LargeTest 61 @Suppress // Failing. 62 public void testClickingHeaderWhenOtherItemHasFocusGivesHeaderFocus() { 63 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 64 assertEquals("selected position", 1, mListView.getSelectedItemPosition()); 65 TouchUtils.clickView(this, mListView.getChildAt(0)); 66 assertTrue("header does not have focus", mListView.getChildAt(0).isFocused()); 67 assertEquals("something is selected", AbsListView.INVALID_POSITION, mListView.getSelectedItemPosition()); 68 } 69 70 @LargeTest 71 public void testScrollingDoesNotDetachHeaderViewFromWindow() { 72 View header = mListView.getChildAt(0); 73 assertNotNull("header is not attached to a window (?!)", header.getWindowToken()); 74 75 // Scroll header off the screen and back onto the screen 76 int numItemsOnScreen = mListView.getChildCount(); 77 for (int i = 0; i < numItemsOnScreen; i++) { 78 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 79 } 80 for (int i = 0; i < numItemsOnScreen; i++) { 81 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 82 } 83 84 // Make sure the header was not accidentally left detached from its window 85 assertNotNull("header has lost its window", header.getWindowToken()); 86 } 87 } 88