Question

I found an issue when I am doing CTS R12 test for Android 2.3.x. During the media stress test, the cases all failed because of file exception. It is caused by the static variable "FILE_PATH" is null during the test case. I found it is 100% reproducible on NexusOne/NexusS with Android 2.3.6.

I also write an very simple test project to test it, code attached below.

The activity code:

package com.hw.hello;

import android.app.Activity;
import android.os.Bundle;

public class HelloActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

The test case code:

package com.hw.hello.test;

import com.hw.hello.HelloActivity;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

public class HelloTest extends ActivityInstrumentationTestCase2<HelloActivity> {

    private static final String STR;

    static {
        STR = "XXXXXX";
    }

    public HelloTest() {
        super("com.hw.hello", HelloActivity.class);
    }

    @Override
    public void setUp() {

    }

    public void test1() {
        Log.d("111111", "STR="+STR);
    }

    public void test2() {
        Log.d("222222", "STR="+STR);
    }
}

When you run it, you will find the result is:

02-24 01:24:04.280: D/111111(28075): STR=XXXXXX

02-24 01:24:04.327: D/222222(28075): STR=null

I know Google has fixed this on ICS. But I found that the change of Dalvik VM is to much to merge to 2.3.7. What can I do to fix this on 2.3.7 to get through by CTS R12?

================================================================================

I cannot answer my question myself within 8 hours. So I have the answer here:

My French Colleague gave me the hint to find the final resolution: I have found some change in ICS source code of ActivityTestCase.java

The change is an added condition: && (field.getModifiers() & Modifier.FINAL) == 0

@Override
 protected void scrubClass(final Class<?> testCaseClass)
 throws IllegalAccessException {
     final Field[] fields = getClass().getDeclaredFields();
     for (Field field : fields) {
         final Class<?> fieldClass = field.getDeclaringClass();
         if (testCaseClass.isAssignableFrom(fieldClass) && !field.getType().isPrimitive()
                 && (field.getModifiers() & Modifier.FINAL) == 0) {
             try {
                 field.setAccessible(true);
                 field.set(this, null);
             } catch (Exception e) {
                 android.util.Log.d("TestCase", "Error: Could not nullify field!");
             }

             if (field.get(this) != null) {
                 android.util.Log.d("TestCase", "Error: Could not nullify field!");
             }
         }
     }
 }

I put this section of code into my test case class to override the super class' method, the issue is fixed now.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top