Merge "Added simplified output to UiAutomator tests." into jb-mr1.1-dev
diff --git a/uiautomator/cmds/uiautomator/src/com/android/commands/uiautomator/RunTestCommand.java b/uiautomator/cmds/uiautomator/src/com/android/commands/uiautomator/RunTestCommand.java
index 935a581..7320207 100644
--- a/uiautomator/cmds/uiautomator/src/com/android/commands/uiautomator/RunTestCommand.java
+++ b/uiautomator/cmds/uiautomator/src/com/android/commands/uiautomator/RunTestCommand.java
@@ -17,11 +17,16 @@
 package com.android.commands.uiautomator;
 
 import android.os.Bundle;
+import android.util.Log;
 
 import com.android.commands.uiautomator.Launcher.Command;
 import com.android.uiautomator.testrunner.UiAutomatorTestRunner;
 
+import dalvik.system.DexFile;
+
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.List;
 
 /**
@@ -29,13 +34,16 @@
  *
  */
 public class RunTestCommand extends Command {
+    private static final String LOGTAG = RunTestCommand.class.getSimpleName();
 
     private static final String OUTPUT_SIMPLE = "simple";
     private static final String OUTPUT_FORMAT_KEY = "outputFormat";
     private static final String CLASS_PARAM = "class";
+    private static final String JARS_PARAM = "jars";
     private static final String DEBUG_PARAM = "debug";
     private static final String RUNNER_PARAM = "runner";
     private static final String CLASS_SEPARATOR = ",";
+    private static final String JARS_SEPARATOR = ":";
     private static final int ARG_OK = 0;
     private static final int ARG_FAIL_INCOMPLETE_E = -1;
     private static final int ARG_FAIL_INCOMPLETE_C = -2;
@@ -46,7 +54,8 @@
     private Bundle mParams = new Bundle();
     private List<String> mTestClasses = new ArrayList<String>();
     private boolean mDebug;
-    private String mRunner;
+    private String mRunnerClassName;
+    private UiAutomatorTestRunner mRunner;
 
     public RunTestCommand() {
         super("runtest");
@@ -72,8 +81,11 @@
                 break;
         }
         if (mTestClasses.isEmpty()) {
-            System.err.println("Please specify at least one test class to run.");
-            System.exit(ARG_FAIL_NO_CLASS);
+            addTestClassesFromJars();
+            if (mTestClasses.isEmpty()) {
+                System.err.println("No test classes found.");
+                System.exit(ARG_FAIL_NO_CLASS);
+            }
         }
         getRunner().run(mTestClasses, mParams, mDebug);
     }
@@ -99,7 +111,7 @@
                     } else if (DEBUG_PARAM.equals(key)) {
                         mDebug = "true".equals(value) || "1".equals(value);
                     } else if (RUNNER_PARAM.equals(key)) {
-                        mRunner = value;
+                        mRunnerClassName = value;
                     } else {
                         mParams.putString(key, value);
                     }
@@ -122,26 +134,32 @@
     }
 
     protected UiAutomatorTestRunner getRunner() {
-        if (mRunner == null) {
-            return new UiAutomatorTestRunner();
+        if (mRunner != null) {
+            return mRunner;
+        }
+
+        if (mRunnerClassName == null) {
+            mRunner = new UiAutomatorTestRunner();
+            return mRunner;
         }
         // use reflection to get the runner
         Object o = null;
         try {
-            Class<?> clazz = Class.forName(mRunner);
+            Class<?> clazz = Class.forName(mRunnerClassName);
             o = clazz.newInstance();
         } catch (ClassNotFoundException cnfe) {
-            System.err.println("Cannot find runner: " + mRunner);
+            System.err.println("Cannot find runner: " + mRunnerClassName);
             System.exit(ARG_FAIL_RUNNER);
         } catch (InstantiationException ie) {
-            System.err.println("Cannot instantiate runner: " + mRunner);
+            System.err.println("Cannot instantiate runner: " + mRunnerClassName);
             System.exit(ARG_FAIL_RUNNER);
         } catch (IllegalAccessException iae) {
-            System.err.println("Constructor of runner " + mRunner + " is not accessibile");
+            System.err.println("Constructor of runner " + mRunnerClassName + " is not accessibile");
             System.exit(ARG_FAIL_RUNNER);
         }
         try {
             UiAutomatorTestRunner runner = (UiAutomatorTestRunner)o;
+            mRunner = runner;
             return runner;
         } catch (ClassCastException cce) {
             System.err.println("Specified runner is not subclass of "
@@ -163,6 +181,49 @@
         }
     }
 
+    /**
+     * Add test classes from jars passed on the command line. Use this if nothing was explicitly
+     * specified on the command line.
+     */
+    private void addTestClassesFromJars() {
+        String jars = mParams.getString(JARS_PARAM);
+        if (jars == null) return;
+
+        String[] jarFileNames = jars.split(JARS_SEPARATOR);
+        for (String fileName : jarFileNames) {
+            fileName = fileName.trim();
+            if (fileName.isEmpty()) continue;
+            try {
+                DexFile dexFile = new DexFile(fileName);
+                for(Enumeration<String> e = dexFile.entries(); e.hasMoreElements();) {
+                    String className = e.nextElement();
+                    if (isTestClass(className)) {
+                        mTestClasses.add(className);
+                    }
+                }
+                dexFile.close();
+            } catch (IOException e) {
+                Log.w(LOGTAG, String.format("Could not read %s: %s", fileName, e.getMessage()));
+            }
+        }
+    }
+
+    /**
+     * Tries to determine if a given class is a test class. A test class has to inherit from
+     * UiAutomator test case and it must be a top-level class.
+     * @param className
+     * @return
+     */
+    private boolean isTestClass(String className) {
+        try {
+            Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
+            if (clazz.getEnclosingClass() != null) return false;
+            return getRunner().getTestCaseFilter().accept(clazz);
+        } catch (ClassNotFoundException e) {
+            return false;
+        }
+    }
+
     @Override
     public String detailedOptions() {
         return "    runtest <class spec> [options]\n"
@@ -173,7 +234,8 @@
             + "        specified, separated by space.\n"
             + "      <CLASSES>: a list of test class names to run, separated by comma. To\n"
             + "        a single method, use TestClass#testMethod format. The -e or -c option\n"
-            + "        may be repeated.\n"
+            + "        may be repeated. This option is not required and if not provided then\n"
+            + "        all the tests in provided jars will be run automatically.\n"
             + "    options:\n"
             + "      --nohup: trap SIG_HUP, so test won't terminate even if parent process\n"
             + "               is terminated, e.g. USB is disconnected.\n"
@@ -190,4 +252,4 @@
         return "executes UI automation tests";
     }
 
-}
\ No newline at end of file
+}
diff --git a/uiautomator/cmds/uiautomator/uiautomator b/uiautomator/cmds/uiautomator/uiautomator
index 30ff2e8..9aec2c4 100755
--- a/uiautomator/cmds/uiautomator/uiautomator
+++ b/uiautomator/cmds/uiautomator/uiautomator
@@ -61,6 +61,9 @@
 
 # eventually args will be what get passed down to Java code
 args=
