import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
/**
* Parses the 'raw output mode' results of an instrumentation test run from shell and informs a
/** The number of tests expected to run */
private int mNumTestsExpected = 0;
+ /** True if the parser is parsing a line beginning with "INSTRUMENTATION_RESULT" */
+ private boolean mInInstrumentationResultKey = false;
+
+ /**
+ * Stores key-value pairs under INSTRUMENTATION_RESULT header, these are printed at the
+ * end of a test run, if applicable
+ */
+ private Map<String, String> mInstrumentationResultBundle = new HashMap<String, String>();
+
private static final String LOG_TAG = "InstrumentationResultParser";
/** Error message supplied when no parseable test results are received from test run. */
if (line.startsWith(Prefixes.STATUS_CODE)) {
// Previous status key-value has been collected. Store it.
submitCurrentKeyValue();
+ mInInstrumentationResultKey = false;
parseStatusCode(line);
} else if (line.startsWith(Prefixes.STATUS)) {
// Previous status key-value has been collected. Store it.
submitCurrentKeyValue();
+ mInInstrumentationResultKey = false;
parseKey(line, Prefixes.STATUS.length());
} else if (line.startsWith(Prefixes.RESULT)) {
// Previous status key-value has been collected. Store it.
submitCurrentKeyValue();
+ mInInstrumentationResultKey = true;
parseKey(line, Prefixes.RESULT.length());
} else if (line.startsWith(Prefixes.STATUS_FAILED) ||
line.startsWith(Prefixes.CODE)) {
// Previous status key-value has been collected. Store it.
submitCurrentKeyValue();
+ mInInstrumentationResultKey = false;
// these codes signal the end of the instrumentation run
mTestRunFinished = true;
// just ignore the remaining data on this line
*/
private void submitCurrentKeyValue() {
if (mCurrentKey != null && mCurrentValue != null) {
- TestResult testInfo = getCurrentTestInfo();
- String statusValue = mCurrentValue.toString();
-
- if (mCurrentKey.equals(StatusKeys.CLASS)) {
- testInfo.mTestClass = statusValue.trim();
- } else if (mCurrentKey.equals(StatusKeys.TEST)) {
- testInfo.mTestName = statusValue.trim();
- } else if (mCurrentKey.equals(StatusKeys.NUMTESTS)) {
- try {
- testInfo.mNumTests = Integer.parseInt(statusValue);
- } catch (NumberFormatException e) {
- Log.e(LOG_TAG, "Unexpected integer number of tests, received " + statusValue);
+ if (mInInstrumentationResultKey) {
+ String statusValue = mCurrentValue.toString();
+ mInstrumentationResultBundle.put(mCurrentKey, statusValue);
+ if (mCurrentKey.equals(StatusKeys.SHORTMSG)) {
+ // test run must have failed
+ handleTestRunFailed(statusValue);
+ }
+ } else {
+ TestResult testInfo = getCurrentTestInfo();
+ String statusValue = mCurrentValue.toString();
+
+ if (mCurrentKey.equals(StatusKeys.CLASS)) {
+ testInfo.mTestClass = statusValue.trim();
+ } else if (mCurrentKey.equals(StatusKeys.TEST)) {
+ testInfo.mTestName = statusValue.trim();
+ } else if (mCurrentKey.equals(StatusKeys.NUMTESTS)) {
+ try {
+ testInfo.mNumTests = Integer.parseInt(statusValue);
+ } catch (NumberFormatException e) {
+ Log.e(LOG_TAG, "Unexpected integer number of tests, received "
+ + statusValue);
+ }
+ } else if (mCurrentKey.equals(StatusKeys.ERROR)) {
+ // test run must have failed
+ handleTestRunFailed(statusValue);
+ } else if (mCurrentKey.equals(StatusKeys.STACK)) {
+ testInfo.mStackTrace = statusValue;
}
- } else if (mCurrentKey.equals(StatusKeys.ERROR) ||
- mCurrentKey.equals(StatusKeys.SHORTMSG)) {
- // test run must have failed
- handleTestRunFailed(statusValue);
- } else if (mCurrentKey.equals(StatusKeys.STACK)) {
- testInfo.mStackTrace = statusValue;
}
mCurrentKey = null;
// no tests
listener.testRunStarted(0);
}
- listener.testRunEnded(mTestTime);
+ listener.testRunEnded(mTestTime, mInstrumentationResultBundle);
}
}
}
import junit.framework.TestCase;
+import java.util.Map;
+
/**
* Tests InstrumentationResultParser.
}
/**
+ * Test parsing of a test run that produces INSTRUMENTATION_RESULT output. This mimics launch
+ * performance test output.
+ */
+ public void testRunWithInstrumentationResults() {
+ StringBuilder output = new StringBuilder();
+ addResultKey(output, "other_pss", "2390");
+ addResultKey(output, "java_allocated", "2539");
+ addResultKey(output, "foo", "bar");
+ addLine(output, "INSTRUMENTATION_CODE: -1");
+
+ injectTestString(output.toString());
+
+ assertEquals("2390", mTestResult.mResultBundle.get("other_pss"));
+ assertEquals("2539", mTestResult.mResultBundle.get("java_allocated"));
+ assertEquals("bar", mTestResult.mResultBundle.get("foo"));
+ }
+
+ /**
* Builds a common test result using TEST_NAME and TEST_CLASS.
*/
private StringBuilder buildCommonResult() {
}
/**
+ * Helper method to add a result key value bundle.
+ */
+ private void addResultKey(StringBuilder outputBuilder, String key,
+ String value) {
+ outputBuilder.append("INSTRUMENTATION_RESULT: ");
+ outputBuilder.append(key);
+ outputBuilder.append('=');
+ outputBuilder.append(value);
+ addLineBreak(outputBuilder);
+ }
+
+ /**
* Append a line to output.
*/
private void addLine(StringBuilder outputBuilder, String lineContent) {
boolean mStopped;
/** stores the error message provided to testRunFailed */
String mRunFailedMessage;
+ Map<String, String> mResultBundle;
VerifyingTestResult() {
mNumTestsRun = 0;
mTestStatus = null;
mStopped = false;
mRunFailedMessage = null;
+ mResultBundle = null;
}
public void testEnded(TestIdentifier test) {
assertEquals("Unexpected test ended", mTestName, test.getTestName());
}
- public void testRunEnded(long elapsedTime) {
+ public void testRunEnded(long elapsedTime, Map<String, String> resultBundle) {
mTestTime = elapsedTime;
-
+ mResultBundle = resultBundle;
}
public void testRunStarted(int testCount) {