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.arrowscroll;
18 
19 import android.test.ActivityInstrumentationTestCase;
20 import android.view.KeyEvent;
21 import android.view.View;
22 import android.widget.ListView;
23 import android.widget.TextView;
24 import android.widget.listview.ListWithOffScreenNextSelectable;
25 
26 import androidx.test.filters.MediumTest;
27 import androidx.test.filters.Suppress;
28 
29 @Suppress // Failing.
30 public class ListWithOffScreenNextSelectableTest
31         extends ActivityInstrumentationTestCase<ListWithOffScreenNextSelectable> {
32     private ListView mListView;
33 
ListWithOffScreenNextSelectableTest()34     public ListWithOffScreenNextSelectableTest() {
35         super("com.android.frameworks.coretests", ListWithOffScreenNextSelectable.class);
36     }
37 
38     @Override
setUp()39     protected void setUp() throws Exception {
40         super.setUp();
41 
42         mListView = getActivity().getListView();
43     }
44 
45     @MediumTest
testPreconditions()46     public void testPreconditions() {
47         assertNotNull(mListView);
48         assertEquals(5, mListView.getAdapter().getCount());
49         assertFalse(mListView.getAdapter().areAllItemsEnabled());
50         assertFalse(mListView.getAdapter().isEnabled(1));
51         assertFalse(mListView.getAdapter().isEnabled(2));
52         assertFalse(mListView.getAdapter().isEnabled(3));
53         assertEquals("only 4 children should be on screen (so that next selectable is off " +
54                 "screen) for this test to be meaningful.",
55                 4, mListView.getChildCount());
56         assertEquals(0, mListView.getSelectedItemPosition());
57     }
58 
59     // when the next items on screen are not selectable, we pan until the next selectable item
60     // is (partially visible), then we jump to it
61     @MediumTest
testGoDownToOffScreenSelectable()62     public void testGoDownToOffScreenSelectable() {
63 
64         final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
65 
66         final View lastVisibleView = mListView.getChildAt(mListView.getChildCount() - 1);
67         sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
68         assertEquals("expecting view to be panned to just above fading edge",
69                 listBottom - mListView.getVerticalFadingEdgeLength(), lastVisibleView.getBottom());
70         assertEquals("selection should not have moved yet",
71                 0, mListView.getSelectedItemPosition());
72 
73         sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
74         assertEquals("selection should have moved",
75                 4, mListView.getSelectedItemPosition());
76         assertEquals("wrong view created when scrolling",
77                 getActivity().getValueAtPosition(4), ((TextView) mListView.getSelectedView()).getText());
78         assertEquals(listBottom, mListView.getSelectedView().getBottom());
79     }
80 
81     @MediumTest
testGoUpToOffScreenSelectable()82     public void testGoUpToOffScreenSelectable() {
83         final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom();
84         final int listTop = mListView.getListPaddingTop();
85 
86         sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
87         sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
88 
89         assertEquals(4, mListView.getSelectedItemPosition());
90         assertEquals(listBottom, mListView.getSelectedView().getBottom());
91 
92         // now we have the reverse situation: the next selectable position upward is off screen
93         final View firstVisibleView = mListView.getChildAt(0);
94         sendKeys(KeyEvent.KEYCODE_DPAD_UP);
95         assertEquals("should have panned top view just below vertical fading edge",
96                 listTop + mListView.getVerticalFadingEdgeLength(), firstVisibleView.getTop());
97         assertEquals("selection should not have moved yet",
98                 4, mListView.getSelectedItemPosition());
99 
100         sendKeys(KeyEvent.KEYCODE_DPAD_UP);
101         assertEquals("selection should have moved",
102                 0, mListView.getSelectedItemPosition());
103         assertEquals(getActivity().getValueAtPosition(0),((TextView) mListView.getSelectedView()).getText());
104         assertEquals(listTop, mListView.getSelectedView().getTop());
105 
106     }
107 
108 }
109