OLD | NEW |
| (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 for (String l = r.readLine(); l != null && !l.equals("<<ScopedMainEn
tryLogger"); | |
118 l = r.readLine()) { | |
119 Matcher m = RE_TEST_OUTPUT.matcher(l); | |
120 if (m.matches()) { | |
121 if (m.group(1).equals("RUN")) { | |
122 results.put(m.group(2), TestResult.UNKNOWN); | |
123 } else if (m.group(1).equals("FAILED")) { | |
124 results.put(m.group(2), TestResult.FAILED); | |
125 } else if (m.group(1).equals("OK")) { | |
126 results.put(m.group(2), TestResult.PASSED); | |
127 } | |
128 } | |
129 mLogBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT, l
+ "\n"); | |
130 sendStatus(0, mLogBundle); | |
131 } | |
132 } catch (InterruptedException e) { | |
133 Log.e(TAG, "Interrupted while waiting for FIFO file creation: " + e.
toString()); | |
134 } catch (FileNotFoundException e) { | |
135 Log.e(TAG, "Couldn't find FIFO file: " + e.toString()); | |
136 } catch (IOException e) { | |
137 Log.e(TAG, "Error handling FIFO file: " + e.toString()); | |
138 } finally { | |
139 if (r != null) { | |
140 try { | |
141 r.close(); | |
142 } catch (IOException e) { | |
143 Log.e(TAG, "Error while closing FIFO reader."); | |
144 } | |
145 } | |
146 if (fifo != null) { | |
147 if (!fifo.delete()) { | |
148 Log.e(TAG, "Unable to delete " + fifo.getAbsolutePath()); | |
149 } | |
150 } | |
151 } | |
152 return results; | |
153 } | |
154 | |
155 /** | |
156 * Creates a results bundle that emulates the one created by Robotium. | |
157 */ | |
158 private static class RobotiumBundleGenerator implements ResultsBundleGenerat
or { | |
159 public Bundle generate(Map<String, TestResult> rawResults) { | |
160 Bundle resultsBundle = new Bundle(); | |
161 | |
162 int testsPassed = 0; | |
163 int testsFailed = 0; | |
164 | |
165 for (Map.Entry<String, TestResult> entry : rawResults.entrySet()) { | |
166 switch (entry.getValue()) { | |
167 case PASSED: | |
168 ++testsPassed; | |
169 break; | |
170 case FAILED: | |
171 ++testsFailed; | |
172 break; | |
173 default: | |
174 Log.w(TAG, "Unhandled: " + entry.getKey() + ", " | |
175 + entry.getValue().toString()); | |
176 break; | |
177 } | |
178 } | |
179 | |
180 StringBuilder resultBuilder = new StringBuilder(); | |
181 resultBuilder.append("\nOK (" + Integer.toString(testsPassed) + " te
sts)"); | |
182 if (testsFailed > 0) { | |
183 resultBuilder.append( | |
184 "\nFAILURES!!! Tests run: " + Integer.toString(rawResult
s.size()) | |
185 + ", Failures: " + Integer.toString(testsFailed) + ", Er
rors: 0"); | |
186 } | |
187 resultsBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT, | |
188 resultBuilder.toString()); | |
189 return resultsBundle; | |
190 } | |
191 } | |
192 | |
193 } | |
OLD | NEW |