Prevent ActivityTestCase from erasing constants.

If you test case is declaring a non-primitive constant,
this method would clear out the constant between
individual tests.

Change-Id: I570f2bbbbbb0a170bf63b7473b4aaf84869e93e0
This commit is contained in:
Dmitri Plotnikov
2011-01-07 12:06:47 -08:00
parent cd3676e7b8
commit ac77f4629c

View File

@@ -19,10 +19,11 @@ package android.test;
import android.app.Activity;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* This is common code used to support Activity test cases. For more useful classes, please see
* {@link android.test.ActivityUnitTestCase} and
* {@link android.test.ActivityUnitTestCase} and
* {@link android.test.ActivityInstrumentationTestCase}.
*/
public abstract class ActivityTestCase extends InstrumentationTestCase {
@@ -38,7 +39,7 @@ public abstract class ActivityTestCase extends InstrumentationTestCase {
protected Activity getActivity() {
return mActivity;
}
/**
* Set the activity under test.
* @param testActivity The activity under test
@@ -46,15 +47,15 @@ public abstract class ActivityTestCase extends InstrumentationTestCase {
protected void setActivity(Activity testActivity) {
mActivity = testActivity;
}
/**
* This function is called by various TestCase implementations, at tearDown() time, in order
* to scrub out any class variables. This protects against memory leaks in the case where a
* test case creates a non-static inner class (thus referencing the test case) and gives it to
* someone else to hold onto.
*
*
* @param testCaseClass The class of the derived TestCase implementation.
*
*
* @throws IllegalAccessException
*/
protected void scrubClass(final Class<?> testCaseClass)
@@ -62,7 +63,8 @@ public abstract class ActivityTestCase extends InstrumentationTestCase {
final Field[] fields = getClass().getDeclaredFields();
for (Field field : fields) {
final Class<?> fieldClass = field.getDeclaringClass();
if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()) {
if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
&& (field.getModifiers() & Modifier.FINAL) == 0) {
try {
field.setAccessible(true);
field.set(this, null);
@@ -77,6 +79,6 @@ public abstract class ActivityTestCase extends InstrumentationTestCase {
}
}
}