1 /*
2  * Copyright (C) 2008 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 package android.app.stubs;
17 
18 import android.app.ExpandableListActivity;
19 import android.os.Bundle;
20 import android.os.Looper;
21 import android.os.MessageQueue;
22 import android.view.ContextMenu;
23 import android.view.ContextMenu.ContextMenuInfo;
24 import android.view.View;
25 import android.widget.ExpandableListAdapter;
26 import android.widget.ExpandableListView;
27 import android.widget.SimpleExpandableListAdapter;
28 
29 import com.android.internal.R;
30 import com.android.internal.view.menu.ContextMenuBuilder;
31 
32 import com.google.android.collect.Lists;
33 
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 
38 public class ExpandableListTestActivity extends ExpandableListActivity {
39 
40     public static final int NUMBER_OF_GROUPS = 20;
41     public static final int NUMBER_OF_CHILDREN = 15;
42 
43     private static final String NAME = "NAME";
44     private static final String IS_EVEN = "IS_EVEN";
45     private boolean mOnContentChangedCalled = false;
46     private boolean mOnCreateContextMenuCalled = false;
47     private boolean mOnGroupCollapseCalled = false;
48     private boolean mOnGroupExpandCalled = false;
49     private ExpandableListAdapter mAdapter;
50 
51     @Override
onCreate(Bundle savedInstanceState)52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         final List<Map<String, String>> groupData = Lists.newArrayList();
55         final List<List<Map<String, String>>> childData = Lists.newArrayList();
56         for (int i = 0; i < NUMBER_OF_GROUPS; i++) {
57             final Map<String, String> curGroupMap = new HashMap<>();
58             groupData.add(curGroupMap);
59             curGroupMap.put(NAME, "Group " + i);
60             curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
61 
62             final List<Map<String, String>> children = Lists.newArrayList();
63             for (int j = 0; j < NUMBER_OF_CHILDREN; j++) {
64                 Map<String, String> curChildMap = new HashMap<>();
65                 children.add(curChildMap);
66                 curChildMap.put(NAME, "Child " + j);
67                 curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
68             }
69             childData.add(children);
70         }
71 
72         // Set up our adapter
73         mAdapter = new SimpleExpandableListAdapter(this, groupData,
74                 R.layout.simple_expandable_list_item_1,
75                 new String[] { NAME, IS_EVEN }, new int[] { R.id.text1, R.id.text2 }, childData,
76                 R.layout.simple_expandable_list_item_2,
77                 new String[] { NAME, IS_EVEN }, new int[] { R.id.text1, R.id.text2 });
78         setListAdapter(mAdapter);
79     }
80 
testCallback()81     private int testCallback() {
82         final ExpandableListView v = getExpandableListView();
83         final ExpandableListAdapter a = getExpandableListAdapter();
84         final View convertView = new View(this);
85         final View gv = a.getGroupView(0, true, convertView, v);
86         v.setOnCreateContextMenuListener(this);
87         v.createContextMenu(new ContextMenuBuilder(this));
88         for (int i = 0; i < 20; i++) {
89             v.expandGroup(i);
90             v.performClick();
91             v.performLongClick();
92             for (int k = 0; k < 15; k++) {
93                 v.performItemClick(gv, i, k);
94             }
95             v.collapseGroup(i);
96         }
97         if (mOnContentChangedCalled && mOnCreateContextMenuCalled
98                 && mOnGroupCollapseCalled && mOnGroupExpandCalled)
99             return RESULT_OK;
100 
101         return RESULT_CANCELED;
102     }
103 
testView()104     private int testView() {
105         final ExpandableListView currentView = getExpandableListView();
106         for (int i = 0; i < 20; i++) {
107             if (!currentView.expandGroup(i))
108                 return RESULT_CANCELED;
109             if (!currentView.collapseGroup(i))
110                 return RESULT_CANCELED;
111         }
112         final View otherView = findViewById(android.R.id.list);
113         setContentView(otherView);
114         if (!otherView.equals(getExpandableListView()))
115             return RESULT_CANCELED;
116         setContentView(currentView);
117         return RESULT_OK;
118     }
119 
120     @Override
onResume()121     protected void onResume() {
122         super.onResume();
123         final String action = getIntent().getAction();
124         if (LaunchpadActivity.EXPANDLIST_VIEW.equals(action)) {
125             setResult(testView());
126             Looper.myQueue().addIdleHandler(new Idler());
127         } else if (LaunchpadActivity.EXPANDLIST_CALLBACK.equals(action)) {
128             setResult(testCallback());
129             Looper.myQueue().addIdleHandler(new Idler());
130         }
131     }
132 
onRestoreInstanceState(Bundle state)133     protected void onRestoreInstanceState(Bundle state) {
134         super.onRestoreInstanceState(state);
135     }
136 
137     @Override
onContentChanged()138     public void onContentChanged() {
139         mOnContentChangedCalled = true;
140         super.onContentChanged();
141     }
142 
143     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)144     public void onCreateContextMenu(ContextMenu menu, View v,
145             ContextMenuInfo menuInfo) {
146         mOnCreateContextMenuCalled = true;
147         super.onCreateContextMenu(menu, v, menuInfo);
148     }
149 
150     @Override
onGroupCollapse(int groupPosition)151     public void onGroupCollapse(int groupPosition) {
152         mOnGroupCollapseCalled = true;
153         super.onGroupCollapse(groupPosition);
154     }
155 
156     @Override
onGroupExpand(int groupPosition)157     public void onGroupExpand(int groupPosition) {
158         mOnGroupExpandCalled = true;
159         super.onGroupExpand(groupPosition);
160     }
161 
onSaveInstanceState(Bundle outState)162     protected void onSaveInstanceState(Bundle outState) {
163         super.onSaveInstanceState(outState);
164     }
165 
onStop()166     protected void onStop() {
167         super.onStop();
168     }
169 
170     private class Idler implements MessageQueue.IdleHandler {
queueIdle()171         public final boolean queueIdle() {
172             finish();
173             return false;
174         }
175     }
176 
177 }
178