Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: base/test/android/javatests/src/org/chromium/base/test/BaseChromiumAndroidJUnitRunner.java

Issue 2935503002: List Java Instru Test Information From JUnit Runner (Closed)
Patch Set: address comments Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
jbudorick 2017/07/14 18:43:55 Talking about this offline.
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 = "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
31 private static final String TAG = "BaseJUnitRunner";
32
21 @Override 33 @Override
22 public Application newApplication(ClassLoader cl, String className, Context context) 34 public Application newApplication(ClassLoader cl, String className, Context context)
23 throws ClassNotFoundException, IllegalAccessException, Instantiation Exception { 35 throws ClassNotFoundException, IllegalAccessException, Instantiation Exception {
24 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC ontextWrapper( 36 ChromiumMultiDexInstaller.install(new BaseChromiumRunnerCommon.MultiDexC ontextWrapper(
25 getContext(), getTargetContext())); 37 getContext(), getTargetContext()));
26 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar getContext()); 38 BaseChromiumRunnerCommon.reorderDexPathElements(cl, getContext(), getTar getContext());
27 return super.newApplication(cl, className, context); 39 return super.newApplication(cl, className, context);
28 } 40 }
41
42 @Override
43 public void onCreate(Bundle arguments) {
44 super.onCreate(arguments);
45 mArguments = arguments;
46 }
47
48 /**
49 * Add TestListInstrumentationRunListener when argument ask the runner to li st tests info.
50 *
51 * The running mechanism when argument has "listAllTests" is equivalent to t hat of
52 * {@link android.support.test.runner.AndroidJUnitRunner#onStart()} except i t adds
53 * only TestListInstrumentationRunListener to monitor the tests.
54 */
55 @Override
56 public void onStart() {
57 if (getListTestFlag(mArguments) != null) {
58 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
59 try {
60 TestExecutor.Builder executorBuilder = new TestExecutor.Builder( this);
61 executorBuilder.addRunListener(new TestListInstrumentationRunLis tener(
62 getListTestFlag(mArguments).toString()));
63 TestRequest listTestRequest = createListTestRequest(mArguments);
64 results = executorBuilder.build().execute(listTestRequest);
65 } catch (RuntimeException e) {
66 final String msg = "Fatal exception when running tests";
67 Log.e(TAG, msg, e);
68 // report the exception to instrumentation out
69 results.putString(Instrumentation.REPORT_KEY_STREAMRESULT,
70 msg + "\n" + Log.getStackTraceString(e));
71 }
72 finish(Activity.RESULT_OK, results);
73 } else {
74 super.onStart();
75 }
76 }
77
78 private TestRequest createListTestRequest(Bundle arguments) {
79 RunnerArgs runnerArgs =
80 new RunnerArgs.Builder().fromManifest(this).fromBundle(arguments ).build();
81 TestRequestBuilder builder = new TestRequestBuilder(this, arguments);
82 builder.addApkToScan(getContext().getPackageCodePath());
83 builder.addFromRunnerArgs(runnerArgs);
84 return builder.build();
85 }
86
87 private static CharSequence getListTestFlag(Bundle arguments) {
88 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
89 }
29 } 90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698