OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // This class sets up the environment for running the content browser tests | |
6 // inside an android application. | |
7 | |
8 #include <android/log.h> | |
9 | |
10 #include "base/android/base_jni_registrar.h" | |
11 #include "base/android/jni_android.h" | |
12 #include "base/android/jni_string.h" | |
13 #include "base/android/scoped_java_ref.h" | |
14 #include "base/base_switches.h" | |
15 #include "base/command_line.h" | |
16 #include "base/file_path.h" | |
17 #include "base/file_util.h" | |
18 #include "base/logging.h" | |
19 #include "base/string_tokenizer.h" | |
20 #include "base/string_util.h" | |
21 #include "base/stringprintf.h" | |
22 #include "content/public/app/android_library_loader_hooks.h" | |
23 #include "content/shell/android/shell_jni_registrar.h" | |
24 #include "jni/ContentBrowserTestsActivity_jni.h" | |
25 | |
26 // The main function of the program to be wrapped as an apk. | |
27 extern int main(int argc, char** argv); | |
28 | |
29 namespace { | |
30 | |
31 void ParseArgsFromString(const std::string& command_line, | |
32 std::vector<std::string>* args) { | |
33 StringTokenizer tokenizer(command_line, kWhitespaceASCII); | |
34 tokenizer.set_quote_chars("\""); | |
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 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 | |
80 } // namespace | |
81 | |
82 static void RunTests(JNIEnv* env, | |
83 jobject obj, | |
84 jstring jfiles_dir, | |
85 jobject app_context) { | |
86 | |
87 // Command line basic initialization, will be fully initialized later. | |
88 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; | |
89 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | |
90 | |
91 // Set the application context in base. | |
92 base::android::ScopedJavaLocalRef<jobject> scoped_context( | |
93 env, env->NewLocalRef(app_context)); | |
94 base::android::InitApplicationContext(scoped_context); | |
95 base::android::RegisterJni(env); | |
96 | |
97 std::vector<std::string> args; | |
98 ParseArgsFromCommandLineFile(&args); | |
99 | |
100 // We need to pass in a non-const char**. | |
101 std::vector<char*> argv; | |
102 int argc = ArgsToArgv(args, &argv); | |
103 | |
104 // Fully initialize command line with arguments. | |
105 CommandLine::ForCurrentProcess()->AppendArguments( | |
106 CommandLine(argc, &argv[0]), false); | |
107 | |
108 ScopedMainEntryLogger scoped_main_entry_logger; | |
109 main(argc, &argv[0]); | |
110 } | |
111 | |
112 // This is called by the VM when the shared library is first loaded. | |
113 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | |
114 base::android::InitVM(vm); | |
115 JNIEnv* env = base::android::AttachCurrentThread(); | |
116 | |
117 if (!content::RegisterLibraryLoaderEntryHook(env)) | |
118 return -1; | |
119 | |
120 if (!content::android::RegisterShellJni(env)) | |
121 return -1; | |
122 | |
123 if (!RegisterNativesImpl(env)) | |
124 return -1; | |
125 | |
126 return JNI_VERSION_1_4; | |
127 } | |
OLD | NEW |