WebstorePrivate extension API cleanup, part 1:
[chromium-blink-merge.git] / testing / android / native_test_launcher.cc
blob4cc87d211c3bb172e260f2dd820fee81477462b1
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
3 // found in the LICENSE file.
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
7 // START/PASSED/CRASH of the test suite, FAILURE/SUCCESS of individual tests,
8 // etc.
9 // These markers are read by the test runner script to generate test results.
10 // It installs signal handlers to detect crashes.
12 #include <android/log.h>
13 #include <signal.h>
15 #include "base/android/base_jni_registrar.h"
16 #include "base/android/fifo_utils.h"
17 #include "base/android/jni_android.h"
18 #include "base/android/jni_string.h"
19 #include "base/android/scoped_java_ref.h"
20 #include "base/at_exit.h"
21 #include "base/base_switches.h"
22 #include "base/command_line.h"
23 #include "base/files/file_path.h"
24 #include "base/files/file_util.h"
25 #include "base/logging.h"
26 #include "base/strings/stringprintf.h"
27 #include "gtest/gtest.h"
28 #include "jni/ChromeNativeTestActivity_jni.h"
29 #include "testing/android/native_test_util.h"
31 using testing::native_test_util::ArgsToArgv;
32 using testing::native_test_util::ParseArgsFromCommandLineFile;
33 using testing::native_test_util::ParseArgsFromString;
34 using testing::native_test_util::ScopedMainEntryLogger;
36 // The main function of the program to be wrapped as a test apk.
37 extern int main(int argc, char** argv);
39 namespace {
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";
46 const char kLogTag[] = "chromium";
47 const char kCrashedMarker[] = "[ CRASHED ]\n";
49 // The list of signals which are considered to be crashes.
50 const int kExceptionSignals[] = {
51 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
54 struct sigaction g_old_sa[NSIG];
56 // This function runs in a compromised context. It should not allocate memory.
57 void SignalHandler(int sig, siginfo_t* info, void* reserved) {
58 // Output the crash marker.
59 write(STDOUT_FILENO, kCrashedMarker, sizeof(kCrashedMarker));
60 g_old_sa[sig].sa_sigaction(sig, info, reserved);
63 // Writes printf() style string to Android's logger where |priority| is one of
64 // the levels defined in <android/log.h>.
65 void AndroidLog(int priority, const char* format, ...) {
66 va_list args;
67 va_start(args, format);
68 __android_log_vprint(priority, kLogTag, format, args);
69 va_end(args);
72 } // namespace
74 static void RunTests(JNIEnv* env,
75 jobject obj,
76 jstring jcommand_line_flags,
77 jstring jcommand_line_file_path,
78 jstring jstdout_file_path,
79 jboolean jstdout_fifo,
80 jobject app_context) {
81 // Command line initialized basically, will be fully initialized later.
82 static const char* const kInitialArgv[] = { "ChromeTestActivity" };
83 base::CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
85 // Set the application context in base.
86 base::android::ScopedJavaLocalRef<jobject> scoped_context(
87 env, env->NewLocalRef(app_context));
88 base::android::InitApplicationContext(env, scoped_context);
89 base::android::RegisterJni(env);
91 std::vector<std::string> args;
93 const std::string command_line_file_path(
94 base::android::ConvertJavaStringToUTF8(env, jcommand_line_file_path));
95 if (command_line_file_path.empty())
96 ParseArgsFromCommandLineFile(kCommandLineFilePath, &args);
97 else
98 ParseArgsFromCommandLineFile(command_line_file_path.c_str(), &args);
100 const std::string command_line_flags(
101 base::android::ConvertJavaStringToUTF8(env, jcommand_line_flags));
102 ParseArgsFromString(command_line_flags, &args);
104 std::vector<char*> argv;
105 int argc = ArgsToArgv(args, &argv);
107 // Fully initialize command line with arguments.
108 base::CommandLine::ForCurrentProcess()->AppendArguments(
109 base::CommandLine(argc, &argv[0]), false);
110 const base::CommandLine& command_line =
111 *base::CommandLine::ForCurrentProcess();
113 base::FilePath stdout_file_path(
114 base::android::ConvertJavaStringToUTF8(env, jstdout_file_path));
116 // A few options, such "--gtest_list_tests", will just use printf directly
117 // Always redirect stdout to a known file.
118 unlink(stdout_file_path.value().c_str());
119 if (jstdout_fifo) {
120 if (!base::android::CreateFIFO(stdout_file_path, 0666)) {
121 AndroidLog(ANDROID_LOG_ERROR, "Failed to create fifo %s: %s\n",
122 stdout_file_path.value().c_str(), strerror(errno));
123 exit(EXIT_FAILURE);
126 if (!base::android::RedirectStream(stdout, stdout_file_path, "w")) {
127 AndroidLog(ANDROID_LOG_ERROR, "Failed to redirect stream to file: %s: %s\n",
128 stdout_file_path.value().c_str(), strerror(errno));
129 exit(EXIT_FAILURE);
131 dup2(STDOUT_FILENO, STDERR_FILENO);
133 if (command_line.HasSwitch(switches::kWaitForDebugger)) {
134 AndroidLog(ANDROID_LOG_VERBOSE,
135 "Native test waiting for GDB because flag %s was supplied",
136 switches::kWaitForDebugger);
137 base::debug::WaitForDebugger(24 * 60 * 60, false);
140 ScopedMainEntryLogger scoped_main_entry_logger;
141 main(argc, &argv[0]);
144 bool RegisterNativeTestJNI(JNIEnv* env) {
145 return RegisterNativesImpl(env);
149 // TODO(nileshagrawal): now that we're using FIFO, test scripts can detect EOF.
150 // Remove the signal handlers.
151 void InstallHandlers() {
152 struct sigaction sa;
153 memset(&sa, 0, sizeof(sa));
155 sa.sa_sigaction = SignalHandler;
156 sa.sa_flags = SA_SIGINFO;
158 for (unsigned int i = 0; kExceptionSignals[i] != -1; ++i) {
159 sigaction(kExceptionSignals[i], &sa, &g_old_sa[kExceptionSignals[i]]);