Test summary cleanup.

Add support for parsing plan name for test summaries.
Store cts build id in the buildinfo.

Bug 5171576

Change-Id: I315d0f551676a2ba2f471826635e3fd2a95bc83e
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildHelper.java b/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildHelper.java
index 9bc0945..b1ace51 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildHelper.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildHelper.java
@@ -133,6 +133,16 @@
     }
 
     /**
+     * @return a {@link File} representing the test plan with given name. note: no attempt will be
+     * made to ensure the plan actually exists
+     * @throws FileNotFoundException if plans directory does not exist
+     */
+    public File getTestPlanFile(String planName) throws FileNotFoundException {
+        String ctsPlanRelativePath = String.format("%s.xml", planName);
+        return new File(getTestPlansDir(), ctsPlanRelativePath);
+    }
+
+    /**
      * Check the validity of the CTS build file system structure.
      * @throws FileNotFoundException if any major directories are missing
      */
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildProvider.java b/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildProvider.java
index ccb5ce2..2524281 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildProvider.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/build/CtsBuildProvider.java
@@ -32,6 +32,8 @@
     @Option(name="cts-install-path", description="the path to the cts installation to use")
     private String mCtsRootDirPath = System.getProperty("CTS_ROOT");
 
+    public static final String CTS_BUILD_VERSION = "ICS_tradefed";
+
     /**
      * {@inheritDoc}
      */
@@ -39,7 +41,7 @@
         if (mCtsRootDirPath == null) {
             throw new IllegalArgumentException("Missing --cts-install-path");
         }
-        IFolderBuildInfo ctsBuild = new FolderBuildInfo("0", "cts", "cts");
+        IFolderBuildInfo ctsBuild = new FolderBuildInfo(CTS_BUILD_VERSION, "cts", "cts");
         ctsBuild.setRootDir(new File(mCtsRootDirPath));
         return ctsBuild;
     }
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java b/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java
index 8394df3..b49dead 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/result/CtsXmlResultReporter.java
@@ -17,6 +17,7 @@
 package com.android.cts.tradefed.result;
 
 import com.android.cts.tradefed.build.CtsBuildHelper;
+import com.android.cts.tradefed.build.CtsBuildProvider;
 import com.android.cts.tradefed.device.DeviceInfoCollector;
 import com.android.cts.tradefed.testtype.CtsTest;
 import com.android.ddmlib.Log;
