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::NativeTestUtil; |
25 | 28 |
26 // The main function of the program to be wrapped as an apk. | 29 // The main function of the program to be wrapped as an apk. |
27 extern int main(int argc, char** argv); | 30 extern int main(int argc, char** argv); |
28 | 31 |
29 namespace { | 32 namespace { |
30 | 33 |
31 void ParseArgsFromString(const std::string& command_line, | 34 // The test runner script writes the command line file in |
32 std::vector<std::string>* args) { | 35 // "/data/local/tmp". |
33 base::StringTokenizer tokenizer(command_line, kWhitespaceASCII); | 36 static const char kCommandLineFilePath[] = |
34 tokenizer.set_quote_chars("\""); | 37 "/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 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 | 38 |
80 } // namespace | 39 } // namespace |
81 | 40 |
82 static void RunTests(JNIEnv* env, | 41 static void RunTests(JNIEnv* env, |
83 jobject obj, | 42 jobject obj, |
84 jstring jfiles_dir, | 43 jstring jfiles_dir, |
85 jobject app_context) { | 44 jobject app_context) { |
86 | 45 |
87 // Command line basic initialization, will be fully initialized later. | 46 // Command line basic initialization, will be fully initialized later. |
88 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; | 47 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; |
89 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | 48 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); |
90 | 49 |
91 // Set the application context in base. | 50 // Set the application context in base. |
92 base::android::ScopedJavaLocalRef<jobject> scoped_context( | 51 base::android::ScopedJavaLocalRef<jobject> scoped_context( |
93 env, env->NewLocalRef(app_context)); | 52 env, env->NewLocalRef(app_context)); |
94 base::android::InitApplicationContext(scoped_context); | 53 base::android::InitApplicationContext(scoped_context); |
95 base::android::RegisterJni(env); | 54 base::android::RegisterJni(env); |
96 | 55 |
97 std::vector<std::string> args; | 56 std::vector<std::string> args; |
98 ParseArgsFromCommandLineFile(&args); | 57 NativeTestUtil::ParseArgsFromCommandLineFile(kCommandLineFilePath, &args); |
99 | 58 |
100 // We need to pass in a non-const char**. | |
101 std::vector<char*> argv; | 59 std::vector<char*> argv; |
102 int argc = ArgsToArgv(args, &argv); | 60 int argc = NativeTestUtil::ArgsToArgv(args, &argv); |
103 | 61 |
104 // Fully initialize command line with arguments. | 62 // Fully initialize command line with arguments. |
105 CommandLine::ForCurrentProcess()->AppendArguments( | 63 CommandLine::ForCurrentProcess()->AppendArguments( |
106 CommandLine(argc, &argv[0]), false); | 64 CommandLine(argc, &argv[0]), false); |
107 | 65 |
108 ScopedMainEntryLogger scoped_main_entry_logger; | 66 NativeTestUtil::ScopedMainEntryLogger scoped_main_entry_logger; |
109 main(argc, &argv[0]); | 67 main(argc, &argv[0]); |
110 } | 68 } |
111 | 69 |
112 // This is called by the VM when the shared library is first loaded. | 70 // This is called by the VM when the shared library is first loaded. |
113 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 71 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
114 base::android::InitVM(vm); | 72 base::android::InitVM(vm); |
115 JNIEnv* env = base::android::AttachCurrentThread(); | 73 JNIEnv* env = base::android::AttachCurrentThread(); |
116 | 74 |
117 if (!content::RegisterLibraryLoaderEntryHook(env)) | 75 if (!content::RegisterLibraryLoaderEntryHook(env)) |
118 return -1; | 76 return -1; |
119 | 77 |
120 if (!content::android::RegisterShellJni(env)) | 78 if (!content::android::RegisterShellJni(env)) |
121 return -1; | 79 return -1; |
122 | 80 |
123 if (!RegisterNativesImpl(env)) | 81 if (!RegisterNativesImpl(env)) |
124 return -1; | 82 return -1; |
125 | 83 |
126 return JNI_VERSION_1_4; | 84 return JNI_VERSION_1_4; |
127 } | 85 } |
OLD | NEW |