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"
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"
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
;
60 #if defined(OS_ANDROID)
61 if (content::GetTestUrlForAndroid(path_or_url
, &test_url
))
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
;
70 base::FilePath::StringType wide_path_or_url
=
71 base::SysNativeMBToWide(path_or_url
);
72 base::FilePath
local_file(wide_path_or_url
);
74 base::FilePath
local_file(path_or_url
);
76 if (!base::PathExists(local_file
)) {
77 local_file
= content::GetWebKitRootDirFilePath()
78 .Append(FILE_PATH_LITERAL("LayoutTests"))
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();
90 base::GetCurrentDirectory(current_working_directory
);
95 bool GetNextTest(const base::CommandLine::StringVector
& args
,
98 if (*position
>= args
.size())
100 if (args
[*position
] == FILE_PATH_LITERAL("-"))
101 return !!std::getline(std::cin
, *test
, '\n');
103 *test
= base::WideToUTF8(args
[(*position
)++]);
105 *test
= args
[(*position
)++];
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())
115 if (test_string
== "QUIT")
118 bool enable_pixel_dumps
;
119 std::string pixel_hash
;
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
)) {
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
;
139 if (!content::BlinkTestController::Get()->ResetAfterLayoutTest())
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();
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";
168 while (GetNextTest(args
, &command_line_position
, &test_string
)) {
169 if (!RunOneTest(test_string
, &ran_at_least_once
, main_runner
))
172 if (!ran_at_least_once
) {
173 base::MessageLoop::current()->PostTask(FROM_HERE
,
174 base::MessageLoop::QuitClosure());
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
182 main_runner
->Shutdown();
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();
206 int exit_code
= main_runner
->Initialize(parameters
);
207 DCHECK_LT(exit_code
, 0)
208 << "BrowserMainRunner::Initialize failed in LayoutTestBrowserMain";
213 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
214 switches::kCheckLayoutTestSysDeps
)) {
215 base::MessageLoop::current()->PostTask(FROM_HERE
,
216 base::MessageLoop::QuitClosure());
218 content::Shell::CloseAllWindows();
219 main_runner
->Shutdown();
223 exit_code
= RunTests(main_runner
);
225 #if !defined(OS_ANDROID)
226 main_runner
->Shutdown();