Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e26bad0a authored by Gilles Debunne's avatar Gilles Debunne
Browse files

New tests for ExpandableListView to exercice packed position with headers/footers.

New tests for
- the different conversions between flat and packed positions.
- selectedPosition
- contextMenuInfo.

All these tests currently fail when list contains headers and/or footers.

Change-Id: Ifb8fcc5de4980221e2ff4ebd2b24bc4fa03c6aae
parent da2572ad
Loading
Loading
Loading
Loading
+28 −61
Original line number Diff line number Diff line
@@ -16,14 +16,12 @@

package android.widget.expandablelistview;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import android.util.ExpandableListScenario;
import android.util.ListUtil;
import android.util.ExpandableListScenario.MyGroup;
import android.view.KeyEvent;
import android.view.View;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
@@ -32,7 +30,7 @@ import java.util.List;

public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<ExpandableListSimple> {
    private ExpandableListScenario mActivity;
    private ExpandableListView mListView;
    private ExpandableListView mExpandableListView;
    private ExpandableListAdapter mAdapter;
    private ListUtil mListUtil;
    
@@ -45,27 +43,27 @@ public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<Ex
        super.setUp();
        
        mActivity = getActivity();
        mListView = mActivity.getExpandableListView();
        mAdapter = mListView.getExpandableListAdapter();
        mListUtil = new ListUtil(mListView, getInstrumentation());
        mExpandableListView = mActivity.getExpandableListView();
        mAdapter = mExpandableListView.getExpandableListAdapter();
        mListUtil = new ListUtil(mExpandableListView, getInstrumentation());
    }
    
    @MediumTest
    public void testPreconditions() {
        assertNotNull(mActivity);
        assertNotNull(mListView);
        assertNotNull(mExpandableListView);
    }
    
    private int expandGroup(int numChildren, boolean atLeastOneChild) {
        final int groupPos = mActivity.findGroupWithNumChildren(numChildren, atLeastOneChild);
        
        assertTrue("Could not find group to expand", groupPos >= 0);
        assertFalse("Group is already expanded", mListView.isGroupExpanded(groupPos));

        assertFalse("Group is already expanded", mExpandableListView.isGroupExpanded(groupPos));
        mListUtil.arrowScrollToSelectedPosition(groupPos);
        getInstrumentation().waitForIdleSync();
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        getInstrumentation().waitForIdleSync();
        assertTrue("Group did not expand", mListView.isGroupExpanded(groupPos));
        assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(groupPos));

        return groupPos;
    }
@@ -81,7 +79,7 @@ public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<Ex
        
        sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        getInstrumentation().waitForIdleSync();
        assertFalse("Group did not collapse", mListView.isGroupExpanded(groupPos));
        assertFalse("Group did not collapse", mExpandableListView.isGroupExpanded(groupPos));
    }
    
    @MediumTest
@@ -92,13 +90,13 @@ public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<Ex
        getInstrumentation().waitForIdleSync();

        // Ensure it expanded
        assertTrue("Group did not expand", mListView.isGroupExpanded(0));
        assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(0));
        
        // Wait until that's all good
        getInstrumentation().waitForIdleSync();
        
        // Make sure it expanded
        assertTrue("Group did not expand", mListView.isGroupExpanded(0));
        assertTrue("Group did not expand", mExpandableListView.isGroupExpanded(0));
        
        // Insert a collapsed group in front of the one just expanded
        List<MyGroup> groups = mActivity.getGroups();