@@ -58,11 +59,12 @@
  */
 public class CtsXmlResultReporter extends CollectingTestListener {
 
+
+
     private static final String LOG_TAG = "CtsXmlResultReporter";
 
     static final String TEST_RESULT_FILE_NAME = "testResult.xml";
     private static final String CTS_RESULT_FILE_VERSION = "1.11";
-    private static final String CTS_VERSION = "ICS_tradefed";
     private static final String[] CTS_RESULT_RESOURCES = {"cts_result.xsl", "cts_result.css",
         "logo.gif", "newrule-green.png"};
 
@@ -75,6 +77,8 @@
     static final String TIMEOUT_ATTR = "timeout";
     static final String NOT_EXECUTED_ATTR = "notExecuted";
     static final String FAILED_ATTR = "failed";
+    static final String RESULT_TAG = "TestResult";
+    static final String PLAN_ATTR = "testPlan";
 
     private static final String REPORT_DIR_NAME = "output-file-path";
     @Option(name=REPORT_DIR_NAME, description="root file system path to directory to store xml " +
@@ -179,7 +183,9 @@
     @Override
     public void invocationEnded(long elapsedTime) {
         // display the results of the last completed run
-        logCompleteRun(getCurrentRunResults());
+        if (getCurrentRunResults().isRunComplete()) {
+            logCompleteRun(getCurrentRunResults());
+        }
         super.invocationEnded(elapsedTime);
         createXmlResult(mReportDir, mStartTime, elapsedTime);
         copyFormattingFiles(mReportDir);
@@ -246,8 +252,8 @@
      */
     private void serializeResultsDoc(KXmlSerializer serializer, String startTime, String endTime)
             throws IOException {
-        serializer.startTag(ns, "TestResult");
-        serializer.attribute(ns, "testPlan", mPlanName);
+        serializer.startTag(ns, RESULT_TAG);
+        serializer.attribute(ns, PLAN_ATTR, mPlanName);
         serializer.attribute(ns, "starttime", startTime);
         serializer.attribute(ns, "endtime", endTime);
         serializer.attribute(ns, "version", CTS_RESULT_FILE_VERSION);
@@ -256,6 +262,7 @@
         serializeHostInfo(serializer);
         serializeTestSummary(serializer);
         serializeTestResults(serializer);
+        serializer.endTag(ns, RESULT_TAG);
     }
 
     /**
@@ -423,7 +430,7 @@
         serializer.endTag(ns, "Java");
 
         serializer.startTag(ns, "Cts");
-        serializer.attribute(ns, "version", CTS_VERSION);
+        serializer.attribute(ns, "version", CtsBuildProvider.CTS_BUILD_VERSION);
         // TODO: consider outputting other tradefed options here
         serializer.startTag(ns, "IntValue");
         serializer.attribute(ns, "name", "testStatusTimeoutMs");
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/result/ITestSummary.java b/tools/tradefed-host/src/com/android/cts/tradefed/result/ITestSummary.java
index a2f59ea..ee7b67f 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/result/ITestSummary.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/result/ITestSummary.java
@@ -45,4 +45,9 @@
      */
     int getNumPassed();
 
+    /**
+     * @return the test plan associated with result
+     */
+    String getTestPlan();
+
 }
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/result/TestSummaryXml.java b/tools/tradefed-host/src/com/android/cts/tradefed/result/TestSummaryXml.java
index c7a5d6e..b91a391 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/result/TestSummaryXml.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/result/TestSummaryXml.java
@@ -19,22 +19,21 @@
 
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
-import org.xmlpull.v1.XmlPullParserFactory;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.io.Reader;
 
 /**
  * A {@link ITestSummary} that parses summary data from the CTS result XML.
  */
-public class TestSummaryXml implements ITestSummary  {
+public class TestSummaryXml extends AbstractXmlPullParser implements ITestSummary  {
 
     private final int mId;
     private final String mTimestamp;
     private int mNumFailed = 0;
     private int mNumNotExecuted = 0;
     private int mNumPassed = 0;
+    private String mPlan = "NA";
 
     /**
      * @param id
@@ -88,57 +87,32 @@
     }
 
     /**
-     * Parse the summary data from the given input data.
-     *
-     * @param xmlReader the input XML
-     * @throws ParseException if failed to parse the summary data.
+     * {@inheritDoc}
      */
-    public void parse(Reader xmlReader) throws ParseException {
-        try {
-            XmlPullParserFactory fact = org.xmlpull.v1.XmlPullParserFactory.newInstance();
-            XmlPullParser parser = fact.newPullParser();
-            parser.setInput (xmlReader);
-            parseSummary(parser);
-        } catch (XmlPullParserException e) {
-           throw new ParseException(e);
-        } catch (IOException e) {
-            throw new ParseException(e);
-        }
+    @Override
+    public String getTestPlan() {
+        return mPlan ;
     }
 
-    private void parseSummary(XmlPullParser parser) throws XmlPullParserException, IOException {
+
+    @Override
+    void parse(XmlPullParser parser) throws XmlPullParserException, IOException {
         int eventType = parser.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
             if (eventType == XmlPullParser.START_TAG && parser.getName().equals(
+                    CtsXmlResultReporter.RESULT_TAG)) {
+                mPlan = getAttribute(parser, CtsXmlResultReporter.PLAN_ATTR);
+            } else if (eventType == XmlPullParser.START_TAG && parser.getName().equals(
                     CtsXmlResultReporter.SUMMARY_TAG)) {
                 mNumFailed = parseIntAttr(parser, CtsXmlResultReporter.FAILED_ATTR) +
-                        parseIntAttr(parser, CtsXmlResultReporter.TIMEOUT_ATTR);
+                    parseIntAttr(parser, CtsXmlResultReporter.TIMEOUT_ATTR);
                 mNumNotExecuted = parseIntAttr(parser, CtsXmlResultReporter.NOT_EXECUTED_ATTR);
                 mNumPassed = parseIntAttr(parser, CtsXmlResultReporter.PASS_ATTR);
                 return;
               }
-            eventType = parser.nextTag();
+            eventType = parser.next();
         }
         throw new XmlPullParserException("Could not find Summary tag");
     }
-
-    /**
-     * Parse an integer value from an XML attribute
-     *
-     * @param parser the {@link XmlPullParser}
-     * @param name the attribute name
-     * @return the parsed value or 0 if it could not be parsed
-     */
-    private int parseIntAttr(XmlPullParser parser, String name) {
-        try {
-            String value = parser.getAttributeValue(null, name);
-            if (value != null) {
-                return Integer.parseInt(value);
-            }
-        } catch (NumberFormatException e) {
-            // ignore
-        }
-        return 0;
-    }
 }