Fix link in German terms of service.
[chromium-blink-merge.git] / content / shell / browser / layout_test / layout_test_browser_main.cc
blob44f05495a63856c578d4c1fb17eb47856a98b5db
1 // Copyright 2014 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_browser_main.h"
7 #include <iostream>
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/strings/sys_string_conversions.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_restrictions.h"
19 #include "content/public/browser/browser_main_runner.h"
20 #include "content/public/common/url_constants.h"
21 #include "content/shell/browser/blink_test_controller.h"
22 #include "content/shell/browser/shell.h"
23 #include "content/shell/common/shell_switches.h"
24 #include "content/shell/renderer/layout_test/blink_test_helpers.h"
25 #include "net/base/filename_util.h"
27 #if defined(OS_ANDROID)
28 #include "base/run_loop.h"
29 #include "content/shell/browser/layout_test/layout_test_android.h"
30 #endif
32 namespace {
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);
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);
52 if (enable_pixel_dumping) {
53 *enable_pixel_dumping =
54 (pixel_switch == "--pixel-test" || pixel_switch == "-p");
56 if (expected_pixel_hash)
57 *expected_pixel_hash = pixel_hash;
59 GURL test_url;
60 #if defined(OS_ANDROID)
61 if (content::GetTestUrlForAndroid(path_or_url, &test_url))
62 return test_url;
63 #endif
65 test_url = GURL(path_or_url);
66 if (!(test_url.is_valid() && test_url.has_scheme())) {
67 // We're outside of the message loop here, and 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 local_file = content::GetWebKitRootDirFilePath()
78 .Append(FILE_PATH_LITERAL("LayoutTests"))
79 .Append(local_file);
81 test_url = net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file));
83 base::FilePath local_path;
84 if (current_working_directory) {
85 // We're outside of the message loop here, and this is a test.
86 base::ThreadRestrictions::ScopedAllowIO allow_io;
87 if (net::FileURLToFilePath(test_url, &local_path))
88 *current_working_directory = local_path.DirName();
89 else
90 base::GetCurrentDirectory(current_working_directory);
92 return test_url;
95 bool GetNextTest(const base::CommandLine::StringVector& args,
96 size_t* position,
97 std::string* test) {
98 if (*position >= args.size())
99 return false;
100 if (args[*position] == FILE_PATH_LITERAL("-"))
101 return !!std::getline(std::cin, *test, '\n');
102 #if defined(OS_WIN)
103 *test = base::WideToUTF8(args[(*position)++]);
104 #else
105 *test = args[(*position)++];
106 #endif
107 return true;
110 bool RunOneTest(const std::string& test_string,
111 bool* ran_at_least_once,
112 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
113 if (test_string.empty())
114 return true;
115 if (test_string == "QUIT")
116 return false;
118 bool enable_pixel_dumps;
119 std::string pixel_hash;
120 base::FilePath cwd;
121 GURL test_url =
122 GetURLForLayoutTest(test_string, &cwd, &enable_pixel_dumps, &pixel_hash);
123 if (!content::BlinkTestController::Get()->PrepareForLayoutTest(
124 test_url, cwd, enable_pixel_dumps, pixel_hash)) {
125 return false;
128 *ran_at_least_once = true;
129 #if defined(OS_ANDROID)
130 // The message loop on Android is provided by the system, and does not
131 // offer a blocking Run() method. For layout tests, use a nested loop
132 // together with a base::RunLoop so it can block until a QuitClosure.
133 base::RunLoop run_loop;
134 run_loop.Run();
135 #else
136 main_runner->Run();
137 #endif
139 if (!content::BlinkTestController::Get()->ResetAfterLayoutTest())
140 return false;
142 #if defined(OS_ANDROID)
143 // There will be left-over tasks in the queue for Android because the
144 // main window is being destroyed. Run them before starting the next test.
145 base::MessageLoop::current()->RunUntilIdle();
146 #endif
147 return true;
150 int RunTests(const scoped_ptr<content::BrowserMainRunner>& main_runner) {
151 content::BlinkTestController test_controller;
153 // We're outside of the message loop here, and this is a test.
154 base::ThreadRestrictions::ScopedAllowIO allow_io;
155 base::FilePath temp_path;
156 base::GetTempDir(&temp_path);
157 test_controller.SetTempPath(temp_path);
159 std::string test_string;
160 base::CommandLine::StringVector args =
161 base::CommandLine::ForCurrentProcess()->GetArgs();
162 size_t command_line_position = 0;
163 bool ran_at_least_once = false;
165 std::cout << "#READY\n";
166 std::cout.flush();
168 while (GetNextTest(args, &command_line_position, &test_string)) {
169 if (!RunOneTest(test_string, &ran_at_least_once, main_runner))
170 break;
172 if (!ran_at_least_once) {
173 base::MessageLoop::current()->PostTask(FROM_HERE,
174 base::MessageLoop::QuitClosure());
175 main_runner->Run();
178 #if defined(OS_ANDROID)
179 // We need to execute 'main_runner->Shutdown()' before the test_controller
180 // destructs when running on Android, and after it destructs when running
181 // anywhere else.
182 main_runner->Shutdown();
183 #endif
185 return 0;
188 } // namespace
190 // Main routine for running as the Browser process.
191 int LayoutTestBrowserMain(
192 const content::MainFunctionParams& parameters,
193 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
194 base::ScopedTempDir browser_context_path_for_layout_tests;
196 CHECK(browser_context_path_for_layout_tests.CreateUniqueTempDir());
197 CHECK(!browser_context_path_for_layout_tests.path().MaybeAsASCII().empty());
198 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
199 switches::kContentShellDataPath,
200 browser_context_path_for_layout_tests.path().MaybeAsASCII());
202 #if defined(OS_ANDROID)
203 content::EnsureInitializeForAndroidLayoutTests();
204 #endif
206 int exit_code = main_runner->Initialize(parameters);
207 DCHECK_LT(exit_code, 0)
208 << "BrowserMainRunner::Initialize failed in LayoutTestBrowserMain";
210 if (exit_code >= 0)
211 return exit_code;
213 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
214 switches::kCheckLayoutTestSysDeps)) {
215 base::MessageLoop::current()->PostTask(FROM_HERE,
216 base::MessageLoop::QuitClosure());
217 main_runner->Run();
218 content::Shell::CloseAllWindows();
219 main_runner->Shutdown();
220 return 0;
223 exit_code = RunTests(main_runner);
225 #if !defined(OS_ANDROID)
226 main_runner->Shutdown();
227 #endif
229 return exit_code;