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 a fifo) markers identifying the | 6 // android application. It outputs (to a fifo) markers identifying the |
7 // START/PASSED/CRASH of the test suite, FAILURE/SUCCESS of individual tests, | 7 // START/PASSED/CRASH of the test suite, FAILURE/SUCCESS of individual tests, |
8 // etc. | 8 // etc. |
9 // These markers are read by the test runner script to generate test results. | 9 // These markers are read by the test runner script to generate test results. |
10 // It installs signal handlers to detect crashes. | 10 // It 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 | 14 |
15 #include "base/android/base_jni_registrar.h" | 15 #include "base/android/base_jni_registrar.h" |
| 16 #include "base/android/fifo_utils.h" |
16 #include "base/android/jni_android.h" | 17 #include "base/android/jni_android.h" |
17 #include "base/android/jni_string.h" | 18 #include "base/android/jni_string.h" |
18 #include "base/android/scoped_java_ref.h" | 19 #include "base/android/scoped_java_ref.h" |
19 #include "base/at_exit.h" | 20 #include "base/at_exit.h" |
20 #include "base/base_switches.h" | 21 #include "base/base_switches.h" |
21 #include "base/command_line.h" | 22 #include "base/command_line.h" |
| 23 #include "base/file_util.h" |
22 #include "base/files/file_path.h" | 24 #include "base/files/file_path.h" |
23 #include "base/logging.h" | 25 #include "base/logging.h" |
24 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
25 #include "gtest/gtest.h" | 27 #include "gtest/gtest.h" |
26 #include "testing/android/native_test_util.h" | 28 #include "testing/android/native_test_util.h" |
27 #include "testing/jni/ChromeNativeTestActivity_jni.h" | 29 #include "testing/jni/ChromeNativeTestActivity_jni.h" |
28 | 30 |
29 using testing::native_test_util::ArgsToArgv; | 31 using testing::native_test_util::ArgsToArgv; |
30 using testing::native_test_util::CreateFIFO; | |
31 using testing::native_test_util::ParseArgsFromCommandLineFile; | 32 using testing::native_test_util::ParseArgsFromCommandLineFile; |
32 using testing::native_test_util::RedirectStream; | |
33 using testing::native_test_util::ScopedMainEntryLogger; | 33 using testing::native_test_util::ScopedMainEntryLogger; |
34 | 34 |
35 // 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. |
36 extern int main(int argc, char** argv); | 36 extern int main(int argc, char** argv); |
37 | 37 |
38 namespace { | 38 namespace { |
39 | 39 |
40 // These two command line flags are supported for DumpRenderTree, which needs | 40 // These two command line flags are supported for DumpRenderTree, which needs |
41 // three fifos rather than a combined one: one for stderr, stdin and stdout. | 41 // three fifos rather than a combined one: one for stderr, stdin and stdout. |
42 const char kSeparateStderrFifo[] = "separate-stderr-fifo"; | 42 const char kSeparateStderrFifo[] = "separate-stderr-fifo"; |
(...skipping 28 matching lines...) Expand all Loading... |
71 memset(&sa, 0, sizeof(sa)); | 71 memset(&sa, 0, sizeof(sa)); |
72 | 72 |
73 sa.sa_sigaction = SignalHandler; | 73 sa.sa_sigaction = SignalHandler; |
74 sa.sa_flags = SA_SIGINFO; | 74 sa.sa_flags = SA_SIGINFO; |
75 | 75 |
76 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) { | 76 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) { |
77 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]); | 77 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
| 81 // Writes printf() style string to Android's logger where |priority| is one of |
| 82 // the levels defined in <android/log.h>. |
| 83 void AndroidLog(int priority, const char* format, ...) { |
| 84 va_list args; |
| 85 va_start(args, format); |
| 86 __android_log_vprint(priority, kLogTag, format, args); |
| 87 va_end(args); |
| 88 } |
| 89 |
| 90 // Ensures that the fifo at |path| is created by deleting whatever is at |path| |
| 91 // prior to (re)creating the fifo, otherwise logs the error and terminates the |
| 92 // program. |
| 93 void EnsureCreateFIFO(const base::FilePath& path) { |
| 94 unlink(path.value().c_str()); |
| 95 if (base::android::CreateFIFO(path, 0666)) |
| 96 return; |
| 97 |
| 98 AndroidLog(ANDROID_LOG_ERROR, "Failed to create fifo %s: %s\n", |
| 99 path.value().c_str(), strerror(errno)); |
| 100 exit(EXIT_FAILURE); |
| 101 } |
| 102 |
| 103 // Ensures that |stream| is redirected to |path|, otherwise logs the error and |
| 104 // terminates the program. |
| 105 void EnsureRedirectStream(FILE* stream, |
| 106 const base::FilePath& path, |
| 107 const char* mode) { |
| 108 if (base::android::RedirectStream(stream, path, mode)) |
| 109 return; |
| 110 |
| 111 AndroidLog(ANDROID_LOG_ERROR, "Failed to redirect stream to file: %s: %s\n", |
| 112 path.value().c_str(), strerror(errno)); |
| 113 exit(EXIT_FAILURE); |
| 114 } |
| 115 |
81 } // namespace | 116 } // namespace |
82 | 117 |
83 // This method is called on a separate java thread so that we won't trigger | 118 // This method is called on a separate java thread so that we won't trigger |
84 // an ANR. | 119 // an ANR. |
85 static void RunTests(JNIEnv* env, | 120 static void RunTests(JNIEnv* env, |
86 jobject obj, | 121 jobject obj, |
87 jstring jfiles_dir, | 122 jstring jfiles_dir, |
88 jobject app_context) { | 123 jobject app_context) { |
89 base::AtExitManager exit_manager; | 124 base::AtExitManager exit_manager; |
90 | 125 |
(...skipping 17 matching lines...) Expand all Loading... |
108 CommandLine::ForCurrentProcess()->AppendArguments( | 143 CommandLine::ForCurrentProcess()->AppendArguments( |
109 CommandLine(argc, &argv[0]), false); | 144 CommandLine(argc, &argv[0]), false); |
110 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 145 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
111 | 146 |
112 base::FilePath files_dir( | 147 base::FilePath files_dir( |
113 base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); | 148 base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); |
114 | 149 |
115 // A few options, such "--gtest_list_tests", will just use printf directly | 150 // A few options, such "--gtest_list_tests", will just use printf directly |
116 // Always redirect stdout to a known file. | 151 // Always redirect stdout to a known file. |
117 base::FilePath fifo_path(files_dir.Append(base::FilePath("test.fifo"))); | 152 base::FilePath fifo_path(files_dir.Append(base::FilePath("test.fifo"))); |
118 CreateFIFO(fifo_path.value().c_str()); | 153 EnsureCreateFIFO(fifo_path); |
119 | 154 |
120 base::FilePath stderr_fifo_path, stdin_fifo_path; | 155 base::FilePath stderr_fifo_path, stdin_fifo_path; |
121 | 156 |
122 // DumpRenderTree needs a separate fifo for the stderr output. For all | 157 // DumpRenderTree needs a separate fifo for the stderr output. For all |
123 // other tests, insert stderr content to the same fifo we use for stdout. | 158 // other tests, insert stderr content to the same fifo we use for stdout. |
124 if (command_line.HasSwitch(kSeparateStderrFifo)) { | 159 if (command_line.HasSwitch(kSeparateStderrFifo)) { |
125 stderr_fifo_path = files_dir.Append(base::FilePath("stderr.fifo")); | 160 stderr_fifo_path = files_dir.Append(base::FilePath("stderr.fifo")); |
126 CreateFIFO(stderr_fifo_path.value().c_str()); | 161 EnsureCreateFIFO(stderr_fifo_path); |
127 } | 162 } |
128 | 163 |
129 // DumpRenderTree uses stdin to receive input about which test to run. | 164 // DumpRenderTree uses stdin to receive input about which test to run. |
130 if (command_line.HasSwitch(kCreateStdinFifo)) { | 165 if (command_line.HasSwitch(kCreateStdinFifo)) { |
131 stdin_fifo_path = files_dir.Append(base::FilePath("stdin.fifo")); | 166 stdin_fifo_path = files_dir.Append(base::FilePath("stdin.fifo")); |
132 CreateFIFO(stdin_fifo_path.value().c_str()); | 167 EnsureCreateFIFO(stdin_fifo_path); |
133 } | 168 } |
134 | 169 |
135 // Only redirect the streams after all fifos have been created. | 170 // Only redirect the streams after all fifos have been created. |
136 RedirectStream(stdout, fifo_path.value().c_str(), "w"); | 171 EnsureRedirectStream(stdout, fifo_path, "w"); |
137 if (!stdin_fifo_path.empty()) | 172 if (!stdin_fifo_path.empty()) |
138 RedirectStream(stdin, stdin_fifo_path.value().c_str(), "r"); | 173 EnsureRedirectStream(stdin, stdin_fifo_path, "r"); |
139 if (!stderr_fifo_path.empty()) | 174 if (!stderr_fifo_path.empty()) |
140 RedirectStream(stderr, stderr_fifo_path.value().c_str(), "w"); | 175 EnsureRedirectStream(stderr, stderr_fifo_path, "w"); |
141 else | 176 else |
142 dup2(STDOUT_FILENO, STDERR_FILENO); | 177 dup2(STDOUT_FILENO, STDERR_FILENO); |
143 | 178 |
144 if (command_line.HasSwitch(switches::kWaitForDebugger)) { | 179 if (command_line.HasSwitch(switches::kWaitForDebugger)) { |
145 std::string msg = base::StringPrintf("Native test waiting for GDB because " | 180 AndroidLog(ANDROID_LOG_VERBOSE, |
146 "flag %s was supplied", | 181 "Native test waiting for GDB because flag %s was supplied", |
147 switches::kWaitForDebugger); | 182 switches::kWaitForDebugger); |
148 __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str()); | |
149 base::debug::WaitForDebugger(24 * 60 * 60, false); | 183 base::debug::WaitForDebugger(24 * 60 * 60, false); |
150 } | 184 } |
151 | 185 |
152 ScopedMainEntryLogger scoped_main_entry_logger; | 186 ScopedMainEntryLogger scoped_main_entry_logger; |
153 main(argc, &argv[0]); | 187 main(argc, &argv[0]); |
154 } | 188 } |
155 | 189 |
156 // This is called by the VM when the shared library is first loaded. | 190 // This is called by the VM when the shared library is first loaded. |
157 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 191 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
158 // Install signal handlers to detect crashes. | 192 // Install signal handlers to detect crashes. |
159 InstallHandlers(); | 193 InstallHandlers(); |
160 | 194 |
161 base::android::InitVM(vm); | 195 base::android::InitVM(vm); |
162 JNIEnv* env = base::android::AttachCurrentThread(); | 196 JNIEnv* env = base::android::AttachCurrentThread(); |
163 if (!RegisterNativesImpl(env)) { | 197 if (!RegisterNativesImpl(env)) { |
164 return -1; | 198 return -1; |
165 } | 199 } |
166 | 200 |
167 return JNI_VERSION_1_4; | 201 return JNI_VERSION_1_4; |
168 } | 202 } |
OLD | NEW |