@@ -119,59 +117,28 @@ public class ExpandableListBasicTest extends ActivityInstrumentationTestCase2<Ex
        
        // Make sure the right group is expanded
        assertTrue("The expanded state didn't stay with the proper group",
                mListView.isGroupExpanded(1));
                mExpandableListView.isGroupExpanded(1));
        assertFalse("The expanded state was given to the inserted group",
                mListView.isGroupExpanded(0));
                mExpandableListView.isGroupExpanded(0));
    }

    // Static utility method, shared by different ExpandableListView scenario.
    static void checkGroupAndChildPositions(ExpandableListView elv,
            ActivityInstrumentationTestCase2<? extends ExpandableListScenario> activityInstrumentation) {
        // Add a position tester ContextMenu listener to the ExpandableListView
        PositionTesterContextMenuListener menuListener = new PositionTesterContextMenuListener();
        elv.setOnCreateContextMenuListener(menuListener);

        ListUtil listUtil = new ListUtil(elv, activityInstrumentation.getInstrumentation());
        ExpandableListAdapter adapter = elv.getExpandableListAdapter();
        Instrumentation instrumentation = activityInstrumentation.getInstrumentation();

        int index = elv.getHeaderViewsCount();
        int groupCount = adapter.getGroupCount();
        for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {

            // Expand group
            assertFalse("Group is already expanded", elv.isGroupExpanded(groupIndex));
            listUtil.arrowScrollToSelectedPosition(index);
            instrumentation.waitForIdleSync();
            activityInstrumentation.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
            activityInstrumentation.getInstrumentation().waitForIdleSync();
            assertTrue("Group did not expand " + groupIndex, elv.isGroupExpanded(groupIndex));

            // Check group index in context menu
            menuListener.expectGroupContextMenu(groupIndex);
            // Make sure the group is visible so that getChild finds it
            listUtil.arrowScrollToSelectedPosition(index);
            View groupChild = elv.getChildAt(index - elv.getFirstVisiblePosition());
            elv.showContextMenuForChild(groupChild);
            index++;

            final int childrenCount = adapter.getChildrenCount(groupIndex);
            for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
                // Check child index in context menu
                listUtil.arrowScrollToSelectedPosition(index);
                menuListener.expectChildContextMenu(groupIndex, childIndex);
                View child = elv.getChildAt(index - elv.getFirstVisiblePosition());
                elv.showContextMenuForChild(child);
                index++;
            }
    @MediumTest
    public void testGroupChildPositions() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testGroupAndChildPositions();
    }

        // Cleanup: remove the listener we added.
        elv.setOnCreateContextMenuListener(null);
    @MediumTest
    public void testConvertionBetweenFlatAndPacked() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testConvertionBetweenFlatAndPackedOnGroups();
        tester.testConvertionBetweenFlatAndPackedOnChildren();
    }

    @MediumTest
    public void testGroupChildPositions() {
        checkGroupAndChildPositions(mListView, this);
    public void testSelectedPosition() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testSelectedPositionOnGroups();
        tester.testSelectedPositionOnChildren();
    }
}
+233 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.widget.expandablelistview;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.util.ExpandableListScenario;
import android.util.ListUtil;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import junit.framework.Assert;

public class ExpandableListTester {
    private ExpandableListView mExpandableListView;
    private ExpandableListAdapter mAdapter;
    private ListUtil mListUtil;

    private ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
        mActivityInstrumentation;

    Instrumentation mInstrumentation;

    public ExpandableListTester(
            ExpandableListView expandableListView,
            ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
            activityInstrumentation) {
        mExpandableListView = expandableListView;
        Instrumentation instrumentation = activityInstrumentation.getInstrumentation();
        mListUtil = new ListUtil(mExpandableListView, instrumentation);
        mAdapter = mExpandableListView.getExpandableListAdapter();
        mActivityInstrumentation = activityInstrumentation;
        mInstrumentation = mActivityInstrumentation.getInstrumentation();
    }

    private void expandGroup(final int groupIndex, int flatPosition) {
        Assert.assertFalse("Group is already expanded", mExpandableListView
                .isGroupExpanded(groupIndex));
        mListUtil.arrowScrollToSelectedPosition(flatPosition);
        mInstrumentation.waitForIdleSync();
        mActivityInstrumentation.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
        mActivityInstrumentation.getInstrumentation().waitForIdleSync();
        Assert.assertTrue("Group did not expand " + groupIndex, mExpandableListView
                .isGroupExpanded(groupIndex));
    }

    void testGroupAndChildPositions() {
        // Add a position tester ContextMenu listener to the ExpandableListView
        PositionTesterContextMenuListener menuListener = new PositionTesterContextMenuListener();
        mExpandableListView.setOnCreateContextMenuListener(menuListener);

        int index = 0;

        // Scrolling on header elements should trigger an AdapterContextMenu
        for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
            // Check group index in context menu
            menuListener.expectAdapterContextMenu(i);
            // Make sure the group is visible so that getChild finds it
            mListUtil.arrowScrollToSelectedPosition(index);
            View headerChild = mExpandableListView.getChildAt(index
                    - mExpandableListView.getFirstVisiblePosition());
            mExpandableListView.showContextMenuForChild(headerChild);
            index++;
        }

