am e610a1e: AI 147559: CTS: Use longer timeouts to allow for class initi

Merge commit 'e610a1e958b9b9b2bf6d2f3d87aff38fd9e056a8' into donut

* commit 'e610a1e958b9b9b2bf6d2f3d87aff38fd9e056a8':
  AI 147559: CTS: Use longer timeouts to allow for class initialization delays
diff --git a/tools/host/src/com/android/cts/HostConfig.java b/tools/host/src/com/android/cts/HostConfig.java
index 9bfc5a9..e146fb9 100644
--- a/tools/host/src/com/android/cts/HostConfig.java
+++ b/tools/host/src/com/android/cts/HostConfig.java
@@ -36,6 +36,8 @@
 import javax.xml.parsers.ParserConfigurationException;
 
 import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
 /**
@@ -60,36 +62,36 @@
     static final String[] CTS_RESULT_RESOURCES = {"cts_result.xsl", "cts_result.css",
                                                   "logo.gif", "newrule-green.png"};
 
-    /** Default number of tests executed between reboots. */
-    private static final long MAX_TEST_COUNT_DEFAULT = 200;
-    /** Name of environment variable that can override MAX_TEST_COUNT_DEFAULT. */
-    private static final String MAX_TEST_ENV_VAR = "CTS_RESTART_AFTER";
-    /** Number of tests executed between reboots. A value <= 0 disables reboots. */
-    private static final long MAX_TEST_COUNT; // set in static initializer
-
+    static final String MAX_TEST_COUNT_NAME = "maxTestCount";
+    static final String TEST_STATUS_TIMEOUT_MS = "testStatusTimeoutMs";
+    static final String BATCH_START_TIMEOUT_MS = "batchStartTimeoutMs";
+    static final String INDIVIDUAL_START_TIMEOUT_MS = "individualStartTimeoutMs";
+    
     private String mConfigRoot;
     private CaseRepository mCaseRepos;
     private Repository mResultRepos;
     private PlanRepository mPlanRepos;
     private HashMap<String, TestPackage> mTestPackageMap;
+    private HashMap<String, Integer> mIntValues;
 
