OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "mandoline/ui/test_runner/test_runner_application_delegate.h" |
| 6 |
| 7 #include <iostream> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/location.h" |
| 15 #include "base/path_service.h" |
| 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/threading/thread_restrictions.h" |
| 18 #include "components/view_manager/public/cpp/scoped_view_ptr.h" |
| 19 #include "mandoline/ui/common/mandoline_switches.h" |
| 20 #include "mojo/application/public/cpp/application_impl.h" |
| 21 #include "net/base/filename_util.h" |
| 22 #include "ui/gfx/geometry/size.h" |
| 23 #include "url/gurl.h" |
| 24 |
| 25 #if defined(OS_WIN) |
| 26 #include "base/strings/sys_string_conversions.h" |
| 27 #include "base/strings/utf_string_conversions.h" |
| 28 #endif |
| 29 |
| 30 namespace mandoline { |
| 31 |
| 32 namespace { |
| 33 |
| 34 GURL GetURLForLayoutTest(const std::string& test_name, |
| 35 base::FilePath* current_working_directory, |
| 36 bool* enable_pixel_dumping, |
| 37 std::string* expected_pixel_hash) { |
| 38 // A test name is formated like file:///path/to/test'--pixel-test'pixelhash |
| 39 std::string path_or_url = test_name; |
| 40 std::string pixel_switch; |
| 41 std::string pixel_hash; |
| 42 std::string::size_type separator_position = path_or_url.find('\''); |
| 43 if (separator_position != std::string::npos) { |
| 44 pixel_switch = path_or_url.substr(separator_position + 1); |
| 45 path_or_url.erase(separator_position); |
| 46 } |
| 47 separator_position = pixel_switch.find('\''); |
| 48 if (separator_position != std::string::npos) { |
| 49 pixel_hash = pixel_switch.substr(separator_position + 1); |
| 50 pixel_switch.erase(separator_position); |
| 51 } |
| 52 if (enable_pixel_dumping) { |
| 53 *enable_pixel_dumping = |
| 54 (pixel_switch == "--pixel-test" || pixel_switch == "-p"); |
| 55 } |
| 56 if (expected_pixel_hash) |
| 57 *expected_pixel_hash = pixel_hash; |
| 58 |
| 59 GURL test_url(path_or_url); |
| 60 if (!(test_url.is_valid() && test_url.has_scheme())) { |
| 61 // This is a test. |
| 62 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 63 #if defined(OS_WIN) |
| 64 base::FilePath::StringType wide_path_or_url = |
| 65 base::SysNativeMBToWide(path_or_url); |
| 66 base::FilePath local_file(wide_path_or_url); |
| 67 #else |
| 68 base::FilePath local_file(path_or_url); |
| 69 #endif |
| 70 if (!base::PathExists(local_file)) { |
| 71 base::FilePath base_path; |
| 72 PathService::Get(base::DIR_SOURCE_ROOT, &base_path); |
| 73 local_file = base_path.Append(FILE_PATH_LITERAL("third_party")) |
| 74 .Append(FILE_PATH_LITERAL("WebKit")) |
| 75 .Append(FILE_PATH_LITERAL("LayoutTests")) |
| 76 .Append(local_file); |
| 77 } |
| 78 test_url = net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file)); |
| 79 } |
| 80 base::FilePath local_path; |
| 81 if (current_working_directory) { |
| 82 // We're outside of the message loop here, and this is a test. |
| 83 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 84 if (net::FileURLToFilePath(test_url, &local_path)) |
| 85 *current_working_directory = local_path.DirName(); |
| 86 else |
| 87 base::GetCurrentDirectory(current_working_directory); |
| 88 } |
| 89 return test_url; |
| 90 } |
| 91 |
| 92 bool GetNextTestURL(const base::CommandLine::StringVector& args, |
| 93 size_t* position, |
| 94 GURL* test_url) { |
| 95 std::string test_string; |
| 96 if (*position >= args.size()) |
| 97 return false; |
| 98 if (args[*position] == FILE_PATH_LITERAL("-")) { |
| 99 do { |
| 100 bool success = !!std::getline(std::cin, test_string, '\n'); |
| 101 if (!success) |
| 102 return false; |
| 103 } while (test_string.empty()); |
| 104 } else { |
| 105 #if defined(OS_WIN) |
| 106 test_string = base::WideToUTF8(args[(*position)++]); |
| 107 #else |
| 108 test_string = args[(*position)++]; |
| 109 #endif |
| 110 } |
| 111 |
| 112 DCHECK(!test_string.empty()); |
| 113 if (test_string == "QUIT") |
| 114 return false; |
| 115 bool enable_pixel_dumps; |
| 116 std::string pixel_hash; |
| 117 base::FilePath cwd; |
| 118 *test_url = |
| 119 GetURLForLayoutTest(test_string, &cwd, &enable_pixel_dumps, &pixel_hash); |
| 120 return true; |
| 121 } |
| 122 |
| 123 } // namespace |
| 124 |
| 125 TestRunnerApplicationDelegate::TestRunnerApplicationDelegate() {} |
| 126 TestRunnerApplicationDelegate::~TestRunnerApplicationDelegate() {} |
| 127 |
| 128 void TestRunnerApplicationDelegate::Terminate() { |
| 129 if (root()) |
| 130 mojo::ScopedViewPtr::DeleteViewOrViewManager(root()); |
| 131 } |
| 132 |
| 133 bool TestRunnerApplicationDelegate::ShouldLaunchURLOnInitialize() const { |
| 134 return false; |
| 135 } |
| 136 |
| 137 gfx::Size TestRunnerApplicationDelegate::GetViewportSize() const { |
| 138 return gfx::Size(800, 600); |
| 139 } |
| 140 |
| 141 void TestRunnerApplicationDelegate::OnEmbed(mojo::View* root) { |
| 142 PhoneBrowserApplicationDelegate::OnEmbed(root); |
| 143 |
| 144 // If this is a sys-check, then terminate in the next cycle. |
| 145 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 146 switches::kCheckLayoutTestSysDeps)) { |
| 147 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 148 FROM_HERE, base::Bind(&TestRunnerApplicationDelegate::Terminate, |
| 149 base::Unretained(this))); |
| 150 return; |
| 151 } |
| 152 |
| 153 cmdline_args_ = base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 154 std::cout << "#READY\n"; |
| 155 std::cout.flush(); |
| 156 cmdline_position_ = 0; |
| 157 GURL test_url; |
| 158 if (GetNextTestURL(cmdline_args_, &cmdline_position_, &test_url)) |
| 159 LaunchURL(test_url.spec()); |
| 160 } |
| 161 |
| 162 bool TestRunnerApplicationDelegate::ConfigureIncomingConnection( |
| 163 mojo::ApplicationConnection* connection) { |
| 164 if (!PhoneBrowserApplicationDelegate::ConfigureIncomingConnection(connection)) |
| 165 return false; |
| 166 connection->AddService<html_viewer::LayoutTestRunner>(this); |
| 167 return true; |
| 168 } |
| 169 |
| 170 void TestRunnerApplicationDelegate::TestFinished() { |
| 171 std::cout << "#EOF\n"; |
| 172 std::cout.flush(); |
| 173 |
| 174 std::cerr << "#EOF\n"; |
| 175 std::cerr.flush(); |
| 176 |
| 177 GURL test_url; |
| 178 if (GetNextTestURL(cmdline_args_, &cmdline_position_, &test_url)) |
| 179 LaunchURL(test_url.spec()); |
| 180 else |
| 181 Terminate(); |
| 182 } |
| 183 |
| 184 void TestRunnerApplicationDelegate::Create( |
| 185 mojo::ApplicationConnection* connection, |
| 186 mojo::InterfaceRequest<html_viewer::LayoutTestRunner> request) { |
| 187 layout_test_runner_.AddBinding(this, request.Pass()); |
| 188 } |
| 189 |
| 190 } // namespace mandoline |
OLD | NEW |