AI 143264: am: CL 143117 CTS: Make device restart configurable through environment variable CTS_RESTART_AFTER
  Original author: phillipd
  Merged from: //branches/cupcake/...

Automated import of CL 143264
diff --git a/tools/host/src/com/android/cts/HostConfig.java b/tools/host/src/com/android/cts/HostConfig.java
index 5625840..8a45526 100644
--- a/tools/host/src/com/android/cts/HostConfig.java
+++ b/tools/host/src/com/android/cts/HostConfig.java
@@ -60,12 +60,32 @@
     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 = 500;
+    /** 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
+    
     private String mConfigRoot;
     private CaseRepository mCaseRepos;
     private Repository mResultRepos;
     private PlanRepository mPlanRepos;
     private HashMap<String, TestPackage> mTestPackageMap;
 
+    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 final static HostConfig sInstance = new HostConfig();
 
     private HostConfig() {
@@ -75,6 +95,14 @@
     public static HostConfig getInstance() {
         return sInstance;
     }
+    
+    /**
+     * 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;
+    }
 
     /**
      * Load configuration.
diff --git a/tools/host/src/com/android/cts/TestHost.java b/tools/host/src/com/android/cts/TestHost.java
index a67c8f0..3b5aa76 100644
--- a/tools/host/src/com/android/cts/TestHost.java
+++ b/tools/host/src/com/android/cts/TestHost.java
@@ -488,7 +488,9 @@
         }
 
         TestSession.resetADBServerRestartedMode();
-        sDeviceManager.resetTestDevice(device);
+        if (HostConfig.getMaxTestCount() > 0) {
+            sDeviceManager.resetTestDevice(device);
+        }
     }
 
     /**
diff --git a/tools/host/src/com/android/cts/TestSession.java b/tools/host/src/com/android/cts/TestSession.java
index f994066..c28b4e9 100644
--- a/tools/host/src/com/android/cts/TestSession.java
+++ b/tools/host/src/com/android/cts/TestSession.java
@@ -45,7 +45,7 @@
     private boolean mNeedRestartAdbServer;
     private static boolean mADBServerRestartedMode;
 
-    private static final long MAX_TEST_COUNT = 500;
+    /** Running count of tests executed since last reboot. */
     private static long mTestCount;
 
     public TestSession(final TestSessionLog sessionLog,
@@ -109,12 +109,14 @@
     }
 
     /**
-     * Check if the test count exceeds the max test count.
+     * Check if the test count exceeds the max test count. If the max test count is disabled
+     * (HostConfig.getMaxTestCount() <= 0), this method always returns false.
      *
-     * @return If reached, return true; else, return false.
+     * @return true, if the max count is enabled and exceeded.
      */
     public static boolean exceedsMaxCount() {
-        return mTestCount >= MAX_TEST_COUNT;
+        final long maxTestCount = HostConfig.getMaxTestCount();
+        return (maxTestCount > 0) && (mTestCount >= maxTestCount);
     }
 
     /**
@@ -214,7 +216,7 @@
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
-        if (mNeedRestartAdbServer) {
+        if (mNeedRestartAdbServer && HostConfig.getMaxTestCount() > 0) {
             throw new ADBServerNeedRestartException("Need restart ADB server");
         }
     }