+# we also pass the list of jar files, so we can extract class names for tests
+# if they are not explicitly specified
+jars=
 
 # special case pre-processing for 'runtest' command
 if [ "${cmd}" == "runtest" ]; then
@@ -87,7 +90,7 @@
       cmd="help"
       break
     fi
-    CLASSPATH=${CLASSPATH}:${jar}
+    jars=${jars}:${jar}
     # done processing current arg, moving on
     shift
   done
@@ -108,6 +111,10 @@
 fi
 
 args="${cmd} ${args}"
+if [ -n "${jars}" ]; then
+   args="${args} -e jars ${jars}"
+fi
 
+CLASSPATH=${CLASSPATH}:${jars}
 export CLASSPATH
 exec app_process ${base}/bin com.android.commands.uiautomator.Launcher ${args}
diff --git a/uiautomator/library/src/com/android/uiautomator/testrunner/TestCaseCollector.java b/uiautomator/library/src/com/android/uiautomator/testrunner/TestCaseCollector.java
index 8bc9914..9fc0b0b 100644
--- a/uiautomator/library/src/com/android/uiautomator/testrunner/TestCaseCollector.java
+++ b/uiautomator/library/src/com/android/uiautomator/testrunner/TestCaseCollector.java
@@ -16,7 +16,10 @@
 
 package com.android.uiautomator.testrunner;
 
+import junit.framework.AssertionFailedError;
+import junit.framework.Test;
 import junit.framework.TestCase;
+import junit.framework.TestResult;
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -111,12 +114,23 @@
             testCase.setName(method);
             mTestCases.add(testCase);
         } catch (InstantiationException e) {
-            throw new RuntimeException("Could not instantiate test class. Class: "
-                    + clazz.getName());
+            mTestCases.add(error(clazz, "InstantiationException: could not instantiate " +
+                    "test class. Class: " + clazz.getName()));
         } catch (IllegalAccessException e) {
-            throw new RuntimeException("Could not access test class. Class: " + clazz.getName());
+            mTestCases.add(error(clazz, "IllegalAccessException: could not instantiate " +
+                    "test class. Class: " + clazz.getName()));
         }
+    }
 
+    private UiAutomatorTestCase error(Class<?> clazz, final String message) {
+        UiAutomatorTestCase warning = new UiAutomatorTestCase() {
+            protected void runTest() {
+                fail(message);
+            }
+        };
+
+        warning.setName(clazz.getName());
+        return warning;
     }
 
     /**
diff --git a/uiautomator/library/src/com/android/uiautomator/testrunner/UiAutomatorTestRunner.java b/uiautomator/library/src/com/android/uiautomator/testrunner/UiAutomatorTestRunner.java
index e4010be..4f41a5c 100644
--- a/uiautomator/library/src/com/android/uiautomator/testrunner/UiAutomatorTestRunner.java
+++ b/uiautomator/library/src/com/android/uiautomator/testrunner/UiAutomatorTestRunner.java
@@ -372,7 +372,16 @@
     }
 
     protected TestCaseCollector getTestCaseCollector(ClassLoader classLoader) {
-        return new TestCaseCollector(classLoader, new UiAutomatorTestCaseFilter());
+        return new TestCaseCollector(classLoader, getTestCaseFilter());
+    }
+
+    /**
+     * Returns an object which determines if the class and its methods should be
+     * accepted into the test suite.
+     * @return
+     */
+    public UiAutomatorTestCaseFilter getTestCaseFilter() {
+        return new UiAutomatorTestCaseFilter();
     }
 
     protected void addTestListener(TestListener listener) {