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; | 13 import android.util.Log; |
14 | 14 |
15 import org.chromium.net.test.TestServerSpawner; | |
16 | |
17 import java.io.BufferedInputStream; | 15 import java.io.BufferedInputStream; |
18 import java.io.BufferedReader; | 16 import java.io.BufferedReader; |
19 import java.io.File; | 17 import java.io.File; |
20 import java.io.FileInputStream; | 18 import java.io.FileInputStream; |
21 import java.io.FileNotFoundException; | 19 import java.io.FileNotFoundException; |
22 import java.io.FileOutputStream; | |
23 import java.io.IOException; | 20 import java.io.IOException; |
24 import java.io.InputStreamReader; | 21 import java.io.InputStreamReader; |
25 import java.io.OutputStreamWriter; | |
26 import java.util.HashMap; | 22 import java.util.HashMap; |
27 import java.util.Map; | 23 import java.util.Map; |
28 import java.util.regex.Matcher; | 24 import java.util.regex.Matcher; |
29 import java.util.regex.Pattern; | 25 import java.util.regex.Pattern; |
30 | 26 |
31 /** | 27 /** |
32 * An Instrumentation that runs tests based on ChromeNativeTestActivity. | 28 * An Instrumentation that runs tests based on ChromeNativeTestActivity. |
33 */ | 29 */ |
34 public class ChromeNativeTestInstrumentationTestRunner extends Instrumentation { | 30 public class ChromeNativeTestInstrumentationTestRunner extends Instrumentation { |
35 // TODO(jbudorick): Remove this extra when b/18981674 is fixed. | 31 // TODO(jbudorick): Remove this extra when b/18981674 is fixed. |
36 public static final String EXTRA_ONLY_OUTPUT_FAILURES = | 32 public static final String EXTRA_ONLY_OUTPUT_FAILURES = |
37 "org.chromium.native_test.ChromeNativeTestInstrumentationTestRunner.
" | 33 "org.chromium.native_test.ChromeNativeTestInstrumentationTestRunner.
" |
38 + "OnlyOutputFailures"; | 34 + "OnlyOutputFailures"; |
39 public static final String EXTRA_ENABLE_TEST_SERVER_SPAWNER = | |
40 "org.chromium.native_test.ChromeNativeTestInstrumentationTestRunner.
" | |
41 + "EnableTestServerSpawner"; | |
42 | 35 |
43 private static final String TAG = "ChromeNativeTestInstrumentationTestRunner
"; | 36 private static final String TAG = "ChromeNativeTestInstrumentationTestRunner
"; |
44 | 37 |
45 private static final int ACCEPT_TIMEOUT_MS = 5000; | 38 private static final int ACCEPT_TIMEOUT_MS = 5000; |
46 private static final Pattern RE_TEST_OUTPUT = Pattern.compile("\\[ *([^ ]*)
*\\] ?([^ ]+) .*"); | 39 private static final Pattern RE_TEST_OUTPUT = Pattern.compile("\\[ *([^ ]*)
*\\] ?([^ ]+) .*"); |
47 private static final int SERVER_SPAWNER_PORT = 0; | |
48 | 40 |
49 private static interface ResultsBundleGenerator { | 41 private static interface ResultsBundleGenerator { |
50 public Bundle generate(Map<String, TestResult> rawResults); | 42 public Bundle generate(Map<String, TestResult> rawResults); |
51 } | 43 } |
52 | 44 |
53 private String mCommandLineFile; | 45 private String mCommandLineFile; |
54 private String mCommandLineFlags; | 46 private String mCommandLineFlags; |
55 private File mStdoutFile; | 47 private File mStdoutFile; |
56 private Bundle mLogBundle; | 48 private Bundle mLogBundle; |
57 private ResultsBundleGenerator mBundleGenerator; | 49 private ResultsBundleGenerator mBundleGenerator; |
58 private boolean mOnlyOutputFailures; | 50 private boolean mOnlyOutputFailures; |
59 | 51 |
60 private boolean mEnableTestServerSpawner; | |
61 private TestServerSpawner mTestServerSpawner; | |
62 private Thread mTestServerSpawnerThread; | |
63 | |
64 @Override | 52 @Override |
65 public void onCreate(Bundle arguments) { | 53 public void onCreate(Bundle arguments) { |
66 mCommandLineFile = arguments.getString(ChromeNativeTestActivity.EXTRA_CO
MMAND_LINE_FILE); | 54 mCommandLineFile = arguments.getString(ChromeNativeTestActivity.EXTRA_CO
MMAND_LINE_FILE); |
67 mCommandLineFlags = arguments.getString(ChromeNativeTestActivity.EXTRA_C
OMMAND_LINE_FLAGS); | 55 mCommandLineFlags = arguments.getString(ChromeNativeTestActivity.EXTRA_C
OMMAND_LINE_FLAGS); |
68 try { | 56 try { |
69 mStdoutFile = File.createTempFile( | 57 mStdoutFile = File.createTempFile( |
70 ".temp_stdout_", ".txt", Environment.getExternalStorageDirec
tory()); | 58 ".temp_stdout_", ".txt", Environment.getExternalStorageDirec
tory()); |
71 Log.i(TAG, "stdout file created: " + mStdoutFile.getAbsolutePath()); | 59 Log.i(TAG, "stdout file created: " + mStdoutFile.getAbsolutePath()); |
72 } catch (IOException e) { | 60 } catch (IOException e) { |
73 Log.e(TAG, "Unable to create temporary stdout file." + e.toString())
; | 61 Log.e(TAG, "Unable to create temporary stdout file." + e.toString())
; |
74 finish(Activity.RESULT_CANCELED, new Bundle()); | 62 finish(Activity.RESULT_CANCELED, new Bundle()); |
75 return; | 63 return; |
76 } | 64 } |
77 mLogBundle = new Bundle(); | 65 mLogBundle = new Bundle(); |
78 mBundleGenerator = new RobotiumBundleGenerator(); | 66 mBundleGenerator = new RobotiumBundleGenerator(); |
79 mOnlyOutputFailures = arguments.containsKey(EXTRA_ONLY_OUTPUT_FAILURES); | 67 mOnlyOutputFailures = arguments.containsKey(EXTRA_ONLY_OUTPUT_FAILURES); |
80 mEnableTestServerSpawner = arguments.containsKey(EXTRA_ENABLE_TEST_SERVE
R_SPAWNER); | |
81 mTestServerSpawner = null; | |
82 mTestServerSpawnerThread = null; | |
83 start(); | 68 start(); |
84 } | 69 } |
85 | 70 |
86 @Override | 71 @Override |
87 public void onStart() { | 72 public void onStart() { |
88 super.onStart(); | 73 super.onStart(); |
89 | |
90 setUp(); | |
91 Bundle results = runTests(); | 74 Bundle results = runTests(); |
92 tearDown(); | |
93 | |
94 finish(Activity.RESULT_OK, results); | 75 finish(Activity.RESULT_OK, results); |
95 } | 76 } |
96 | 77 |
97 private void setUp() { | |
98 if (mEnableTestServerSpawner) { | |
99 Log.i(TAG, "Test server spawner enabled."); | |
100 try { | |
101 mTestServerSpawner = new TestServerSpawner(SERVER_SPAWNER_PORT,
ACCEPT_TIMEOUT_MS); | |
102 | |
103 File portFile = new File( | |
104 Environment.getExternalStorageDirectory(), "net-test-ser
ver-ports"); | |
105 OutputStreamWriter writer = | |
106 new OutputStreamWriter(new FileOutputStream(portFile)); | |
107 writer.write(Integer.toString(mTestServerSpawner.getServerPort()
) + ":0"); | |
108 writer.close(); | |
109 | |
110 mTestServerSpawnerThread = new Thread(mTestServerSpawner); | |
111 mTestServerSpawnerThread.start(); | |
112 } catch (IOException e) { | |
113 Log.e(TAG, "Error creating TestServerSpawner: " + e.toString()); | |
114 } | |
115 } | |
116 } | |
117 | |
118 private void tearDown() { | |
119 if (mTestServerSpawnerThread != null) { | |
120 try { | |
121 mTestServerSpawner.stop(); | |
122 mTestServerSpawnerThread.join(); | |
123 } catch (InterruptedException e) { | |
124 Log.e(TAG, "Interrupted while shutting down test server spawner:
" + e.toString()); | |
125 } | |
126 } | |
127 } | |
128 | |
129 /** Runs the tests in the ChromeNativeTestActivity and returns a Bundle cont
aining the results. | 78 /** Runs the tests in the ChromeNativeTestActivity and returns a Bundle cont
aining the results. |
130 */ | 79 */ |
131 private Bundle runTests() { | 80 private Bundle runTests() { |
132 Log.i(TAG, "Creating activity."); | 81 Log.i(TAG, "Creating activity."); |
133 Activity activityUnderTest = startNativeTestActivity(); | 82 Activity activityUnderTest = startNativeTestActivity(); |
134 | 83 |
135 Log.i(TAG, "Waiting for tests to finish."); | 84 Log.i(TAG, "Waiting for tests to finish."); |
136 try { | 85 try { |
137 while (!activityUnderTest.isFinishing()) { | 86 while (!activityUnderTest.isFinishing()) { |
138 Thread.sleep(100); | 87 Thread.sleep(100); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 } else { | 221 } else { |
273 resultBuilder.append("\nOK (" + Integer.toString(testsPassed) +
" tests)"); | 222 resultBuilder.append("\nOK (" + Integer.toString(testsPassed) +
" tests)"); |
274 } | 223 } |
275 resultsBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT, | 224 resultsBundle.putString(Instrumentation.REPORT_KEY_STREAMRESULT, |
276 resultBuilder.toString()); | 225 resultBuilder.toString()); |
277 return resultsBundle; | 226 return resultsBundle; |
278 } | 227 } |
279 } | 228 } |
280 | 229 |
281 } | 230 } |
OLD | NEW |