Merge "Set the per test timeout for the AJUR from CoreTestRunner" into nyc-dev
am: 76054d7dba

* commit '76054d7dba5fde1baff883cdb41e6a22681c4212':
  Set the per test timeout for the AJUR from CoreTestRunner

Change-Id: Ib1d9e344a5539d76c478d0adced8cd023b2a13ff
diff --git a/tests/core/runner/src/com/android/cts/core/runner/AndroidJUnitRunnerConstants.java b/tests/core/runner/src/com/android/cts/core/runner/AndroidJUnitRunnerConstants.java
index 8875c13..8bc2f01 100644
--- a/tests/core/runner/src/com/android/cts/core/runner/AndroidJUnitRunnerConstants.java
+++ b/tests/core/runner/src/com/android/cts/core/runner/AndroidJUnitRunnerConstants.java
@@ -92,6 +92,11 @@
     String ARGUMENT_COUNT = "count";
 
     /**
+     * The per test timeout value.
+     */
+    String ARGUMENT_TIMEOUT = "timeout_msec";
+
+    /**
      * Token representing how long (in seconds) the current test took to execute.
      *
      * <p>The equivalent constant in {@code InstrumentationTestRunner} is private.
diff --git a/tests/core/runner/src/com/android/cts/core/runner/CoreTestRunner.java b/tests/core/runner/src/com/android/cts/core/runner/CoreTestRunner.java
index 58f34c3..887762f 100644
--- a/tests/core/runner/src/com/android/cts/core/runner/CoreTestRunner.java
+++ b/tests/core/runner/src/com/android/cts/core/runner/CoreTestRunner.java
@@ -59,6 +59,7 @@
 import static com.android.cts.core.runner.AndroidJUnitRunnerConstants.ARGUMENT_TEST_CLASS;
 import static com.android.cts.core.runner.AndroidJUnitRunnerConstants.ARGUMENT_TEST_FILE;
 import static com.android.cts.core.runner.AndroidJUnitRunnerConstants.ARGUMENT_TEST_PACKAGE;
+import static com.android.cts.core.runner.AndroidJUnitRunnerConstants.ARGUMENT_TIMEOUT;
 
 /**
  * A drop-in replacement for AndroidJUnitTestRunner, which understands the same arguments, and has
@@ -86,6 +87,9 @@
     /** Only log the number of tests, and not run them. */
     private boolean logOnly;
 
+    /** The amount of time in millis to wait for a single test to complete. */
+    private long testTimeout;
+
     /**
      * The container for any test expectations.
      */
@@ -120,6 +124,7 @@
 
         this.logOnly = "true".equalsIgnoreCase(args.getString(ARGUMENT_LOG_ONLY));
         this.testCountOnly = args.getBoolean(ARGUMENT_COUNT);
+        this.testTimeout = parseUnsignedLong(args.getString(ARGUMENT_TIMEOUT), ARGUMENT_TIMEOUT);
 
         try {
             // Get the set of resource names containing the expectations.
@@ -228,7 +233,7 @@
         }
 
         AndroidRunnerParams runnerParams = new AndroidRunnerParams(this, args,
-                logOnly || testCountOnly, -1, false);
+                logOnly || testCountOnly, testTimeout, false /*ignoreSuiteMethods*/);
 
         JUnitCore core = new JUnitCore();
 
@@ -307,4 +312,22 @@
             throw err;
         }
     }
+
+    /**
+     * Parse long from given value - except either Long or String.
+     *
+     * @return the value, -1 if not found
+     * @throws NumberFormatException if value is negative or not a number
+     */
+    private static long parseUnsignedLong(Object value, String name) {
+        if (value != null) {
+            long longValue = Long.parseLong(value.toString());
+            if (longValue < 0) {
+                throw new NumberFormatException(name + " can not be negative");
+            }
+            return longValue;
+        }
+        return -1;
+    }
+
 }