d4dd0b2221e12c8a3cd7e25f2df7546baaa6efe4
[platform/framework/web/crosswalk.git] / src / content / shell / android / browsertests_apk / content_browser_tests_android.cc
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 #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/app/android_library_loader_hooks.h"
25 #include "content/public/app/content_main.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/test/nested_message_pump_android.h"
28 #include "content/public/test/test_launcher.h"
29 #include "content/shell/android/shell_jni_registrar.h"
30 #include "content/shell/app/shell_main_delegate.h"
31 #include "jni/ContentBrowserTestsActivity_jni.h"
32 #include "testing/android/native_test_util.h"
33
34 using testing::native_test_util::ArgsToArgv;
35 using testing::native_test_util::ParseArgsFromCommandLineFile;
36 using testing::native_test_util::ScopedMainEntryLogger;
37
38 // The main function of the program to be wrapped as an apk.
39 extern int main(int argc, char** argv);
40
41 namespace {
42
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
48 }  // namespace
49
50 namespace content {
51
52 // TODO(nileshagrawal): Refactor and deduplicate with
53 // testing/android/native_test_launcher.cc
54 static void RunTests(JNIEnv* env,
55                      jobject obj,
56                      jstring jfiles_dir,
57                      jobject app_context) {
58   // Command line basic initialization, will be fully initialized later.
59   static const char* const kInitialArgv[] = { "ContentBrowserTestsActivity" };
60   CommandLine::Init(arraysize(kInitialArgv), kInitialArgv);
61
62   // Set the application context in base.
63   base::android::ScopedJavaLocalRef<jobject> scoped_context(
64       env, env->NewLocalRef(app_context));
65   base::android::InitApplicationContext(env, scoped_context);
66   base::android::RegisterJni(env);
67
68   std::vector<std::string> args;
69   ParseArgsFromCommandLineFile(kCommandLineFilePath, &args);
70
71   std::vector<char*> argv;
72   int argc = ArgsToArgv(args, &argv);
73
74   // Fully initialize command line with arguments.
75   CommandLine* command_line = CommandLine::ForCurrentProcess();
76   command_line->AppendArguments(CommandLine(argc, &argv[0]), false);
77
78   // Append required switches.
79   command_line->AppendSwitch(content::kSingleProcessTestsFlag);
80   command_line->AppendSwitch(switches::kUseFakeDeviceForMediaStream);
81   command_line->AppendSwitch(switches::kUseFakeUIForMediaStream);
82   // Specify a socket name to not conflict with the default one used
83   // in content_shell.
84   command_line->AppendSwitchASCII(switches::kRemoteDebuggingSocketName,
85                                   "content_browsertests_devtools_remote");
86
87   // Create fifo and redirect stdout and stderr to it.
88   base::FilePath files_dir(
89       base::android::ConvertJavaStringToUTF8(env, jfiles_dir));
90   base::FilePath fifo_path(files_dir.Append(base::FilePath("test.fifo")));
91   base::android::CreateFIFO(fifo_path, 0666);
92   base::android::RedirectStream(stdout, fifo_path, "w");
93   dup2(STDOUT_FILENO, STDERR_FILENO);
94
95   ScopedMainEntryLogger scoped_main_entry_logger;
96   main(argc, &argv[0]);
97 }
98 }  // namespace content
99
100 // This is called by the VM when the shared library is first loaded.
101 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
102
103   base::android::SetLibraryLoadedHook(&content::LibraryLoaded);
104
105   base::android::InitVM(vm);
106   JNIEnv* env = base::android::AttachCurrentThread();
107
108   if (!base::android::RegisterLibraryLoaderEntryHook(env))
109     return -1;
110
111   if (!content::android::RegisterShellJni(env))
112     return -1;
113
114   if (!content::NestedMessagePumpAndroid::RegisterJni(env))
115     return -1;
116
117   if (!content::RegisterNativesImpl(env))
118     return -1;
119
120   content::SetContentMainDelegate(new content::ShellMainDelegate());
121   return JNI_VERSION_1_4;
122 }