1 // Copyright 2013 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/shell_browser_main.h"
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/files/file_path.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/shell/browser/shell.h"
21 #include "content/shell/browser/webkit_test_controller.h"
22 #include "content/shell/common/shell_switches.h"
23 #include "content/shell/common/webkit_test_helpers.h"
24 #include "content/test/webkit_support.h"
25 #include "net/base/net_util.h"
27 #if defined(OS_ANDROID)
28 #include "base/run_loop.h"
29 #include "content/shell/browser/shell_layout_tests_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 std::wstring 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")).Append(local_file
);
80 test_url
= net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file
));
82 base::FilePath local_path
;
83 if (current_working_directory
) {
84 // We're outside of the message loop here, and this is a test.
85 base::ThreadRestrictions::ScopedAllowIO allow_io
;
86 if (net::FileURLToFilePath(test_url
, &local_path
))
87 *current_working_directory
= local_path
.DirName();
89 file_util::GetCurrentDirectory(current_working_directory
);
94 bool GetNextTest(const CommandLine::StringVector
& args
,
97 if (*position
>= args
.size())
99 if (args
[*position
] == FILE_PATH_LITERAL("-"))
100 return !!std::getline(std::cin
, *test
, '\n');
102 *test
= WideToUTF8(args
[(*position
)++]);
104 *test
= args
[(*position
)++];
111 // Main routine for running as the Browser process.
112 int ShellBrowserMain(
113 const content::MainFunctionParams
& parameters
,
114 const scoped_ptr
<content::BrowserMainRunner
>& main_runner
) {
115 bool layout_test_mode
=
116 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
);
117 base::ScopedTempDir browser_context_path_for_layout_tests
;
119 if (layout_test_mode
) {
120 CHECK(browser_context_path_for_layout_tests
.CreateUniqueTempDir());
121 CHECK(!browser_context_path_for_layout_tests
.path().MaybeAsASCII().empty());
122 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
123 switches::kContentShellDataPath
,
124 browser_context_path_for_layout_tests
.path().MaybeAsASCII());
126 #if defined(OS_ANDROID)
127 content::EnsureInitializeForAndroidLayoutTests();
131 int exit_code
= main_runner
->Initialize(parameters
);
132 DCHECK_LT(exit_code
, 0)
133 << "BrowserMainRunner::Initialize failed in ShellBrowserMain";
138 if (CommandLine::ForCurrentProcess()->HasSwitch(
139 switches::kCheckLayoutTestSysDeps
)) {
140 base::MessageLoop::current()->PostTask(FROM_HERE
,
141 base::MessageLoop::QuitClosure());
143 content::Shell::CloseAllWindows();
144 main_runner
->Shutdown();
148 if (layout_test_mode
) {
149 content::WebKitTestController test_controller
;
151 // We're outside of the message loop here, and this is a test.
152 base::ThreadRestrictions::ScopedAllowIO allow_io
;
153 base::FilePath temp_path
;
154 file_util::GetTempDir(&temp_path
);
155 test_controller
.SetTempPath(temp_path
);
157 std::string test_string
;
158 CommandLine::StringVector args
=
159 CommandLine::ForCurrentProcess()->GetArgs();
160 size_t command_line_position
= 0;
161 bool ran_at_least_once
= false;
163 std::cout
<< "#READY\n";
166 while (GetNextTest(args
, &command_line_position
, &test_string
)) {
167 if (test_string
.empty())
169 if (test_string
== "QUIT")
172 bool enable_pixel_dumps
;
173 std::string pixel_hash
;
175 GURL test_url
= GetURLForLayoutTest(
176 test_string
, &cwd
, &enable_pixel_dumps
, &pixel_hash
);
177 if (!content::WebKitTestController::Get()->PrepareForLayoutTest(
178 test_url
, cwd
, enable_pixel_dumps
, pixel_hash
)) {
182 ran_at_least_once
= true;
183 #if defined(OS_ANDROID)
184 // The message loop on Android is provided by the system, and does not
185 // offer a blocking Run() method. For layout tests, use a nested loop
186 // together with a base::RunLoop so it can block until a QuitClosure.
187 base::RunLoop run_loop
;
193 if (!content::WebKitTestController::Get()->ResetAfterLayoutTest())
196 #if defined(OS_ANDROID)
197 // There will be left-over tasks in the queue for Android because the
198 // main window is being destroyed. Run them before starting the next test.
199 base::MessageLoop::current()->RunUntilIdle();
202 if (!ran_at_least_once
) {
203 base::MessageLoop::current()->PostTask(FROM_HERE
,
204 base::MessageLoop::QuitClosure());
208 #if defined(OS_ANDROID)
209 // Android should only execute Shutdown() here when running layout tests.
210 main_runner
->Shutdown();
216 #if !defined(OS_ANDROID)
217 if (!layout_test_mode
)
218 exit_code
= main_runner
->Run();
220 main_runner
->Shutdown();