OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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.native_test; | 5 package org.chromium.native_test; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.app.Instrumentation; | 8 import android.app.Instrumentation; |
9 import android.content.ComponentName; | 9 import android.content.ComponentName; |
10 import android.content.Intent; | 10 import android.content.Intent; |
11 import android.os.Bundle; | 11 import android.os.Bundle; |
12 import android.os.Environment; | 12 import android.os.Environment; |
13 import android.util.Log; | |
14 | 13 |
| 14 import org.chromium.base.Log; |
15 import org.chromium.test.support.ResultsBundleGenerator; | 15 import org.chromium.test.support.ResultsBundleGenerator; |
16 import org.chromium.test.support.RobotiumBundleGenerator; | 16 import org.chromium.test.support.RobotiumBundleGenerator; |
17 | 17 |
18 import java.io.BufferedInputStream; | 18 import java.io.BufferedInputStream; |
19 import java.io.BufferedReader; | 19 import java.io.BufferedReader; |
20 import java.io.File; | 20 import java.io.File; |
21 import java.io.FileInputStream; | 21 import java.io.FileInputStream; |
22 import java.io.FileNotFoundException; | 22 import java.io.FileNotFoundException; |
23 import java.io.IOException; | 23 import java.io.IOException; |
24 import java.io.InputStreamReader; | 24 import java.io.InputStreamReader; |
25 import java.util.HashMap; | 25 import java.util.HashMap; |
26 import java.util.Map; | 26 import java.util.Map; |
27 import java.util.regex.Matcher; | 27 import java.util.regex.Matcher; |
28 import java.util.regex.Pattern; | 28 import java.util.regex.Pattern; |
29 | 29 |
30 /** | 30 /** |
31 * An Instrumentation that runs tests based on ChromeNativeTestActivity. | 31 * An Instrumentation that runs tests based on NativeTestActivity. |
32 */ | 32 */ |
33 public class ChromeNativeTestInstrumentationTestRunner extends Instrumentation { | 33 public class NativeTestInstrumentationTestRunner extends Instrumentation { |
34 // TODO(jbudorick): Remove this extra when b/18981674 is fixed. | 34 // TODO(jbudorick): Remove this extra when b/18981674 is fixed. |
35 public static final String EXTRA_ONLY_OUTPUT_FAILURES = | 35 public static final String EXTRA_ONLY_OUTPUT_FAILURES = |
36 "org.chromium.native_test.ChromeNativeTestInstrumentationTestRunner.
" | 36 "org.chromium.native_test.NativeTestInstrumentationTestRunner." |
37 + "OnlyOutputFailures"; | 37 + "OnlyOutputFailures"; |
38 | 38 |
39 private static final String TAG = "ChromeNativeTestInstrumentationTestRunner
"; | 39 private static final String TAG = Log.makeTag("native_test"); |
40 | 40 |
41 private static final int ACCEPT_TIMEOUT_MS = 5000; | 41 private static final int ACCEPT_TIMEOUT_MS = 5000; |
42 private static final Pattern RE_TEST_OUTPUT = Pattern.compile("\\[ *([^ ]*)
*\\] ?([^ ]+) .*"); | 42 private static final Pattern RE_TEST_OUTPUT = Pattern.compile("\\[ *([^ ]*)
*\\] ?([^ ]+) .*"); |
43 | 43 |
44 private String mCommandLineFile; | 44 private String mCommandLineFile; |
45 private String mCommandLineFlags; | 45 private String mCommandLineFlags; |
46 private File mStdoutFile; | 46 private File mStdoutFile; |
47 private Bundle mLogBundle; | 47 private Bundle mLogBundle; |
48 private ResultsBundleGenerator mBundleGenerator; | 48 private ResultsBundleGenerator mBundleGenerator; |
49 private boolean mOnlyOutputFailures; | 49 private boolean mOnlyOutputFailures; |
50 | 50 |
51 @Override | 51 @Override |
52 public void onCreate(Bundle arguments) { | 52 public void onCreate(Bundle arguments) { |
53 mCommandLineFile = arguments.getString(ChromeNativeTestActivity.EXTRA_CO
MMAND_LINE_FILE); | 53 mCommandLineFile = arguments.getString(NativeTestActivity.EXTRA_COMMAND_
LINE_FILE); |
54 mCommandLineFlags = arguments.getString(ChromeNativeTestActivity.EXTRA_C
OMMAND_LINE_FLAGS); | 54 mCommandLineFlags = arguments.getString(NativeTestActivity.EXTRA_COMMAND
_LINE_FLAGS); |
55 try { | 55 try { |
56 mStdoutFile = File.createTempFile( | 56 mStdoutFile = File.createTempFile( |
57 ".temp_stdout_", ".txt", Environment.getExternalStorageDirec
tory()); | 57 ".temp_stdout_", ".txt", Environment.getExternalStorageDirec
tory()); |
58 Log.i(TAG, "stdout file created: " + mStdoutFile.getAbsolutePath()); | 58 Log.i(TAG, "stdout file created: %s", mStdoutFile.getAbsolutePath())
; |
59 } catch (IOException e) { | 59 } catch (IOException e) { |
60 Log.e(TAG, "Unable to create temporary stdout file." + e.toString())
; | 60 Log.e(TAG, "Unable to create temporary stdout file.", e); |
61 finish(Activity.RESULT_CANCELED, new Bundle()); | 61 finish(Activity.RESULT_CANCELED, new Bundle()); |
62 return; | 62 return; |
63 } | 63 } |
64 mLogBundle = new Bundle(); | 64 mLogBundle = new Bundle(); |
65 mBundleGenerator = new RobotiumBundleGenerator(); | 65 mBundleGenerator = new RobotiumBundleGenerator(); |
66 mOnlyOutputFailures = arguments.containsKey(EXTRA_ONLY_OUTPUT_FAILURES); | 66 mOnlyOutputFailures = arguments.containsKey(EXTRA_ONLY_OUTPUT_FAILURES); |
67 start(); | 67 start(); |
68 } | 68 } |
69 | 69 |
70 @Override | 70 @Override |
71 public void onStart() { | 71 public void onStart() { |
72 super.onStart(); | 72 super.onStart(); |
73 Bundle results = runTests(); | 73 Bundle results = runTests(); |
74 finish(Activity.RESULT_OK, results); | 74 finish(Activity.RESULT_OK, results); |
75 } | 75 } |
76 | 76 |
77 /** Runs the tests in the ChromeNativeTestActivity and returns a Bundle cont
aining the results. | 77 /** Runs the tests in the NativeTestActivity and returns a Bundle containing
the results. |
78 */ | 78 */ |
79 private Bundle runTests() { | 79 private Bundle runTests() { |
80 Log.i(TAG, "Creating activity."); | 80 Log.i(TAG, "Creating activity."); |
81 Activity activityUnderTest = startNativeTestActivity(); | 81 Activity activityUnderTest = startNativeTestActivity(); |
82 | 82 |
83 Log.i(TAG, "Waiting for tests to finish."); | 83 Log.i(TAG, "Waiting for tests to finish."); |
84 try { | 84 try { |
85 while (!activityUnderTest.isFinishing()) { | 85 while (!activityUnderTest.isFinishing()) { |
86 Thread.sleep(100); | 86 Thread.sleep(100); |
87 } | 87 } |
88 } catch (InterruptedException e) { | 88 } catch (InterruptedException e) { |
89 Log.e(TAG, "Interrupted while waiting for activity to be destroyed:
" + e.toString()); | 89 Log.e(TAG, "Interrupted while waiting for activity to be destroyed:
", e); |
90 } | 90 } |
91 | 91 |
92 Log.i(TAG, "Getting results."); | 92 Log.i(TAG, "Getting results."); |
93 Map<String, ResultsBundleGenerator.TestResult> results = parseResults(ac
tivityUnderTest); | 93 Map<String, ResultsBundleGenerator.TestResult> results = parseResults(ac
tivityUnderTest); |
94 | 94 |
95 Log.i(TAG, "Parsing results and generating output."); | 95 Log.i(TAG, "Parsing results and generating output."); |
96 return mBundleGenerator.generate(results); | 96 return mBundleGenerator.generate(results); |
97 } | 97 } |
98 | 98 |
99 /** Starts the ChromeNativeTestActivty. | 99 /** Starts the NativeTestActivty. |
100 */ | 100 */ |
101 private Activity startNativeTestActivity() { | 101 private Activity startNativeTestActivity() { |
102 Intent i = new Intent(Intent.ACTION_MAIN); | 102 Intent i = new Intent(Intent.ACTION_MAIN); |
103 i.setComponent(new ComponentName( | 103 i.setComponent(new ComponentName( |
104 "org.chromium.native_test", | 104 "org.chromium.native_test", |
105 "org.chromium.native_test.ChromeNativeTestActivity")); | 105 "org.chromium.native_test.NativeTestActivity")); |
106 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 106 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
107 if (mCommandLineFile != null) { | 107 if (mCommandLineFile != null) { |
108 Log.i(TAG, "Passing command line file extra: " + mCommandLineFile); | 108 Log.i(TAG, "Passing command line file extra: %s", mCommandLineFile); |
109 i.putExtra(ChromeNativeTestActivity.EXTRA_COMMAND_LINE_FILE, mComman
dLineFile); | 109 i.putExtra(NativeTestActivity.EXTRA_COMMAND_LINE_FILE, mCommandLineF
ile); |
110 } | 110 } |
111 if (mCommandLineFlags != null) { | 111 if (mCommandLineFlags != null) { |
112 Log.i(TAG, "Passing command line flag extra: " + mCommandLineFlags); | 112 Log.i(TAG, "Passing command line flag extra: %s", mCommandLineFlags)
; |
113 i.putExtra(ChromeNativeTestActivity.EXTRA_COMMAND_LINE_FLAGS, mComma
ndLineFlags); | 113 i.putExtra(NativeTestActivity.EXTRA_COMMAND_LINE_FLAGS, mCommandLine
Flags); |
114 } | 114 } |
115 i.putExtra(ChromeNativeTestActivity.EXTRA_STDOUT_FILE, mStdoutFile.getAb
solutePath()); | 115 i.putExtra(NativeTestActivity.EXTRA_STDOUT_FILE, mStdoutFile.getAbsolute
Path()); |
116 return startActivitySync(i); | 116 return startActivitySync(i); |
117 } | 117 } |
118 | 118 |
119 /** | 119 /** |
120 * Generates a map between test names and test results from the instrumente
d Activity's | 120 * Generates a map between test names and test results from the instrumente
d Activity's |
121 * output. | 121 * output. |
122 */ | 122 */ |
123 private Map<String, ResultsBundleGenerator.TestResult> parseResults( | 123 private Map<String, ResultsBundleGenerator.TestResult> parseResults( |
124 Activity activityUnderTest) { | 124 Activity activityUnderTest) { |
125 Map<String, ResultsBundleGenerator.TestResult> results = | 125 Map<String, ResultsBundleGenerator.TestResult> results = |
(...skipping 29 matching lines...) Expand all Loading... |
155 | 155 |
156 // TODO(jbudorick): mOnlyOutputFailures is a workaround for b/18
981674. Remove it | 156 // TODO(jbudorick): mOnlyOutputFailures is a workaround for b/18
981674. Remove it |
157 // once that issue is fixed. | 157 // once that issue is fixed. |
158 if (!mOnlyOutputFailures || isFailure) { | 158 if (!mOnlyOutputFailures || isFailure) { |
159 mLogBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT
, l + "\n"); | 159 mLogBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT
, l + "\n"); |
160 sendStatus(0, mLogBundle); | 160 sendStatus(0, mLogBundle); |
161 } | 161 } |
162 Log.i(TAG, l); | 162 Log.i(TAG, l); |
163 } | 163 } |
164 } catch (FileNotFoundException e) { | 164 } catch (FileNotFoundException e) { |
165 Log.e(TAG, "Couldn't find stdout file file: " + e.toString()); | 165 Log.e(TAG, "Couldn't find stdout file file: ", e); |
166 } catch (IOException e) { | 166 } catch (IOException e) { |
167 Log.e(TAG, "Error handling stdout file: " + e.toString()); | 167 Log.e(TAG, "Error handling stdout file: ", e); |
168 } finally { | 168 } finally { |
169 if (r != null) { | 169 if (r != null) { |
170 try { | 170 try { |
171 r.close(); | 171 r.close(); |
172 } catch (IOException e) { | 172 } catch (IOException e) { |
173 Log.e(TAG, "Error while closing stdout reader."); | 173 Log.e(TAG, "Error while closing stdout reader.", e); |
174 } | 174 } |
175 } | 175 } |
176 if (mStdoutFile != null) { | 176 if (mStdoutFile != null) { |
177 if (!mStdoutFile.delete()) { | 177 if (!mStdoutFile.delete()) { |
178 Log.e(TAG, "Unable to delete " + mStdoutFile.getAbsolutePath
()); | 178 Log.e(TAG, "Unable to delete %s", mStdoutFile.getAbsolutePat
h()); |
179 } | 179 } |
180 } | 180 } |
181 } | 181 } |
182 return results; | 182 return results; |
183 } | 183 } |
184 | 184 |
185 } | 185 } |
OLD | NEW |