Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / shell / browser / shell_layout_tests_android.cc
blob5047bb613c70759e57cfd8512844fe09663c214e
1 // Copyright 2013 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 #include "content/shell/browser/shell_layout_tests_android.h"
7 #include "base/android/fifo_utils.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_string.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/message_loop/message_loop.h"
13 #include "content/public/test/nested_message_pump_android.h"
14 #include "content/shell/common/shell_switches.h"
15 #include "jni/ShellLayoutTestUtils_jni.h"
16 #include "url/gurl.h"
18 namespace {
20 // Path to search for when translating a layout test path to an URL.
21 const char kAndroidLayoutTestPath[] =
22 "/data/local/tmp/third_party/WebKit/LayoutTests/";
24 // The base URL from which layout tests are being served on Android.
25 const char kAndroidLayoutTestBase[] = "http://127.0.0.1:8000/all-tests/";
27 base::FilePath GetTestFilesDirectory(JNIEnv* env) {
28 ScopedJavaLocalRef<jstring> directory =
29 content::Java_ShellLayoutTestUtils_getApplicationFilesDirectory(
30 env, base::android::GetApplicationContext());
31 return base::FilePath(ConvertJavaStringToUTF8(directory));
34 void EnsureCreateFIFO(const base::FilePath& path) {
35 unlink(path.value().c_str());
36 CHECK(base::android::CreateFIFO(path, 0666))
37 << "Unable to create the Android's FIFO: " << path.value().c_str();
40 scoped_ptr<base::MessagePump> CreateMessagePumpForUI() {
41 return scoped_ptr<base::MessagePump>(new content::NestedMessagePumpAndroid());
44 } // namespace
46 namespace content {
48 bool GetTestUrlForAndroid(std::string& path_or_url, GURL* url) {
49 if (path_or_url.find(kAndroidLayoutTestPath) == std::string::npos)
50 return false;
52 std::string test_location(kAndroidLayoutTestBase);
53 test_location.append(path_or_url.substr(strlen(kAndroidLayoutTestPath)));
55 *url = GURL(test_location);
56 return true;
59 void EnsureInitializeForAndroidLayoutTests() {
60 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree));
62 JNIEnv* env = base::android::AttachCurrentThread();
63 content::NestedMessagePumpAndroid::RegisterJni(env);
64 content::RegisterNativesImpl(env);
66 bool success = base::MessageLoop::InitMessagePumpForUIFactory(
67 &CreateMessagePumpForUI);
68 CHECK(success) << "Unable to initialize the message pump for Android.";
70 // Android will need three FIFOs to communicate with the Blink test runner,
71 // one for each of [stdout, stderr, stdin].
72 base::FilePath files_dir(GetTestFilesDirectory(env));
74 base::FilePath stdout_fifo(files_dir.Append(FILE_PATH_LITERAL("test.fifo")));
75 EnsureCreateFIFO(stdout_fifo);
77 base::FilePath stderr_fifo(
78 files_dir.Append(FILE_PATH_LITERAL("stderr.fifo")));
79 EnsureCreateFIFO(stderr_fifo);
81 base::FilePath stdin_fifo(files_dir.Append(FILE_PATH_LITERAL("stdin.fifo")));
82 EnsureCreateFIFO(stdin_fifo);
84 // Redirecting stdout needs to happen before redirecting stdin, which needs
85 // to happen before redirecting stderr.
86 success = base::android::RedirectStream(stdout, stdout_fifo, "w") &&
87 base::android::RedirectStream(stdin, stdin_fifo, "r") &&
88 base::android::RedirectStream(stderr, stderr_fifo, "w");
90 CHECK(success) << "Unable to initialize the Android FIFOs.";
93 } // namespace content