Merge "Refactor in PositionTesterContextMenuListener." into froyo

This commit is contained in:
Gilles Debunne
2010-04-01 14:47:00 -07:00
committed by Android (Google) Code Review
2 changed files with 57 additions and 17 deletions

View File

@@ -28,11 +28,11 @@ import android.widget.ExpandableListView;
import junit.framework.Assert; import junit.framework.Assert;
public class ExpandableListTester { public class ExpandableListTester {
private ExpandableListView mExpandableListView; private final ExpandableListView mExpandableListView;
private ExpandableListAdapter mAdapter; private final ExpandableListAdapter mAdapter;
private ListUtil mListUtil; private final ListUtil mListUtil;
private ActivityInstrumentationTestCase2<? extends ExpandableListScenario> private final ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
mActivityInstrumentation; mActivityInstrumentation;
Instrumentation mInstrumentation; Instrumentation mInstrumentation;
@@ -76,6 +76,8 @@ public class ExpandableListTester {
View headerChild = mExpandableListView.getChildAt(index View headerChild = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(headerChild); mExpandableListView.showContextMenuForChild(headerChild);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++; index++;
} }
@@ -92,6 +94,8 @@ public class ExpandableListTester {
View groupChild = mExpandableListView.getChildAt(index View groupChild = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(groupChild); mExpandableListView.showContextMenuForChild(groupChild);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++; index++;
final int childrenCount = mAdapter.getChildrenCount(groupIndex); final int childrenCount = mAdapter.getChildrenCount(groupIndex);
@@ -102,6 +106,8 @@ public class ExpandableListTester {
View child = mExpandableListView.getChildAt(index View child = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(child); mExpandableListView.showContextMenuForChild(child);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++; index++;
} }
} }
@@ -115,6 +121,8 @@ public class ExpandableListTester {
View footerChild = mExpandableListView.getChildAt(index View footerChild = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition()); - mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(footerChild); mExpandableListView.showContextMenuForChild(footerChild);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++; index++;
} }

View File

@@ -23,8 +23,6 @@ import android.view.View.OnCreateContextMenuListener;
import android.widget.ExpandableListView; import android.widget.ExpandableListView;
import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.AdapterContextMenuInfo;
import junit.framework.Assert;
public class PositionTesterContextMenuListener implements OnCreateContextMenuListener { public class PositionTesterContextMenuListener implements OnCreateContextMenuListener {
private int groupPosition, childPosition; private int groupPosition, childPosition;
@@ -33,6 +31,9 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis
private static final int ADAPTER_TYPE = -1; private static final int ADAPTER_TYPE = -1;
private int testType; // as returned by getPackedPositionType 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) { public void expectGroupContextMenu(int groupPosition) {
this.groupPosition = groupPosition; this.groupPosition = groupPosition;
testType = ExpandableListView.PACKED_POSITION_TYPE_GROUP; testType = ExpandableListView.PACKED_POSITION_TYPE_GROUP;
@@ -50,30 +51,61 @@ public class PositionTesterContextMenuListener implements OnCreateContextMenuLis
} }
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
errorMessage = null;
if (testType == ADAPTER_TYPE) { if (testType == ADAPTER_TYPE) {
Assert.assertTrue("MenuInfo is not an AdapterContextMenuInfo", if (!isTrue("MenuInfo is not an AdapterContextMenuInfo",
menuInfo instanceof AdapterContextMenuInfo); menuInfo instanceof AdapterContextMenuInfo)) {
return;
}
AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo; AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
Assert.assertEquals("Wrong flat position", if (!areEqual("Wrong flat position", groupPosition, adapterContextMenuInfo.position)) {
groupPosition, return;
adapterContextMenuInfo.position); }
} else { } else {
Assert.assertTrue("MenuInfo is not an ExpandableListContextMenuInfo", if (!isTrue("MenuInfo is not an ExpandableListContextMenuInfo",
menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo); menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) {
return;
}
ExpandableListView.ExpandableListContextMenuInfo elvMenuInfo = ExpandableListView.ExpandableListContextMenuInfo elvMenuInfo =
(ExpandableListView.ExpandableListContextMenuInfo) menuInfo; (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
long packedPosition = elvMenuInfo.packedPosition; long packedPosition = elvMenuInfo.packedPosition;
int packedPositionType = ExpandableListView.getPackedPositionType(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); 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) { if (testType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int packedPosChild = ExpandableListView.getPackedPositionChild(packedPosition); int packedPositionChild = ExpandableListView.getPackedPositionChild(packedPosition);
Assert.assertEquals("Wrong child position", childPosition, packedPosChild); 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;
}
} }