diff options
author | Brett Chabot <brettchabot@android.com> | 2010-03-09 19:58:47 -0800 |
---|---|---|
committer | Brett Chabot <brettchabot@android.com> | 2010-03-10 12:38:55 -0800 |
commit | 1ef43eaeffba173dfccd2242933fce1f5243cdec (patch) | |
tree | 831960397672a5d4cc27067d5b6f2f0935eeb3fc /ddms | |
parent | 2d1900375c5b523caf25c4786597df164ef9c878 (diff) | |
download | sdk-1ef43eaeffba173dfccd2242933fce1f5243cdec.zip sdk-1ef43eaeffba173dfccd2242933fce1f5243cdec.tar.gz sdk-1ef43eaeffba173dfccd2242933fce1f5243cdec.tar.bz2 |
Add support for multiple instrumentation test result listeners.
Change-Id: I12b273582880fadefcd233df45960811b8f16d49
Diffstat (limited to 'ddms')
3 files changed, 87 insertions, 33 deletions
diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/IRemoteAndroidTestRunner.java b/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/IRemoteAndroidTestRunner.java index f51a127..75c8eeb 100644 --- a/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/IRemoteAndroidTestRunner.java +++ b/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/IRemoteAndroidTestRunner.java @@ -16,6 +16,8 @@ package com.android.ddmlib.testrunner; +import java.util.Collection; + /** * Interface for running a Android test command remotely and reporting result to a listener. */ @@ -106,10 +108,19 @@ public interface IRemoteAndroidTestRunner { /** * Execute this test run. + * <p/> + * Convenience method for {@link #run(Collection)}. + * + * @param listeners listens for test results + */ + public void run(ITestRunListener... listeners); + + /** + * Execute this test run. * - * @param listener listens for test results + * @param listeners collection of listeners for test results */ - public void run(ITestRunListener listener); + public void run(Collection<ITestRunListener> listeners); /** * Requests cancellation of this test run. diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/InstrumentationResultParser.java b/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/InstrumentationResultParser.java index 30c72a5..a3f89cc 100755 --- a/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/InstrumentationResultParser.java +++ b/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/InstrumentationResultParser.java @@ -20,6 +20,9 @@ import com.android.ddmlib.IShellOutputReceiver; import com.android.ddmlib.Log; import com.android.ddmlib.MultiLineReceiver; +import java.util.ArrayList; +import java.util.Collection; + /** * Parses the 'raw output mode' results of an instrumentation test run from shell and informs a * ITestRunListener of the results. @@ -85,7 +88,7 @@ public class InstrumentationResultParser extends MultiLineReceiver { private static final String TIME_REPORT = "Time: "; } - private final ITestRunListener mTestListener; + private final Collection<ITestRunListener> mTestListeners; /** * Test result data @@ -152,10 +155,20 @@ public class InstrumentationResultParser extends MultiLineReceiver { /** * Creates the InstrumentationResultParser. * + * @param listeners informed of test results as the tests are executing + */ + public InstrumentationResultParser(Collection<ITestRunListener> listeners) { + mTestListeners = new ArrayList<ITestRunListener>(listeners); + } + + /** + * Creates the InstrumentationResultParser for a single listener. + * * @param listener informed of test results as the tests are executing */ public InstrumentationResultParser(ITestRunListener listener) { - mTestListener = listener; + mTestListeners = new ArrayList<ITestRunListener>(1); + mTestListeners.add(listener); } /** @@ -338,27 +351,38 @@ public class InstrumentationResultParser extends MultiLineReceiver { switch (testInfo.mCode) { case StatusCodes.START: - mTestListener.testStarted(testId); + for (ITestRunListener listener : mTestListeners) { + listener.testStarted(testId); + } break; case StatusCodes.FAILURE: - mTestListener.testFailed(ITestRunListener.TestFailure.FAILURE, testId, + for (ITestRunListener listener : mTestListeners) { + listener.testFailed(ITestRunListener.TestFailure.FAILURE, testId, getTrace(testInfo)); - mTestListener.testEnded(testId); + + listener.testEnded(testId); + } mNumTestsRun++; break; case StatusCodes.ERROR: - mTestListener.testFailed(ITestRunListener.TestFailure.ERROR, testId, + for (ITestRunListener listener : mTestListeners) { + listener.testFailed(ITestRunListener.TestFailure.ERROR, testId, getTrace(testInfo)); - mTestListener.testEnded(testId); + listener.testEnded(testId); + } mNumTestsRun++; break; case StatusCodes.OK: - mTestListener.testEnded(testId); + for (ITestRunListener listener : mTestListeners) { + listener.testEnded(testId); + } mNumTestsRun++; break; default: Log.e(LOG_TAG, "Unknown status code received: " + testInfo.mCode); - mTestListener.testEnded(testId); + for (ITestRunListener listener : mTestListeners) { + listener.testEnded(testId); + } mNumTestsRun++; break; } @@ -374,7 +398,9 @@ public class InstrumentationResultParser extends MultiLineReceiver { private void reportTestRunStarted(TestResult testInfo) { // if start test run not reported yet if (!mTestStartReported && testInfo.mNumTests != null) { - mTestListener.testRunStarted(testInfo.mNumTests); + for (ITestRunListener listener : mTestListeners) { + listener.testRunStarted(testInfo.mNumTests); + } mNumTestsExpected = testInfo.mNumTests; mTestStartReported = true; } @@ -410,7 +436,9 @@ public class InstrumentationResultParser extends MultiLineReceiver { */ private void handleTestRunFailed(String errorMsg) { Log.i(LOG_TAG, String.format("test run failed %s", errorMsg)); - mTestListener.testRunFailed(errorMsg == null ? "Unknown error" : errorMsg); + for (ITestRunListener listener : mTestListeners) { + listener.testRunFailed(errorMsg == null ? "Unknown error" : errorMsg); + } mTestRunFailReported = true; } @@ -425,9 +453,13 @@ public class InstrumentationResultParser extends MultiLineReceiver { String.format("Test run incomplete. Expected %d tests, received %d", mNumTestsExpected, mNumTestsRun); Log.w(LOG_TAG, message); - mTestListener.testRunFailed(message); + for (ITestRunListener listener : mTestListeners) { + listener.testRunFailed(message); + } } else { - mTestListener.testRunEnded(mTestTime); + for (ITestRunListener listener : mTestListeners) { + listener.testRunEnded(mTestTime); + } } } } diff --git a/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunner.java b/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunner.java index 6e3ff2c..f45d267 100644 --- a/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunner.java +++ b/ddms/libs/ddmlib/src/com/android/ddmlib/testrunner/RemoteAndroidTestRunner.java @@ -21,6 +21,8 @@ import com.android.ddmlib.IDevice; import com.android.ddmlib.Log; import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; import java.util.Hashtable; import java.util.Map; import java.util.Map.Entry; @@ -44,25 +46,25 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { private static final char METHOD_SEPARATOR = '#'; private static final char RUNNER_SEPARATOR = '/'; - // defined instrumentation argument names + // defined instrumentation argument names private static final String CLASS_ARG_NAME = "class"; private static final String LOG_ARG_NAME = "log"; private static final String DEBUG_ARG_NAME = "debug"; private static final String COVERAGE_ARG_NAME = "coverage"; private static final String PACKAGE_ARG_NAME = "package"; - + /** * Creates a remote Android test runner. - * - * @param packageName the Android application package that contains the tests to run + * + * @param packageName the Android application package that contains the tests to run * @param runnerName the instrumentation test runner to execute. If null, will use default - * runner + * runner * @param remoteDevice the Android device to execute tests on */ - public RemoteAndroidTestRunner(String packageName, + public RemoteAndroidTestRunner(String packageName, String runnerName, IDevice remoteDevice) { - + mPackageName = packageName; mRunnerName = runnerName; mRemoteDevice = remoteDevice; @@ -71,11 +73,11 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { /** * Alternate constructor. Uses default instrumentation runner. - * - * @param packageName the Android application package that contains the tests to run + * + * @param packageName the Android application package that contains the tests to run * @param remoteDevice the Android device to execute tests on */ - public RemoteAndroidTestRunner(String packageName, + public RemoteAndroidTestRunner(String packageName, IDevice remoteDevice) { this(packageName, null, remoteDevice); } @@ -116,7 +118,7 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { */ public void setClassNames(String[] classNames) { StringBuilder classArgBuilder = new StringBuilder(); - + for (int i = 0; i < classNames.length; i++) { if (i != 0) { classArgBuilder.append(CLASS_SEPARATOR); @@ -147,7 +149,7 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { if (name == null || value == null) { throw new IllegalArgumentException("name or value arguments cannot be null"); } - mArgMap.put(name, value); + mArgMap.put(name, value); } /** @@ -156,7 +158,7 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { public void addBooleanArg(String name, boolean value) { addInstrumentationArg(name, Boolean.toString(value)); } - + /** * {@inheritDoc} */ @@ -181,17 +183,26 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { /** * {@inheritDoc} */ - public void run(ITestRunListener listener) { + public void run(ITestRunListener... listeners) { + run(Arrays.asList(listeners)); + } + + /** + * {@inheritDoc} + */ + public void run(Collection<ITestRunListener> listeners) { final String runCaseCommandStr = String.format("am instrument -w -r %s %s", getArgsCommand(), getRunnerPath()); Log.d(LOG_TAG, runCaseCommandStr); - mParser = new InstrumentationResultParser(listener); + mParser = new InstrumentationResultParser(listeners); try { mRemoteDevice.executeShellCommand(runCaseCommandStr, mParser); } catch (IOException e) { Log.e(LOG_TAG, e); - listener.testRunFailed(e.toString()); + for (ITestRunListener listener : listeners) { + listener.testRunFailed(e.toString()); + } } } @@ -205,8 +216,8 @@ public class RemoteAndroidTestRunner implements IRemoteAndroidTestRunner { } /** - * Returns the full instrumentation command line syntax for the provided instrumentation - * arguments. + * Returns the full instrumentation command line syntax for the provided instrumentation + * arguments. * Returns an empty string if no arguments were specified. */ private String getArgsCommand() { |