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. |
nilesh
2012/09/18 15:18:02
I think this needs to be updated.
bulach
2012/09/19 08:43:42
Done.
| |
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 <stdarg.h> | |
14 #include <stdio.h> | 15 #include <stdio.h> |
15 | 16 |
16 #include "base/android/base_jni_registrar.h" | 17 #include "base/android/base_jni_registrar.h" |
17 #include "base/android/jni_android.h" | 18 #include "base/android/jni_android.h" |
18 #include "base/android/jni_string.h" | 19 #include "base/android/jni_string.h" |
19 #include "base/android/locale_utils.h" | 20 #include "base/android/locale_utils.h" |
20 #include "base/android/path_utils.h" | 21 #include "base/android/path_utils.h" |
21 #include "base/android/scoped_java_ref.h" | 22 #include "base/android/scoped_java_ref.h" |
22 #include "base/at_exit.h" | 23 #include "base/at_exit.h" |
23 #include "base/base_switches.h" | 24 #include "base/base_switches.h" |
24 #include "base/command_line.h" | 25 #include "base/command_line.h" |
25 #include "base/file_path.h" | 26 #include "base/file_path.h" |
26 #include "base/file_util.h" | 27 #include "base/file_util.h" |
27 #include "base/logging.h" | 28 #include "base/logging.h" |
28 #include "base/stringprintf.h" | 29 #include "base/stringprintf.h" |
29 #include "base/string_tokenizer.h" | 30 #include "base/string_tokenizer.h" |
30 #include "base/string_util.h" | 31 #include "base/string_util.h" |
31 #include "gtest/gtest.h" | 32 #include "gtest/gtest.h" |
32 #include "testing/jni/ChromeNativeTestActivity_jni.h" | 33 #include "testing/jni/ChromeNativeTestActivity_jni.h" |
33 | 34 |
34 // The main function of the program to be wrapped as a test apk. | 35 // The main function of the program to be wrapped as a test apk. |
35 extern int main(int argc, char** argv); | 36 extern int main(int argc, char** argv); |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 void log_write(int level, const char* msg) { | 40 const char kLogTag[] = "chromium"; |
40 __android_log_write(level, "chromium", msg); | 41 const char kCrashedMarker[] = "[ CRASHED ]\n"; |
42 | |
43 void AndroidLogError(const char* format, ...) { | |
44 va_list args; | |
45 va_start(args, format); | |
46 __android_log_vprint(ANDROID_LOG_ERROR, kLogTag, format, args); | |
47 va_end(args); | |
41 } | 48 } |
42 | 49 |
43 // The list of signals which are considered to be crashes. | 50 // The list of signals which are considered to be crashes. |
44 const int kExceptionSignals[] = { | 51 const int kExceptionSignals[] = { |
45 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 | 52 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 |
46 }; | 53 }; |
47 | 54 |
48 struct sigaction g_old_sa[NSIG]; | 55 struct sigaction g_old_sa[NSIG]; |
49 | 56 |
50 // This function runs in a compromised context. It should not allocate memory. | 57 // This function runs in a compromised context. It should not allocate memory. |
nilesh
2012/09/18 15:18:02
Now that we have a FIFO, we can use EOF in test sc
bulach
2012/09/19 08:43:42
Done.
| |
51 void SignalHandler(int sig, siginfo_t *info, void *reserved) | 58 void SignalHandler(int sig, siginfo_t* info, void* reserved) { |
52 { | |
53 // Output the crash marker. | 59 // Output the crash marker. |
54 log_write(ANDROID_LOG_ERROR, "[ CRASHED ]"); | 60 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker)); |
55 g_old_sa[sig].sa_sigaction(sig, info, reserved); | 61 g_old_sa[sig].sa_sigaction(sig, info, reserved); |
56 } | 62 } |
57 | 63 |
58 void InstallHandlers() { | 64 void InstallHandlers() { |
59 struct sigaction sa; | 65 struct sigaction sa; |
60 memset(&sa, 0, sizeof(sa)); | 66 memset(&sa, 0, sizeof(sa)); |
61 | 67 |
62 sa.sa_sigaction = SignalHandler; | 68 sa.sa_sigaction = SignalHandler; |
63 sa.sa_flags = SA_SIGINFO; | 69 sa.sa_flags = SA_SIGINFO; |
64 | 70 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 int argc = args.size(); | 102 int argc = args.size(); |
97 | 103 |
98 argv->resize(argc + 1); | 104 argv->resize(argc + 1); |
99 for (int i = 0; i < argc; ++i) | 105 for (int i = 0; i < argc; ++i) |
100 (*argv)[i] = const_cast<char*>(args[i].c_str()); | 106 (*argv)[i] = const_cast<char*>(args[i].c_str()); |
101 (*argv)[argc] = NULL; // argv must be NULL terminated. | 107 (*argv)[argc] = NULL; // argv must be NULL terminated. |
102 | 108 |
103 return argc; | 109 return argc; |
104 } | 110 } |
105 | 111 |
106 // As we are the native side of an Android app, we don't have any 'console', so | 112 void CreateFIFO(const char* fifo_path) { |
107 // gtest's standard output goes nowhere. | 113 unlink(fifo_path); |
108 // Instead, we inject an "EventListener" in gtest and then we print the results | 114 if (mkfifo(fifo_path, 0666)) { |
109 // using LOG, which goes to adb logcat. | 115 AndroidLogError("Failed to create fifo %s: %s\n", |
110 class AndroidLogPrinter : public ::testing::EmptyTestEventListener { | 116 fifo_path, strerror(errno)); |
111 public: | 117 exit(EXIT_FAILURE); |
112 void Init(int* argc, char** argv); | 118 } |
113 | |
114 // EmptyTestEventListener | |
115 virtual void OnTestProgramStart( | |
116 const ::testing::UnitTest& unit_test) OVERRIDE; | |
117 virtual void OnTestStart(const ::testing::TestInfo& test_info) OVERRIDE; | |
118 virtual void OnTestPartResult( | |
119 const ::testing::TestPartResult& test_part_result) OVERRIDE; | |
120 virtual void OnTestEnd(const ::testing::TestInfo& test_info) OVERRIDE; | |
121 virtual void OnTestProgramEnd(const ::testing::UnitTest& unit_test) OVERRIDE; | |
122 }; | |
123 | |
124 void AndroidLogPrinter::Init(int* argc, char** argv) { | |
125 // InitGoogleTest must be called befure we add ourselves as a listener. | |
126 ::testing::InitGoogleTest(argc, argv); | |
127 ::testing::TestEventListeners& listeners = | |
128 ::testing::UnitTest::GetInstance()->listeners(); | |
129 // Adds a listener to the end. Google Test takes the ownership. | |
130 listeners.Append(this); | |
131 } | 119 } |
132 | 120 |
133 void AndroidLogPrinter::OnTestProgramStart( | 121 void Redirect(FILE* stream, const char* path, const char* mode) { |
134 const ::testing::UnitTest& unit_test) { | 122 if (!freopen(path, mode, stream)) { |
135 std::string msg = StringPrintf("[ START ] %d", | 123 AndroidLogError("Failed to redirect stream to file: %s: %s\n", |
136 unit_test.test_to_run_count()); | 124 path, strerror(errno)); |
137 log_write(ANDROID_LOG_ERROR, msg.c_str()); | 125 exit(EXIT_FAILURE); |
126 } | |
138 } | 127 } |
139 | 128 |
140 void AndroidLogPrinter::OnTestStart(const ::testing::TestInfo& test_info) { | 129 class ScopedMainEntryLogger { |
141 std::string msg = StringPrintf("[ RUN ] %s.%s", | 130 public: |
142 test_info.test_case_name(), test_info.name()); | 131 ScopedMainEntryLogger() { |
143 log_write(ANDROID_LOG_ERROR, msg.c_str()); | 132 printf(">>ScopedMainEntryLogger\n"); |
144 } | 133 } |
145 | 134 |
146 void AndroidLogPrinter::OnTestPartResult( | 135 ~ScopedMainEntryLogger() { |
147 const ::testing::TestPartResult& test_part_result) { | 136 printf("<<ScopedMainEntryLogger\n"); |
148 std::string msg = StringPrintf( | 137 fflush(stdout); |
149 "%s in %s:%d\n%s\n", | 138 fflush(stderr); |
150 test_part_result.failed() ? "*** Failure" : "Success", | 139 } |
151 test_part_result.file_name(), | 140 }; |
152 test_part_result.line_number(), | |
153 test_part_result.summary()); | |
154 log_write(ANDROID_LOG_ERROR, msg.c_str()); | |
155 } | |
156 | |
157 void AndroidLogPrinter::OnTestEnd(const ::testing::TestInfo& test_info) { | |
158 std::string msg = StringPrintf("%s %s.%s", | |
159 test_info.result()->Failed() ? "[ FAILED ]" : "[ OK ]", | |
160 test_info.test_case_name(), test_info.name()); | |
161 log_write(ANDROID_LOG_ERROR, msg.c_str()); | |
162 } | |
163 | |
164 void AndroidLogPrinter::OnTestProgramEnd( | |
165 const ::testing::UnitTest& unit_test) { | |
166 std::string msg = StringPrintf("[ END ] %d", | |
167 unit_test.successful_test_count()); | |
168 log_write(ANDROID_LOG_ERROR, msg.c_str()); | |
169 } | |
170 | 141 |
171 } // namespace | 142 } // namespace |
172 | 143 |
173 // This method is called on a separate java thread so that we won't trigger | 144 // This method is called on a separate java thread so that we won't trigger |
174 // an ANR. | 145 // an ANR. |
175 static void RunTests(JNIEnv* env, | 146 static void RunTests(JNIEnv* env, |
176 jobject obj, | 147 jobject obj, |
177 jstring jfiles_dir, | 148 jstring jfiles_dir, |
178 jobject app_context) { | 149 jobject app_context) { |
179 base::AtExitManager exit_manager; | 150 base::AtExitManager exit_manager; |
180 | 151 |
181 // Command line initialized basically, will be fully initialized later. | 152 // Command line initialized basically, will be fully initialized later. |
182 static const char* const kInitialArgv[] = { "ChromeTestActivity" }; | 153 static const char* const kInitialArgv[] = { "ChromeTestActivity" }; |
183 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | 154 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); |
184 | 155 |
185 // Set the application context in base. | 156 // Set the application context in base. |
186 base::android::ScopedJavaLocalRef<jobject> scoped_context( | 157 base::android::ScopedJavaLocalRef<jobject> scoped_context( |
187 env, env->NewLocalRef(app_context)); | 158 env, env->NewLocalRef(app_context)); |
188 base::android::InitApplicationContext(scoped_context); | 159 base::android::InitApplicationContext(scoped_context); |
189 base::android::RegisterJni(env); | 160 base::android::RegisterJni(env); |
190 | 161 |
191 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); | 162 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); |
192 // A few options, such "--gtest_list_tests", will just use printf directly | 163 // A few options, such "--gtest_list_tests", will just use printf directly |
193 // and won't use the "AndroidLogPrinter". Redirect stdout to a known file. | 164 // Redirect stdout and stderr to a known file. |
194 FilePath stdout_path(files_dir.Append(FilePath("stdout.txt"))); | 165 FilePath fifo_path(files_dir.Append(FilePath("test.fifo"))); |
195 freopen(stdout_path.value().c_str(), "w", stdout); | 166 CreateFIFO(fifo_path.value().c_str()); |
167 Redirect(stdout, fifo_path.value().c_str(), "w"); | |
168 Redirect(stderr, fifo_path.value().c_str(), "w"); | |
196 | 169 |
197 std::vector<std::string> args; | 170 std::vector<std::string> args; |
198 ParseArgsFromCommandLineFile(&args); | 171 ParseArgsFromCommandLineFile(&args); |
199 | 172 |
200 // We need to pass in a non-const char**. | 173 // We need to pass in a non-const char**. |
201 std::vector<char*> argv; | 174 std::vector<char*> argv; |
202 int argc = ArgsToArgv(args, &argv); | 175 int argc = ArgsToArgv(args, &argv); |
203 | 176 |
204 // This object is owned by gtest. | |
205 AndroidLogPrinter* log = new AndroidLogPrinter(); | |
206 log->Init(&argc, &argv[0]); | |
207 | |
208 // Fully initialize command line with arguments. | 177 // Fully initialize command line with arguments. |
209 CommandLine::ForCurrentProcess()->AppendArguments( | 178 CommandLine::ForCurrentProcess()->AppendArguments( |
210 CommandLine(argc, &argv[0]), false); | 179 CommandLine(argc, &argv[0]), false); |
211 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 180 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
212 if (command_line.HasSwitch(switches::kWaitForDebugger)) { | 181 if (command_line.HasSwitch(switches::kWaitForDebugger)) { |
213 std::string msg = StringPrintf("Native test waiting for GDB because " | 182 std::string msg = StringPrintf("Native test waiting for GDB because " |
214 "flag %s was supplied", | 183 "flag %s was supplied", |
215 switches::kWaitForDebugger); | 184 switches::kWaitForDebugger); |
216 log_write(ANDROID_LOG_VERBOSE, msg.c_str()); | 185 __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str()); |
217 base::debug::WaitForDebugger(24 * 60 * 60, false); | 186 base::debug::WaitForDebugger(24 * 60 * 60, false); |
218 } | 187 } |
219 | 188 |
189 ScopedMainEntryLogger scoped_main_entry_logger; | |
220 main(argc, &argv[0]); | 190 main(argc, &argv[0]); |
221 } | 191 } |
222 | 192 |
223 // This is called by the VM when the shared library is first loaded. | 193 // This is called by the VM when the shared library is first loaded. |
224 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 194 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
225 // Install signal handlers to detect crashes. | 195 // Install signal handlers to detect crashes. |
226 InstallHandlers(); | 196 InstallHandlers(); |
227 | 197 |
228 base::android::InitVM(vm); | 198 base::android::InitVM(vm); |
229 JNIEnv* env = base::android::AttachCurrentThread(); | 199 JNIEnv* env = base::android::AttachCurrentThread(); |
230 if (!RegisterNativesImpl(env)) { | 200 if (!RegisterNativesImpl(env)) { |
231 return -1; | 201 return -1; |
232 } | 202 } |
233 | 203 |
234 return JNI_VERSION_1_4; | 204 return JNI_VERSION_1_4; |
235 } | 205 } |
OLD | NEW |