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

Commit 13c9a1f4 authored by Gilles Debunne's avatar Gilles Debunne
Browse files

Refactor in PositionTesterContextMenuListener.

The asserts were removed from that class and replaced by a status String object.

This allows ExpandableListTester to do the asserts instead.
These tests passed on a sapphire and passion devices as well as in the emulator.
The asserts in the main thread are expected to make these tests pass during the
continuous build too.

This is 7fbddb1d cherrypicked to froyo.

http://b/issue?id=2525846
parent 33eb7767
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -28,11 +28,11 @@ import android.widget.ExpandableListView;
import junit.framework.Assert;

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

    private ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
    private final ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
        mActivityInstrumentation;

    Instrumentation mInstrumentation;
@@ -76,6 +76,8 @@ public class ExpandableListTester {
            View headerChild = mExpandableListView.getChildAt(index
                    - mExpandableListView.getFirstVisiblePosition());
            mExpandableListView.showContextMenuForChild(headerChild);
            mInstrumentation.waitForIdleSync();
            Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
            index++;
        }

@@ -92,6 +94,8 @@ public class ExpandableListTester {
            View groupChild = mExpandableListView.getChildAt(index
                    - mExpandableListView.getFirstVisiblePosition());
            mExpandableListView.showContextMenuForChild(groupChild);
            mInstrumentation.waitForIdleSync();
            Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
            index++;

            final int childrenCount = mAdapter.getChildrenCount(groupIndex);
@@ -102,6 +106,8 @@ public class ExpandableListTester {
                View child = mExpandableListView.getChildAt(index
                        - mExpandableListView.getFirstVisiblePosition());
                mExpandableListView.showContextMenuForChild(child);
                mInstrumentation.waitForIdleSync();
                Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
                index++;
            }
        }
@@ -115,6 +121,8 @@ public class ExpandableListTester {
            View footerChild = mExpandableListView.getChildAt(index
                    - mExpandableListView.getFirstVisiblePosition());
            mExpandableListView.showContextMenuForChild(footerChild);
            mInstrumentation.waitForIdleSync();
            Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
            index++;
        }

+45 −13
Original line number Diff line number Diff line
@@ -23,8 +23,6 @@ import android.view.View.OnCreateContextMenuListener;
import android.widget.ExpandableListView;
import android.widget.AdapterView.AdapterContextMenuInfo;

import junit.framework.Assert;

public class PositionTesterContextMenuListener implements OnCreateContextMenuListener {

    private int groupPosition, childPosition;
@@ -33,6 +31,9 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis
    private static final int ADAPTER_TYPE = -1;
    private int testType; // as returned by getPackedPositionType

    // Will be set to null by each call to onCreateContextMenu, unless an error occurred. 
    private String errorMessage;

    public void expectGroupContextMenu(int groupPosition) {
        this.groupPosition = groupPosition;
        testType = ExpandableListView.PACKED_POSITION_TYPE_GROUP;
@@ -50,30 +51,61 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis
    }

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

            int packedPositionType = ExpandableListView.getPackedPositionType(packedPosition);
            Assert.assertEquals("Wrong packed position type", testType, packedPositionType);
            if (!areEqual("Wrong packed position type", testType, packedPositionType)) {
                return;
            }

            int packedPositionGroup = ExpandableListView.getPackedPositionGroup(packedPosition);
            Assert.assertEquals("Wrong group position", groupPosition, packedPositionGroup);
            if (!areEqual("Wrong group position", groupPosition, packedPositionGroup)) {
                return;
            }

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

    private boolean areEqual(String message, int expected, int actual) {
        if (expected != actual) {
            errorMessage = String.format(message + " (%d vs %d", expected, actual);
            return false;
        }
        return true;
    }

    private boolean isTrue(String message, boolean value) {
        if (!value) {
            errorMessage = message;
            return false;
        }
        return true;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}