| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.base.test; | 5 package org.chromium.base.test; |
| 6 | 6 |
| 7 import android.app.Activity; |
| 7 import android.app.Application; | 8 import android.app.Application; |
| 9 import android.app.Instrumentation; |
| 8 import android.content.Context; | 10 import android.content.Context; |
| 11 import android.os.Bundle; |
| 12 import android.support.test.internal.runner.RunnerArgs; |
| 13 import android.support.test.internal.runner.TestExecutor; |
| 14 import android.support.test.internal.runner.TestRequest; |
| 15 import android.support.test.internal.runner.TestRequestBuilder; |
| 9 import android.support.test.runner.AndroidJUnitRunner; | 16 import android.support.test.runner.AndroidJUnitRunner; |
| 10 | 17 |
| 18 import org.chromium.base.Log; |
| 11 import org.chromium.base.multidex.ChromiumMultiDexInstaller; | 19 import org.chromium.base.multidex.ChromiumMultiDexInstaller; |
| 12 | 20 |
| 13 /** | 21 /** |
| 14 * A custom AndroidJUnitRunner that supports multidex installer. | 22 * A custom AndroidJUnitRunner that supports multidex installer and list out tes
t information. |
| 15 * | 23 * |
| 16 * This class is the equivalent of BaseChromiumInstrumentationTestRunner in JUni
t3. Please | 24 * This class is the equivalent of BaseChromiumInstrumentationTestRunner in JUni
t3. Please |
| 17 * beware that is this not a class runner. It is declared in test apk AndroidMan
ifest.xml | 25 * beware that is this not a class runner. It is declared in test apk AndroidMan
ifest.xml |
| 18 * <instrumentation> | 26 * <instrumentation> |
| 19 */ | 27 */ |
| 20 public class BaseChromiumAndroidJUnitRunner extends AndroidJUnitRunner { | 28 public class BaseChromiumAndroidJUnitRunner extends AndroidJUnitRunner { |
| 29 private static final String LIST_ALL_TESTS_FLAG = |
| 30 "org.chromium.base.test.BaseChromiumAndroidJUnitRunner.TestList"; |
| 31 private static final String TAG = "BaseJUnitRunner"; |
| 32 |
| 33 private Bundle mArguments; |
| 34 |
| 21 @Override | 35 @Override |
| 22 public Application newApplication(ClassLoader cl, String className, Context
context) | 36 public Application newApplication(ClassLoader cl, String className, Context
context) |
| 23 throws ClassNotFoundException, IllegalAccessException, Instantiation
Exception { | 37 throws ClassNotFoundException, IllegalAccessException, Instantiation
Exception { |
| 24 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC
ontextWrapper( | 38 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC
ontextWrapper( |
| 25 getContext(), getTargetContext())); | 39 getContext(), getTargetContext())); |
| 26 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar
getContext()); | 40 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar
getContext()); |
| 27 return super.newApplication(cl, className, context); | 41 return super.newApplication(cl, className, context); |
| 28 } | 42 } |
| 43 |
| 44 @Override |
| 45 public void onCreate(Bundle arguments) { |
| 46 super.onCreate(arguments); |
| 47 mArguments = arguments; |
| 48 } |
| 49 |
| 50 /** |
| 51 * Add TestListInstrumentationRunListener when argument ask the runner to li
st tests info. |
| 52 * |
| 53 * The running mechanism when argument has "listAllTests" is equivalent to t
hat of |
| 54 * {@link android.support.test.runner.AndroidJUnitRunner#onStart()} except i
t adds |
| 55 * only TestListInstrumentationRunListener to monitor the tests. |
| 56 */ |
| 57 @Override |
| 58 public void onStart() { |
| 59 if (mArguments != null && mArguments.getString(LIST_ALL_TESTS_FLAG) != n
ull) { |
| 60 Log.w(TAG, "Runner will list out tests info in JSON without running
tests"); |
| 61 listTests(); // Intentionally not calling super.onStart() to avoid a
dditional work. |
| 62 } else { |
| 63 super.onStart(); |
| 64 } |
| 65 } |
| 66 |
| 67 private void listTests() { |
| 68 Bundle results = new Bundle(); |
| 69 try { |
| 70 TestExecutor.Builder executorBuilder = new TestExecutor.Builder(this
); |
| 71 executorBuilder.addRunListener(new TestListInstrumentationRunListene
r( |
| 72 mArguments.getString(LIST_ALL_TESTS_FLAG))); |
| 73 TestRequest listTestRequest = createListTestRequest(mArguments); |
| 74 results = executorBuilder.build().execute(listTestRequest); |
| 75 } catch (RuntimeException e) { |
| 76 String msg = "Fatal exception when running tests"; |
| 77 Log.e(TAG, msg, e); |
| 78 // report the exception to instrumentation out |
| 79 results.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 80 msg + "\n" + Log.getStackTraceString(e)); |
| 81 } |
| 82 finish(Activity.RESULT_OK, results); |
| 83 } |
| 84 |
| 85 private TestRequest createListTestRequest(Bundle arguments) { |
| 86 RunnerArgs runnerArgs = |
| 87 new RunnerArgs.Builder().fromManifest(this).fromBundle(arguments
).build(); |
| 88 TestRequestBuilder builder = new TestRequestBuilder(this, arguments); |
| 89 builder.addApkToScan(getContext().getPackageCodePath()); |
| 90 builder.addFromRunnerArgs(runnerArgs); |
| 91 return builder.build(); |
| 92 } |
| 29 } | 93 } |
| OLD | NEW |