        int groupCount = mAdapter.getGroupCount();
        for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {

            // Expand group
            expandGroup(groupIndex, index);

            // Check group index in context menu
            menuListener.expectGroupContextMenu(groupIndex);
            // Make sure the group is visible so that getChild finds it
            mListUtil.arrowScrollToSelectedPosition(index);
            View groupChild = mExpandableListView.getChildAt(index
                    - mExpandableListView.getFirstVisiblePosition());
            mExpandableListView.showContextMenuForChild(groupChild);
            index++;

            final int childrenCount = mAdapter.getChildrenCount(groupIndex);
            for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
                // Check child index in context menu
                mListUtil.arrowScrollToSelectedPosition(index);
                menuListener.expectChildContextMenu(groupIndex, childIndex);
                View child = mExpandableListView.getChildAt(index
                        - mExpandableListView.getFirstVisiblePosition());
                mExpandableListView.showContextMenuForChild(child);
                index++;
            }
        }

        // Scrolling on footer elements should trigger an AdapterContextMenu
        for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
            // Check group index in context menu
            menuListener.expectAdapterContextMenu(i+1);
            // Make sure the group is visible so that getChild finds it
            mListUtil.arrowScrollToSelectedPosition(index);
            View footerChild = mExpandableListView.getChildAt(index
                    - mExpandableListView.getFirstVisiblePosition());
            mExpandableListView.showContextMenuForChild(footerChild);
            index++;
        }

        // Cleanup: remove the listener we added.
        mExpandableListView.setOnCreateContextMenuListener(null);
    }

    private int expandAGroup() {
        final int groupIndex = 2;
        final int headerCount = mExpandableListView.getHeaderViewsCount();
        Assert.assertTrue("Not enough groups", groupIndex < mAdapter.getGroupCount());
        expandGroup(groupIndex, groupIndex + headerCount);
        return groupIndex;
    }

    // This method assumes that NO group is expanded when called
    void testConvertionBetweenFlatAndPackedOnGroups() {
        final int headerCount = mExpandableListView.getHeaderViewsCount();

        for (int i=0; i<headerCount; i++) {
            Assert.assertEquals("Non NULL position for header item",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL, mExpandableListView
                    .getExpandableListPosition(i));
        }

        // Test all (non expanded) groups
        final int groupCount = mAdapter.getGroupCount();
        for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
            int expectedFlatPosition = headerCount + groupIndex;
            long packedPositionForGroup = ExpandableListView.getPackedPositionForGroup(groupIndex);
            Assert.assertEquals("Group not found at flat position " + expectedFlatPosition,
                    packedPositionForGroup,
                    mExpandableListView.getExpandableListPosition(expectedFlatPosition));

            Assert.assertEquals("Wrong flat position for group " + groupIndex,
                    expectedFlatPosition,
                    mExpandableListView.getFlatListPosition(packedPositionForGroup));
        }

        for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
            Assert.assertEquals("Non NULL position for header item",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL,
                    mExpandableListView.getExpandableListPosition(headerCount + groupCount + i));
        }
    }

    // This method assumes that NO group is expanded when called
    void testConvertionBetweenFlatAndPackedOnChildren() {
        // Test with an expanded group
        final int headerCount = mExpandableListView.getHeaderViewsCount();
        final int groupIndex = expandAGroup();

        final int childrenCount = mAdapter.getChildrenCount(groupIndex);
        for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
            int expectedFlatPosition = headerCount + groupIndex + 1 + childIndex;
            long childPos = ExpandableListView.getPackedPositionForChild(groupIndex, childIndex);

            Assert.assertEquals("Wrong flat position for child ",
                    childPos,
                    mExpandableListView.getExpandableListPosition(expectedFlatPosition));

            Assert.assertEquals("Wrong flat position for child ",
                    expectedFlatPosition,
                    mExpandableListView.getFlatListPosition(childPos));
        }
    }

    // This method assumes that NO group is expanded when called
    void testSelectedPositionOnGroups() {
        int index = 0;

        // Scrolling on header elements should not give a valid selected position.
        for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
            mListUtil.arrowScrollToSelectedPosition(index);
            Assert.assertEquals("Header item is selected",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL,
                    mExpandableListView.getSelectedPosition());
            index++;
        }

        // Check selection on group items
        final int groupCount = mAdapter.getGroupCount();
        for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
            mListUtil.arrowScrollToSelectedPosition(index);
            Assert.assertEquals("Group item is not selected",
                    ExpandableListView.getPackedPositionForGroup(groupIndex),
                    mExpandableListView.getSelectedPosition());
            index++;
        }

        // Scrolling on footer elements should not give a valid selected position.
        for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
            mListUtil.arrowScrollToSelectedPosition(index);
            Assert.assertEquals("Footer item is selected",
                    ExpandableListView.PACKED_POSITION_TYPE_NULL,
                    mExpandableListView.getSelectedPosition());
            index++;
        }
    }

    // This method assumes that NO group is expanded when called
    void testSelectedPositionOnChildren() {
        // Test with an expanded group
        final int headerCount = mExpandableListView.getHeaderViewsCount();
        final int groupIndex = expandAGroup();

        final int childrenCount = mAdapter.getChildrenCount(groupIndex);
        for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
            int childFlatPosition = headerCount + groupIndex + 1 + childIndex;
            mListUtil.arrowScrollToSelectedPosition(childFlatPosition);
            Assert.assertEquals("Group item is not selected",
                    ExpandableListView.getPackedPositionForChild(groupIndex, childIndex),
                    mExpandableListView.getSelectedPosition());
        }
    }
}
+16 −1
Original line number Diff line number Diff line
@@ -64,6 +64,21 @@ public class ExpandableListWithHeadersTest extends

    @MediumTest
    public void testGroupChildPositions() {
        ExpandableListBasicTest.checkGroupAndChildPositions(mExpandableListView, this);
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testGroupAndChildPositions();
    }

    @MediumTest
    public void testConvertionBetweenFlatAndPacked() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testConvertionBetweenFlatAndPackedOnGroups();
        tester.testConvertionBetweenFlatAndPackedOnChildren();
    }

    @MediumTest
    public void testSelectedPosition() {
        ExpandableListTester tester = new ExpandableListTester(mExpandableListView, this);
        tester.testSelectedPositionOnGroups();
        tester.testSelectedPositionOnChildren();
    }
}
+32 −12
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.ExpandableListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import junit.framework.Assert;

