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 content browser tests | 5 // This class sets up the environment for running the content browser tests |
6 // inside an android application. | 6 // inside an android application. |
7 | 7 |
8 #include <android/log.h> | 8 #include <android/log.h> |
9 | 9 |
10 #include "base/android/base_jni_registrar.h" | 10 #include "base/android/base_jni_registrar.h" |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
14 #include "base/base_switches.h" | 14 #include "base/base_switches.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/file_path.h" | 16 #include "base/file_path.h" |
17 #include "base/file_util.h" | 17 #include "base/file_util.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/stringprintf.h" | 20 #include "base/stringprintf.h" |
21 #include "base/strings/string_tokenizer.h" | 21 #include "base/strings/string_tokenizer.h" |
22 #include "content/public/app/android_library_loader_hooks.h" | 22 #include "content/public/app/android_library_loader_hooks.h" |
23 #include "content/shell/android/shell_jni_registrar.h" | 23 #include "content/shell/android/shell_jni_registrar.h" |
24 #include "jni/ContentBrowserTestsActivity_jni.h" | 24 #include "jni/ContentBrowserTestsActivity_jni.h" |
| 25 #include "testing/android/native_test_util.h" |
| 26 |
| 27 using testing::native_test_util::ArgsToArgv; |
| 28 using testing::native_test_util::CreateFIFO; |
| 29 using testing::native_test_util::ParseArgsFromCommandLineFile; |
| 30 using testing::native_test_util::RedirectStream; |
| 31 using testing::native_test_util::ScopedMainEntryLogger; |
25 | 32 |
26 // The main function of the program to be wrapped as an apk. | 33 // The main function of the program to be wrapped as an apk. |
27 extern int main(int argc, char** argv); | 34 extern int main(int argc, char** argv); |
28 | 35 |
29 namespace { | 36 namespace { |
30 | 37 |
31 void ParseArgsFromString(const std::string& command_line, | 38 // The test runner script writes the command line file in |
32 std::vector<std::string>* args) { | 39 // "/data/local/tmp". |
33 base::StringTokenizer tokenizer(command_line, kWhitespaceASCII); | 40 static const char kCommandLineFilePath[] = |
34 tokenizer.set_quote_chars("\""); | 41 "/data/local/tmp/content-browser-tests-command-line"; |
35 while (tokenizer.GetNext()) { | |
36 std::string token; | |
37 RemoveChars(tokenizer.token(), "\"", &token); | |
38 args->push_back(token); | |
39 } | |
40 } | |
41 | |
42 void ParseArgsFromCommandLineFile(std::vector<std::string>* args) { | |
43 // The test runner script writes the command line file in | |
44 // "/data/local/tmp". | |
45 static const char kCommandLineFilePath[] = | |
46 "/data/local/tmp/content-browser-tests-command-line"; | |
47 base::FilePath command_line(kCommandLineFilePath); | |
48 std::string command_line_string; | |
49 if (file_util::ReadFileToString(command_line, &command_line_string)) { | |
50 ParseArgsFromString(command_line_string, args); | |
51 } | |
52 } | |
53 | |
54 int ArgsToArgv(const std::vector<std::string>& args, | |
55 std::vector<char*>* argv) { | |
56 // We need to pass in a non-const char**. | |
57 int argc = args.size(); | |
58 | |
59 argv->resize(argc + 1); | |
60 for (int i = 0; i < argc; ++i) | |
61 (*argv)[i] = const_cast<char*>(args[i].c_str()); | |
62 (*argv)[argc] = NULL; // argv must be NULL terminated. | |
63 | |
64 return argc; | |
65 } | |
66 | |
67 class ScopedMainEntryLogger { | |
68 public: | |
69 ScopedMainEntryLogger() { | |
70 printf(">>ScopedMainEntryLogger\n"); | |
71 } | |
72 | |
73 ~ScopedMainEntryLogger() { | |
74 printf("<<ScopedMainEntryLogger\n"); | |
75 fflush(stdout); | |
76 fflush(stderr); | |
77 } | |
78 }; | |
79 | 42 |
80 } // namespace | 43 } // namespace |
81 | 44 |
82 static void RunTests(JNIEnv* env, | 45 static void RunTests(JNIEnv* env, |
83 jobject obj, | 46 jobject obj, |
84 jstring jfiles_dir, | 47 jstring jfiles_dir, |
85 jobject app_context) { | 48 jobject app_context) { |
86 | 49 |
87 // Command line basic initialization, will be fully initialized later. | 50 // Command line basic initialization, will be fully initialized later. |
88 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; | 51 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; |
89 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | 52 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); |
90 | 53 |
91 // Set the application context in base. | 54 // Set the application context in base. |
92 base::android::ScopedJavaLocalRef<jobject> scoped_context( | 55 base::android::ScopedJavaLocalRef<jobject> scoped_context( |
93 env, env->NewLocalRef(app_context)); | 56 env, env->NewLocalRef(app_context)); |
94 base::android::InitApplicationContext(scoped_context); | 57 base::android::InitApplicationContext(scoped_context); |
95 base::android::RegisterJni(env); | 58 base::android::RegisterJni(env); |
96 | 59 |
97 std::vector<std::string> args; | 60 std::vector<std::string> args; |
98 ParseArgsFromCommandLineFile(&args); | 61 ParseArgsFromCommandLineFile(kCommandLineFilePath, &args); |
99 | 62 |
100 // We need to pass in a non-const char**. | |
101 std::vector<char*> argv; | 63 std::vector<char*> argv; |
102 int argc = ArgsToArgv(args, &argv); | 64 int argc = ArgsToArgv(args, &argv); |
103 | 65 |
104 // Fully initialize command line with arguments. | 66 // Fully initialize command line with arguments. |
105 CommandLine::ForCurrentProcess()->AppendArguments( | 67 CommandLine::ForCurrentProcess()->AppendArguments( |
106 CommandLine(argc, &argv[0]), false); | 68 CommandLine(argc, &argv[0]), false); |
107 | 69 |
| 70 // Create fifo and redirect stdout and stderr to it. |
| 71 FilePath files_dir(base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); |
| 72 FilePath fifo_path(files_dir.Append(FilePath("test.fifo"))); |
| 73 CreateFIFO(fifo_path.value().c_str()); |
| 74 RedirectStream(stdout, fifo_path.value().c_str(), "w"); |
| 75 dup2(STDOUT_FILENO, STDERR_FILENO); |
| 76 |
108 ScopedMainEntryLogger scoped_main_entry_logger; | 77 ScopedMainEntryLogger scoped_main_entry_logger; |
109 main(argc, &argv[0]); | 78 main(argc, &argv[0]); |
110 } | 79 } |
111 | 80 |
112 // This is called by the VM when the shared library is first loaded. | 81 // This is called by the VM when the shared library is first loaded. |
113 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 82 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
114 base::android::InitVM(vm); | 83 base::android::InitVM(vm); |
115 JNIEnv* env = base::android::AttachCurrentThread(); | 84 JNIEnv* env = base::android::AttachCurrentThread(); |
116 | 85 |
117 if (!content::RegisterLibraryLoaderEntryHook(env)) | 86 if (!content::RegisterLibraryLoaderEntryHook(env)) |
118 return -1; | 87 return -1; |
119 | 88 |
120 if (!content::android::RegisterShellJni(env)) | 89 if (!content::android::RegisterShellJni(env)) |
121 return -1; | 90 return -1; |
122 | 91 |
123 if (!RegisterNativesImpl(env)) | 92 if (!RegisterNativesImpl(env)) |
124 return -1; | 93 return -1; |
125 | 94 |
126 return JNI_VERSION_1_4; | 95 return JNI_VERSION_1_4; |
127 } | 96 } |
OLD | NEW |