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.gridview.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.GridView;
25
26import android.widget.gridview.GridSimple;
27
28/**
29 * Tests setting the selection in touch mode
30 */
31public class GridTouchSetSelectionTest extends ActivityInstrumentationTestCase<GridSimple> {
32    private GridSimple mActivity;
33    private GridView mGridView;
34
35    public GridTouchSetSelectionTest() {
36        super("com.android.frameworks.coretests", GridSimple.class);
37    }
38
39    @Override
40    protected void setUp() throws Exception {
41        super.setUp();
42
43        mActivity = getActivity();
44        mGridView = getActivity().getGridView();
45    }
46
47    @MediumTest
48    public void testPreconditions() {
49        assertNotNull(mActivity);
50        assertNotNull(mGridView);
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                mGridView.getSelectedItemPosition());
61
62        final int targetPosition = mGridView.getAdapter().getCount() / 2;
63
64        mActivity.runOnUiThread(new Runnable() {
65            public void run() {
66                mGridView.setSelection(targetPosition);
67            }
68        });
69        getInstrumentation().waitForIdleSync();
70
71        boolean found = false;
72        int childCount = mGridView.getChildCount();
73        for (int i=0; i<childCount; i++) {
74            View child = mGridView.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