Index: mandoline/ui/test_runner/test_runner_application_delegate.cc |
diff --git a/mandoline/ui/test_runner/test_runner_application_delegate.cc b/mandoline/ui/test_runner/test_runner_application_delegate.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fcb952753f937b3dc2697b306b69eb2760fe8f72 |
--- /dev/null |
+++ b/mandoline/ui/test_runner/test_runner_application_delegate.cc |
@@ -0,0 +1,190 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "mandoline/ui/test_runner/test_runner_application_delegate.h" |
+ |
+#include <iostream> |
+ |
+#include "base/bind.h" |
+#include "base/command_line.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/location.h" |
+#include "base/path_service.h" |
+#include "base/thread_task_runner_handle.h" |
+#include "base/threading/thread_restrictions.h" |
+#include "components/view_manager/public/cpp/scoped_view_ptr.h" |
+#include "mandoline/ui/common/mandoline_switches.h" |
+#include "mojo/application/public/cpp/application_impl.h" |
+#include "net/base/filename_util.h" |
+#include "ui/gfx/geometry/size.h" |
+#include "url/gurl.h" |
+ |
+#if defined(OS_WIN) |
+#include "base/strings/sys_string_conversions.h" |
+#include "base/strings/utf_string_conversions.h" |
+#endif |
+ |
+namespace mandoline { |
+ |
+namespace { |
+ |
+GURL GetURLForLayoutTest(const std::string& test_name, |
+ base::FilePath* current_working_directory, |
+ bool* enable_pixel_dumping, |
+ std::string* expected_pixel_hash) { |
+ // A test name is formated like file:///path/to/test'--pixel-test'pixelhash |
+ std::string path_or_url = test_name; |
+ std::string pixel_switch; |
+ std::string pixel_hash; |
+ std::string::size_type separator_position = path_or_url.find('\''); |
+ if (separator_position != std::string::npos) { |
+ pixel_switch = path_or_url.substr(separator_position + 1); |
+ path_or_url.erase(separator_position); |
+ } |
+ separator_position = pixel_switch.find('\''); |
+ if (separator_position != std::string::npos) { |
+ pixel_hash = pixel_switch.substr(separator_position + 1); |
+ pixel_switch.erase(separator_position); |
+ } |
+ if (enable_pixel_dumping) { |
+ *enable_pixel_dumping = |
+ (pixel_switch == "--pixel-test" || pixel_switch == "-p"); |
+ } |
+ if (expected_pixel_hash) |
+ *expected_pixel_hash = pixel_hash; |
+ |
+ GURL test_url(path_or_url); |
+ if (!(test_url.is_valid() && test_url.has_scheme())) { |
+ // This is a test. |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+#if defined(OS_WIN) |
+ base::FilePath::StringType wide_path_or_url = |
+ base::SysNativeMBToWide(path_or_url); |
+ base::FilePath local_file(wide_path_or_url); |
+#else |
+ base::FilePath local_file(path_or_url); |
+#endif |
+ if (!base::PathExists(local_file)) { |
+ base::FilePath base_path; |
+ PathService::Get(base::DIR_SOURCE_ROOT, &base_path); |
+ local_file = base_path.Append(FILE_PATH_LITERAL("third_party")) |
+ .Append(FILE_PATH_LITERAL("WebKit")) |
+ .Append(FILE_PATH_LITERAL("LayoutTests")) |
+ .Append(local_file); |
+ } |
+ test_url = net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file)); |
+ } |
+ base::FilePath local_path; |
+ if (current_working_directory) { |
+ // We're outside of the message loop here, and this is a test. |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+ if (net::FileURLToFilePath(test_url, &local_path)) |
+ *current_working_directory = local_path.DirName(); |
+ else |
+ base::GetCurrentDirectory(current_working_directory); |
+ } |
+ return test_url; |
+} |
+ |
+bool GetNextTestURL(const base::CommandLine::StringVector& args, |
+ size_t* position, |
+ GURL* test_url) { |
+ std::string test_string; |
+ if (*position >= args.size()) |
+ return false; |
+ if (args[*position] == FILE_PATH_LITERAL("-")) { |
+ do { |
+ bool success = !!std::getline(std::cin, test_string, '\n'); |
+ if (!success) |
+ return false; |
+ } while (test_string.empty()); |
+ } else { |
+#if defined(OS_WIN) |
+ test_string = base::WideToUTF8(args[(*position)++]); |
+#else |
+ test_string = args[(*position)++]; |
+#endif |
+ } |
+ |
+ DCHECK(!test_string.empty()); |
+ if (test_string == "QUIT") |
+ return false; |
+ bool enable_pixel_dumps; |
+ std::string pixel_hash; |
+ base::FilePath cwd; |
+ *test_url = |
+ GetURLForLayoutTest(test_string, &cwd, &enable_pixel_dumps, &pixel_hash); |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+TestRunnerApplicationDelegate::TestRunnerApplicationDelegate() {} |
+TestRunnerApplicationDelegate::~TestRunnerApplicationDelegate() {} |
+ |
+void TestRunnerApplicationDelegate::Terminate() { |
+ if (root()) |
+ mojo::ScopedViewPtr::DeleteViewOrViewManager(root()); |
+} |
+ |
+bool TestRunnerApplicationDelegate::ShouldLaunchURLOnInitialize() const { |
+ return false; |
+} |
+ |
+gfx::Size TestRunnerApplicationDelegate::GetViewportSize() const { |
+ return gfx::Size(800, 600); |
+} |
+ |
+void TestRunnerApplicationDelegate::OnEmbed(mojo::View* root) { |
+ PhoneBrowserApplicationDelegate::OnEmbed(root); |
+ |
+ // If this is a sys-check, then terminate in the next cycle. |
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kCheckLayoutTestSysDeps)) { |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(&TestRunnerApplicationDelegate::Terminate, |
+ base::Unretained(this))); |
+ return; |
+ } |
+ |
+ cmdline_args_ = base::CommandLine::ForCurrentProcess()->GetArgs(); |
+ std::cout << "#READY\n"; |
+ std::cout.flush(); |
+ cmdline_position_ = 0; |
+ GURL test_url; |
+ if (GetNextTestURL(cmdline_args_, &cmdline_position_, &test_url)) |
+ LaunchURL(test_url.spec()); |
+} |
+ |
+bool TestRunnerApplicationDelegate::ConfigureIncomingConnection( |
+ mojo::ApplicationConnection* connection) { |
+ if (!PhoneBrowserApplicationDelegate::ConfigureIncomingConnection(connection)) |
+ return false; |
+ connection->AddService<html_viewer::LayoutTestRunner>(this); |
+ return true; |
+} |
+ |
+void TestRunnerApplicationDelegate::TestFinished() { |
+ std::cout << "#EOF\n"; |
+ std::cout.flush(); |
+ |
+ std::cerr << "#EOF\n"; |
+ std::cerr.flush(); |
+ |
+ GURL test_url; |
+ if (GetNextTestURL(cmdline_args_, &cmdline_position_, &test_url)) |
+ LaunchURL(test_url.spec()); |
+ else |
+ Terminate(); |
+} |
+ |
+void TestRunnerApplicationDelegate::Create( |
+ mojo::ApplicationConnection* connection, |
+ mojo::InterfaceRequest<html_viewer::LayoutTestRunner> request) { |
+ layout_test_runner_.AddBinding(this, request.Pass()); |
+} |
+ |
+} // namespace mandoline |