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 Bundle mArguments; |
| 30 private static final String LIST_ALL_TESTS_FLAG = |
| 31 "org.chromium.base.test.BaseChromiumAndroidJUnitRunner.TestList"; |
| 32 private static final String TAG = "BaseJUnitRunner"; |
| 33 |
21 @Override | 34 @Override |
22 public Application newApplication(ClassLoader cl, String className, Context
context) | 35 public Application newApplication(ClassLoader cl, String className, Context
context) |
23 throws ClassNotFoundException, IllegalAccessException, Instantiation
Exception { | 36 throws ClassNotFoundException, IllegalAccessException, Instantiation
Exception { |
24 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC
ontextWrapper( | 37 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC
ontextWrapper( |
25 getContext(), getTargetContext())); | 38 getContext(), getTargetContext())); |
26 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar
getContext()); | 39 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar
getContext()); |
27 return super.newApplication(cl, className, context); | 40 return super.newApplication(cl, className, context); |
28 } | 41 } |
| 42 |
| 43 @Override |
| 44 public void onCreate(Bundle arguments) { |
| 45 super.onCreate(arguments); |
| 46 mArguments = arguments; |
| 47 } |
| 48 |
| 49 /** |
| 50 * Add TestListInstrumentationRunListener when argument ask the runner to li
st tests info. |
| 51 * |
| 52 * The running mechanism when argument has "listAllTests" is equivalent to t
hat of |
| 53 * {@link android.support.test.runner.AndroidJUnitRunner#onStart()} except i
t adds |
| 54 * only TestListInstrumentationRunListener to monitor the tests. |
| 55 */ |
| 56 @Override |
| 57 public void onStart() { |
| 58 if (mArguments.getString(LIST_ALL_TESTS_FLAG) != null) { |
| 59 listTests(); |
| 60 } else { |
| 61 super.onStart(); |
| 62 } |
| 63 } |
| 64 |
| 65 private void listTests() { |
| 66 Bundle results = new Bundle(); |
| 67 try { |
| 68 TestExecutor.Builder executorBuilder = new TestExecutor.Builder(this
); |
| 69 executorBuilder.addRunListener(new TestListInstrumentationRunListene
r( |
| 70 mArguments.getString(LIST_ALL_TESTS_FLAG))); |
| 71 TestRequest listTestRequest = createListTestRequest(mArguments); |
| 72 results = executorBuilder.build().execute(listTestRequest); |
| 73 } catch (RuntimeException e) { |
| 74 final String msg = "Fatal exception when running tests"; |
| 75 Log.e(TAG, msg, e); |
| 76 // report the exception to instrumentation out |
| 77 results.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
| 78 msg + "\n" + Log.getStackTraceString(e)); |
| 79 } |
| 80 finish(Activity.RESULT_OK, results); |
| 81 } |
| 82 |
| 83 private TestRequest createListTestRequest(Bundle arguments) { |
| 84 RunnerArgs runnerArgs = |
| 85 new RunnerArgs.Builder().fromManifest(this).fromBundle(arguments
).build(); |
| 86 TestRequestBuilder builder = new TestRequestBuilder(this, arguments); |
| 87 builder.addApkToScan(getContext().getPackageCodePath()); |
| 88 builder.addFromRunnerArgs(runnerArgs); |
| 89 return builder.build(); |
| 90 } |
29 } | 91 } |
OLD | NEW |