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 androidx.test.InstrumentationRegistry;
30 
31 import com.android.internal.R;
32 import com.android.internal.view.menu.ContextMenuBuilder;
33 
34 import com.google.android.collect.Lists;
35 
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 
40 public class ExpandableListTestActivity extends ExpandableListActivity {
41     private static final String NAME = "NAME";
42     private static final String IS_EVEN = "IS_EVEN";
43     private boolean mOnContentChangedCalled = false;
44     private boolean mOnCreateContextMenuCalled = false;
45     private boolean mOnGroupCollapseCalled = false;
46     private boolean mOnGroupExpandCalled = false;
47     private ExpandableListAdapter mAdapter;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52 
53         final List<Map<String, String>> groupData = Lists.newArrayList();
54         final List<List<Map<String, String>>> childData = Lists.newArrayList();
55         for (int i = 0; i < 20; i++) {
56             final Map<String, String> curGroupMap = new HashMap<String, String>();
57             groupData.add(curGroupMap);
58             curGroupMap.put(NAME, "Group " + i);
59             curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
60 
61             final List<Map<String, String>> children = Lists.newArrayList();
62             for (int j = 0; j < 15; j++) {
63                 Map<String, String> curChildMap = new HashMap<String, String>();
64                 children.add(curChildMap);
65                 curChildMap.put(NAME, "Child " + j);
66                 curChildMap.put(IS_EVEN, (j % 2 == 0) ? "This child is even" : "This child is odd");
67             }
68             childData.add(children);
69         }
70 
71         // Set up our adapter
72         mAdapter = new SimpleExpandableListAdapter(this, groupData,
73                 R.layout.simple_expandable_list_item_1,
74                 new String[] { NAME, IS_EVEN }, new int[] { R.id.text1, R.id.text2 }, childData,
75                 R.layout.simple_expandable_list_item_2,
76                 new String[] { NAME, IS_EVEN }, new int[] { R.id.text1, R.id.text2 });
77         setListAdapter(mAdapter);
78 
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 
testSelect()120     private int testSelect() {
121         final ExpandableListView v = getExpandableListView();
122         try {
123             // Make sure the touch mode is disabled since selection doesn't work in touch mode.
124             InstrumentationRegistry.getInstrumentation().setInTouchMode(false);
125             for (int i = 0; i < 20; i++) {
126                 v.expandGroup(i);
127                 setSelectedGroup(i);
128                 for (int k = 0; k < 15; k++) {
129                     setSelectedChild(i, k, false);
130                     if (ExpandableListView.getPackedPositionForChild(i, k) != getSelectedPosition())
131                         return RESULT_CANCELED;
132                 }
133 
134                 for (int k = 0; k < 15; k++) {
135                     setSelectedChild(i, k, true);
136                     if (ExpandableListView.getPackedPositionForChild(i, k) != getSelectedPosition())
137                         return RESULT_CANCELED;
138                 }
139                 v.collapseGroup(i);
140             }
141         } finally {
142             InstrumentationRegistry.getInstrumentation().setInTouchMode(true);
143         }
144         return RESULT_OK;
145     }
146 
147     @Override
onResume()148     protected void onResume() {
149         super.onResume();
150         final String action = getIntent().getAction();
151         if (LaunchpadActivity.EXPANDLIST_SELECT.equals(action)) {
152             setResult(testSelect());
153         } else if (LaunchpadActivity.EXPANDLIST_VIEW.equals(action)) {
154             setResult(testView());
155         } else if (LaunchpadActivity.EXPANDLIST_CALLBACK.equals(action)) {
156             setResult(testCallback());
157         }
158         Looper.myQueue().addIdleHandler(new Idler());
159     }
160 
onRestoreInstanceState(Bundle state)161     protected void onRestoreInstanceState(Bundle state) {
162         super.onRestoreInstanceState(state);
163     }
164 
165     @Override
onContentChanged()166     public void onContentChanged() {
167         mOnContentChangedCalled = true;
168         super.onContentChanged();
169     }
170 
171     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)172     public void onCreateContextMenu(ContextMenu menu, View v,
173             ContextMenuInfo menuInfo) {
174         mOnCreateContextMenuCalled = true;
175         super.onCreateContextMenu(menu, v, menuInfo);
176     }
177 
178     @Override
onGroupCollapse(int groupPosition)179     public void onGroupCollapse(int groupPosition) {
180         mOnGroupCollapseCalled = true;
181         super.onGroupCollapse(groupPosition);
182     }
183 
184     @Override
onGroupExpand(int groupPosition)185     public void onGroupExpand(int groupPosition) {
186         mOnGroupExpandCalled = true;
187         super.onGroupExpand(groupPosition);
188     }
189 
onSaveInstanceState(Bundle outState)190     protected void onSaveInstanceState(Bundle outState) {
191         super.onSaveInstanceState(outState);
192     }
193 
onStop()194     protected void onStop() {
195         super.onStop();
196     }
197 
198     private class Idler implements MessageQueue.IdleHandler {
queueIdle()199         public final boolean queueIdle() {
200             finish();
201             return false;
202         }
203     }
204 
205 }
206