Add ICU message format support
[chromium-blink-merge.git] / content / shell / browser / layout_test / layout_test_browser_main.cc
blobec347517324c260f1d80ed684a99959437e615dc
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/location.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "content/public/browser/browser_main_runner.h"
22 #include "content/public/common/url_constants.h"
23 #include "content/shell/browser/blink_test_controller.h"
24 #include "content/shell/browser/shell.h"
25 #include "content/shell/common/shell_switches.h"
26 #include "content/shell/renderer/layout_test/blink_test_helpers.h"
27 #include "net/base/filename_util.h"
29 #if defined(OS_ANDROID)
30 #include "base/run_loop.h"
31 #include "content/shell/browser/layout_test/layout_test_android.h"
32 #endif
34 namespace {
36 GURL GetURLForLayoutTest(const std::string& test_name,
37 base::FilePath* current_working_directory,
38 bool* enable_pixel_dumping,
39 std::string* expected_pixel_hash) {
40 // A test name is formated like file:///path/to/test'--pixel-test'pixelhash
41 std::string path_or_url = test_name;
42 std::string pixel_switch;
43 std::string pixel_hash;
44 std::string::size_type separator_position = path_or_url.find('\'');
45 if (separator_position != std::string::npos) {
46 pixel_switch = path_or_url.substr(separator_position + 1);
47 path_or_url.erase(separator_position);
49 separator_position = pixel_switch.find('\'');
50 if (separator_position != std::string::npos) {
51 pixel_hash = pixel_switch.substr(separator_position + 1);
52 pixel_switch.erase(separator_position);
54 if (enable_pixel_dumping) {
55 *enable_pixel_dumping =
56 (pixel_switch == "--pixel-test" || pixel_switch == "-p");
58 if (expected_pixel_hash)
59 *expected_pixel_hash = pixel_hash;
61 GURL test_url;
62 #if defined(OS_ANDROID)
63 if (content::GetTestUrlForAndroid(path_or_url, &test_url))
64 return test_url;
65 #endif
67 test_url = GURL(path_or_url);
68 if (!(test_url.is_valid() && test_url.has_scheme())) {
69 // We're outside of the message loop here, and this is a test.
70 base::ThreadRestrictions::ScopedAllowIO allow_io;
71 #if defined(OS_WIN)
72 base::FilePath::StringType wide_path_or_url =
73 base::SysNativeMBToWide(path_or_url);
74 base::FilePath local_file(wide_path_or_url);
75 #else
76 base::FilePath local_file(path_or_url);
77 #endif
78 if (!base::PathExists(local_file)) {
79 local_file = content::GetWebKitRootDirFilePath()
80 .Append(FILE_PATH_LITERAL("LayoutTests"))
81 .Append(local_file);
83 test_url = net::FilePathToFileURL(base::MakeAbsoluteFilePath(local_file));
85 base::FilePath local_path;
86 if (current_working_directory) {
87 // We're outside of the message loop here, and this is a test.
88 base::ThreadRestrictions::ScopedAllowIO allow_io;
89 if (net::FileURLToFilePath(test_url, &local_path))
90 *current_working_directory = local_path.DirName();
91 else
92 base::GetCurrentDirectory(current_working_directory);
94 return test_url;
97 bool GetNextTest(const base::CommandLine::StringVector& args,
98 size_t* position,
99 std::string* test) {
100 if (*position >= args.size())
101 return false;
102 if (args[*position] == FILE_PATH_LITERAL("-"))
103 return !!std::getline(std::cin, *test, '\n');
104 #if defined(OS_WIN)
105 *test = base::WideToUTF8(args[(*position)++]);
106 #else
107 *test = args[(*position)++];
108 #endif
109 return true;
112 bool RunOneTest(const std::string& test_string,
113 bool* ran_at_least_once,
114 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
115 if (test_string.empty())
116 return true;
117 if (test_string == "QUIT")
118 return false;
120 bool enable_pixel_dumps;
121 std::string pixel_hash;
122 base::FilePath cwd;
123 GURL test_url =
124 GetURLForLayoutTest(test_string, &cwd, &enable_pixel_dumps, &pixel_hash);
125 if (!content::BlinkTestController::Get()->PrepareForLayoutTest(
126 test_url, cwd, enable_pixel_dumps, pixel_hash)) {
127 return false;
130 *ran_at_least_once = true;
131 #if defined(OS_ANDROID)
132 // The message loop on Android is provided by the system, and does not
133 // offer a blocking Run() method. For layout tests, use a nested loop
134 // together with a base::RunLoop so it can block until a QuitClosure.
135 base::RunLoop run_loop;
136 run_loop.Run();
137 #else
138 main_runner->Run();
139 #endif
141 if (!content::BlinkTestController::Get()->ResetAfterLayoutTest())
142 return false;
144 #if defined(OS_ANDROID)
145 // There will be left-over tasks in the queue for Android because the
146 // main window is being destroyed. Run them before starting the next test.
147 base::MessageLoop::current()->RunUntilIdle();
148 #endif
149 return true;
152 int RunTests(const scoped_ptr<content::BrowserMainRunner>& main_runner) {
153 content::BlinkTestController test_controller;
155 // We're outside of the message loop here, and this is a test.
156 base::ThreadRestrictions::ScopedAllowIO allow_io;
157 base::FilePath temp_path;
158 base::GetTempDir(&temp_path);
159 test_controller.SetTempPath(temp_path);
161 std::string test_string;
162 base::CommandLine::StringVector args =
163 base::CommandLine::ForCurrentProcess()->GetArgs();
164 size_t command_line_position = 0;
165 bool ran_at_least_once = false;
167 std::cout << "#READY\n";
168 std::cout.flush();
170 while (GetNextTest(args, &command_line_position, &test_string)) {
171 if (!RunOneTest(test_string, &ran_at_least_once, main_runner))
172 break;
174 if (!ran_at_least_once) {
175 base::ThreadTaskRunnerHandle::Get()->PostTask(
176 FROM_HERE, base::MessageLoop::QuitClosure());
177 main_runner->Run();
180 #if defined(OS_ANDROID)
181 // We need to execute 'main_runner->Shutdown()' before the test_controller
182 // destructs when running on Android, and after it destructs when running
183 // anywhere else.
184 main_runner->Shutdown();
185 #endif
187 return 0;
190 } // namespace
192 // Main routine for running as the Browser process.
193 int LayoutTestBrowserMain(
194 const content::MainFunctionParams& parameters,
195 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
196 base::ScopedTempDir browser_context_path_for_layout_tests;
198 CHECK(browser_context_path_for_layout_tests.CreateUniqueTempDir());
199 CHECK(!browser_context_path_for_layout_tests.path().MaybeAsASCII().empty());
200 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
201 switches::kContentShellDataPath,
202 browser_context_path_for_layout_tests.path().MaybeAsASCII());
204 #if defined(OS_ANDROID)
205 content::EnsureInitializeForAndroidLayoutTests();
206 #endif
208 int exit_code = main_runner->Initialize(parameters);
209 DCHECK_LT(exit_code, 0)
210 << "BrowserMainRunner::Initialize failed in LayoutTestBrowserMain";
212 if (exit_code >= 0)
213 return exit_code;
215 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
216 switches::kCheckLayoutTestSysDeps)) {
217 base::ThreadTaskRunnerHandle::Get()->PostTask(
218 FROM_HERE, base::MessageLoop::QuitClosure());
219 main_runner->Run();
220 content::Shell::CloseAllWindows();
221 main_runner->Shutdown();
222 return 0;
225 exit_code = RunTests(main_runner);
227 #if !defined(OS_ANDROID)
228 main_runner->Shutdown();
229 #endif
231 return exit_code;