Merge "Make debug logging configurable via the logging setprop properties." into jb-mr1.1-dev
diff --git a/uiautomator/api/16.txt b/uiautomator/api/16.txt
index 6634d7c..f3b0eb7 100644
--- a/uiautomator/api/16.txt
+++ b/uiautomator/api/16.txt
@@ -68,7 +68,6 @@
     method public java.lang.String getPackageName() throws com.android.uiautomator.core.UiObjectNotFoundException;
     method public final com.android.uiautomator.core.UiSelector getSelector();
     method public java.lang.String getText() throws com.android.uiautomator.core.UiObjectNotFoundException;
-    method public android.graphics.Rect getVisibleBounds() throws com.android.uiautomator.core.UiObjectNotFoundException;
     method public boolean isCheckable() throws com.android.uiautomator.core.UiObjectNotFoundException;
     method public boolean isChecked() throws com.android.uiautomator.core.UiObjectNotFoundException;
     method public boolean isClickable() throws com.android.uiautomator.core.UiObjectNotFoundException;
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 862c341..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,11 +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;
@@ -44,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");
@@ -70,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);
     }
@@ -85,6 +99,7 @@
         // key is "debug", parameter will determine whether to wait for debugger
         // to attach
         // -c <class name>
+        // -s turns on the simple output format
         // equivalent to -e class <class name>, i.e. passed onto JUnit
         for (int i = 0; i < args.length; i++) {
             if (args[i].equals("-e")) {
@@ -96,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);
                     }
@@ -109,6 +124,8 @@
                 } else {
                     return ARG_FAIL_INCOMPLETE_C;
                 }
+            } else if (args[i].equals("-s")) {
+                mParams.putString(OUTPUT_FORMAT_KEY, OUTPUT_SIMPLE);
             } else {
                 return ARG_FAIL_UNSUPPORTED;
             }
@@ -117,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 "
@@ -158,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"
@@ -168,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"
@@ -176,7 +243,8 @@
             + "      -e runner [CLASS]: use specified test runner class instead. If\n"
             + "        unspecified, framework default runner will be used.\n"
             + "      -e <NAME> <VALUE>: other name-value pairs to be passed to test classes.\n"
-            + "        May be repeated.\n";
+            + "        May be repeated.\n"
+            + "      -e outputFormat simple | -s: enabled less verbose JUnit style output.\n";
     }
 
     @Override
@@ -184,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 a376aa0..4f41a5c 100644
--- a/uiautomator/library/src/com/android/uiautomator/testrunner/UiAutomatorTestRunner.java
+++ b/uiautomator/library/src/com/android/uiautomator/testrunner/UiAutomatorTestRunner.java
@@ -23,11 +23,11 @@
 import android.os.Bundle;
 import android.os.Debug;
 import android.os.IBinder;
+import android.os.SystemClock;
 import android.test.RepetitiveTest;
 import android.util.Log;
 
 import com.android.uiautomator.core.Tracer;
-import com.android.uiautomator.core.Tracer.Mode;
 import com.android.uiautomator.core.UiDevice;
 
 import junit.framework.AssertionFailedError;
@@ -105,8 +105,6 @@
         mUiDevice = UiDevice.getInstance();
         List<TestCase> testCases = collector.getTestCases();
         Bundle testRunOutput = new Bundle();
-        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
-        PrintStream writer = new PrintStream(byteArrayOutputStream);
 
         String traceType = mParams.getString("traceOutputMode");
         if(traceType != null) {
@@ -122,39 +120,35 @@
             Tracer.getInstance().setOutputMode(mode);
         }
 
-        try {
-            StringResultPrinter resultPrinter = new StringResultPrinter(writer);
+        TestResult testRunResult = new TestResult();
+        ResultReporter resultPrinter;
+        String outputFormat = mParams.getString("outputFormat");
+        if ("simple".equals(outputFormat)) {
+            resultPrinter = new SimpleResultPrinter(System.out, true);
+        } else {
+            resultPrinter = new WatcherResultPrinter(testCases.size());
+        }
 
-            TestResult testRunResult = new TestResult();
+        long startTime = SystemClock.uptimeMillis();
+        try {
             // add test listeners
-            testRunResult.addListener(new WatcherResultPrinter(testCases.size()));
             testRunResult.addListener(resultPrinter);
             // add all custom listeners
             for (TestListener listener : mTestListeners) {
                 testRunResult.addListener(listener);
             }
-            long startTime = System.currentTimeMillis();
 
             // run tests for realz!
             for (TestCase testCase : testCases) {
                 prepareTestCase(testCase);
                 testCase.run(testRunResult);
             }
-            long runTime = System.currentTimeMillis() - startTime;
-
-            resultPrinter.print2(testRunResult, runTime);
         } catch (Throwable t) {
             // catch all exceptions so a more verbose error message can be outputted
-            writer.println(String.format("Test run aborted due to unexpected exception: %s",
-                            t.getMessage()));
-            t.printStackTrace(writer);
+            resultPrinter.printUnexpectedError(t);
         } finally {
-            testRunOutput.putString(Instrumentation.REPORT_KEY_STREAMRESULT,
-                    String.format("\nTest results for %s=%s",
-                    getClass().getSimpleName(),
-                    byteArrayOutputStream.toString()));
-            writer.close();
-            mAutomationSupport.sendStatus(Activity.RESULT_OK, testRunOutput);
+            long runTime = SystemClock.uptimeMillis() - startTime;
+            resultPrinter.print(testRunResult, runTime, testRunOutput);
         }
     }
 
