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