@@ -12,6 +13,8 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis

    private int groupPosition, childPosition;

    // Fake constant to store in testType a test type specific to headers and footers
    private static final int ADAPTER_TYPE = -1;
    private int testType; // as returned by getPackedPositionType

    public void expectGroupContextMenu(int groupPosition) {
@@ -25,8 +28,24 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis
        testType = ExpandableListView.PACKED_POSITION_TYPE_CHILD;
    }

    public void expectAdapterContextMenu(int flatPosition) {
        this.groupPosition = flatPosition;
        testType = ADAPTER_TYPE;
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        ExpandableListView.ExpandableListContextMenuInfo elvMenuInfo = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
        if (testType == ADAPTER_TYPE) {
            Assert.assertTrue("MenuInfo is not an AdapterContextMenuInfo",
                    menuInfo instanceof AdapterContextMenuInfo);
            AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
            Assert.assertEquals("Wrong flat position",
                    groupPosition,
                    adapterContextMenuInfo.position);
        } else {
            Assert.assertTrue("MenuInfo is not an ExpandableListContextMenuInfo",
                    menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo);
            ExpandableListView.ExpandableListContextMenuInfo elvMenuInfo =
                (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
            long packedPosition = elvMenuInfo.packedPosition;

            int packedPositionType = ExpandableListView.getPackedPositionType(packedPosition);
@@ -36,8 +55,9 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis
            Assert.assertEquals("Wrong group position", groupPosition, packedPositionGroup);

            if (testType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int packedPositionChild = ExpandableListView.getPackedPositionChild(packedPosition);
            Assert.assertEquals("Wrong child position", childPosition, packedPositionChild);
                int packedPosChild = ExpandableListView.getPackedPositionChild(packedPosition);
                Assert.assertEquals("Wrong child position", childPosition, packedPosChild);
            }
        }
    }
}