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