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

Side by Side Diff: testing/android/java/src/org/chromium/native_test/ChromiumNativeTestInstrumentationTestRunner.java

Issue 723343002: Update from https://crrev.com/304121 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.native_test;
6
7 import android.app.Activity;
8 import android.app.Instrumentation;
9 import android.content.ComponentName;
10 import android.content.Intent;
11 import android.os.Bundle;
12 import android.util.Log;
13
14 import java.io.BufferedInputStream;
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.util.HashMap;
22 import java.util.Map;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 /**
27 * An Instrumentation that runs tests based on ChromeNativeTestActivity.
28 */
29 public class ChromiumNativeTestInstrumentationTestRunner extends Instrumentation {
30
31 private static final String TAG = "ChromiumNativeTestInstrumentationTestRunn er";
32 private static final Pattern RE_TEST_OUTPUT = Pattern.compile("\\[ *([^ ]*) *\\] ?([^ ]*) .*");
33
34 private static interface ResultsBundleGenerator {
35 public Bundle generate(Map<String, TestResult> rawResults);
36 }
37
38 private String mCommandLineFile;
39 private String mCommandLineFlags;
40 private Bundle mLogBundle;
41 private ResultsBundleGenerator mBundleGenerator;
42
43 @Override
44 public void onCreate(Bundle arguments) {
45 mCommandLineFile = arguments.getString(ChromeNativeTestActivity.EXTRA_CO MMAND_LINE_FILE);
46 mCommandLineFlags = arguments.getString(ChromeNativeTestActivity.EXTRA_C OMMAND_LINE_FLAGS);
47 mLogBundle = new Bundle();
48 mBundleGenerator = new RobotiumBundleGenerator();
49 start();
50 }
51
52 @Override
53 public void onStart() {
54 super.onStart();
55 Bundle results = runTests();
56 finish(Activity.RESULT_OK, results);
57 }
58
59 /** Runs the tests in the ChromeNativeTestActivity and returns a Bundle cont aining the results.
60 */
61 private Bundle runTests() {
62 Log.i(TAG, "Creating activity.");
63 Activity activityUnderTest = startNativeTestActivity();
64
65 Log.i(TAG, "Getting results from FIFO.");
66 Map<String, TestResult> results = parseResultsFromFifo(activityUnderTest );
67
68 Log.i(TAG, "Finishing activity.");
69 activityUnderTest.finish();
70
71 Log.i(TAG, "Parsing results and generating output.");
72 return mBundleGenerator.generate(results);
73 }
74
75 /** Starts the ChromeNativeTestActivty.
76 */
77 private Activity startNativeTestActivity() {
78 Intent i = new Intent(Intent.ACTION_MAIN);
79 i.setComponent(new ComponentName(
80 "org.chromium.native_test",
81 "org.chromium.native_test.ChromeNativeTestActivity"));
82 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83 if (mCommandLineFile != null) {
84 Log.i(TAG, "Passing command line file extra: " + mCommandLineFile);
85 i.putExtra(ChromeNativeTestActivity.EXTRA_COMMAND_LINE_FILE, mComman dLineFile);
86 }
87 if (mCommandLineFlags != null) {
88 Log.i(TAG, "Passing command line flag extra: " + mCommandLineFlags);
89 i.putExtra(ChromeNativeTestActivity.EXTRA_COMMAND_LINE_FLAGS, mComma ndLineFlags);
90 }
91 return startActivitySync(i);
92 }
93
94 private static enum TestResult {
95 PASSED, FAILED, ERROR, UNKNOWN
96 }
97
98 /**
99 * Generates a map between test names and test results from the instrumente d Activity's FIFO.
100 */
101 private Map<String, TestResult> parseResultsFromFifo(Activity activityUnderT est) {
102 Map<String, TestResult> results = new HashMap<String, TestResult>();
103
104 File fifo = null;
105 BufferedReader r = null;
106
107 try {
108 // Wait for the test to create the FIFO.
109 fifo = new File(getTargetContext().getFilesDir().getAbsolutePath(), "test.fifo");
110 while (!fifo.exists()) {
111 Thread.sleep(1000);
112 }
113
114 r = new BufferedReader(
115 new InputStreamReader(new BufferedInputStream(new FileInputS tream(fifo))));
116
117 StringBuilder resultsStr = new StringBuilder();
118 for (String l = r.readLine(); l != null && !l.equals("<<ScopedMainEn tryLogger");
119 l = r.readLine()) {
120 Matcher m = RE_TEST_OUTPUT.matcher(l);
121 if (m.matches()) {
122 if (m.group(1).equals("RUN")) {
123 results.put(m.group(2), TestResult.UNKNOWN);
124 } else if (m.group(1).equals("FAILED")) {
125 results.put(m.group(2), TestResult.FAILED);
126 } else if (m.group(1).equals("OK")) {
127 results.put(m.group(2), TestResult.PASSED);
128 }
129 }
130 resultsStr.append(l);
131 resultsStr.append("\n");
132 }
133 mLogBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT, result sStr.toString());
134 sendStatus(0, mLogBundle);
135 } catch (InterruptedException e) {
136 Log.e(TAG, "Interrupted while waiting for FIFO file creation: " + e. toString());
137 } catch (FileNotFoundException e) {
138 Log.e(TAG, "Couldn't find FIFO file: " + e.toString());
139 } catch (IOException e) {
140 Log.e(TAG, "Error handling FIFO file: " + e.toString());
141 } finally {
142 if (r != null) {
143 try {
144 r.close();
145 } catch (IOException e) {
146 Log.e(TAG, "Error while closing FIFO reader.");
147 }
148 }
149 if (fifo != null) {
150 if (!fifo.delete()) {
151 Log.e(TAG, "Unable to delete " + fifo.getAbsolutePath());
152 }
153 }
154 }
155 return results;
156 }
157
158 /**
159 * Creates a results bundle that emulates the one created by Robotium.
160 */
161 private static class RobotiumBundleGenerator implements ResultsBundleGenerat or {
162 public Bundle generate(Map<String, TestResult> rawResults) {
163 Bundle resultsBundle = new Bundle();
164
165 int testsPassed = 0;
166 int testsFailed = 0;
167
168 for (Map.Entry<String, TestResult> entry : rawResults.entrySet()) {
169 switch (entry.getValue()) {
170 case PASSED:
171 ++testsPassed;
172 break;
173 case FAILED:
174 ++testsFailed;
175 break;
176 default:
177 Log.w(TAG, "Unhandled: " + entry.getKey() + ", "
178 + entry.getValue().toString());
179 break;
180 }
181 }
182
183 StringBuilder resultBuilder = new StringBuilder();
184 resultBuilder.append("\nOK (" + Integer.toString(testsPassed) + " te sts)");
185 if (testsFailed > 0) {
186 resultBuilder.append(
187 "\nFAILURES!!! Tests run: " + Integer.toString(rawResult s.size())
188 + ", Failures: " + Integer.toString(testsFailed) + ", Er rors: 0");
189 }
190 resultsBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT,
191 resultBuilder.toString());
192 return resultsBundle;
193 }
194 }
195
196 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698