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

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: Rebase 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
« no previous file with comments | « testing/android/native_test.gyp ('k') | testing/android/native_test_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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::native_test_util::ArgsToArgv;
30 using testing::native_test_util::CreateFIFO;
31 using testing::native_test_util::ParseArgsFromCommandLineFile;
32 using testing::native_test_util::RedirectStream;
33 using testing::native_test_util::ScopedMainEntryLogger;
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";
43 const char kCreateStdinFifo[] = "create-stdin-fifo"; 43 const char kCreateStdinFifo[] = "create-stdin-fifo";
44 44
45 // The test runner script writes the command line file in
46 // "/data/local/tmp".
47 static const char kCommandLineFilePath[] =
48 "/data/local/tmp/chrome-native-tests-command-line";
49
45 const char kLogTag[] = "chromium"; 50 const char kLogTag[] = "chromium";
46 const char kCrashedMarker[] = "[ CRASHED ]\n"; 51 const char kCrashedMarker[] = "[ CRASHED ]\n";
47 52
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. 53 // The list of signals which are considered to be crashes.
56 const int kExceptionSignals[] = { 54 const int kExceptionSignals[] = {
57 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 55 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
58 }; 56 };
59 57
60 struct sigaction g_old_sa[NSIG]; 58 struct sigaction g_old_sa[NSIG];
61 59
62 // This function runs in a compromised context. It should not allocate memory. 60 // This function runs in a compromised context. It should not allocate memory.
63 void SignalHandler(int sig, siginfo_t* info, void* reserved) { 61 void SignalHandler(int sig, siginfo_t* info, void* reserved) {
64 // Output the crash marker. 62 // Output the crash marker.
65 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker)); 63 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker));
66 g_old_sa[sig].sa_sigaction(sig, info, reserved); 64 g_old_sa[sig].sa_sigaction(sig, info, reserved);
67 } 65 }
68 66
69 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF. 67 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
70 // Remove the signal handlers. 68 // Remove the signal handlers.
71 void InstallHandlers() { 69 void InstallHandlers() {
72 struct sigaction sa; 70 struct sigaction sa;
73 memset(&sa, 0, sizeof(sa)); 71 memset(&sa, 0, sizeof(sa));
74 72
75 sa.sa_sigaction = SignalHandler; 73 sa.sa_sigaction = SignalHandler;
76 sa.sa_flags = SA_SIGINFO; 74 sa.sa_flags = SA_SIGINFO;
77 75
78 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) { 76 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
79 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]); 77 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]);
80 } 78 }
81 } 79 }
82 80
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 81 } // namespace
151 82
152 // This method is called on a separate java thread so that we won't trigger 83 // This method is called on a separate java thread so that we won't trigger
153 // an ANR. 84 // an ANR.
154 static void RunTests(JNIEnv* env, 85 static void RunTests(JNIEnv* env,
155 jobject obj, 86 jobject obj,
156 jstring jfiles_dir, 87 jstring jfiles_dir,
157 jobject app_context) { 88 jobject app_context) {
158 base::AtExitManager exit_manager; 89 base::AtExitManager exit_manager;
159 90
160 // Command line initialized basically, will be fully initialized later. 91 // Command line initialized basically, will be fully initialized later.
161 static const char* const kInitialArgv[] = { "ChromeTestActivity" }; 92 static const char* const kInitialArgv[] = { "ChromeTestActivity" };
162 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); 93 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
163 94
164 // Set the application context in base. 95 // Set the application context in base.
165 base::android::ScopedJavaLocalRef<jobject> scoped_context( 96 base::android::ScopedJavaLocalRef<jobject> scoped_context(
166 env, env->NewLocalRef(app_context)); 97 env, env->NewLocalRef(app_context));
167 base::android::InitApplicationContext(scoped_context); 98 base::android::InitApplicationContext(scoped_context);
168 base::android::RegisterJni(env); 99 base::android::RegisterJni(env);
169 100
170 std::vector<std::string> args; 101 std::vector<std::string> args;
171 ParseArgsFromCommandLineFile(&args); 102 ParseArgsFromCommandLineFile(kCommandLineFilePath, &args);
172 103
173 // We need to pass in a non-const char**.
174 std::vector<char*> argv; 104 std::vector<char*> argv;
175 int argc = ArgsToArgv(args, &argv); 105 int argc = ArgsToArgv(args, &argv);
176 106
177 // Fully initialize command line with arguments. 107 // Fully initialize command line with arguments.
178 CommandLine::ForCurrentProcess()->AppendArguments( 108 CommandLine::ForCurrentProcess()->AppendArguments(
179 CommandLine(argc, &argv[0]), false); 109 CommandLine(argc, &argv[0]), false);
180 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 110 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
181 111
182 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); 112 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir));
183 113
(...skipping 11 matching lines...) Expand all
195 CreateFIFO(stderr_fifo_path.value().c_str()); 125 CreateFIFO(stderr_fifo_path.value().c_str());
196 } 126 }
197 127
198 // DumpRenderTree uses stdin to receive input about which test to run. 128 // DumpRenderTree uses stdin to receive input about which test to run.
199 if (command_line.HasSwitch(kCreateStdinFifo)) { 129 if (command_line.HasSwitch(kCreateStdinFifo)) {
200 stdin_fifo_path = files_dir.Append(FilePath("stdin.fifo")); 130 stdin_fifo_path = files_dir.Append(FilePath("stdin.fifo"));
201 CreateFIFO(stdin_fifo_path.value().c_str()); 131 CreateFIFO(stdin_fifo_path.value().c_str());
202 } 132 }
203 133
204 // Only redirect the streams after all fifos have been created. 134 // Only redirect the streams after all fifos have been created.
205 Redirect(stdout, fifo_path.value().c_str(), "w"); 135 RedirectStream(stdout, fifo_path.value().c_str(), "w");
206 if (!stdin_fifo_path.empty()) 136 if (!stdin_fifo_path.empty())
207 Redirect(stdin, stdin_fifo_path.value().c_str(), "r"); 137 RedirectStream(stdin, stdin_fifo_path.value().c_str(), "r");
208 if (!stderr_fifo_path.empty()) 138 if (!stderr_fifo_path.empty())
209 Redirect(stderr, stderr_fifo_path.value().c_str(), "w"); 139 RedirectStream(stderr, stderr_fifo_path.value().c_str(), "w");
210 else 140 else
211 dup2(STDOUT_FILENO, STDERR_FILENO); 141 dup2(STDOUT_FILENO, STDERR_FILENO);
212 142
213 if (command_line.HasSwitch(switches::kWaitForDebugger)) { 143 if (command_line.HasSwitch(switches::kWaitForDebugger)) {
214 std::string msg = StringPrintf("Native test waiting for GDB because " 144 std::string msg = StringPrintf("Native test waiting for GDB because "
215 "flag %s was supplied", 145 "flag %s was supplied",
216 switches::kWaitForDebugger); 146 switches::kWaitForDebugger);
217 __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str()); 147 __android_log_write(ANDROID_LOG_VERBOSE, kLogTag, msg.c_str());
218 base::debug::WaitForDebugger(24 * 60 * 60, false); 148 base::debug::WaitForDebugger(24 * 60 * 60, false);
219 } 149 }
220 150
221 ScopedMainEntryLogger scoped_main_entry_logger; 151 ScopedMainEntryLogger scoped_main_entry_logger;
222 main(argc, &argv[0]); 152 main(argc, &argv[0]);
223 } 153 }
224 154
225 // This is called by the VM when the shared library is first loaded. 155 // This is called by the VM when the shared library is first loaded.
226 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { 156 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
227 // Install signal handlers to detect crashes. 157 // Install signal handlers to detect crashes.
228 InstallHandlers(); 158 InstallHandlers();
229 159
230 base::android::InitVM(vm); 160 base::android::InitVM(vm);
231 JNIEnv* env = base::android::AttachCurrentThread(); 161 JNIEnv* env = base::android::AttachCurrentThread();
232 if (!RegisterNativesImpl(env)) { 162 if (!RegisterNativesImpl(env)) {
233 return -1; 163 return -1;
234 } 164 }
235 165
236 return JNI_VERSION_1_4; 166 return JNI_VERSION_1_4;
237 } 167 }
OLDNEW
« no previous file with comments | « testing/android/native_test.gyp ('k') | testing/android/native_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698