-    static {
-        long maxTestCount = MAX_TEST_COUNT_DEFAULT;
-        String prop = System.getenv(MAX_TEST_ENV_VAR);
-        if (prop != null) {
-            try {
-                maxTestCount = Long.parseLong(prop);
-            } catch (NumberFormatException ignored) {
-                // just use default value
-            }
-        }
-        MAX_TEST_COUNT = maxTestCount;
+    private void setDefaultConfigValues() {
+        mIntValues = new HashMap<String, Integer>();
+        
+        // Number of tests executed between reboots. A value <= 0 disables reboots.
+        mIntValues.put(MAX_TEST_COUNT_NAME, 200);
+        // Max time [ms] between test status updates for both individual and batch mode.
+        mIntValues.put(TEST_STATUS_TIMEOUT_MS, 60 * 1000);
+        // Max time [ms] from start of package in batch mode and the first test status update.
+        mIntValues.put(BATCH_START_TIMEOUT_MS, 30 * 60 * 1000);
+        // Max time [ms] from start of test in individual mode to the first test status update.
+        mIntValues.put(INDIVIDUAL_START_TIMEOUT_MS, 5 * 60 * 1000);
     }
-
+    
     private final static HostConfig sInstance = new HostConfig();
 
     private HostConfig() {
         mTestPackageMap = new HashMap<String, TestPackage>();
+        setDefaultConfigValues();
     }
 
     public static HostConfig getInstance() {
@@ -100,8 +102,23 @@
      * Returns the max number of tests to run between reboots. A value of 0 or smaller indicates
      * that reboots should not be used.
      */
-    public static long getMaxTestCount() {
-        return MAX_TEST_COUNT;
+    public static int getMaxTestCount() {
+        return sInstance.mIntValues.get(MAX_TEST_COUNT_NAME);
+    }
+    
+    /**
+     * Get the integer configuration value with the given name.
+     * 
+     * @param name The name of the value to read.
+     * @return The value, if it exists, otherwise 0.
+     */
+    public static int getIntValue(String name) {
+        Integer value = sInstance.mIntValues.get(name);
+        if (value == null) {
+            Log.e("Unknown config value read: " + name, null);
+            return 0;
+        }
+        return value;
     }
 
     /**
@@ -150,6 +167,8 @@
             return false;
         }
 
+        getConfigValues(doc);
+        
         String caseRoot = repositoryRoot + File.separator + caseCfg;
         String planRoot = repositoryRoot + File.separator + planCfg;
         String resRoot = repositoryRoot + File.separator + resCfg;
@@ -285,6 +304,26 @@
 
         return cfgStr;
     }
+    
+    /**
+     * Load configuration values from config file.
+     * 
+     * @param doc The document from which to load the values.
+     */
+    private void getConfigValues(final Document doc) {
+        NodeList intValues = doc.getElementsByTagName("IntValue");
+        for (int i = 0; i < intValues.getLength(); i++) {
+            Node n = intValues.item(i);
+            String name = getStringAttributeValue(n, "name");
+            String value = getStringAttributeValue(n, "value");
+            try {
+                Integer v = Integer.parseInt(value);
+                mIntValues.put(name, v);
+            } catch (NumberFormatException e) {
+                Log.e("Configuration error. Illegal value for " + name, e);
+            }
+        }
+    }
 
     /**
      * Validate the directory.
diff --git a/tools/host/src/com/android/cts/Test.java b/tools/host/src/com/android/cts/Test.java
index ecfa501..57c43da 100644
--- a/tools/host/src/com/android/cts/Test.java
+++ b/tools/host/src/com/android/cts/Test.java
@@ -256,8 +256,6 @@
      * running test.
      */
     class TimeOutTask extends TimerTask {
-        protected final static int DELAY = 60000;
-
         private Test mTest;
 
         public TimeOutTask(final Test testResult) {
@@ -322,7 +320,8 @@
 
         mTestStop = false;
         mDevice = device;
-        mTimeOutTimer = new HostTimer(new TimeOutTask(this), TimeOutTask.DELAY);
+        mTimeOutTimer = new HostTimer(new TimeOutTask(this),
+                HostConfig.getIntValue(HostConfig.INDIVIDUAL_START_TIMEOUT_MS));
         mTimeOutTimer.start();
         mProgressObserver = new ProgressObserver();
         mProgressObserver.start();
diff --git a/tools/host/src/com/android/cts/TestPackage.java b/tools/host/src/com/android/cts/TestPackage.java
index a67a88c..d119eff 100644
--- a/tools/host/src/com/android/cts/TestPackage.java
+++ b/tools/host/src/com/android/cts/TestPackage.java
@@ -517,7 +517,8 @@
                 mCurrentTest = null;
             }
             // restart the timer even for unexpected tests
-            mTimeOutTimer.restart(new TimeOutTask(this), TimeOutTask.DELAY);
+            mTimeOutTimer.restart(new TimeOutTask(this), 
+                    HostConfig.getIntValue(HostConfig.TEST_STATUS_TIMEOUT_MS));
         }
     }
 
@@ -817,7 +818,8 @@
      */
     private void runInBatchMode(final String javaPkgName)
             throws DeviceDisconnectedException {
-        mTimeOutTimer = new HostTimer(new TimeOutTask(this), TimeOutTask.DELAY);
+        mTimeOutTimer = new HostTimer(new TimeOutTask(this),
+                HostConfig.getIntValue(HostConfig.BATCH_START_TIMEOUT_MS));
         mTimeOutTimer.start();
         mProgressObserver = new ProgressObserver();
 
@@ -879,8 +881,6 @@
      * of the running package.
      */
     class TimeOutTask extends TimerTask {
-        private final static int DELAY = 60000;
-
         private TestPackage mTestPackage;
 
         public TimeOutTask(final TestPackage testPackage) {
diff --git a/tools/host/src/com/android/cts/TestSessionLog.java b/tools/host/src/com/android/cts/TestSessionLog.java
index 9e097e9..acb155d 100644
--- a/tools/host/src/com/android/cts/TestSessionLog.java
+++ b/tools/host/src/com/android/cts/TestSessionLog.java
@@ -22,16 +22,12 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.ProcessingInstruction;
 
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Date;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import java.util.zip.ZipOutputStream;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 
diff --git a/tools/utils/host_config.xml b/tools/utils/host_config.xml
index 682dfb3..02b232e 100644
--- a/tools/utils/host_config.xml
+++ b/tools/utils/host_config.xml
@@ -23,4 +23,14 @@
         <!-- Specific OEM test result directory (optional) -->
         <TestResult path="results" />
     </Repository>
+
+    <!-- Number of tests executed between reboots. A value <= 0 disables reboots. -->
+    <IntValue name="maxTestCount" value="200" />
+
+    <!-- Max time [ms] between test status updates. -->
+    <IntValue name="testStatusTimeoutMs" value="60000" />
+    <!-- Max time [ms] from start of package in batch mode and the first test status update. -->
+    <IntValue name="batchStartTimeoutMs" value="1800000" />
+    <!-- Max time [ms] from start of test in individual mode to the first test status update. -->
+    <IntValue name="individualStartTimeoutMs" value="300000" />
 </HostConfiguration>