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.app.Instrumentation;
20import android.test.ActivityInstrumentationTestCase;
21import android.test.TouchUtils;
22import android.test.suitebuilder.annotation.LargeTest;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.view.KeyEvent;
25import android.widget.AbsListView;
26import android.widget.ListView;
27
28public class ListScrollListenerTest extends ActivityInstrumentationTestCase<ListScrollListener> implements
29        AbsListView.OnScrollListener {
30    private ListScrollListener mActivity;
31    private ListView mListView;
32    private int mFirstVisibleItem = -1;
33    private int mVisibleItemCount = -1;
34    private int mTotalItemCount = -1;
35
36    public ListScrollListenerTest() {
37        super("com.android.frameworks.coretests", ListScrollListener.class);
38    }
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43
44        mActivity = getActivity();
45        mListView = getActivity().getListView();
46        mListView.setOnScrollListener(this);
47    }
48
49    @MediumTest
50    public void testPreconditions() {
51        assertNotNull(mActivity);
52        assertNotNull(mListView);
53
54        assertEquals(0, mFirstVisibleItem);
55    }
56
57    @LargeTest
58    public void testKeyScrolling() {
59        Instrumentation inst = getInstrumentation();
60        // focus the listview
61        mActivity.runOnUiThread(() -> mListView.requestFocus());
62        inst.waitForIdleSync();
63
64        int firstVisibleItem = mFirstVisibleItem;
65        for (int i = 0; i < mVisibleItemCount * 2; i++) {
66            inst.sendCharacterSync(KeyEvent.KEYCODE_DPAD_DOWN);
67        }
68        inst.waitForIdleSync();
69        assertTrue("Arrow scroll did not happen", mFirstVisibleItem > firstVisibleItem);
70
71        firstVisibleItem = mFirstVisibleItem;
72        KeyEvent upDown = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
73                KeyEvent.KEYCODE_DPAD_UP, 0, KeyEvent.META_ALT_ON);
74        KeyEvent upUp = new KeyEvent(0, 0, KeyEvent.ACTION_UP,
75                KeyEvent.KEYCODE_DPAD_UP, 0, KeyEvent.META_ALT_ON);
76        inst.sendKeySync(upDown);
77        inst.sendKeySync(upUp);
78        inst.waitForIdleSync();
79        assertTrue("Page scroll did not happen", mFirstVisibleItem < firstVisibleItem);
80
81        firstVisibleItem = mFirstVisibleItem;
82        KeyEvent down = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
83                KeyEvent.KEYCODE_DPAD_DOWN, 0, KeyEvent.META_ALT_ON);
84        KeyEvent up = new KeyEvent(0, 0, KeyEvent.ACTION_UP,
85                KeyEvent.KEYCODE_DPAD_DOWN, 0, KeyEvent.META_ALT_ON);
86        inst.sendKeySync(down);
87        inst.sendKeySync(up);
88        inst.waitForIdleSync();
89
90        assertTrue("Full scroll did not happen", mFirstVisibleItem > firstVisibleItem);
91        assertEquals("Full scroll did not happen", mTotalItemCount,
92                mFirstVisibleItem + mVisibleItemCount);
93    }
94
95    @LargeTest
96    public void testTouchScrolling() {
97        int firstVisibleItem = mFirstVisibleItem;
98        TouchUtils.dragQuarterScreenUp(this);
99        TouchUtils.dragQuarterScreenUp(this);
100        assertTrue("Touch scroll did not happen", mFirstVisibleItem > firstVisibleItem);
101    }
102
103
104    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
105        mFirstVisibleItem = firstVisibleItem;
106        mVisibleItemCount = visibleItemCount;
107        mTotalItemCount = totalItemCount;
108    }
109
110    public void onScrollStateChanged(AbsListView view, int scrollState) {
111    }
112}
113