Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / chrome / test / automation / proxy_launcher.cc
blob3871966ee8b51f27312a3e3737e8009b7bdfab97
1 // Copyright (c) 2012 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 "chrome/test/automation/proxy_launcher.h"
7 #include <vector>
9 #include "base/environment.h"
10 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/process/kill.h"
13 #include "base/process/launch.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/test/test_file_util.h"
19 #include "base/test/test_timeouts.h"
20 #include "chrome/app/chrome_command_ids.h"
21 #include "chrome/common/automation_constants.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/logging_chrome.h"
25 #include "chrome/common/url_constants.h"
26 #include "chrome/test/automation/automation_proxy.h"
27 #include "chrome/test/base/chrome_process_util.h"
28 #include "chrome/test/base/test_launcher_utils.h"
29 #include "chrome/test/base/test_switches.h"
30 #include "chrome/test/ui/ui_test.h"
31 #include "content/public/common/process_type.h"
32 #include "ipc/ipc_channel.h"
33 #include "ipc/ipc_descriptors.h"
34 #include "sql/connection.h"
36 #if defined(OS_POSIX)
37 #include <signal.h>
38 #endif
40 namespace {
42 // Passed as value of kTestType.
43 const char kUITestType[] = "ui";
45 // Copies the contents of the given source directory to the given dest
46 // directory. This is somewhat different than CopyDirectory in base which will
47 // copies "source/" to "dest/source/". This version will copy "source/*" to
48 // "dest/*", overwriting existing files as necessary.
50 // This also kicks the files out of the memory cache for the startup tests.
51 // TODO(brettw) bug 237904: This is the wrong place for this code. It means all
52 // startup tests other than the "cold" ones run more slowly than necessary.
53 bool CopyDirectoryContentsNoCache(const base::FilePath& source,
54 const base::FilePath& dest) {
55 base::FileEnumerator en(source, false,
56 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
57 for (base::FilePath cur = en.Next(); !cur.empty(); cur = en.Next()) {
58 base::FileEnumerator::FileInfo info = en.GetInfo();
59 if (info.IsDirectory()) {
60 if (!base::CopyDirectory(cur, dest, true))
61 return false;
62 } else {
63 if (!base::CopyFile(cur, dest.Append(cur.BaseName())))
64 return false;
68 // Kick out the profile files, this must happen after SetUp which creates the
69 // profile. It might be nicer to use EvictFileFromSystemCacheWrapper from
70 // UITest which will retry on failure.
71 base::FileEnumerator kickout(dest, true, base::FileEnumerator::FILES);
72 for (base::FilePath cur = kickout.Next(); !cur.empty(); cur = kickout.Next())
73 base::EvictFileFromSystemCacheWithRetry(cur);
74 return true;
77 // We want to have a current history database when we start the browser so
78 // things like the NTP will have thumbnails. This method updates the dates
79 // in the history to be more recent.
80 void UpdateHistoryDates(const base::FilePath& user_data_dir) {
81 // Migrate the times in the segment_usage table to yesterday so we get
82 // actual thumbnails on the NTP.
83 sql::Connection db;
84 base::FilePath history =
85 user_data_dir.AppendASCII("Default").AppendASCII("History");
86 // Not all test profiles have a history file.
87 if (!base::PathExists(history))
88 return;
90 ASSERT_TRUE(db.Open(history));
91 base::Time yesterday = base::Time::Now() - base::TimeDelta::FromDays(1);
92 std::string yesterday_str = base::Int64ToString(yesterday.ToInternalValue());
93 std::string query = base::StringPrintf(
94 "UPDATE segment_usage "
95 "SET time_slot = %s "
96 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);",
97 yesterday_str.c_str());
98 ASSERT_TRUE(db.Execute(query.c_str()));
99 db.Close();
100 file_util::EvictFileFromSystemCache(history);
103 } // namespace
105 // ProxyLauncher functions
107 #if defined(OS_WIN)
108 const char ProxyLauncher::kDefaultInterfaceId[] = "ChromeTestingInterface";
109 #elif defined(OS_POSIX)
110 const char ProxyLauncher::kDefaultInterfaceId[] =
111 "/var/tmp/ChromeTestingInterface";
112 #endif
114 ProxyLauncher::ProxyLauncher()
115 : process_(base::kNullProcessHandle),
116 process_id_(-1),
117 shutdown_type_(WINDOW_CLOSE),
118 no_sandbox_(CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kNoSandbox)),
120 full_memory_dump_(CommandLine::ForCurrentProcess()->HasSwitch(
121 switches::kFullMemoryCrashReport)),
122 show_error_dialogs_(CommandLine::ForCurrentProcess()->HasSwitch(
123 switches::kEnableErrorDialogs)),
124 enable_dcheck_(CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kEnableDCHECK)),
126 silent_dump_on_dcheck_(CommandLine::ForCurrentProcess()->HasSwitch(
127 switches::kSilentDumpOnDCHECK)),
128 disable_breakpad_(CommandLine::ForCurrentProcess()->HasSwitch(
129 switches::kDisableBreakpad)),
130 js_flags_(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
131 switches::kJavaScriptFlags)),
132 log_level_(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
133 switches::kLoggingLevel)) {
136 ProxyLauncher::~ProxyLauncher() {}
138 bool ProxyLauncher::WaitForBrowserLaunch(bool wait_for_initial_loads) {
139 AutomationLaunchResult app_launched = automation_proxy_->WaitForAppLaunch();
140 EXPECT_EQ(AUTOMATION_SUCCESS, app_launched)
141 << "Error while awaiting automation ping from browser process";
142 if (app_launched != AUTOMATION_SUCCESS)
143 return false;
145 if (wait_for_initial_loads) {
146 if (!automation_proxy_->WaitForInitialLoads()) {
147 LOG(ERROR) << "WaitForInitialLoads failed.";
148 return false;
150 } else {
151 #if defined(OS_WIN)
152 // TODO(phajdan.jr): Get rid of this Sleep when logging_chrome_uitest
153 // stops "relying" on it.
154 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
155 #endif
158 return true;
161 bool ProxyLauncher::LaunchBrowserAndServer(const LaunchState& state,
162 bool wait_for_initial_loads) {
163 // Set up IPC testing interface as a server.
164 automation_proxy_.reset(CreateAutomationProxy(
165 TestTimeouts::action_max_timeout()));
167 if (!LaunchBrowser(state))
168 return false;
170 if (!WaitForBrowserLaunch(wait_for_initial_loads))
171 return false;
173 return true;
176 bool ProxyLauncher::ConnectToRunningBrowser(bool wait_for_initial_loads) {
177 // Set up IPC testing interface as a client.
178 automation_proxy_.reset(CreateAutomationProxy(
179 TestTimeouts::action_max_timeout()));
181 return WaitForBrowserLaunch(wait_for_initial_loads);
184 void ProxyLauncher::CloseBrowserAndServer() {
185 QuitBrowser();
187 // Suppress spammy failures that seem to be occurring when running
188 // the UI tests in single-process mode.
189 // TODO(jhughes): figure out why this is necessary at all, and fix it
190 AssertAppNotRunning(
191 base::StringPrintf(
192 "Unable to quit all browser processes. Original PID %d",
193 process_id_));
195 DisconnectFromRunningBrowser();
198 void ProxyLauncher::DisconnectFromRunningBrowser() {
199 automation_proxy_.reset(); // Shut down IPC testing interface.
202 bool ProxyLauncher::LaunchBrowser(const LaunchState& state) {
203 if (state.clear_profile || !temp_profile_dir_.IsValid()) {
204 if (temp_profile_dir_.IsValid() && !temp_profile_dir_.Delete()) {
205 LOG(ERROR) << "Failed to delete temporary directory.";
206 return false;
209 if (!temp_profile_dir_.CreateUniqueTempDir()) {
210 LOG(ERROR) << "Failed to create temporary directory.";
211 return false;
214 if (!test_launcher_utils::OverrideUserDataDir(user_data_dir())) {
215 LOG(ERROR) << "Failed to override user data directory.";
216 return false;
220 if (!state.template_user_data.empty()) {
221 // Recursively copy the template directory to the user_data_dir.
222 if (!CopyDirectoryContentsNoCache(state.template_user_data,
223 user_data_dir())) {
224 LOG(ERROR) << "Failed to copy user data directory template.";
225 return false;
228 // Update the history file to include recent dates.
229 UpdateHistoryDates(user_data_dir());
232 // Optionally do any final setup of the test environment.
233 if (!state.setup_profile_callback.is_null())
234 state.setup_profile_callback.Run();
236 if (!LaunchBrowserHelper(state, true, false, &process_)) {
237 LOG(ERROR) << "LaunchBrowserHelper failed.";
238 return false;
240 process_id_ = base::GetProcId(process_);
242 return true;
245 void ProxyLauncher::QuitBrowser() {
246 // If we have already finished waiting for the browser to exit
247 // (or it hasn't launched at all), there's nothing to do here.
248 if (process_ == base::kNullProcessHandle || !automation_proxy_.get())
249 return;
251 if (SESSION_ENDING == shutdown_type_) {
252 TerminateBrowser();
253 return;
256 base::TimeTicks quit_start = base::TimeTicks::Now();
258 if (WINDOW_CLOSE == shutdown_type_) {
259 int window_count = 0;
260 EXPECT_TRUE(automation()->GetBrowserWindowCount(&window_count));
262 // Synchronously close all but the last browser window. Closing them
263 // one-by-one may help with stability.
264 while (window_count > 1) {
265 scoped_refptr<BrowserProxy> browser_proxy =
266 automation()->GetBrowserWindow(0);
267 EXPECT_TRUE(browser_proxy.get());
268 if (browser_proxy.get()) {
269 EXPECT_TRUE(browser_proxy->RunCommand(IDC_CLOSE_WINDOW));
270 EXPECT_TRUE(automation()->GetBrowserWindowCount(&window_count));
271 } else {
272 break;
276 // Close the last window asynchronously, because the browser may
277 // shutdown faster than it will be able to send a synchronous response
278 // to our message.
279 scoped_refptr<BrowserProxy> browser_proxy =
280 automation()->GetBrowserWindow(0);
281 EXPECT_TRUE(browser_proxy.get());
282 if (browser_proxy.get()) {
283 EXPECT_TRUE(browser_proxy->is_valid());
284 EXPECT_TRUE(browser_proxy->ApplyAccelerator(IDC_CLOSE_WINDOW));
285 browser_proxy = NULL;
287 } else if (USER_QUIT == shutdown_type_) {
288 scoped_refptr<BrowserProxy> browser_proxy =
289 automation()->GetBrowserWindow(0);
290 EXPECT_TRUE(browser_proxy.get());
291 if (browser_proxy.get()) {
292 EXPECT_TRUE(browser_proxy->RunCommandAsync(IDC_EXIT));
294 } else {
295 NOTREACHED() << "Invalid shutdown type " << shutdown_type_;
298 ChromeProcessList processes = GetRunningChromeProcesses(process_id_);
300 // Now, drop the automation IPC channel so that the automation provider in
301 // the browser notices and drops its reference to the browser process.
302 if (automation_proxy_.get())
303 automation_proxy_->Disconnect();
305 // Wait for the browser process to quit. It should quit once all tabs have
306 // been closed.
307 int exit_code = -1;
308 EXPECT_TRUE(WaitForBrowserProcessToQuit(
309 TestTimeouts::action_max_timeout(), &exit_code));
310 EXPECT_EQ(0, exit_code); // Expect a clean shutdown.
312 browser_quit_time_ = base::TimeTicks::Now() - quit_start;
314 // Ensure no child processes are left dangling.
315 TerminateAllChromeProcesses(processes);
318 void ProxyLauncher::TerminateBrowser() {
319 // If we have already finished waiting for the browser to exit
320 // (or it hasn't launched at all), there's nothing to do here.
321 if (process_ == base::kNullProcessHandle || !automation_proxy_.get())
322 return;
324 base::TimeTicks quit_start = base::TimeTicks::Now();
326 #if defined(OS_WIN) && !defined(USE_AURA)
327 scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
328 ASSERT_TRUE(browser.get());
329 ASSERT_TRUE(browser->TerminateSession());
330 #endif // defined(OS_WIN)
332 ChromeProcessList processes = GetRunningChromeProcesses(process_id_);
334 // Now, drop the automation IPC channel so that the automation provider in
335 // the browser notices and drops its reference to the browser process.
336 if (automation_proxy_.get())
337 automation_proxy_->Disconnect();
339 #if defined(OS_POSIX)
340 EXPECT_EQ(kill(process_, SIGTERM), 0);
341 #endif // OS_POSIX
343 int exit_code = -1;
344 EXPECT_TRUE(WaitForBrowserProcessToQuit(
345 TestTimeouts::action_max_timeout(), &exit_code));
346 EXPECT_EQ(0, exit_code); // Expect a clean shutdown.
348 browser_quit_time_ = base::TimeTicks::Now() - quit_start;
350 // Ensure no child processes are left dangling.
351 TerminateAllChromeProcesses(processes);
354 void ProxyLauncher::AssertAppNotRunning(const std::string& error_message) {
355 std::string final_error_message(error_message);
357 ChromeProcessList processes = GetRunningChromeProcesses(process_id_);
358 if (!processes.empty()) {
359 final_error_message += " Leftover PIDs: [";
360 for (ChromeProcessList::const_iterator it = processes.begin();
361 it != processes.end(); ++it) {
362 final_error_message += base::StringPrintf(" %d", *it);
364 final_error_message += " ]";
366 ASSERT_TRUE(processes.empty()) << final_error_message;
369 bool ProxyLauncher::WaitForBrowserProcessToQuit(
370 base::TimeDelta timeout,
371 int* exit_code) {
372 #ifdef WAIT_FOR_DEBUGGER_ON_OPEN
373 timeout = base::TimeDelta::FromSeconds(500);
374 #endif
375 bool success = false;
377 // Only wait for exit if the "browser, please terminate" message had a
378 // chance of making it through.
379 if (!automation_proxy_->channel_disconnected_on_failure())
380 success = base::WaitForExitCodeWithTimeout(process_, exit_code, timeout);
382 base::CloseProcessHandle(process_);
383 process_ = base::kNullProcessHandle;
384 process_id_ = -1;
386 return success;
389 void ProxyLauncher::PrepareTestCommandline(CommandLine* command_line,
390 bool include_testing_id) {
391 // Add any explicit command line flags passed to the process.
392 CommandLine::StringType extra_chrome_flags =
393 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
394 switches::kExtraChromeFlags);
395 if (!extra_chrome_flags.empty()) {
396 // Split by spaces and append to command line.
397 std::vector<CommandLine::StringType> flags;
398 base::SplitStringAlongWhitespace(extra_chrome_flags, &flags);
399 for (size_t i = 0; i < flags.size(); ++i)
400 command_line->AppendArgNative(flags[i]);
403 // Also look for extra flags in environment.
404 scoped_ptr<base::Environment> env(base::Environment::Create());
405 std::string extra_from_env;
406 if (env->GetVar("EXTRA_CHROME_FLAGS", &extra_from_env)) {
407 std::vector<std::string> flags;
408 base::SplitStringAlongWhitespace(extra_from_env, &flags);
409 for (size_t i = 0; i < flags.size(); ++i)
410 command_line->AppendArg(flags[i]);
413 // No default browser check, it would create an info-bar (if we are not the
414 // default browser) that could conflicts with some tests expectations.
415 command_line->AppendSwitch(switches::kNoDefaultBrowserCheck);
417 // This is a UI test.
418 command_line->AppendSwitchASCII(switches::kTestType, kUITestType);
420 // Tell the browser to use a temporary directory just for this test
421 // if it is not already set.
422 if (command_line->GetSwitchValuePath(switches::kUserDataDir).empty()) {
423 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir());
426 if (include_testing_id)
427 command_line->AppendSwitchASCII(switches::kTestingChannelID,
428 PrefixedChannelID());
430 if (!show_error_dialogs_)
431 command_line->AppendSwitch(switches::kNoErrorDialogs);
432 if (no_sandbox_)
433 command_line->AppendSwitch(switches::kNoSandbox);
434 if (full_memory_dump_)
435 command_line->AppendSwitch(switches::kFullMemoryCrashReport);
436 if (enable_dcheck_)
437 command_line->AppendSwitch(switches::kEnableDCHECK);
438 if (silent_dump_on_dcheck_)
439 command_line->AppendSwitch(switches::kSilentDumpOnDCHECK);
440 if (disable_breakpad_)
441 command_line->AppendSwitch(switches::kDisableBreakpad);
443 if (!js_flags_.empty())
444 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags_);
445 if (!log_level_.empty())
446 command_line->AppendSwitchASCII(switches::kLoggingLevel, log_level_);
448 command_line->AppendSwitch(switches::kMetricsRecordingOnly);
450 if (!CommandLine::ForCurrentProcess()->HasSwitch(
451 switches::kEnableErrorDialogs))
452 command_line->AppendSwitch(switches::kEnableLogging);
454 #ifdef WAIT_FOR_DEBUGGER_ON_OPEN
455 command_line->AppendSwitch(switches::kDebugOnStart);
456 #endif
458 // Force the app to always exit when the last browser window is closed.
459 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests);
461 // Allow file:// access on ChromeOS.
462 command_line->AppendSwitch(switches::kAllowFileAccess);
464 // The tests assume that file:// URIs can freely access other file:// URIs.
465 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
468 bool ProxyLauncher::LaunchBrowserHelper(const LaunchState& state,
469 bool main_launch,
470 bool wait,
471 base::ProcessHandle* process) {
472 CommandLine command_line(state.command);
474 // Add command line arguments that should be applied to all UI tests.
475 PrepareTestCommandline(&command_line, state.include_testing_id);
477 // Sometimes one needs to run the browser under a special environment
478 // (e.g. valgrind) without also running the test harness (e.g. python)
479 // under the special environment. Provide a way to wrap the browser
480 // commandline with a special prefix to invoke the special environment.
481 const char* browser_wrapper = getenv("BROWSER_WRAPPER");
482 if (browser_wrapper) {
483 #if defined(OS_WIN)
484 command_line.PrependWrapper(ASCIIToWide(browser_wrapper));
485 #elif defined(OS_POSIX)
486 command_line.PrependWrapper(browser_wrapper);
487 #endif
488 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with "
489 << browser_wrapper;
492 if (main_launch)
493 browser_launch_time_ = base::TimeTicks::Now();
495 base::LaunchOptions options;
496 options.wait = wait;
498 #if defined(OS_WIN)
499 options.start_hidden = !state.show_window;
500 #elif defined(OS_POSIX)
501 int ipcfd = -1;
502 file_util::ScopedFD ipcfd_closer(&ipcfd);
503 base::FileHandleMappingVector fds;
504 if (main_launch && automation_proxy_.get()) {
505 ipcfd = automation_proxy_->channel()->TakeClientFileDescriptor();
506 fds.push_back(std::make_pair(ipcfd, kPrimaryIPCChannel + 3));
507 options.fds_to_remap = &fds;
509 #endif
511 return base::LaunchProcess(command_line, options, process);
514 AutomationProxy* ProxyLauncher::automation() const {
515 EXPECT_TRUE(automation_proxy_.get());
516 return automation_proxy_.get();
519 base::FilePath ProxyLauncher::user_data_dir() const {
520 EXPECT_TRUE(temp_profile_dir_.IsValid());
521 return temp_profile_dir_.path();
524 base::ProcessHandle ProxyLauncher::process() const {
525 return process_;
528 base::ProcessId ProxyLauncher::process_id() const {
529 return process_id_;
532 base::TimeTicks ProxyLauncher::browser_launch_time() const {
533 return browser_launch_time_;
536 base::TimeDelta ProxyLauncher::browser_quit_time() const {
537 return browser_quit_time_;
540 // NamedProxyLauncher functions
542 NamedProxyLauncher::NamedProxyLauncher(const std::string& channel_id,
543 bool launch_browser,
544 bool disconnect_on_failure)
545 : channel_id_(channel_id),
546 launch_browser_(launch_browser),
547 disconnect_on_failure_(disconnect_on_failure) {
550 AutomationProxy* NamedProxyLauncher::CreateAutomationProxy(
551 base::TimeDelta execution_timeout) {
552 AutomationProxy* proxy = new AutomationProxy(execution_timeout,
553 disconnect_on_failure_);
554 proxy->InitializeChannel(channel_id_, true);
555 return proxy;
558 bool NamedProxyLauncher::InitializeConnection(const LaunchState& state,
559 bool wait_for_initial_loads) {
560 if (launch_browser_) {
561 #if defined(OS_POSIX)
562 // Because we are waiting on the existence of the testing file below,
563 // make sure there isn't one already there before browser launch.
564 if (!base::DeleteFile(base::FilePath(channel_id_), false)) {
565 LOG(ERROR) << "Failed to delete " << channel_id_;
566 return false;
568 #endif
570 if (!LaunchBrowser(state)) {
571 LOG(ERROR) << "Failed to LaunchBrowser";
572 return false;
576 // Wait for browser to be ready for connections.
577 bool channel_initialized = false;
578 base::TimeDelta sleep_time = base::TimeDelta::FromMilliseconds(
579 automation::kSleepTime);
580 for (base::TimeDelta wait_time = base::TimeDelta();
581 wait_time < TestTimeouts::action_max_timeout();
582 wait_time += sleep_time) {
583 channel_initialized = IPC::Channel::IsNamedServerInitialized(channel_id_);
584 if (channel_initialized)
585 break;
586 base::PlatformThread::Sleep(sleep_time);
588 if (!channel_initialized) {
589 LOG(ERROR) << "Failed to wait for testing channel presence.";
590 return false;
593 if (!ConnectToRunningBrowser(wait_for_initial_loads)) {
594 LOG(ERROR) << "Failed to ConnectToRunningBrowser";
595 return false;
597 return true;
600 void NamedProxyLauncher::TerminateConnection() {
601 if (launch_browser_)
602 CloseBrowserAndServer();
603 else
604 DisconnectFromRunningBrowser();
607 std::string NamedProxyLauncher::PrefixedChannelID() const {
608 std::string channel_id;
609 channel_id.append(automation::kNamedInterfacePrefix).append(channel_id_);
610 return channel_id;
613 // AnonymousProxyLauncher functions
615 AnonymousProxyLauncher::AnonymousProxyLauncher(bool disconnect_on_failure)
616 : disconnect_on_failure_(disconnect_on_failure) {
617 channel_id_ = AutomationProxy::GenerateChannelID();
620 AutomationProxy* AnonymousProxyLauncher::CreateAutomationProxy(
621 base::TimeDelta execution_timeout) {
622 AutomationProxy* proxy = new AutomationProxy(execution_timeout,
623 disconnect_on_failure_);
624 proxy->InitializeChannel(channel_id_, false);
625 return proxy;
628 bool AnonymousProxyLauncher::InitializeConnection(const LaunchState& state,
629 bool wait_for_initial_loads) {
630 return LaunchBrowserAndServer(state, wait_for_initial_loads);
633 void AnonymousProxyLauncher::TerminateConnection() {
634 CloseBrowserAndServer();
637 std::string AnonymousProxyLauncher::PrefixedChannelID() const {
638 return channel_id_;