OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 #include <unistd.h> | |
10 | |
11 #include "base/android/base_jni_registrar.h" | |
12 #include "base/android/fifo_utils.h" | |
13 #include "base/android/jni_android.h" | |
14 #include "base/android/jni_string.h" | |
15 #include "base/android/library_loader/library_loader_hooks.h" | |
16 #include "base/android/scoped_java_ref.h" | |
17 #include "base/base_switches.h" | |
18 #include "base/command_line.h" | |
19 #include "base/files/file_path.h" | |
20 #include "base/logging.h" | |
21 #include "base/strings/string_tokenizer.h" | |
22 #include "base/strings/string_util.h" | |
23 #include "base/strings/stringprintf.h" | |
24 #include "content/public/common/content_switches.h" | |
25 #include "content/public/test/test_launcher.h" | |
26 #include "jni/ComponentsBrowserTestsActivity_jni.h" | |
27 #include "media/base/media_switches.h" | |
28 #include "testing/android/native_test/native_test_util.h" | |
29 | |
30 using testing::native_test_util::ArgsToArgv; | |
31 using testing::native_test_util::ParseArgsFromCommandLineFile; | |
32 using testing::native_test_util::ScopedMainEntryLogger; | |
33 | |
34 // The main function of the program to be wrapped as an apk. | |
35 extern int main(int argc, char** argv); | |
36 | |
37 namespace { | |
38 | |
39 // The test runner script writes the command line file in | |
40 // "/data/local/tmp". | |
41 static const char kCommandLineFilePath[] = | |
42 "/data/local/tmp/components-browser-tests-command-line"; | |
43 | |
44 } // namespace | |
45 | |
46 namespace components { | |
47 | |
48 // TODO(jaekyun): Refactor and deduplicate with | |
49 // testing/android/native_test/native_test_launcher.cc (http://crbug.com/476410) | |
50 static void RunTests(JNIEnv* env, | |
51 jobject obj, | |
52 jstring jfiles_dir, | |
53 jobject app_context) { | |
54 // Command line basic initialization, will be fully initialized later. | |
55 static const char* const kInitialArgv[] = {"ComponentsBrowserTestsActivity"}; | |
56 base::CommandLine::Init(arraysize(kInitialArgv), kInitialArgv); | |
57 | |
58 // Set the application context in base. | |
59 base::android::ScopedJavaLocalRef<jobject> scoped_context( | |
60 env, env->NewLocalRef(app_context)); | |
61 base::android::InitApplicationContext(env, scoped_context); | |
62 base::android::RegisterJni(env); | |
63 | |
64 std::vector<std::string> args; | |
65 ParseArgsFromCommandLineFile(kCommandLineFilePath, &args); | |
66 | |
67 std::vector<char*> argv; | |
68 int argc = ArgsToArgv(args, &argv); | |
69 | |
70 // Fully initialize command line with arguments. | |
71 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
72 command_line->AppendArguments(base::CommandLine(argc, &argv[0]), false); | |
73 | |
74 // Append required switches. | |
75 command_line->AppendSwitch(content::kSingleProcessTestsFlag); | |
76 command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream); | |
77 command_line->AppendSwitch(switches::kUseFakeUIForMediaStream); | |
78 // Specify a socket name to not conflict with the default one used | |
79 // in content_shell. | |
80 command_line->AppendSwitchASCII(switches::kRemoteDebuggingSocketName, | |
81 "components_browsertests_devtools_remote"); | |
82 | |
83 // Create fifo and redirect stdout and stderr to it. | |
84 base::FilePath files_dir( | |
85 base::android::ConvertJavaStringToUTF8(env, jfiles_dir)); | |
86 base::FilePath fifo_path(files_dir.Append(base::FilePath("test.fifo"))); | |
87 base::android::CreateFIFO(fifo_path, 0666); | |
88 base::android::RedirectStream(stdout, fifo_path, "w+"); | |
89 dup2(STDOUT_FILENO, STDERR_FILENO); | |
90 | |
91 ScopedMainEntryLogger scoped_main_entry_logger; | |
92 main(argc, &argv[0]); | |
93 } | |
94 | |
95 bool RegisterComponentsBrowserTestsAndroid(JNIEnv* env) { | |
96 return RegisterNativesImpl(env); | |
97 } | |
98 | |
99 } // namespace components | |
OLD | NEW |