Chromium Code Reviews| Index: base/test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java |
| diff --git a/base/test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java b/base/test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java |
| index a48c4ab71301190bef89adf33a2a056832068600..cbfd5c0e78f1375450ba849c72eefccdb724a4dd 100644 |
| --- a/base/test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java |
| +++ b/base/test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java |
| @@ -4,20 +4,32 @@ |
| package org.chromium.base.test; |
| +import android.app.Activity; |
| import android.app.Application; |
| +import android.app.Instrumentation; |
| import android.content.Context; |
| +import android.os.Bundle; |
| +import android.support.test.internal.runner.RunnerArgs; |
|
jbudorick
2017/07/14 18:43:55
Talking about this offline.
|
| +import android.support.test.internal.runner.TestExecutor; |
| +import android.support.test.internal.runner.TestRequest; |
| +import android.support.test.internal.runner.TestRequestBuilder; |
| import android.support.test.runner.AndroidJUnitRunner; |
| +import org.chromium.base.Log; |
| import org.chromium.base.multidex.ChromiumMultiDexInstaller; |
| /** |
| - * A custom AndroidJUnitRunner that supports multidex installer. |
| + * A custom AndroidJUnitRunner that supports multidex installer and list out test information. |
| * |
| * This class is the equivalent of BaseChromiumInstrumentationTestRunner in JUnit3. Please |
| * beware that is this not a class runner. It is declared in test apk AndroidManifest.xml |
| * <instrumentation> |
| */ |
| public class BaseChromiumAndroidJUnitRunner extends AndroidJUnitRunner { |
| + private Bundle mArguments; |
| + private static final String LIST_ALL_TESTS_FLAG = "listAllTests"; |
|
jbudorick
2017/07/14 18:43:55
flags should include the class name, e.g. https://
the real yoland
2017/07/18 04:20:21
Done
|
| + private static final String TAG = "BaseJUnitRunner"; |
| + |
| @Override |
| public Application newApplication(ClassLoader cl, String className, Context context) |
| throws ClassNotFoundException, IllegalAccessException, InstantiationException { |
| @@ -26,4 +38,53 @@ public class BaseChromiumAndroidJUnitRunner extends AndroidJUnitRunner { |
| BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTargetContext()); |
| return super.newApplication(cl, className, context); |
| } |
| + |
| + @Override |
| + public void onCreate(Bundle arguments) { |
| + super.onCreate(arguments); |
| + mArguments = arguments; |
| + } |
| + |
| + /** |
| + * Add TestListInstrumentationRunListener when argument ask the runner to list tests info. |
| + * |
| + * The running mechanism when argument has "listAllTests" is equivalent to that of |
| + * {@link android.support.test.runner.AndroidJUnitRunner#onStart()} except it adds |
| + * only TestListInstrumentationRunListener to monitor the tests. |
| + */ |
| + @Override |
| + public void onStart() { |
| + if (getListTestFlag(mArguments) != null) { |
| + Bundle results = new Bundle(); |
|
jbudorick
2017/07/14 18:43:55
nit: extract the body of this if block to a separa
the real yoland
2017/07/18 04:20:21
Done
|
| + try { |
| + TestExecutor.Builder executorBuilder = new TestExecutor.Builder(this); |
| + executorBuilder.addRunListener(new TestListInstrumentationRunListener( |
| + getListTestFlag(mArguments).toString())); |
| + TestRequest listTestRequest = createListTestRequest(mArguments); |
| + results = executorBuilder.build().execute(listTestRequest); |
| + } catch (RuntimeException e) { |
| + final String msg = "Fatal exception when running tests"; |
| + Log.e(TAG, msg, e); |
| + // report the exception to instrumentation out |
| + results.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| + msg + "\n" + Log.getStackTraceString(e)); |
| + } |
| + finish(Activity.RESULT_OK, results); |
| + } else { |
| + super.onStart(); |
| + } |
| + } |
| + |
| + private TestRequest createListTestRequest(Bundle arguments) { |
| + RunnerArgs runnerArgs = |
| + new RunnerArgs.Builder().fromManifest(this).fromBundle(arguments).build(); |
| + TestRequestBuilder builder = new TestRequestBuilder(this, arguments); |
| + builder.addApkToScan(getContext().getPackageCodePath()); |
| + builder.addFromRunnerArgs(runnerArgs); |
| + return builder.build(); |
| + } |
| + |
| + private static CharSequence getListTestFlag(Bundle arguments) { |
| + return arguments != null ? arguments.getCharSequence(LIST_ALL_TESTS_FLAG) : null; |
|
jbudorick
2017/07/14 18:43:55
Why getCharSequence and not getString?
the real yoland
2017/07/18 04:20:21
Done
|
| + } |
| } |