From 13c9a1f4901d921f84501893bf5f0b637297132f Mon Sep 17 00:00:00 2001 From: Gilles Debunne Date: Thu, 25 Mar 2010 17:46:25 -0700 Subject: [PATCH] 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 7fbddb1db1beeac7c6762fb7a11612e348f6ff90 cherrypicked to froyo. http://b/issue?id=2525846 --- .../ExpandableListTester.java | 16 +++-- .../PositionTesterContextMenuListener.java | 58 ++++++++++++++----- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java index ad99ee80308de..dfb10fbe1cfdb 100644 --- a/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java +++ b/core/tests/coretests/src/android/widget/expandablelistview/ExpandableListTester.java @@ -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 + private final ActivityInstrumentationTestCase2 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++; } diff --git a/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java b/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java index b4820055b9181..2dbdff86a8beb 100644 --- a/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java +++ b/core/tests/coretests/src/android/widget/expandablelistview/PositionTesterContextMenuListener.java @@ -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; + } }