Home | History | Annotate | Download | only in cts
      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 
     17 package android.widget.cts;
     18 
     19 import android.app.Instrumentation;
     20 import android.test.ActivityInstrumentationTestCase2;
     21 import android.view.KeyEvent;
     22 import android.view.View;
     23 import android.widget.ExpandableListAdapter;
     24 import android.widget.ExpandableListView;
     25 import android.widget.cts.util.ExpandableListScenario;
     26 import android.widget.cts.util.ListUtil;
     27 
     28 import junit.framework.Assert;
     29 
     30 public class ExpandableListTester {
     31     private final ExpandableListView mExpandableListView;
     32     private final ExpandableListAdapter mAdapter;
     33     private final ListUtil mListUtil;
     34 
     35     private final ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
     36         mActivityInstrumentation;
     37 
     38     Instrumentation mInstrumentation;
     39 
     40     public ExpandableListTester(
     41             ExpandableListView expandableListView,
     42             ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
     43             activityInstrumentation) {
     44         mExpandableListView = expandableListView;
     45         Instrumentation instrumentation = activityInstrumentation.getInstrumentation();
     46         mListUtil = new ListUtil(mExpandableListView, instrumentation);
     47         mAdapter = mExpandableListView.getExpandableListAdapter();
     48         mActivityInstrumentation = activityInstrumentation;
     49         mInstrumentation = mActivityInstrumentation.getInstrumentation();
     50     }
     51 
     52     private void expandGroup(final int groupIndex, int flatPosition) {
     53         Assert.assertFalse("Group is already expanded", mExpandableListView
     54                 .isGroupExpanded(groupIndex));
     55         mListUtil.arrowScrollToSelectedPosition(flatPosition);
     56         mInstrumentation.waitForIdleSync();
     57         mActivityInstrumentation.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
     58         mActivityInstrumentation.getInstrumentation().waitForIdleSync();
     59         Assert.assertTrue("Group did not expand " + groupIndex,
     60                 mExpandableListView.isGroupExpanded(groupIndex));
     61     }
     62 
     63     void testContextMenus() {
     64         // Add a position tester ContextMenu listener to the ExpandableListView
     65         PositionTesterContextMenuListener menuListener = new PositionTesterContextMenuListener();
     66         mExpandableListView.setOnCreateContextMenuListener(menuListener);
     67 
     68         int index = 0;
     69 
     70         // Scrolling on header elements should trigger an AdapterContextMenu
     71         for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
     72             // Check group index in context menu
     73             menuListener.expectAdapterContextMenu(i);
     74             // Make sure the group is visible so that getChild finds it
     75             mListUtil.arrowScrollToSelectedPosition(index);
     76             View headerChild = mExpandableListView.getChildAt(index
     77                     - mExpandableListView.getFirstVisiblePosition());
     78             mExpandableListView.showContextMenuForChild(headerChild);
     79             mInstrumentation.waitForIdleSync();
     80             Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
     81             index++;
     82         }
     83 
     84         int groupCount = mAdapter.getGroupCount();
     85         for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
     86 
     87             // Expand group
     88             expandGroup(groupIndex, index);
     89 
     90             // Check group index in context menu
     91             menuListener.expectGroupContextMenu(groupIndex);
     92             // Make sure the group is visible so that getChild finds it
     93             mListUtil.arrowScrollToSelectedPosition(index);
     94             View groupChild = mExpandableListView.getChildAt(index
     95                     - mExpandableListView.getFirstVisiblePosition());
     96             mExpandableListView.showContextMenuForChild(groupChild);
     97             mInstrumentation.waitForIdleSync();
     98             Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
     99             index++;
    100 
    101             final int childrenCount = mAdapter.getChildrenCount(groupIndex);
    102             for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
    103                 // Check child index in context menu
    104                 mListUtil.arrowScrollToSelectedPosition(index);
    105                 menuListener.expectChildContextMenu(groupIndex, childIndex);
    106                 View child = mExpandableListView.getChildAt(index
    107                         - mExpandableListView.getFirstVisiblePosition());
    108                 mExpandableListView.showContextMenuForChild(child);
    109                 mInstrumentation.waitForIdleSync();
    110                 Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
    111                 index++;
    112             }
    113         }
    114 
    115         // Scrolling on footer elements should trigger an AdapterContextMenu
    116         for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
    117             // Check group index in context menu
    118             menuListener.expectAdapterContextMenu(index);
    119             // Make sure the group is visible so that getChild finds it
    120             mListUtil.arrowScrollToSelectedPosition(index);
    121             View footerChild = mExpandableListView.getChildAt(index
    122                     - mExpandableListView.getFirstVisiblePosition());
    123             mExpandableListView.showContextMenuForChild(footerChild);
    124             mInstrumentation.waitForIdleSync();
    125             Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
    126             index++;
    127         }
    128 
    129         // Cleanup: remove the listener we added.
    130         mExpandableListView.setOnCreateContextMenuListener(null);
    131     }
    132 
    133     private int expandAGroup() {
    134         final int groupIndex = 2;
    135         final int headerCount = mExpandableListView.getHeaderViewsCount();
    136         Assert.assertTrue("Not enough groups", groupIndex < mAdapter.getGroupCount());
    137         expandGroup(groupIndex, groupIndex + headerCount);
    138         return groupIndex;
    139     }
    140 
    141     // This method assumes that NO group is expanded when called
    142     void testConvertionBetweenFlatAndPackedOnGroups() {
    143         final int headerCount = mExpandableListView.getHeaderViewsCount();
    144 
    145         for (int i=0; i<headerCount; i++) {
    146             Assert.assertEquals("Non NULL position for header item",
    147                     ExpandableListView.PACKED_POSITION_VALUE_NULL,
    148                     mExpandableListView.getExpandableListPosition(i));
    149         }
    150 
    151         // Test all (non expanded) groups
    152         final int groupCount = mAdapter.getGroupCount();
    153         for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
    154             int expectedFlatPosition = headerCount + groupIndex;
    155             long packedPositionForGroup = ExpandableListView.getPackedPositionForGroup(groupIndex);
    156             Assert.assertEquals("Group not found at flat position " + expectedFlatPosition,
    157                     packedPositionForGroup,
    158                     mExpandableListView.getExpandableListPosition(expectedFlatPosition));
    159 
    160             Assert.assertEquals("Wrong flat position for group " + groupIndex,
    161                     expectedFlatPosition,
    162                     mExpandableListView.getFlatListPosition(packedPositionForGroup));
    163         }
    164 
    165         for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
    166             Assert.assertEquals("Non NULL position for header item",
    167                     ExpandableListView.PACKED_POSITION_VALUE_NULL,
    168                     mExpandableListView.getExpandableListPosition(headerCount + groupCount + i));
    169         }
    170     }
    171 
    172     // This method assumes that NO group is expanded when called
    173     void testConvertionBetweenFlatAndPackedOnChildren() {
    174         // Test with an expanded group
    175         final int headerCount = mExpandableListView.getHeaderViewsCount();
    176         final int groupIndex = expandAGroup();
    177 
    178         final int childrenCount = mAdapter.getChildrenCount(groupIndex);
    179         for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
    180             int expectedFlatPosition = headerCount + groupIndex + 1 + childIndex;
    181             long childPos = ExpandableListView.getPackedPositionForChild(groupIndex, childIndex);
    182 
    183             Assert.assertEquals("Wrong flat position for child ",
    184                     childPos,
    185                     mExpandableListView.getExpandableListPosition(expectedFlatPosition));
    186 
    187             Assert.assertEquals("Wrong flat position for child ",
    188                     expectedFlatPosition,
    189                     mExpandableListView.getFlatListPosition(childPos));
    190         }
    191     }
    192 
    193     // This method assumes that NO group is expanded when called
    194     void testSelectedPositionOnGroups() {
    195         int index = 0;
    196 
    197         // Scrolling on header elements should not give a valid selected position.
    198         for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
    199             mListUtil.arrowScrollToSelectedPosition(index);
    200             Assert.assertEquals("Header item is selected",
    201                     ExpandableListView.PACKED_POSITION_VALUE_NULL,
    202                     mExpandableListView.getSelectedPosition());
    203             index++;
    204         }
    205 
    206         // Check selection on group items
    207         final int groupCount = mAdapter.getGroupCount();
    208         for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
    209             mListUtil.arrowScrollToSelectedPosition(index);
    210             Assert.assertEquals("Group item is not selected",
    211                     ExpandableListView.getPackedPositionForGroup(groupIndex),
    212                     mExpandableListView.getSelectedPosition());
    213             index++;
    214         }
    215 
    216         // Scrolling on footer elements should not give a valid selected position.
    217         for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
    218             mListUtil.arrowScrollToSelectedPosition(index);
    219             Assert.assertEquals("Footer item is selected",
    220                     ExpandableListView.PACKED_POSITION_VALUE_NULL,
    221                     mExpandableListView.getSelectedPosition());
    222             index++;
    223         }
    224     }
    225 
    226     // This method assumes that NO group is expanded when called
    227     void testSelectedPositionOnChildren() {
    228         // Test with an expanded group
    229         final int headerCount = mExpandableListView.getHeaderViewsCount();
    230         final int groupIndex = expandAGroup();
    231 
    232         final int childrenCount = mAdapter.getChildrenCount(groupIndex);
    233         for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
    234             int childFlatPosition = headerCount + groupIndex + 1 + childIndex;
    235             mListUtil.arrowScrollToSelectedPosition(childFlatPosition);
    236             Assert.assertEquals("Group item is not selected",
    237                     ExpandableListView.getPackedPositionForChild(groupIndex, childIndex),
    238                     mExpandableListView.getSelectedPosition());
    239         }
    240     }
    241 }
    242