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