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/layout_test/layout_test_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"
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());
48 bool GetTestUrlForAndroid(std::string
& path_or_url
, GURL
* url
) {
49 if (path_or_url
.find(kAndroidLayoutTestPath
) == std::string::npos
)
52 std::string
test_location(kAndroidLayoutTestBase
);
53 test_location
.append(path_or_url
.substr(strlen(kAndroidLayoutTestPath
)));
55 *url
= GURL(test_location
);
59 void EnsureInitializeForAndroidLayoutTests() {
60 JNIEnv
* env
= base::android::AttachCurrentThread();
61 content::NestedMessagePumpAndroid::RegisterJni(env
);
62 content::RegisterNativesImpl(env
);
64 bool success
= base::MessageLoop::InitMessagePumpForUIFactory(
65 &CreateMessagePumpForUI
);
66 CHECK(success
) << "Unable to initialize the message pump for Android.";
68 // Android will need three FIFOs to communicate with the Blink test runner,
69 // one for each of [stdout, stderr, stdin].
70 base::FilePath
files_dir(GetTestFilesDirectory(env
));
72 base::FilePath
stdout_fifo(files_dir
.Append(FILE_PATH_LITERAL("test.fifo")));
73 EnsureCreateFIFO(stdout_fifo
);
75 base::FilePath
stderr_fifo(
76 files_dir
.Append(FILE_PATH_LITERAL("stderr.fifo")));
77 EnsureCreateFIFO(stderr_fifo
);
79 base::FilePath
stdin_fifo(files_dir
.Append(FILE_PATH_LITERAL("stdin.fifo")));
80 EnsureCreateFIFO(stdin_fifo
);
82 // Redirecting stdout needs to happen before redirecting stdin, which needs
83 // to happen before redirecting stderr.
84 success
= base::android::RedirectStream(stdout
, stdout_fifo
, "w") &&
85 base::android::RedirectStream(stdin
, stdin_fifo
, "r") &&
86 base::android::RedirectStream(stderr
, stderr_fifo
, "w");
88 CHECK(success
) << "Unable to initialize the Android FIFOs.";
91 } // namespace content