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;
18
19import android.util.GridScenario;
20
21import android.test.ActivityInstrumentationTestCase;
22import android.test.suitebuilder.annotation.MediumTest;
23import android.test.ViewAsserts;
24import android.widget.GridView;
25
26public class GridSetSelectionBaseTest<T extends GridScenario> extends ActivityInstrumentationTestCase<T> {
27    private T mActivity;
28    private GridView mGridView;
29
30    protected GridSetSelectionBaseTest(Class<T> klass) {
31        super("com.android.frameworks.coretests", klass);
32    }
33
34    @Override
35    protected void setUp() throws Exception {
36        super.setUp();
37
38        mActivity = getActivity();
39        mGridView = getActivity().getGridView();
40    }
41
42    @MediumTest
43    public void testPreconditions() {
44        assertNotNull(mActivity);
45        assertNotNull(mGridView);
46
47        // First item should be selected
48        if (mGridView.isStackFromBottom()) {
49            assertEquals(mGridView.getAdapter().getCount() - 1,
50                    mGridView.getSelectedItemPosition());
51        } else {
52            assertEquals(0, mGridView.getSelectedItemPosition());
53        }
54    }
55
56    @MediumTest
57    public void testSetSelectionToTheEnd() {
58        final int target = mGridView.getAdapter().getCount() - 1;
59        mActivity.runOnUiThread(new Runnable() {
60            public void run() {
61                mGridView.setSelection(target);
62            }
63        });
64        getInstrumentation().waitForIdleSync();
65
66        assertEquals(mGridView.getSelectedItemPosition(), target);
67        assertNotNull(mGridView.getSelectedView());
68
69        ViewAsserts.assertOnScreen(mGridView, mGridView.getSelectedView());
70    }
71
72    @MediumTest
73    public void testSetSelectionToMiddle() {
74        final int target = mGridView.getAdapter().getCount() / 2;
75        mActivity.runOnUiThread(new Runnable() {
76            public void run() {
77                mGridView.setSelection(target);
78            }
79        });
80        getInstrumentation().waitForIdleSync();
81
82        assertEquals(mGridView.getSelectedItemPosition(), target);
83        assertNotNull(mGridView.getSelectedView());
84
85        ViewAsserts.assertOnScreen(mGridView, mGridView.getSelectedView());
86    }
87
88    @MediumTest
89    public void testSetSelectionToTheTop() {
90        mActivity.runOnUiThread(new Runnable() {
91            public void run() {
92                mGridView.setSelection(0);
93            }
94        });
95        getInstrumentation().waitForIdleSync();
96
97        assertEquals(mGridView.getSelectedItemPosition(), 0);
98        assertNotNull(mGridView.getSelectedView());
99
100        ViewAsserts.assertOnScreen(mGridView, mGridView.getSelectedView());
101    }
102}
103