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