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.Activity; |
8 import android.app.Application; | 8 import android.app.Application; |
9 import android.app.Instrumentation; | 9 import android.app.Instrumentation; |
10 import android.content.Context; | 10 import android.content.Context; |
11 import android.os.Bundle; | 11 import android.os.Bundle; |
12 import android.support.test.internal.runner.RunnerArgs; | 12 import android.support.test.internal.runner.RunnerArgs; |
13 import android.support.test.internal.runner.TestExecutor; | 13 import android.support.test.internal.runner.TestExecutor; |
14 import android.support.test.internal.runner.TestRequest; | 14 import android.support.test.internal.runner.TestRequest; |
15 import android.support.test.internal.runner.TestRequestBuilder; | 15 import android.support.test.internal.runner.TestRequestBuilder; |
16 import android.support.test.runner.AndroidJUnitRunner; | 16 import android.support.test.runner.AndroidJUnitRunner; |
17 | 17 |
18 import org.chromium.base.Log; | 18 import org.chromium.base.Log; |
19 import org.chromium.base.multidex.ChromiumMultiDexInstaller; | 19 import org.chromium.base.multidex.ChromiumMultiDexInstaller; |
20 | 20 |
| 21 import java.io.IOException; |
| 22 |
21 /** | 23 /** |
22 * A custom AndroidJUnitRunner that supports multidex installer and list out tes
t information. | 24 * A custom AndroidJUnitRunner that supports multidex installer and list out tes
t information. |
23 * | 25 * |
24 * This class is the equivalent of BaseChromiumInstrumentationTestRunner in JUni
t3. Please | 26 * This class is the equivalent of BaseChromiumInstrumentationTestRunner in JUni
t3. Please |
25 * beware that is this not a class runner. It is declared in test apk AndroidMan
ifest.xml | 27 * beware that is this not a class runner. It is declared in test apk AndroidMan
ifest.xml |
26 * <instrumentation> | 28 * <instrumentation> |
27 */ | 29 */ |
28 public class BaseChromiumAndroidJUnitRunner extends AndroidJUnitRunner { | 30 public class BaseChromiumAndroidJUnitRunner extends AndroidJUnitRunner { |
29 private static final String LIST_ALL_TESTS_FLAG = | 31 public static final String LIST_ALL_TESTS_FLAG = |
30 "org.chromium.base.test.BaseChromiumAndroidJUnitRunner.TestList"; | 32 "org.chromium.base.test.BaseChromiumAndroidJUnitRunner.TestList"; |
| 33 |
| 34 private static final String ARGUMENT_ANNOTATION = "annotation"; |
| 35 private static final String ARGUMENT_NOT_ANNOTATION = "notAnnotation"; |
| 36 private static final String ARGUMENT_LOG_ONLY = "log"; |
31 private static final String TAG = "BaseJUnitRunner"; | 37 private static final String TAG = "BaseJUnitRunner"; |
32 | 38 |
33 private Bundle mArguments; | 39 private Bundle mArguments; |
34 | 40 |
35 @Override | 41 @Override |
36 public Application newApplication(ClassLoader cl, String className, Context
context) | 42 public Application newApplication(ClassLoader cl, String className, Context
context) |
37 throws ClassNotFoundException, IllegalAccessException, Instantiation
Exception { | 43 throws ClassNotFoundException, IllegalAccessException, Instantiation
Exception { |
38 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC
ontextWrapper( | 44 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC
ontextWrapper( |
39 getContext(), getTargetContext())); | 45 getContext(), getTargetContext())); |
40 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar
getContext()); | 46 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar
getContext()); |
41 return super.newApplication(cl, className, context); | 47 return super.newApplication(cl, className, context); |
42 } | 48 } |
43 | 49 |
44 @Override | 50 @Override |
45 public void onCreate(Bundle arguments) { | 51 public void onCreate(Bundle arguments) { |
46 super.onCreate(arguments); | 52 super.onCreate(arguments); |
47 mArguments = arguments; | 53 mArguments = arguments; |
48 } | 54 } |
49 | 55 |
50 /** | 56 /** |
51 * Add TestListInstrumentationRunListener when argument ask the runner to li
st tests info. | 57 * Add TestListInstrumentationRunListener when argument ask the runner to li
st tests info. |
52 * | 58 * |
53 * The running mechanism when argument has "listAllTests" is equivalent to t
hat of | 59 * 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 | 60 * {@link android.support.test.runner.AndroidJUnitRunner#onStart()} except i
t adds |
55 * only TestListInstrumentationRunListener to monitor the tests. | 61 * only TestListInstrumentationRunListener to monitor the tests. |
56 */ | 62 */ |
57 @Override | 63 @Override |
58 public void onStart() { | 64 public void onStart() { |
59 if (mArguments != null && mArguments.getString(LIST_ALL_TESTS_FLAG) != n
ull) { | 65 if (toListTests(mArguments)) { |
60 Log.w(TAG, "Runner will list out tests info in JSON without running
tests"); | 66 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. | 67 listTests(); // Intentionally not calling super.onStart() to avoid a
dditional work. |
62 } else { | 68 } else { |
63 super.onStart(); | 69 super.onStart(); |
64 } | 70 } |
65 } | 71 } |
66 | 72 |
67 private void listTests() { | 73 private void listTests() { |
68 Bundle results = new Bundle(); | 74 Bundle results = new Bundle(); |
| 75 TestListInstrumentationRunListener listener = new TestListInstrumentatio
nRunListener(); |
69 try { | 76 try { |
70 TestExecutor.Builder executorBuilder = new TestExecutor.Builder(this
); | 77 TestExecutor.Builder executorBuilder = new TestExecutor.Builder(this
); |
71 executorBuilder.addRunListener(new TestListInstrumentationRunListene
r( | 78 executorBuilder.addRunListener(listener); |
72 mArguments.getString(LIST_ALL_TESTS_FLAG))); | 79 Bundle junit3Arguments = new Bundle(mArguments); |
73 TestRequest listTestRequest = createListTestRequest(mArguments); | 80 junit3Arguments.putString(ARGUMENT_NOT_ANNOTATION, "org.junit.runner
.RunWith"); |
74 results = executorBuilder.build().execute(listTestRequest); | 81 TestRequest listJUnit3TestRequest = createListTestRequest(junit3Argu
ments); |
75 } catch (RuntimeException e) { | 82 results = executorBuilder.build().execute(listJUnit3TestRequest); |
| 83 |
| 84 Bundle junit4Arguments = new Bundle(mArguments); |
| 85 junit4Arguments.putString(ARGUMENT_ANNOTATION, "org.junit.runner.Run
With"); |
| 86 //Do not use Log runner from android test support |
| 87 junit4Arguments.remove(ARGUMENT_LOG_ONLY); |
| 88 TestRequest listJUnit4TestRequest = createListTestRequest(junit4Argu
ments); |
| 89 results.putAll(executorBuilder.build().execute(listJUnit4TestRequest
)); |
| 90 listener.saveTestsToJson(mArguments.getString(LIST_ALL_TESTS_FLAG)); |
| 91 } catch (IOException | RuntimeException e) { |
76 String msg = "Fatal exception when running tests"; | 92 String msg = "Fatal exception when running tests"; |
77 Log.e(TAG, msg, e); | 93 Log.e(TAG, msg, e); |
78 // report the exception to instrumentation out | 94 // report the exception to instrumentation out |
79 results.putString(Instrumentation.REPORT_KEY_STREAMRESULT, | 95 results.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
80 msg + "\n" + Log.getStackTraceString(e)); | 96 msg + "\n" + Log.getStackTraceString(e)); |
81 } | 97 } |
82 finish(Activity.RESULT_OK, results); | 98 finish(Activity.RESULT_OK, results); |
83 } | 99 } |
84 | 100 |
85 private TestRequest createListTestRequest(Bundle arguments) { | 101 private TestRequest createListTestRequest(Bundle arguments) { |
86 RunnerArgs runnerArgs = | 102 RunnerArgs runnerArgs = |
87 new RunnerArgs.Builder().fromManifest(this).fromBundle(arguments
).build(); | 103 new RunnerArgs.Builder().fromManifest(this).fromBundle(arguments
).build(); |
88 TestRequestBuilder builder = new TestRequestBuilder(this, arguments); | 104 TestRequestBuilder builder = new TestRequestBuilder(this, arguments); |
89 builder.addApkToScan(getContext().getPackageCodePath()); | 105 builder.addApkToScan(getContext().getPackageCodePath()); |
90 builder.addFromRunnerArgs(runnerArgs); | 106 builder.addFromRunnerArgs(runnerArgs); |
91 return builder.build(); | 107 return builder.build(); |
92 } | 108 } |
| 109 |
| 110 static boolean toListTests(Bundle arguments) { |
| 111 return arguments != null && arguments.getString(LIST_ALL_TESTS_FLAG) !=
null; |
| 112 } |
93 } | 113 } |
OLD | NEW |