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. 4 5 #include "content/shell/browser/shell_layout_tests_android.h" 6 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" 17 18 namespace { 19 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/"; 23 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/"; 26 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)); 32 } 33 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(); 38 } 39 40 base::MessagePump* CreateMessagePumpForUI() { 41 return new content::NestedMessagePumpAndroid(); 42 } 43 44 } // namespace 45 46 namespace content { 47 48 bool GetTestUrlForAndroid(std::string& path_or_url, GURL* url) { 49 if (path_or_url.find(kAndroidLayoutTestPath) == std::string::npos) 50 return false; 51 52 std::string test_location(kAndroidLayoutTestBase); 53 test_location.append(path_or_url.substr(strlen(kAndroidLayoutTestPath))); 54 55 *url = GURL(test_location); 56 return true; 57 } 58 59 void EnsureInitializeForAndroidLayoutTests() { 60 CHECK(CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree)); 61 62 JNIEnv* env = base::android::AttachCurrentThread(); 63 content::NestedMessagePumpAndroid::RegisterJni(env); 64 content::RegisterNativesImpl(env); 65 66 bool success = base::MessageLoop::InitMessagePumpForUIFactory( 67 &CreateMessagePumpForUI); 68 CHECK(success) << "Unable to initialize the message pump for Android."; 69 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)); 73 74 base::FilePath stdout_fifo(files_dir.Append(FILE_PATH_LITERAL("test.fifo"))); 75 EnsureCreateFIFO(stdout_fifo); 76 77 base::FilePath stderr_fifo( 78 files_dir.Append(FILE_PATH_LITERAL("stderr.fifo"))); 79 EnsureCreateFIFO(stderr_fifo); 80 81 base::FilePath stdin_fifo(files_dir.Append(FILE_PATH_LITERAL("stdin.fifo"))); 82 EnsureCreateFIFO(stdin_fifo); 83 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"); 89 90 CHECK(success) << "Unable to initialize the Android FIFOs."; 91 } 92 93 } // namespace content 94