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 |
| 6 // android application. It outputs (to logcat) markers identifying the |
| 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. |
| 9 // It injects an event listener in gtest to detect various test stages and |
| 10 // installs signal handlers to detect crashes. |
| 11 |
| 12 #include <android/log.h> |
| 13 #include <signal.h> |
5 #include <stdio.h> | 14 #include <stdio.h> |
6 | 15 |
7 #include "base/android/jni_android.h" | 16 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 17 #include "base/android/jni_string.h" |
9 #include "base/android/locale_utils.h" | 18 #include "base/android/locale_utils.h" |
10 #include "base/android/path_utils.h" | 19 #include "base/android/path_utils.h" |
11 #include "base/android/scoped_java_ref.h" | 20 #include "base/android/scoped_java_ref.h" |
12 #include "base/at_exit.h" | 21 #include "base/at_exit.h" |
13 #include "base/command_line.h" | 22 #include "base/command_line.h" |
14 #include "base/file_path.h" | 23 #include "base/file_path.h" |
15 #include "base/file_util.h" | 24 #include "base/file_util.h" |
16 #include "base/logging.h" | 25 #include "base/logging.h" |
17 #include "base/stringprintf.h" | 26 #include "base/stringprintf.h" |
18 #include "base/string_tokenizer.h" | 27 #include "base/string_tokenizer.h" |
19 #include "base/string_util.h" | 28 #include "base/string_util.h" |
20 #include "base/test/test_suite.h" | 29 #include "base/test/test_suite.h" |
21 #include "testing/android/jni/chrome_native_test_activity_jni.h" | 30 #include "testing/android/jni/chrome_native_test_activity_jni.h" |
22 #include "gtest/gtest.h" | 31 #include "gtest/gtest.h" |
23 | 32 |
24 // GTest's main function. | 33 // GTest's main function. |
25 extern int main(int argc, char** argv); | 34 extern int main(int argc, char** argv); |
26 | 35 |
27 namespace { | 36 namespace { |
28 | 37 |
| 38 // The list of signals which are considered to be crashes. |
| 39 const int kExceptionSignals[] = { |
| 40 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 |
| 41 }; |
| 42 |
| 43 struct sigaction g_old_sa[NSIG]; |
| 44 |
| 45 // This function runs in a compromised context. It should not allocate memory. |
| 46 void SignalHandler(int sig, siginfo_t *info, void *reserved) |
| 47 { |
| 48 // Output the crash marker. |
| 49 __android_log_write(ANDROID_LOG_ERROR, "chromium", "[ CRASHED ]"); |
| 50 g_old_sa[sig].sa_sigaction(sig, info, reserved); |
| 51 } |
| 52 |
| 53 void InstallHandlers() { |
| 54 struct sigaction sa; |
| 55 memset(&sa, 0, sizeof(sa)); |
| 56 |
| 57 sa.sa_sigaction = SignalHandler; |
| 58 sa.sa_flags = SA_SIGINFO; |
| 59 |
| 60 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) { |
| 61 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]); |
| 62 } |
| 63 } |
| 64 |
29 void ParseArgsFromString(const std::string& command_line, | 65 void ParseArgsFromString(const std::string& command_line, |
30 std::vector<std::string>* args) { | 66 std::vector<std::string>* args) { |
31 StringTokenizer tokenizer(command_line, kWhitespaceASCII); | 67 StringTokenizer tokenizer(command_line, kWhitespaceASCII); |
32 tokenizer.set_quote_chars("\""); | 68 tokenizer.set_quote_chars("\""); |
33 while (tokenizer.GetNext()) { | 69 while (tokenizer.GetNext()) { |
34 std::string token; | 70 std::string token; |
35 RemoveChars(tokenizer.token(), "\"", &token); | 71 RemoveChars(tokenizer.token(), "\"", &token); |
36 args->push_back(token); | 72 args->push_back(token); |
37 } | 73 } |
38 } | 74 } |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 // Set the application context in base. | 220 // Set the application context in base. |
185 base::android::ScopedJavaLocalRef<jobject> scoped_context( | 221 base::android::ScopedJavaLocalRef<jobject> scoped_context( |
186 env, env->NewLocalRef(app_context)); | 222 env, env->NewLocalRef(app_context)); |
187 base::android::InitApplicationContext(scoped_context); | 223 base::android::InitApplicationContext(scoped_context); |
188 | 224 |
189 main(argc, &argv[0]); | 225 main(argc, &argv[0]); |
190 } | 226 } |
191 | 227 |
192 // This is called by the VM when the shared library is first loaded. | 228 // This is called by the VM when the shared library is first loaded. |
193 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 229 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 230 |
| 231 // Install signal handlers to detect crashes. |
| 232 InstallHandlers(); |
| 233 |
194 base::android::InitVM(vm); | 234 base::android::InitVM(vm); |
195 JNIEnv* env = base::android::AttachCurrentThread(); | 235 JNIEnv* env = base::android::AttachCurrentThread(); |
196 if (!RegisterNativesImpl(env)) { | 236 if (!RegisterNativesImpl(env)) { |
197 return -1; | 237 return -1; |
198 } | 238 } |
199 LibraryLoadedOnMainThread(env); | 239 LibraryLoadedOnMainThread(env); |
| 240 |
200 return JNI_VERSION_1_4; | 241 return JNI_VERSION_1_4; |
201 } | 242 } |
OLD | NEW |