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.expandablelistview;
18
19import android.test.ActivityInstrumentationTestCase2;
20import android.test.suitebuilder.annotation.MediumTest;
21import android.util.ExpandableListScenario;
22import android.util.ListUtil;
23import android.util.ExpandableListScenario.MyGroup;
24import android.view.KeyEvent;
25import android.widget.BaseExpandableListAdapter;
26import android.widget.ExpandableListAdapter;
27import android.widget.ExpandableListView;
28
29import java.util.List;
30
31public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<ExpandableListSimple> {
32    private ExpandableListScenario mActivity;
33    private ExpandableListView mExpandableListView;
34    private ExpandableListAdapter mAdapter;
35    private ListUtil mListUtil;
36
37    public ExpandableListBasicTest() {
38        super(ExpandableListSimple.class);
39    }
40
41    @Override
42    protected void setUp() throws Exception {
43        super.setUp();
44
45        mActivity = getActivity();
46        mExpandableListView = mActivity.getExpandableListView();
47        mAdapter = mExpandableListView.getExpandableListAdapter();
48        mListUtil = new ListUtil(mExpandableListView, getInstrumentation());
49    }
50
51    @MediumTest
52    public void testPreconditions() {
53        assertNotNull(mActivity);
54        assertNotNull(mExpandableListView);
55    }
56
57    private int expandGroup(int numChildren, boolean atLeastOneChild) {
58        final int groupPos = mActivity.findGroupWithNumChildren(numChildren, atLeastOneChild);
59        assertTrue("Could not find group to expand", groupPos >= 0);
60
61        assertFalse("Group is already expanded", mExpandableListView.isGroupExpanded(groupPos));
62        mListUtil.arrowScrollToSelectedPosition(groupPos);
63        getInstrumentation().waitForIdleSync();
64        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
65        getInstrumentation().waitForIdleSync();
66        assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(groupPos));
67
68        return groupPos;
69    }
70
71    @MediumTest
72    public void testExpandGroup() {
73        expandGroup(-1, true);
74    }
75
76    @MediumTest
77    public void testCollapseGroup() {
78        final int groupPos = expandGroup(-1, true);
79
80        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
81        getInstrumentation().waitForIdleSync();
82        assertFalse("Group did not collapse", mExpandableListView.isGroupExpanded(groupPos));
83    }
84
85    @MediumTest
86    public void testExpandedGroupMovement() {
87        // Expand the first group
88        mListUtil.arrowScrollToSelectedPosition(0);
89        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
90        getInstrumentation().waitForIdleSync();
91
92        // Ensure it expanded
93        assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(0));
94
95        // Wait until that's all good
96        getInstrumentation().waitForIdleSync();
97
98        // Make sure it expanded
99        assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(0));
100
101        // Insert a collapsed group in front of the one just expanded
102        List<MyGroup> groups = mActivity.getGroups();
103        MyGroup insertedGroup = new MyGroup(1);
104        groups.add(0, insertedGroup);
105
106        // Notify data change
107        assertTrue("Adapter is not an instance of the base adapter",
108                mAdapter instanceof BaseExpandableListAdapter);
109        final BaseExpandableListAdapter adapter = (BaseExpandableListAdapter) mAdapter;
110
111        mActivity.runOnUiThread(new Runnable() {
112            public void run() {
113                adapter.notifyDataSetChanged();
114            }
115        });
116        getInstrumentation().waitForIdleSync();
117
118        // Make sure the right group is expanded
119        assertTrue("The expanded state didn't stay with the proper group",
120                mExpandableListView.isGroupExpanded(1));
121        assertFalse("The expanded state was given to the inserted group",
122                mExpandableListView.isGroupExpanded(0));
123    }
124
125    @MediumTest
126    public void testContextMenus() {
127        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
128        tester.testContextMenus();
129    }
130
131    @MediumTest
132    public void testConvertionBetweenFlatAndPacked() {
133        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
134        tester.testConvertionBetweenFlatAndPackedOnGroups();
135        tester.testConvertionBetweenFlatAndPackedOnChildren();
136    }
137
138    @MediumTest
139    public void testSelectedPosition() {
140        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
141        tester.testSelectedPositionOnGroups();
142        tester.testSelectedPositionOnChildren();
143    }
144}
145