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.touch;
18
19import android.test.ActivityInstrumentationTestCase;
20import android.test.suitebuilder.annotation.LargeTest;
21import android.test.suitebuilder.annotation.MediumTest;
22import android.test.TouchUtils;
23import android.view.View;
24import android.widget.ListView;
25
26import android.widget.listview.ListSimple;
27
28/**
29 * Tests setting the selection in touch mode
30 */
31public class ListSetSelectionTest extends ActivityInstrumentationTestCase<ListSimple> {
32    private ListSimple mActivity;
33    private ListView mListView;
34
35    public ListSetSelectionTest() {
36        super("com.android.frameworks.coretests", ListSimple.class);
37    }
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42
43        mActivity = getActivity();
44        mListView = getActivity().getListView();
45    }
46
47    @MediumTest
48    public void testPreconditions() {
49        assertNotNull(mActivity);
50        assertNotNull(mListView);
51    }
52
53    @LargeTest
54    public void testSetSelection() {
55        TouchUtils.dragQuarterScreenDown(this);
56        TouchUtils.dragQuarterScreenUp(this);
57
58        // Nothing should be selected
59        assertEquals("Selection still available after touch", -1,
60                mListView.getSelectedItemPosition());
61
62        final int targetPosition = mListView.getAdapter().getCount() / 2;
63
64        mActivity.runOnUiThread(new Runnable() {
65            public void run() {
66                mListView.setSelection(targetPosition);
67            }
68        });
69        getInstrumentation().waitForIdleSync();
70
71        boolean found = false;
72        int childCount = mListView.getChildCount();
73        for (int i=0; i<childCount; i++) {
74            View child = mListView.getChildAt(i);
75            if (child.getId() == targetPosition) {
76                found = true;
77                break;
78            }
79        }
80        assertTrue("Selected item not visible in list", found);
81    }
82
83    @LargeTest
84    public void testSetSelectionFromTop() {
85        TouchUtils.dragQuarterScreenDown(this);
86        TouchUtils.dragQuarterScreenUp(this);
87
88        // Nothing should be selected
89        assertEquals("Selection still available after touch", -1,
90                mListView.getSelectedItemPosition());
91
92        final int targetPosition = mListView.getAdapter().getCount() / 2;
93
94        mActivity.runOnUiThread(new Runnable() {
95            public void run() {
96                mListView.setSelectionFromTop(targetPosition, 100);
97            }
98        });
99        getInstrumentation().waitForIdleSync();
100
101        View target = null;
102        boolean found = false;
103        int childCount = mListView.getChildCount();
104        for (int i=0; i<childCount; i++) {
105            View child = mListView.getChildAt(i);
106            if (child.getId() == targetPosition) {
107                target = child;
108                found = true;
109                break;
110            }
111        }
112        assertTrue("Selected item not visible in list", found);
113
114        if (target != null) {
115            assertEquals("Selection not at correct location", 100 + mListView.getPaddingTop(),
116                    target.getTop());
117        }
118    }
119
120    @LargeTest
121    public void testSetSelection0() {
122        TouchUtils.dragQuarterScreenDown(this);
123        TouchUtils.dragQuarterScreenDown(this);
124        TouchUtils.dragQuarterScreenDown(this);
125
126        // Nothing should be selected
127        assertEquals("Selection still available after touch", -1,
128                mListView.getSelectedItemPosition());
129
130        mActivity.runOnUiThread(new Runnable() {
131            public void run() {
132                mListView.setSelection(0);
133            }
134        });
135        getInstrumentation().waitForIdleSync();
136
137        boolean found = false;
138        int childCount = mListView.getChildCount();
139        for (int i=0; i<childCount; i++) {
140            View child = mListView.getChildAt(i);
141            if (child.getId() == 0 && i == 0) {
142                found = true;
143                break;
144            }
145        }
146        assertTrue("Selected item not visible in list", found);
147    }
148}
149