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

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

Issue 10408091: Chromium support of running DumpRenderTree as an apk on Android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and move out net dependency from testing/android Created 8 years, 6 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') | webkit/support/DEPS » ('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 logcat) markers identifying the 6 // android application. It outputs (to logcat) markers identifying the
7 // START/END/CRASH of the test suite, FAILURE/SUCCESS of individual tests etc. 7 // START/END/CRASH of the test suite, FAILURE/SUCCESS of individual tests etc.
8 // These markers are read by the test runner script to generate test results. 8 // These markers are read by the test runner script to generate test results.
9 // It injects an event listener in gtest to detect various test stages and 9 // It injects an event listener in gtest to detect various test stages and
10 // installs signal handlers to detect crashes. 10 // 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 <stdio.h> 14 #include <stdio.h>
15 15
16 #include "base/android/jni_android.h" 16 #include "base/android/jni_android.h"
17 #include "base/android/jni_string.h" 17 #include "base/android/jni_string.h"
18 #include "base/android/locale_utils.h" 18 #include "base/android/locale_utils.h"
19 #include "base/android/path_utils.h" 19 #include "base/android/path_utils.h"
20 #include "base/android/scoped_java_ref.h" 20 #include "base/android/scoped_java_ref.h"
21 #include "base/at_exit.h" 21 #include "base/at_exit.h"
22 #include "base/command_line.h" 22 #include "base/command_line.h"
23 #include "base/file_path.h" 23 #include "base/file_path.h"
24 #include "base/file_util.h" 24 #include "base/file_util.h"
25 #include "base/logging.h" 25 #include "base/logging.h"
26 #include "base/stringprintf.h" 26 #include "base/stringprintf.h"
27 #include "base/string_tokenizer.h" 27 #include "base/string_tokenizer.h"
28 #include "base/string_util.h" 28 #include "base/string_util.h"
29 #include "base/test/test_suite.h" 29 #include "base/test/test_support_android.h"
30 #include "gtest/gtest.h"
30 #include "testing/android/jni/chrome_native_test_activity_jni.h" 31 #include "testing/android/jni/chrome_native_test_activity_jni.h"
31 #include "gtest/gtest.h"
32 32
33 // GTest's main function. 33 // The main function of the program to be wrapped as a test apk.
34 extern int main(int argc, char** argv); 34 extern int main(int argc, char** argv);
35 35
36 namespace { 36 namespace {
37 37
38 // The list of signals which are considered to be crashes. 38 // The list of signals which are considered to be crashes.
39 const int kExceptionSignals[] = { 39 const int kExceptionSignals[] = {
40 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1 40 SIGSEGV, SIGABRT, SIGFPE, SIGILL, SIGBUS, -1
41 }; 41 };
42 42
43 struct sigaction g_old_sa[NSIG]; 43 struct sigaction g_old_sa[NSIG];
(...skipping 23 matching lines...) Expand all
67 StringTokenizer tokenizer(command_line, kWhitespaceASCII); 67 StringTokenizer tokenizer(command_line, kWhitespaceASCII);
68 tokenizer.set_quote_chars("\""); 68 tokenizer.set_quote_chars("\"");
69 while (tokenizer.GetNext()) { 69 while (tokenizer.GetNext()) {
70 std::string token; 70 std::string token;
71 RemoveChars(tokenizer.token(), "\"", &token); 71 RemoveChars(tokenizer.token(), "\"", &token);
72 args->push_back(token); 72 args->push_back(token);
73 } 73 }
74 } 74 }
75 75
76 void ParseArgsFromCommandLineFile(std::vector<std::string>* args) { 76 void ParseArgsFromCommandLineFile(std::vector<std::string>* args) {
77 // The test runner script can write to "/data/local/tmp". 77 // The test runner script writes the command line file in
78 // "/data/local/tmp".
78 static const char kCommandLineFilePath[] = 79 static const char kCommandLineFilePath[] =
79 "/data/local/tmp/chrome-native-tests-command-line"; 80 "/data/local/tmp/chrome-native-tests-command-line";
80 FilePath command_line(kCommandLineFilePath); 81 FilePath command_line(kCommandLineFilePath);
81 std::string command_line_string; 82 std::string command_line_string;
82 if (file_util::ReadFileToString(command_line, &command_line_string)) { 83 if (file_util::ReadFileToString(command_line, &command_line_string)) {
83 ParseArgsFromString(command_line_string, args); 84 ParseArgsFromString(command_line_string, args);
84 } 85 }
85 } 86 }
86 87
87 void ArgsToArgv(const std::vector<std::string>& args, 88 void ArgsToArgv(const std::vector<std::string>& args,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 LOG(ERROR) << msg; 152 LOG(ERROR) << msg;
152 } 153 }
153 154
154 void AndroidLogPrinter::OnTestProgramEnd( 155 void AndroidLogPrinter::OnTestProgramEnd(
155 const ::testing::UnitTest& unit_test) { 156 const ::testing::UnitTest& unit_test) {
156 std::string msg = StringPrintf("[ END ] %d", 157 std::string msg = StringPrintf("[ END ] %d",
157 unit_test.successful_test_count()); 158 unit_test.successful_test_count());
158 LOG(ERROR) << msg; 159 LOG(ERROR) << msg;
159 } 160 }
160 161
161 void LibraryLoadedOnMainThread(JNIEnv* env) {
162 static const char* const kInitialArgv[] = { "ChromeTestActivity" };
163
164 {
165 // We need a test suite to be created before we do any tracing or
166 // logging: it creates a global at_exit_manager and initializes
167 // internal gtest data structures based on the command line.
168 // It needs to be scoped as it also resets the CommandLine.
169 std::vector<std::string> args;
170 ParseArgsFromCommandLineFile(&args);
171 std::vector<char*> argv;
172 ArgsToArgv(args, &argv);
173 base::TestSuite test_suite(argv.size(), &argv[0]);
174 }
175
176 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
177
178 logging::InitLogging(NULL,
179 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG,
180 logging::DONT_LOCK_LOG_FILE,
181 logging::DELETE_OLD_LOG_FILE,
182 logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS);
183 // To view log output with IDs and timestamps use "adb logcat -v threadtime".
184 logging::SetLogItems(false, // Process ID
185 false, // Thread ID
186 false, // Timestamp
187 false); // Tick count
188 VLOG(0) << "Chromium logging enabled: level = " << logging::GetMinLogLevel()
189 << ", default verbosity = " << logging::GetVlogVerbosity();
190 base::android::RegisterLocaleUtils(env);
191 base::android::RegisterPathUtils(env);
192 }
193
194 } // namespace 162 } // namespace
195 163
196 // This method is called on a separate java thread so that we won't trigger 164 // This method is called on a separate java thread so that we won't trigger
197 // an ANR. 165 // an ANR.
198 static void RunTests(JNIEnv* env, 166 static void RunTests(JNIEnv* env,
199 jobject obj, 167 jobject obj,
200 jstring jfiles_dir, 168 jstring jfiles_dir,
201 jobject app_context) { 169 jobject app_context) {
170 base::AtExitManager exit_manager;
171
172 static const char* const kInitialArgv[] = { "ChromeTestActivity" };
173 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
174
175 // Set the application context in base.
176 base::android::ScopedJavaLocalRef<jobject> scoped_context(
177 env, env->NewLocalRef(app_context));
178 base::android::InitApplicationContext(scoped_context);
179
180 base::android::RegisterLocaleUtils(env);
181 base::android::RegisterPathUtils(env);
182
183 InitAndroidTest();
184
202 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); 185 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir));
203 // A few options, such "--gtest_list_tests", will just use printf directly 186 // A few options, such "--gtest_list_tests", will just use printf directly
204 // and won't use the "AndroidLogPrinter". Redirect stdout to a known file. 187 // and won't use the "AndroidLogPrinter". Redirect stdout to a known file.
205 FilePath stdout_path(files_dir.Append(FilePath("stdout.txt"))); 188 FilePath stdout_path(files_dir.Append(FilePath("stdout.txt")));
206 freopen(stdout_path.value().c_str(), "w", stdout); 189 freopen(stdout_path.value().c_str(), "w", stdout);
207 190
208 std::vector<std::string> args; 191 std::vector<std::string> args;
209 ParseArgsFromCommandLineFile(&args); 192 ParseArgsFromCommandLineFile(&args);
210 193
211 // We need to pass in a non-const char**. 194 // We need to pass in a non-const char**.
212 std::vector<char*> argv; 195 std::vector<char*> argv;
213 ArgsToArgv(args, &argv); 196 ArgsToArgv(args, &argv);
214 197
215 int argc = argv.size(); 198 int argc = argv.size();
216 // This object is owned by gtest. 199 // This object is owned by gtest.
217 AndroidLogPrinter* log = new AndroidLogPrinter(); 200 AndroidLogPrinter* log = new AndroidLogPrinter();
218 log->Init(&argc, &argv[0]); 201 log->Init(&argc, &argv[0]);
219 202
220 // Set the application context in base.
221 base::android::ScopedJavaLocalRef<jobject> scoped_context(
222 env, env->NewLocalRef(app_context));
223 base::android::InitApplicationContext(scoped_context);
224
225 main(argc, &argv[0]); 203 main(argc, &argv[0]);
226 } 204 }
227 205
228 // This is called by the VM when the shared library is first loaded. 206 // This is called by the VM when the shared library is first loaded.
229 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { 207 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
230
231 // Install signal handlers to detect crashes. 208 // Install signal handlers to detect crashes.
232 InstallHandlers(); 209 InstallHandlers();
233 210
234 base::android::InitVM(vm); 211 base::android::InitVM(vm);
235 JNIEnv* env = base::android::AttachCurrentThread(); 212 JNIEnv* env = base::android::AttachCurrentThread();
236 if (!RegisterNativesImpl(env)) { 213 if (!RegisterNativesImpl(env)) {
237 return -1; 214 return -1;
238 } 215 }
239 LibraryLoadedOnMainThread(env);
240 216
241 return JNI_VERSION_1_4; 217 return JNI_VERSION_1_4;
242 } 218 }
OLDNEW
« no previous file with comments | « testing/android/native_test.gyp ('k') | webkit/support/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698