@@ -215,8 +209,13 @@
         }
     }
 
+    private interface ResultReporter extends TestListener {
+        public void print(TestResult result, long runTime, Bundle testOutput);
+        public void printUnexpectedError(Throwable t);
+    }
+
     // Copy & pasted from InstrumentationTestRunner.WatcherResultPrinter
-    private class WatcherResultPrinter implements TestListener {
+    private class WatcherResultPrinter implements ResultReporter {
 
         private static final String REPORT_KEY_NUM_TOTAL = "numtests";
         private static final String REPORT_KEY_NAME_CLASS = "class";
@@ -236,10 +235,18 @@
         int mTestResultCode = 0;
         String mTestClass = null;
 
+        private SimpleResultPrinter mPrinter;
+        private ByteArrayOutputStream mStream;
+        private PrintStream mWriter;
+
         public WatcherResultPrinter(int numTests) {
             mResultTemplate = new Bundle();
             mResultTemplate.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID);
             mResultTemplate.putInt(REPORT_KEY_NUM_TOTAL, numTests);
+
+            mStream = new ByteArrayOutputStream();
+            mWriter = new PrintStream(mStream);
+            mPrinter = new SimpleResultPrinter(mWriter, false);
         }
 
         /**
@@ -280,6 +287,8 @@
 
             mAutomationSupport.sendStatus(REPORT_VALUE_RESULT_START, mTestResult);
             mTestResultCode = 0;
+
+            mPrinter.startTest(test);
         }
 
         @Override
@@ -290,6 +299,8 @@
             mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT,
                 String.format("\nError in %s:\n%s",
                     ((TestCase)test).getName(), BaseTestRunner.getFilteredTrace(t)));
+
+            mPrinter.addError(test, t);
         }
 
         @Override
@@ -300,6 +311,8 @@
             mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT,
                 String.format("\nFailure in %s:\n%s",
                     ((TestCase)test).getName(), BaseTestRunner.getFilteredTrace(t)));
+
+            mPrinter.addFailure(test, t);
         }
 
         @Override
@@ -308,25 +321,67 @@
                 mTestResult.putString(Instrumentation.REPORT_KEY_STREAMRESULT, ".");
             }
             mAutomationSupport.sendStatus(mTestResultCode, mTestResult);
+
+            mPrinter.endTest(test);
         }
 
+        public void print(TestResult result, long runTime, Bundle testOutput) {
+            mPrinter.print(result, runTime, testOutput);
+            testOutput.putString(Instrumentation.REPORT_KEY_STREAMRESULT,
+                  String.format("\nTest results for %s=%s",
+                  getClass().getSimpleName(),
+                  mStream.toString()));
+            mWriter.close();
+            mAutomationSupport.sendStatus(Activity.RESULT_OK, testOutput);
+        }
+
+        public void printUnexpectedError(Throwable t) {
+            mWriter.println(String.format("Test run aborted due to unexpected exception: %s",
+                    t.getMessage()));
+            t.printStackTrace(mWriter);
+        }
     }
 
-    // copy pasted from InstrumentationTestRunner
-    private class StringResultPrinter extends ResultPrinter {
-
-        public StringResultPrinter(PrintStream writer) {
+    /**
+     * Class that produces the same output as JUnit when running from command line. Can be
+     * used when default UiAutomator output is too verbose.
+     */
+    private class SimpleResultPrinter extends ResultPrinter implements ResultReporter {
+        private boolean mFullOutput;
+        public SimpleResultPrinter(PrintStream writer, boolean fullOutput) {
             super(writer);
+            mFullOutput = fullOutput;
         }
 
-        synchronized void print2(TestResult result, long runTime) {
+        public void print(TestResult result, long runTime, Bundle testOutput) {
             printHeader(runTime);
+            if (mFullOutput) {
+                printErrors(result);
+                printFailures(result);
+            }
             printFooter(result);
         }
+
+        public void printUnexpectedError(Throwable t) {
+            if (mFullOutput) {
+                getWriter().printf("Test run aborted due to unexpected exeption: %s",
+                        t.getMessage());
+                t.printStackTrace(getWriter());
+            }
+        }
     }
 
     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) {