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 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
11 #include "base/android/base_jni_registrar.h" | 11 #include "base/android/base_jni_registrar.h" |
| 12 #include "base/android/fifo_utils.h" |
12 #include "base/android/jni_android.h" | 13 #include "base/android/jni_android.h" |
13 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
14 #include "base/android/scoped_java_ref.h" | 15 #include "base/android/scoped_java_ref.h" |
15 #include "base/base_switches.h" | 16 #include "base/base_switches.h" |
16 #include "base/command_line.h" | 17 #include "base/command_line.h" |
17 #include "base/files/file_path.h" | 18 #include "base/files/file_path.h" |
18 #include "base/logging.h" | 19 #include "base/logging.h" |
19 #include "base/strings/string_tokenizer.h" | 20 #include "base/strings/string_tokenizer.h" |
20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
22 #include "content/public/app/android_library_loader_hooks.h" | 23 #include "content/public/app/android_library_loader_hooks.h" |
23 #include "content/public/app/content_main.h" | 24 #include "content/public/app/content_main.h" |
24 #include "content/public/common/content_switches.h" | 25 #include "content/public/common/content_switches.h" |
25 #include "content/public/test/test_launcher.h" | 26 #include "content/public/test/test_launcher.h" |
26 #include "content/shell/android/shell_jni_registrar.h" | 27 #include "content/shell/android/shell_jni_registrar.h" |
27 #include "content/shell/app/shell_main_delegate.h" | 28 #include "content/shell/app/shell_main_delegate.h" |
28 #include "content/test/browser_test_message_pump_android.h" | 29 #include "content/test/browser_test_message_pump_android.h" |
29 #include "jni/ContentBrowserTestsActivity_jni.h" | 30 #include "jni/ContentBrowserTestsActivity_jni.h" |
30 #include "testing/android/native_test_util.h" | 31 #include "testing/android/native_test_util.h" |
31 | 32 |
32 using testing::native_test_util::ArgsToArgv; | 33 using testing::native_test_util::ArgsToArgv; |
33 using testing::native_test_util::CreateFIFO; | |
34 using testing::native_test_util::ParseArgsFromCommandLineFile; | 34 using testing::native_test_util::ParseArgsFromCommandLineFile; |
35 using testing::native_test_util::RedirectStream; | |
36 using testing::native_test_util::ScopedMainEntryLogger; | 35 using testing::native_test_util::ScopedMainEntryLogger; |
37 | 36 |
38 // The main function of the program to be wrapped as an apk. | 37 // The main function of the program to be wrapped as an apk. |
39 extern int main(int argc, char** argv); | 38 extern int main(int argc, char** argv); |
40 | 39 |
41 namespace { | 40 namespace { |
42 | 41 |
43 // The test runner script writes the command line file in | 42 // The test runner script writes the command line file in |
44 // "/data/local/tmp". | 43 // "/data/local/tmp". |
45 static const char kCommandLineFilePath[] = | 44 static const char kCommandLineFilePath[] = |
46 "/data/local/tmp/content-browser-tests-command-line"; | 45 "/data/local/tmp/content-browser-tests-command-line"; |
47 | 46 |
48 } // namespace | 47 } // namespace |
49 | 48 |
50 namespace content { | 49 namespace content { |
51 | 50 |
| 51 // TODO(nileshagrawal): Refactor and deduplicate with |
| 52 // testing/android/native_test_launcher.cc |
52 static void RunTests(JNIEnv* env, | 53 static void RunTests(JNIEnv* env, |
53 jobject obj, | 54 jobject obj, |
54 jstring jfiles_dir, | 55 jstring jfiles_dir, |
55 jobject app_context) { | 56 jobject app_context) { |
56 // Command line basic initialization, will be fully initialized later. | 57 // Command line basic initialization, will be fully initialized later. |
57 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; | 58 static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" }; |
58 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | 59 CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); |
59 | 60 |
60 // Set the application context in base. | 61 // Set the application context in base. |
61 base::android::ScopedJavaLocalRef<jobject> scoped_context( | 62 base::android::ScopedJavaLocalRef<jobject> scoped_context( |
(...skipping 16 matching lines...) Expand all Loading... |
78 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | 79 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); |
79 // Specify a socket name to not conflict with the default one used | 80 // Specify a socket name to not conflict with the default one used |
80 // in content_shell. | 81 // in content_shell. |
81 command_line->AppendSwitchASCII(switches::kRemoteDebuggingSocketName, | 82 command_line->AppendSwitchASCII(switches::kRemoteDebuggingSocketName, |
82 "content_browsertests_devtools_remote"); | 83 "content_browsertests_devtools_remote"); |
83 | 84 |
84 // Create fifo and redirect stdout and stderr to it. | 85 // Create fifo and redirect stdout and stderr to it. |
85 base::FilePath files_dir( | 86 base::FilePath files_dir( |
86 base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); | 87 base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); |
87 base::FilePath fifo_path(files_dir.Append(base::FilePath("test.fifo"))); | 88 base::FilePath fifo_path(files_dir.Append(base::FilePath("test.fifo"))); |
88 CreateFIFO(fifo_path.value().c_str()); | 89 base::android::CreateFIFO(fifo_path, 0666); |
89 RedirectStream(stdout, fifo_path.value().c_str(), "w"); | 90 base::android::RedirectStream(stdout, fifo_path, "w"); |
90 dup2(STDOUT_FILENO, STDERR_FILENO); | 91 dup2(STDOUT_FILENO, STDERR_FILENO); |
91 | 92 |
92 ScopedMainEntryLogger scoped_main_entry_logger; | 93 ScopedMainEntryLogger scoped_main_entry_logger; |
93 main(argc, &argv[0]); | 94 main(argc, &argv[0]); |
94 } | 95 } |
95 } // namespace content | 96 } // namespace content |
96 | 97 |
97 // This is called by the VM when the shared library is first loaded. | 98 // This is called by the VM when the shared library is first loaded. |
98 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { | 99 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
99 base::android::InitVM(vm); | 100 base::android::InitVM(vm); |
100 JNIEnv* env = base::android::AttachCurrentThread(); | 101 JNIEnv* env = base::android::AttachCurrentThread(); |
101 | 102 |
102 if (!content::RegisterLibraryLoaderEntryHook(env)) | 103 if (!content::RegisterLibraryLoaderEntryHook(env)) |
103 return -1; | 104 return -1; |
104 | 105 |
105 if (!content::android::RegisterShellJni(env)) | 106 if (!content::android::RegisterShellJni(env)) |
106 return -1; | 107 return -1; |
107 | 108 |
108 if (!content::BrowserTestMessagePumpAndroid::RegisterJni(env)) | 109 if (!content::BrowserTestMessagePumpAndroid::RegisterJni(env)) |
109 return -1; | 110 return -1; |
110 | 111 |
111 if (!content::RegisterNativesImpl(env)) | 112 if (!content::RegisterNativesImpl(env)) |
112 return -1; | 113 return -1; |
113 | 114 |
114 content::SetContentMainDelegate(new content::ShellMainDelegate()); | 115 content::SetContentMainDelegate(new content::ShellMainDelegate()); |
115 return JNI_VERSION_1_4; | 116 return JNI_VERSION_1_4; |
116 } | 117 } |
OLD | NEW |