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 "components/web_view/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 "components/view_manager/public/cpp/view.h" |
| 20 #include "components/view_manager/public/cpp/view_tree_connection.h" |
| 21 #include "components/view_manager/public/cpp/view_tree_host_factory.h" |
| 22 #include "mojo/application/public/cpp/application_connection.h" |
| 23 #include "mojo/application/public/cpp/application_impl.h" |
| 24 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 25 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" |
| 26 #include "net/base/filename_util.h" |
| 27 #include "ui/gfx/geometry/rect.h" |
| 28 #include "ui/gfx/geometry/size.h" |
| 29 #include "url/gurl.h" |
| 30 |
| 31 #if defined(OS_WIN) |
| 32 #include "base/strings/sys_string_conversions.h" |
| 33 #include "base/strings/utf_string_conversions.h" |
| 34 #endif |
| 35 |
| 36 namespace web_view { |
| 37 |
| 38 namespace { |
| 39 |
| 40 GURL GetURLForLayoutTest(const std::string& test_name, |
| 41 base::FilePath* current_working_directory, |
| 42 bool* enable_pixel_dumping, |
| 43 std::string* expected_pixel_hash) { |
| 44 // A test name is formated like file:///path/to/test'--pixel-test'pixelhash |
| 45 std::string path_or_url = test_name; |
| 46 std::string pixel_switch; |
| 47 std::string pixel_hash; |
| 48 std::string::size_type separator_position = path_or_url.find('\''); |
| 49 if (separator_position != std::string::npos) { |
| 50 pixel_switch = path_or_url.substr(separator_position + 1); |
| 51 path_or_url.erase(separator_position); |
| 52 } |
| 53 separator_position = pixel_switch.find('\''); |
| 54 if (separator_position != std::string::npos) { |
| 55 pixel_hash = pixel_switch.substr(separator_position + 1); |
| 56 pixel_switch.erase(separator_position); |
| 57 } |
| 58 if (enable_pixel_dumping) { |
| 59 *enable_pixel_dumping = |
| 60 (pixel_switch == "--pixel-test" || pixel_switch == "-p"); |
| 61 } |
| 62 if (expected_pixel_hash) |
| 63 *expected_pixel_hash = pixel_hash; |
| 64 |
| 65 GURL test_url(path_or_url); |
| 66 if (!(test_url.is_valid() && test_url.has_scheme())) { |
| 67 // This is a test. |
| 68 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 69 #if defined(OS_WIN) |
| 70 base::FilePath::StringType wide_path_or_url = |
| 71 base::SysNativeMBToWide(path_or_url); |
| 72 base::FilePath local_file(wide_path_or_url); |
| 73 #else |
| 74 base::FilePath local_file(path_or_url); |
| 75 #endif |
| 76 if (!base::PathExists(local_file)) { |
| 77 base::FilePath base_path; |
| 78 PathService::Get(base::DIR_SOURCE_ROOT, &base_path); |
| 79 local_file = base_path.Append(FILE_PATH_LITERAL("third_party")) |
| 80 .Append(FILE_PATH_LITERAL("WebKit")) |
| 81 .Append(FILE_PATH_LITERAL("LayoutTests")) |
| 82 .Append(local_file); |
| 83 } |
| 84 test_url = net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file)); |
| 85 } |
| 86 base::FilePath local_path; |
| 87 if (current_working_directory) { |
| 88 // We're outside of the message loop here, and this is a test. |
| 89 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 90 if (net::FileURLToFilePath(test_url, &local_path)) |
| 91 *current_working_directory = local_path.DirName(); |
| 92 else |
| 93 base::GetCurrentDirectory(current_working_directory); |
| 94 } |
| 95 return test_url; |
| 96 } |
| 97 |
| 98 bool GetNextTestURL(const base::CommandLine::StringVector& args, |
| 99 size_t* position, |
| 100 GURL* test_url) { |
| 101 std::string test_string; |
| 102 if (*position >= args.size()) |
| 103 return false; |
| 104 if (args[*position] == FILE_PATH_LITERAL("-")) { |
| 105 do { |
| 106 bool success = !!std::getline(std::cin, test_string, '\n'); |
| 107 if (!success) |
| 108 return false; |
| 109 } while (test_string.empty()); |
| 110 } else { |
| 111 #if defined(OS_WIN) |
| 112 test_string = base::WideToUTF8(args[(*position)++]); |
| 113 #else |
| 114 test_string = args[(*position)++]; |
| 115 #endif |
| 116 } |
| 117 |
| 118 DCHECK(!test_string.empty()); |
| 119 if (test_string == "QUIT") |
| 120 return false; |
| 121 bool enable_pixel_dumps; |
| 122 std::string pixel_hash; |
| 123 base::FilePath cwd; |
| 124 *test_url = |
| 125 GetURLForLayoutTest(test_string, &cwd, &enable_pixel_dumps, &pixel_hash); |
| 126 return true; |
| 127 } |
| 128 |
| 129 } // namespace |
| 130 |
| 131 TestRunnerApplicationDelegate::TestRunnerApplicationDelegate() |
| 132 : app_(nullptr), |
| 133 root_(nullptr), |
| 134 content_(nullptr), |
| 135 cmdline_position_(0u) {} |
| 136 |
| 137 TestRunnerApplicationDelegate::~TestRunnerApplicationDelegate() { |
| 138 if (root_) |
| 139 mojo::ScopedViewPtr::DeleteViewOrViewManager(root_); |
| 140 } |
| 141 |
| 142 void TestRunnerApplicationDelegate::LaunchURL(const GURL& test_url) { |
| 143 if (!web_view_) { |
| 144 content_->SetAccessPolicy(mojo::ViewTree::ACCESS_POLICY_EMBED_ROOT); |
| 145 web_view_.reset(new WebView(this)); |
| 146 web_view_->Init(app_, content_); |
| 147 } |
| 148 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 149 request->url = test_url.spec(); |
| 150 web_view_->web_view()->LoadRequest(request.Pass()); |
| 151 } |
| 152 |
| 153 void TestRunnerApplicationDelegate::Terminate() { |
| 154 if (root_) |
| 155 mojo::ScopedViewPtr::DeleteViewOrViewManager(root_); |
| 156 } |
| 157 |
| 158 //////////////////////////////////////////////////////////////////////////////// |
| 159 // mojo::ApplicationDelegate implementation: |
| 160 |
| 161 void TestRunnerApplicationDelegate::Initialize(mojo::ApplicationImpl* app) { |
| 162 app_ = app; |
| 163 mojo::CreateSingleViewTreeHost(app_, this, &host_); |
| 164 } |
| 165 |
| 166 bool TestRunnerApplicationDelegate::ConfigureIncomingConnection( |
| 167 mojo::ApplicationConnection* connection) { |
| 168 connection->AddService<web_view::LayoutTestRunner>(this); |
| 169 return true; |
| 170 } |
| 171 |
| 172 //////////////////////////////////////////////////////////////////////////////// |
| 173 // mojo::ViewTreeDelegate implementation: |
| 174 |
| 175 void TestRunnerApplicationDelegate::OnEmbed(mojo::View* root) { |
| 176 root_ = root; |
| 177 |
| 178 // If this is a sys-check, then terminate in the next cycle. |
| 179 const char kCheckLayoutTestSysDeps[] = "check-layout-test-sys-deps"; |
| 180 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 181 kCheckLayoutTestSysDeps)) { |
| 182 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 183 FROM_HERE, base::Bind(&TestRunnerApplicationDelegate::Terminate, |
| 184 base::Unretained(this))); |
| 185 return; |
| 186 } |
| 187 |
| 188 const gfx::Size kViewportSize(800, 600); |
| 189 host_->SetSize(mojo::Size::From(kViewportSize)); |
| 190 |
| 191 content_ = root_->connection()->CreateView(); |
| 192 root_->AddChild(content_); |
| 193 content_->SetBounds(*mojo::Rect::From(gfx::Rect(kViewportSize))); |
| 194 content_->SetVisible(true); |
| 195 |
| 196 cmdline_args_ = base::CommandLine::ForCurrentProcess()->GetArgs(); |
| 197 std::cout << "#READY\n"; |
| 198 std::cout.flush(); |
| 199 |
| 200 cmdline_position_ = 0; |
| 201 GURL test_url; |
| 202 if (GetNextTestURL(cmdline_args_, &cmdline_position_, &test_url)) |
| 203 LaunchURL(test_url); |
| 204 } |
| 205 |
| 206 void TestRunnerApplicationDelegate::OnConnectionLost( |
| 207 mojo::ViewTreeConnection* connection) { |
| 208 root_ = nullptr; |
| 209 app_->Quit(); |
| 210 } |
| 211 |
| 212 //////////////////////////////////////////////////////////////////////////////// |
| 213 // mojom::WebViewClient implementation: |
| 214 |
| 215 void TestRunnerApplicationDelegate::TopLevelNavigate( |
| 216 mojo::URLRequestPtr request) { |
| 217 web_view_->web_view()->LoadRequest(request.Pass()); |
| 218 } |
| 219 |
| 220 void TestRunnerApplicationDelegate::LoadingStateChanged(bool is_loading) {} |
| 221 void TestRunnerApplicationDelegate::ProgressChanged(double progress) {} |
| 222 void TestRunnerApplicationDelegate::TitleChanged(const mojo::String& title) {} |
| 223 |
| 224 //////////////////////////////////////////////////////////////////////////////// |
| 225 // LayoutTestRunner implementation: |
| 226 |
| 227 void TestRunnerApplicationDelegate::TestFinished() { |
| 228 std::cout << "#EOF\n"; |
| 229 std::cout.flush(); |
| 230 |
| 231 std::cerr << "#EOF\n"; |
| 232 std::cerr.flush(); |
| 233 |
| 234 GURL test_url; |
| 235 if (GetNextTestURL(cmdline_args_, &cmdline_position_, &test_url)) |
| 236 LaunchURL(test_url); |
| 237 else |
| 238 Terminate(); |
| 239 } |
| 240 |
| 241 //////////////////////////////////////////////////////////////////////////////// |
| 242 // mojo::InterfaceFactory<LayoutTestRunner> implementation: |
| 243 |
| 244 void TestRunnerApplicationDelegate::Create( |
| 245 mojo::ApplicationConnection* connection, |
| 246 mojo::InterfaceRequest<web_view::LayoutTestRunner> request) { |
| 247 layout_test_runner_.AddBinding(this, request.Pass()); |
| 248 } |
| 249 |
| 250 } // namespace web_view |
OLD | NEW |