Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: testing/android/native_test_launcher.cc

Issue 12213035: Android: Refactor native test setup in a util class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include <stdarg.h>
15 #include <stdio.h>
16 14
17 #include "base/android/base_jni_registrar.h" 15 #include "base/android/base_jni_registrar.h"
18 #include "base/android/jni_android.h" 16 #include "base/android/jni_android.h"
19 #include "base/android/jni_string.h" 17 #include "base/android/jni_string.h"
20 #include "base/android/locale_utils.h"
21 #include "base/android/path_utils.h"
22 #include "base/android/scoped_java_ref.h" 18 #include "base/android/scoped_java_ref.h"
23 #include "base/at_exit.h" 19 #include "base/at_exit.h"
24 #include "base/base_switches.h" 20 #include "base/base_switches.h"
25 #include "base/command_line.h" 21 #include "base/command_line.h"
26 #include "base/file_path.h" 22 #include "base/file_path.h"
27 #include "base/file_util.h"
28 #include "base/logging.h" 23 #include "base/logging.h"
29 #include "base/string_util.h"
30 #include "base/stringprintf.h" 24 #include "base/stringprintf.h"
31 #include "base/strings/string_tokenizer.h"
32 #include "gtest/gtest.h" 25 #include "gtest/gtest.h"
26 #include "testing/android/native_test_util.h"
33 #include "testing/jni/ChromeNativeTestActivity_jni.h" 27 #include "testing/jni/ChromeNativeTestActivity_jni.h"
34 28
29 using testing::NativeTestUtil;
30
35 // The main function of the program to be wrapped as a test apk. 31 // The main function of the program to be wrapped as a test apk.
36 extern int main(int argc, char** argv); 32 extern int main(int argc, char** argv);
37 33
38 namespace { 34 namespace {
39 35
40 // These two command line flags are supported for DumpRenderTree, which needs 36 // 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. 37 // three fifos rather than a combined one: one for stderr, stdin and stdout.
42 const char kSeparateStderrFifo[] = "separate-stderr-fifo"; 38 const char kSeparateStderrFifo[] = "separate-stderr-fifo";
43 const char kCreateStdinFifo[] = "create-stdin-fifo"; 39 const char kCreateStdinFifo[] = "create-stdin-fifo";
44 40
41 // The test runner script writes the command line file in
42 // "/data/local/tmp".
43 static const char kCommandLineFilePath[] =
44 "/data/local/tmp/chrome-native-tests-command-line";
45
45 const char kLogTag[] = "chromium"; 46 const char kLogTag[] = "chromium";
46 const char kCrashedMarker[] = "[ CRASHED ]\n"; 47 const char kCrashedMarker[] = "[ CRASHED ]\n";
47 48
48 void AndroidLogError(const char* format, ...) {
49 va_list args;
50 va_start(args, format);
51 __android_log_vprint(ANDROID_LOG_ERROR, kLogTag, format, args);
52 va_end(args);
53 }
54
55 // The list of signals which are considered to be crashes. 49 // The list of signals which are considered to be crashes.
56 const int kExceptionSignals[] = { 50 const int kExceptionSignals[] = {
57 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 51 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
58 }; 52 };
59 53
60 struct sigaction g_old_sa[NSIG]; 54 struct sigaction g_old_sa[NSIG];
61 55
62 // This function runs in a compromised context. It should not allocate memory. 56 // This function runs in a compromised context. It should not allocate memory.
63 void SignalHandler(int sig, siginfo_t* info, void* reserved) { 57 void SignalHandler(int sig, siginfo_t* info, void* reserved) {
64 // Output the crash marker. 58 // Output the crash marker.
65 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker)); 59 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker));
66 g_old_sa[sig].sa_sigaction(sig, info, reserved); 60 g_old_sa[sig].sa_sigaction(sig, info, reserved);
67 } 61 }
68 62
69 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF. 63 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
70 // Remove the signal handlers. 64 // Remove the signal handlers.
71 void InstallHandlers() { 65 void InstallHandlers() {
72 struct sigaction sa; 66 struct sigaction sa;
73 memset(&sa, 0, sizeof(sa)); 67 memset(&sa, 0, sizeof(sa));
74 68
75 sa.sa_sigaction = SignalHandler; 69 sa.sa_sigaction = SignalHandler;
76 sa.sa_flags = SA_SIGINFO; 70 sa.sa_flags = SA_SIGINFO;
77 71
78 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) { 72 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
79 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]); 73 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]);
80 } 74 }
81 } 75 }
82 76
83 void ParseArgsFromString(const std::string& command_line,
84 std::vector<std::string>* args) {
85 base::StringTokenizer tokenizer(command_line, kWhitespaceASCII);
86 tokenizer.set_quote_chars("\"");
87 while (tokenizer.GetNext()) {
88 std::string token;
89 RemoveChars(tokenizer.token(), "\"", &token);
90 args->push_back(token);
91 }
92 }
93
94 void ParseArgsFromCommandLineFile(std::vector<std::string>* args) {
95 // The test runner script writes the command line file in
96 // "/data/local/tmp".
97 static const char kCommandLineFilePath[] =
98 "/data/local/tmp/chrome-native-tests-command-line";
99 FilePath command_line(kCommandLineFilePath);
100 std::string command_line_string;
101 if (file_util::ReadFileToString(command_line, &command_line_string)) {
102 ParseArgsFromString(command_line_string, args);
103 }
104 }
105
106 int ArgsToArgv(const std::vector<std::string>& args,
107 std::vector<char*>* argv) {
108 // We need to pass in a non-const char**.
109 int argc = args.size();
110
111 argv->resize(argc + 1);
112 for (int i = 0; i < argc; ++i)
113 (*argv)[i] = const_cast<char*>(args[i].c_str());
114 (*argv)[argc] = NULL; // argv must be NULL terminated.
115
116 return argc;
117 }
118
119 void CreateFIFO(const char* fifo_path) {
120 unlink(fifo_path);
121 // Default permissions for mkfifo is ignored, chmod is required.
122 if (mkfifo(fifo_path, 0666) || chmod(fifo_path, 0666)) {
123 AndroidLogError("Failed to create fifo %s: %s\n",
124 fifo_path, strerror(errno));
125 exit(EXIT_FAILURE);
126 }
127 }
128
129 void Redirect(FILE* stream, const char* path, const char* mode) {
130 if (!freopen(path, mode, stream)) {
131 AndroidLogError("Failed to redirect stream to file: %s: %s\n",
132 path, strerror(errno));
133 exit(EXIT_FAILURE);
134 }
135 }
136
137 class ScopedMainEntryLogger {
138 public:
139 ScopedMainEntryLogger() {
140 printf(">>ScopedMainEntryLogger\n");
141 }
142
143 ~ScopedMainEntryLogger() {
144 printf("<<ScopedMainEntryLogger\n");
145 fflush(stdout);
146 fflush(stderr);
147 }
148 };
149
150 } // namespace 77 } // namespace
151 78
152 // This method is called on a separate java thread so that we won't trigger 79 // This method is called on a separate java thread so that we won't trigger
153 // an ANR. 80 // an ANR.
154 static void RunTests(JNIEnv* env, 81 static void RunTests(JNIEnv* env,
155 jobject obj, 82 jobject obj,
156 jstring jfiles_dir, 83 jstring jfiles_dir,
157 jobject app_context) { 84 jobject app_context) {
158 base::AtExitManager exit_manager; 85 base::AtExitManager exit_manager;
159 86
160 // Command line initialized basically, will be fully initialized later. 87 // Command line initialized basically, will be fully initialized later.
161 static const char* const kInitialArgv[] = { "ChromeTestActivity" }; 88 static const char* const kInitialArgv[] = { "ChromeTestActivity" };
162 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); 89 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
163 90
164 // Set the application context in base. 91 // Set the application context in base.
165 base::android::ScopedJavaLocalRef<jobject> scoped_context( 92 base::android::ScopedJavaLocalRef<jobject> scoped_context(
166 env, env->NewLocalRef(app_context)); 93 env, env->NewLocalRef(app_context));
167 base::android::InitApplicationContext(scoped_context); 94 base::android::InitApplicationContext(scoped_context);
168 base::android::RegisterJni(env); 95 base::android::RegisterJni(env);
169 96
170 std::vector<std::string> args; 97 std::vector<std::string> args;
171 ParseArgsFromCommandLineFile(&args); 98 NativeTestUtil::ParseArgsFromCommandLineFile(kCommandLineFilePath, &args);
172 99
173 // We need to pass in a non-const char**.
174 std::vector<char*> argv; 100 std::vector<char*> argv;
175 int argc = ArgsToArgv(args, &argv); 101 int argc = NativeTestUtil::ArgsToArgv(args, &argv);
176 102
177 // Fully initialize command line with arguments. 103 // Fully initialize command line with arguments.
178 CommandLine::ForCurrentProcess()->AppendArguments( 104 CommandLine::ForCurrentProcess()->AppendArguments(
179 CommandLine(argc, &argv[0]), false); 105 CommandLine(argc, &argv[0]), false);
180 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 106 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
181 107
182 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); 108 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir));
183 109
184 // A few options, such "--gtest_list_tests", will just use printf directly 110 // A few options, such "--gtest_list_tests", will just use printf directly
185 // Always redirect stdout to a known file. 111 // Always redirect stdout to a known file.
186 FilePath fifo_path(files_dir.Append(FilePath("test.fifo"))); 112 FilePath fifo_path(files_dir.Append(FilePath("test.fifo")));
187 CreateFIFO(fifo_path.value().c_str()); 113 NativeTestUtil::CreateFIFO(fifo_path.value().c_str());
188 114
189 FilePath stderr_fifo_path, stdin_fifo_path; 115 FilePath stderr_fifo_path, stdin_fifo_path;
190 116
191 // DumpRenderTree needs a separate fifo for the stderr output. For all 117 // DumpRenderTree needs a separate fifo for the stderr output. For all
192 // other tests, insert stderr content to the same fifo we use for stdout. 118 // other tests, insert stderr content to the same fifo we use for stdout.
193 if (command_line.HasSwitch(kSeparateStderrFifo)) { 119 if (command_line.HasSwitch(kSeparateStderrFifo)) {
194 stderr_fifo_path = files_dir.Append(FilePath("stderr.fifo")); 120 stderr_fifo_path = files_dir.Append(FilePath("stderr.fifo"));
195 CreateFIFO(stderr_fifo_path.value().c_str()); 121 NativeTestUtil::CreateFIFO(stderr_fifo_path.value().c_str());
196 } 122 }
197 123
198 // DumpRenderTree uses stdin to receive input about which test to run. 124 // DumpRenderTree uses stdin to receive input about which test to run.
199 if (command_line.HasSwitch(kCreateStdinFifo)) { 125 if (command_line.HasSwitch(kCreateStdinFifo)) {
200 stdin_fifo_path = files_dir.Append(FilePath("stdin.fifo")); 126 stdin_fifo_path = files_dir.Append(FilePath("stdin.fifo"));
201 CreateFIFO(stdin_fifo_path.value().c_str()); 127 NativeTestUtil::CreateFIFO(stdin_fifo_path.value().c_str());
202 } 128 }
203 129
204 // Only redirect the streams after all fifos have been created. 130 // Only redirect the streams after all fifos have been created.
205 Redirect(stdout, fifo_path.value().c_str(), "w"); 131 NativeTestUtil::RedirectStream(stdout, fifo_path.value().c_str(), "w");
206 if (!stdin_fifo_path.empty()) 132 if (!stdin_fifo_path.empty())
207 Redirect(stdin, stdin_fifo_path.value().c_str(), "r"); 133 NativeTestUtil::RedirectStream(stdin, stdin_fifo_path.value().c_str(), "r");
208 if (!stderr_fifo_path.empty()) 134 if (!stderr_fifo_path.empty())
209 Redirect(stderr, stderr_fifo_path.value().c_str(), "w"); 135 NativeTestUtil::RedirectStream(
136 stderr, stderr_fifo_path.value().c_str(), "w");
210 else 137 else
211 dup2(STDOUT_FILENO, STDERR_FILENO); 138 dup2(STDOUT_FILENO, STDERR_FILENO);
212 139
213 if (command_line.HasSwitch(switches::kWaitForDebugger)) { 140 if (command_line.HasSwitch(switches::kWaitForDebugger)) {
214 std::string msg = StringPrintf("Native test waiting for GDB because " 141 std::string msg = StringPrintf("Native test waiting for GDB because "
215 "flag %s was supplied", 142 "flag %s was supplied",
216 switches::kWaitForDebugger); 143 switches::kWaitForDebugger);
217 __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str()); 144 __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str());
218 base::debug::WaitForDebugger(24 * 60 * 60, false); 145 base::debug::WaitForDebugger(24 * 60 * 60, false);
219 } 146 }
220 147
221 ScopedMainEntryLogger scoped_main_entry_logger; 148 NativeTestUtil::ScopedMainEntryLogger scoped_main_entry_logger;
222 main(argc, &argv[0]); 149 main(argc, &argv[0]);
223 } 150 }
224 151
225 // This is called by the VM when the shared library is first loaded. 152 // This is called by the VM when the shared library is first loaded.
226 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { 153 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
227 // Install signal handlers to detect crashes. 154 // Install signal handlers to detect crashes.
228 InstallHandlers(); 155 InstallHandlers();
229 156
230 base::android::InitVM(vm); 157 base::android::InitVM(vm);
231 JNIEnv* env = base::android::AttachCurrentThread(); 158 JNIEnv* env = base::android::AttachCurrentThread();
232 if (!RegisterNativesImpl(env)) { 159 if (!RegisterNativesImpl(env)) {
233 return -1; 160 return -1;
234 } 161 }
235 162
236 return JNI_VERSION_1_4; 163 return JNI_VERSION_1_4;
237 } 164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698