views:

23

answers:

0

Hi,

Can someone tell me how to write an Android PerformanceTestCase using Intermediates? I got the code snippet below for my activity and execute it via the adb shell:

adb shell am instrument -w -e perf true org.foo.test/android.test.InstrumentationTestRunner

My code:

public class PerformanceTest extends ActivityInstrumentationTestCase2<AboutActivity> implements PerformanceTestCase {

private static final int ITERATIONS = 1000;

AboutActivity activity;

Intermediates i;

public PerformanceTest() {
    super("org.foo", AboutActivity.class);
}

@Override
public boolean isPerformanceOnly() {
    return true;
}

@Override
public int startPerformance(Intermediates intermediates) {
    intermediates.setInternalIterations(ITERATIONS * 10);
    i = intermediates;
    return 1;
}

@Override
protected void setUp() throws Exception {
    super.setUp();

    activity = getActivity();
}

public void startTiming(boolean realTime) {
    if (i != null) {
        i.startTiming(realTime);
    }
}

public void finishTiming(boolean realTime) {
    if (i != null) {
        i.finishTiming(realTime);
    }
}

public void addIntermediate(String name) {
    if (i != null) {
        i.addIntermediate(name);
    }
}

public void addIntermediate(String name, long timeInNS) {
    if (i != null) {
        i.addIntermediate(name, timeInNS);
    }
}

public void testSomething() {
    startTiming(true);
    Button b = (Button) activity.findViewById(org.foo.R.id.ButtonWikipedia);

    assertEquals(getActivity().getText(org.foo.R.string.about_button_wikipedia), b.getText().toString());
    finishTiming(true);
}

}

Somehow the startPerformance method is never entered, so I get no timings. I already looked at the LaunchpadActivity test in the android sources (com.android.unit_tests.activity), but still can't find an answer... Am I missing something?

Is there also a way to execute a performance test from Eclipse ADT? I only saw the 'perf' option for the adb shell.

--Martin