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.
5 // This class sets up the environment for running the content browser tests
6 // inside an android application.
8 #include <android/log.h>
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_util.h"
17 #include "base/files/file_path.h"
18 #include "base/logging.h"
19 #include "base/string_util.h"
20 #include "base/stringprintf.h"
21 #include "base/strings/string_tokenizer.h"
22 #include "content/public/app/android_library_loader_hooks.h"
23 #include "content/public/app/content_main.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/test/test_launcher.h"
26 #include "content/shell/android/shell_jni_registrar.h"
27 #include "content/shell/shell_main_delegate.h"
28 #include "content/test/browser_test_message_pump_android.h"
29 #include "jni/ContentBrowserTestsActivity_jni.h"
30 #include "testing/android/native_test_util.h"
32 using testing::native_test_util::ArgsToArgv
;
33 using testing::native_test_util::CreateFIFO
;
34 using testing::native_test_util::ParseArgsFromCommandLineFile
;
35 using testing::native_test_util::RedirectStream
;
36 using testing::native_test_util::ScopedMainEntryLogger
;
38 // The main function of the program to be wrapped as an apk.
39 extern int main(int argc
, char** argv
);
43 // The test runner script writes the command line file in
45 static const char kCommandLineFilePath
[] =
46 "/data/local/tmp/content-browser-tests-command-line";
52 static void RunTests(JNIEnv
* env
,
55 jobject app_context
) {
57 // Command line basic initialization, will be fully initialized later.
58 static const char* const kInitialArgv
[] = { "ContentBrowserTestsActivity" };
59 CommandLine::Init(arraysize(kInitialArgv
), kInitialArgv
);
61 // Set the application context in base.
62 base::android::ScopedJavaLocalRef
<jobject
> scoped_context(
63 env
, env
->NewLocalRef(app_context
));
64 base::android::InitApplicationContext(scoped_context
);
65 base::android::RegisterJni(env
);
67 std::vector
<std::string
> args
;
68 ParseArgsFromCommandLineFile(kCommandLineFilePath
, &args
);
70 std::vector
<char*> argv
;
71 int argc
= ArgsToArgv(args
, &argv
);
73 // Fully initialize command line with arguments.
74 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
75 command_line
->AppendArguments(CommandLine(argc
, &argv
[0]), false);
77 // Append required switches.
78 command_line
->AppendSwitch(content::kSingleProcessTestsFlag
);
79 command_line
->AppendSwitch(switches::kUseFakeDeviceForMediaStream
);
80 // Specify a socket name to not conflict with the default one used
82 command_line
->AppendSwitchASCII(switches::kRemoteDebuggingSocketName
,
83 "content_browsertests_devtools_remote");
85 // Create fifo and redirect stdout and stderr to it.
86 base::FilePath
files_dir(
87 base::android::ConvertJavaStringToUTF8(env
, jfiles_dir
));
88 base::FilePath
fifo_path(files_dir
.Append(base::FilePath("test.fifo")));
89 CreateFIFO(fifo_path
.value().c_str());
90 RedirectStream(stdout
, fifo_path
.value().c_str(), "w");
91 dup2(STDOUT_FILENO
, STDERR_FILENO
);
93 ScopedMainEntryLogger scoped_main_entry_logger
;
96 } // namespace content
98 // This is called by the VM when the shared library is first loaded.
99 JNI_EXPORT jint
JNI_OnLoad(JavaVM
* vm
, void* reserved
) {
100 base::android::InitVM(vm
);
101 JNIEnv
* env
= base::android::AttachCurrentThread();
103 if (!content::RegisterLibraryLoaderEntryHook(env
))
106 if (!content::android::RegisterShellJni(env
))
109 if (!content::BrowserTestMessagePumpAndroid::RegisterJni(env
))
112 if (!content::RegisterNativesImpl(env
))
115 content::SetContentMainDelegate(new content::ShellMainDelegate());
116 return JNI_VERSION_1_4
;