OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This class sets up the environment for running the native tests inside an | 5 // This class sets up the environment for running the native tests inside an |
6 // android application. It outputs (to logcat) markers identifying the | 6 // android application. It outputs (to logcat) markers identifying the |
7 // START/END/CRASH of the test suite, FAILURE/SUCCESS of individual tests etc. | 7 // START/END/CRASH of the test suite, FAILURE/SUCCESS of individual tests etc. |
8 // These markers are read by the test runner script to generate test results. | 8 // These markers are read by the test runner script to generate test results. |
9 // It injects an event listener in gtest to detect various test stages and | 9 // It injects an event listener in gtest to detect various test stages and |
10 // installs signal handlers to detect crashes. | 10 // installs signal handlers to detect crashes. |
11 | 11 |
12 #include <android/log.h> | 12 #include <android/log.h> |
13 #include <signal.h> | 13 #include <signal.h> |
14 #include <stdio.h> | 14 #include <stdio.h> |
15 | 15 |
16 #include "base/android/jni_android.h" | 16 #include "base/android/jni_android.h" |
17 #include "base/android/jni_string.h" | 17 #include "base/android/jni_string.h" |
18 #include "base/android/locale_utils.h" | 18 #include "base/android/locale_utils.h" |
19 #include "base/android/path_utils.h" | 19 #include "base/android/path_utils.h" |
20 #include "base/android/scoped_java_ref.h" | 20 #include "base/android/scoped_java_ref.h" |
21 #include "base/at_exit.h" | 21 #include "base/at_exit.h" |
22 #include "base/command_line.h" | 22 #include "base/command_line.h" |
23 #include "base/file_path.h" | 23 #include "base/file_path.h" |
24 #include "base/file_util.h" | 24 #include "base/file_util.h" |
25 #include "base/logging.h" | 25 #include "base/logging.h" |
26 #include "base/stringprintf.h" | 26 #include "base/stringprintf.h" |
27 #include "base/string_tokenizer.h" | 27 #include "base/string_tokenizer.h" |
28 #include "base/string_util.h" | 28 #include "base/string_util.h" |
29 #include "base/test/test_suite.h" | 29 #include "base/test/test_support_android.h" |
| 30 #include "gtest/gtest.h" |
| 31 #include "net/android/network_library.h" |
30 #include "testing/android/jni/chrome_native_test_activity_jni.h" | 32 #include "testing/android/jni/chrome_native_test_activity_jni.h" |
31 #include "gtest/gtest.h" | |
32 | 33 |
33 // GTest's main function. | 34 // The main function of the program to be wrapped as a test apk. |
34 extern int main(int argc, char** argv); | 35 extern int main(int argc, char** argv); |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
38 // The list of signals which are considered to be crashes. | 39 // The list of signals which are considered to be crashes. |
39 const int kExceptionSignals[] = { | 40 const int kExceptionSignals[] = { |
40 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 | 41 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 |
41 }; | 42 }; |
42 | 43 |
43 struct sigaction g_old_sa[NSIG]; | 44 struct sigaction g_old_sa[NSIG]; |
(...skipping 23 matching lines...) Expand all Loading... |
67 StringTokenizer tokenizer(command_line, kWhitespaceASCII); | 68 StringTokenizer tokenizer(command_line, kWhitespaceASCII); |
68 tokenizer.set_quote_chars("\""); | 69 tokenizer.set_quote_chars("\""); |
69 while (tokenizer.GetNext()) { | 70 while (tokenizer.GetNext()) { |
70 std::string token; | 71 std::string token; |
71 RemoveChars(tokenizer.token(), "\"", &token); | 72 RemoveChars(tokenizer.token(), "\"", &token); |
72 args->push_back(token); | 73 args->push_back(token); |
73 } | 74 } |
74 } | 75 } |
75 | 76 |
76 void ParseArgsFromCommandLineFile(std::vector<std::string>* args) { | 77 void ParseArgsFromCommandLineFile(std::vector<std::string>* args) { |
77 // The test runner script can write to "/data/local/tmp". | 78 // The test runner script writes the command line file in |
| 79 // "/data/local/tmp". |
78 static const char kCommandLineFilePath[] = | 80 static const char kCommandLineFilePath[] = |
79 "/data/local/tmp/chrome-native-tests-command-line"; | 81 "/data/local/tmp/chrome-native-tests-command-line"; |
80 FilePath command_line(kCommandLineFilePath); | 82 FilePath command_line(kCommandLineFilePath); |
81 std::string command_line_string; | 83 std::string command_line_string; |
82 if (file_util::ReadFileToString(command_line, &command_line_string)) { | 84 if (file_util::ReadFileToString(command_line, &command_line_string)) { |
83 ParseArgsFromString(command_line_string, args); | 85 ParseArgsFromString(command_line_string, args); |
84 } | 86 } |
85 } | 87 } |
86 | 88 |
87 void ArgsToArgv(const std::vector<std::string>& args, | 89 void ArgsToArgv(const std::vector<std::string>& args, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 LOG(ERROR) << msg; | 153 LOG(ERROR) << msg; |
152 } | 154 } |
153 | 155 |
154 void AndroidLogPrinter::OnTestProgramEnd( | 156 void AndroidLogPrinter::OnTestProgramEnd( |
155 const ::testing::UnitTest& unit_test) { | 157 const ::testing::UnitTest& unit_test) { |
156 std::string msg = StringPrintf("[ END ] %d", | 158 std::string msg = StringPrintf("[ END ] %d", |
157 unit_test.successful_test_count()); | 159 unit_test.successful_test_count()); |
158 LOG(ERROR) << msg; | 160 LOG(ERROR) << msg; |
159 } | 161 } |
160 | 162 |
161 void LibraryLoadedOnMainThread(JNIEnv* env) { | |
162 static const char* const kInitialArgv[] = { "ChromeTestActivity" }; | |
163 | |
164 { | |
165 // We need a test suite to be created before we do any tracing or | |
166 // logging: it creates a global at_exit_manager and initializes | |
167 // internal gtest data structures based on the command line. | |
168 // It needs to be scoped as it also resets the CommandLine. | |
169 std::vector<std::string> args; | |
170 ParseArgsFromCommandLineFile(&args); | |
171 std::vector<char*> argv; | |
172 ArgsToArgv(args, &argv); | |
173 base::TestSuite test_suite(argv.size(), &argv[0]); | |
174 } | |
175 | |
176 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | |
177 | |
178 logging::InitLogging(NULL, | |
179 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | |
180 logging::DONT_LOCK_LOG_FILE, | |
181 logging::DELETE_OLD_LOG_FILE, | |
182 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); | |
183 // To view log output with IDs and timestamps use "adb logcat -v threadtime". | |
184 logging::SetLogItems(false, // Process ID | |
185 false, // Thread ID | |
186 false, // Timestamp | |
187 false); // Tick count | |
188 VLOG(0) << "Chromium logging enabled: level = " << logging::GetMinLogLevel() | |
189 << ", default verbosity = " << logging::GetVlogVerbosity(); | |
190 base::android::RegisterLocaleUtils(env); | |
191 base::android::RegisterPathUtils(env); | |
192 } | |
193 | |
194 } // namespace | 163 } // namespace |
195 | 164 |
196 // This method is called on a separate java thread so that we won't trigger | 165 // This method is called on a separate java thread so that we won't trigger |
197 // an ANR. | 166 // an ANR. |
198 static void RunTests(JNIEnv* env, | 167 static void RunTests(JNIEnv* env, |
199 jobject obj, | 168 jobject obj, |
200 jstring jfiles_dir, | 169 jstring jfiles_dir, |
201 jobject app_context) { | 170 jobject app_context) { |
| 171 base::AtExitManager exit_manager; |
| 172 |
| 173 static const char* const kInitialArgv[] = { "ChromeTestActivity" }; |
| 174 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); |
| 175 |
| 176 // Set the application context in base. |
| 177 base::android::ScopedJavaLocalRef<jobject> scoped_context( |
| 178 env, env->NewLocalRef(app_context)); |
| 179 base::android::InitApplicationContext(scoped_context); |
| 180 |
| 181 base::android::RegisterLocaleUtils(env); |
| 182 base::android::RegisterPathUtils(env); |
| 183 net::android::RegisterNetworkLibrary(env); |
| 184 |
| 185 InitAndroidTest(); |
| 186 |
202 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); | 187 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); |
203 // A few options, such "--gtest_list_tests", will just use printf directly | 188 // A few options, such "--gtest_list_tests", will just use printf directly |
204 // and won't use the "AndroidLogPrinter". Redirect stdout to a known file. | 189 // and won't use the "AndroidLogPrinter". Redirect stdout to a known file. |
205 FilePath stdout_path(files_dir.Append(FilePath("stdout.txt"))); | 190 FilePath stdout_path(files_dir.Append(FilePath("stdout.txt"))); |
206 freopen(stdout_path.value().c_str(), "w", stdout); | 191 freopen(stdout_path.value().c_str(), "w", stdout); |
207 | 192 |
208 std::vector<std::string> args; | 193 std::vector<std::string> args; |
209 ParseArgsFromCommandLineFile(&args); | 194 ParseArgsFromCommandLineFile(&args); |
210 | 195 |
211 // We need to pass in a non-const char**. | 196 // We need to pass in a non-const char**. |
212 std::vector<char*> argv; | 197 std::vector<char*> argv; |
213 ArgsToArgv(args, &argv); | 198 ArgsToArgv(args, &argv); |
214 | 199 |
215 int argc = argv.size(); | 200 int argc = argv.size(); |
216 // This object is owned by gtest. | 201 // This object is owned by gtest. |
217 AndroidLogPrinter* log = new AndroidLogPrinter(); | 202 AndroidLogPrinter* log = new AndroidLogPrinter(); |
218 log->Init(&argc, &argv[0]); | 203 log->Init(&argc, &argv[0]); |
219 | 204 |
220 // Set the application context in base. | |
221 base::android::ScopedJavaLocalRef<jobject> scoped_context( | |
222 env, env->NewLocalRef(app_context)); | |
223 base::android::InitApplicationContext(scoped_context); | |
224 | |
225 main(argc, &argv[0]); | 205 main(argc, &argv[0]); |
226 } | 206 } |
227 | 207 |
228 // This is called by the VM when the shared library is first loaded. | 208 // This is called by the VM when the shared library is first loaded. |
229 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 209 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
230 | |
231 // Install signal handlers to detect crashes. | 210 // Install signal handlers to detect crashes. |
232 InstallHandlers(); | 211 InstallHandlers(); |
233 | 212 |
234 base::android::InitVM(vm); | 213 base::android::InitVM(vm); |
235 JNIEnv* env = base::android::AttachCurrentThread(); | 214 JNIEnv* env = base::android::AttachCurrentThread(); |
236 if (!RegisterNativesImpl(env)) { | 215 if (!RegisterNativesImpl(env)) { |
237 return -1; | 216 return -1; |
238 } | 217 } |
239 LibraryLoadedOnMainThread(env); | |
240 | 218 |
241 return JNI_VERSION_1_4; | 219 return JNI_VERSION_1_4; |
242 } | 220 } |
OLD | NEW |