Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / shell / browser / shell_browser_main.cc
blobf8e1c26ad9ec343248e292f5a8ecfbba02b25dd9
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"
7 #include <iostream>
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/public/common/url_constants.h"
21 #include "content/shell/browser/shell.h"
22 #include "content/shell/browser/webkit_test_controller.h"
23 #include "content/shell/common/shell_switches.h"
24 #include "content/shell/common/webkit_test_helpers.h"
25 #include "content/test/webkit_support.h"
26 #include "net/base/net_util.h"
28 #if defined(OS_ANDROID)
29 #include "base/run_loop.h"
30 #include "content/shell/browser/shell_layout_tests_android.h"
31 #endif
33 namespace {
35 GURL GetURLForLayoutTest(const std::string& test_name,
36 base::FilePath* current_working_directory,
37 bool* enable_pixel_dumping,
38 std::string* expected_pixel_hash) {
39 // A test name is formated like file:///path/to/test'--pixel-test'pixelhash
40 std::string path_or_url = test_name;
41 std::string pixel_switch;
42 std::string pixel_hash;
43 std::string::size_type separator_position = path_or_url.find('\'');
44 if (separator_position != std::string::npos) {
45 pixel_switch = path_or_url.substr(separator_position + 1);
46 path_or_url.erase(separator_position);
48 separator_position = pixel_switch.find('\'');
49 if (separator_position != std::string::npos) {
50 pixel_hash = pixel_switch.substr(separator_position + 1);
51 pixel_switch.erase(separator_position);
53 if (enable_pixel_dumping) {
54 *enable_pixel_dumping =
55 (pixel_switch == "--pixel-test" || pixel_switch == "-p");
57 if (expected_pixel_hash)
58 *expected_pixel_hash = pixel_hash;
60 GURL test_url;
61 #if defined(OS_ANDROID)
62 if (content::GetTestUrlForAndroid(path_or_url, &test_url))
63 return test_url;
64 #endif
66 test_url = GURL(path_or_url);
67 if (!(test_url.is_valid() && test_url.has_scheme())) {
68 // We're outside of the message loop here, and this is a test.
69 base::ThreadRestrictions::ScopedAllowIO allow_io;
70 #if defined(OS_WIN)
71 std::wstring wide_path_or_url =
72 base::SysNativeMBToWide(path_or_url);
73 base::FilePath local_file(wide_path_or_url);
74 #else
75 base::FilePath local_file(path_or_url);
76 #endif
77 if (!base::PathExists(local_file)) {
78 local_file = content::GetWebKitRootDirFilePath()
79 .Append(FILE_PATH_LITERAL("LayoutTests")).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 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 = GetURLForLayoutTest(
122 test_string, &cwd, &enable_pixel_dumps, &pixel_hash);
123 if (!content::WebKitTestController::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::WebKitTestController::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 } // namespace
152 // Main routine for running as the Browser process.
153 int ShellBrowserMain(
154 const content::MainFunctionParams& parameters,
155 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
156 bool layout_test_mode =
157 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree);
158 base::ScopedTempDir browser_context_path_for_layout_tests;
160 if (layout_test_mode) {
161 CHECK(browser_context_path_for_layout_tests.CreateUniqueTempDir());
162 CHECK(!browser_context_path_for_layout_tests.path().MaybeAsASCII().empty());
163 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
164 switches::kContentShellDataPath,
165 browser_context_path_for_layout_tests.path().MaybeAsASCII());
167 #if defined(OS_ANDROID)
168 content::EnsureInitializeForAndroidLayoutTests();
169 #endif
172 int exit_code = main_runner->Initialize(parameters);
173 DCHECK_LT(exit_code, 0)
174 << "BrowserMainRunner::Initialize failed in ShellBrowserMain";
176 if (exit_code >= 0)
177 return exit_code;
179 if (CommandLine::ForCurrentProcess()->HasSwitch(
180 switches::kCheckLayoutTestSysDeps)) {
181 base::MessageLoop::current()->PostTask(FROM_HERE,
182 base::MessageLoop::QuitClosure());
183 main_runner->Run();
184 content::Shell::CloseAllWindows();
185 main_runner->Shutdown();
186 return 0;
189 if (layout_test_mode) {
190 content::WebKitTestController test_controller;
192 // We're outside of the message loop here, and this is a test.
193 base::ThreadRestrictions::ScopedAllowIO allow_io;
194 base::FilePath temp_path;
195 base::GetTempDir(&temp_path);
196 test_controller.SetTempPath(temp_path);
198 std::string test_string;
199 CommandLine::StringVector args =
200 CommandLine::ForCurrentProcess()->GetArgs();
201 size_t command_line_position = 0;
202 bool ran_at_least_once = false;
204 std::cout << "#READY\n";
205 std::cout.flush();
207 while (GetNextTest(args, &command_line_position, &test_string)) {
208 if (!RunOneTest(test_string, &ran_at_least_once, main_runner))
209 break;
211 if (!ran_at_least_once) {
212 base::MessageLoop::current()->PostTask(FROM_HERE,
213 base::MessageLoop::QuitClosure());
214 main_runner->Run();
217 #if defined(OS_ANDROID)
218 // Android should only execute Shutdown() here when running layout tests.
219 main_runner->Shutdown();
220 #endif
222 exit_code = 0;
225 #if !defined(OS_ANDROID)
226 if (!layout_test_mode)
227 exit_code = main_runner->Run();
229 main_runner->Shutdown();
230 #endif
232 return exit_code;