Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / test / automation / proxy_launcher.cc
blobda9eba9bcd07725def4751ebeb323ebd1714e5df
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 #include "base/posix/global_descriptors.h"
39 #endif
41 namespace {
43 // Passed as value of kTestType.
44 const char kUITestType[] = "ui";
46 // Copies the contents of the given source directory to the given dest
47 // directory. This is somewhat different than CopyDirectory in base which will
48 // copies "source/" to "dest/source/". This version will copy "source/*" to
49 // "dest/*", overwriting existing files as necessary.
51 // This also kicks the files out of the memory cache for the startup tests.
52 // TODO(brettw) bug 237904: This is the wrong place for this code. It means all
53 // startup tests other than the "cold" ones run more slowly than necessary.
54 bool CopyDirectoryContentsNoCache(const base::FilePath& source,
55 const base::FilePath& dest) {
56 base::FileEnumerator en(source, false,
57 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES);
58 for (base::FilePath cur = en.Next(); !cur.empty(); cur = en.Next()) {
59 base::FileEnumerator::FileInfo info = en.GetInfo();
60 if (info.IsDirectory()) {
61 if (!base::CopyDirectory(cur, dest, true))
62 return false;
63 } else {
64 if (!base::CopyFile(cur, dest.Append(cur.BaseName())))
65 return false;
69 // Kick out the profile files, this must happen after SetUp which creates the
70 // profile. It might be nicer to use EvictFileFromSystemCacheWrapper from
71 // UITest which will retry on failure.
72 base::FileEnumerator kickout(dest, true, base::FileEnumerator::FILES);
73 for (base::FilePath cur = kickout.Next(); !cur.empty(); cur = kickout.Next())
74 base::EvictFileFromSystemCacheWithRetry(cur);
75 return true;
78 // We want to have a current history database when we start the browser so
79 // things like the NTP will have thumbnails. This method updates the dates
80 // in the history to be more recent.
81 void UpdateHistoryDates(const base::FilePath& user_data_dir) {
82 // Migrate the times in the segment_usage table to yesterday so we get
83 // actual thumbnails on the NTP.
84 sql::Connection db;
85 base::FilePath history =
86 user_data_dir.AppendASCII("Default").AppendASCII("History");
87 // Not all test profiles have a history file.
88 if (!base::PathExists(history))
89 return;
91 ASSERT_TRUE(db.Open(history));
92 base::Time yesterday = base::Time::Now() - base::TimeDelta::FromDays(1);
93 std::string yesterday_str = base::Int64ToString(yesterday.ToInternalValue());
94 std::string query = base::StringPrintf(
95 "UPDATE segment_usage "
96 "SET time_slot = %s "
97 "WHERE id IN (SELECT id FROM segment_usage WHERE time_slot > 0);",
98 yesterday_str.c_str());
99 ASSERT_TRUE(db.Execute(query.c_str()));
100 db.Close();
101 file_util::EvictFileFromSystemCache(history);
104 } // namespace
106 // ProxyLauncher functions
108 #if defined(OS_WIN)
109 const char ProxyLauncher::kDefaultInterfaceId[] = "ChromeTestingInterface";
110 #elif defined(OS_POSIX)
111 const char ProxyLauncher::kDefaultInterfaceId[] =
112 "/var/tmp/ChromeTestingInterface";
113 #endif
115 ProxyLauncher::ProxyLauncher()
116 : process_(base::kNullProcessHandle),
117 process_id_(-1),
118 shutdown_type_(WINDOW_CLOSE),
119 no_sandbox_(CommandLine::ForCurrentProcess()->HasSwitch(
120 switches::kNoSandbox)),
121 full_memory_dump_(CommandLine::ForCurrentProcess()->HasSwitch(
122 switches::kFullMemoryCrashReport)),
123 show_error_dialogs_(CommandLine::ForCurrentProcess()->HasSwitch(
124 switches::kEnableErrorDialogs)),
125 enable_dcheck_(CommandLine::ForCurrentProcess()->HasSwitch(
126 switches::kEnableDCHECK)),
127 silent_dump_on_dcheck_(CommandLine::ForCurrentProcess()->HasSwitch(
128 switches::kSilentDumpOnDCHECK)),
129 disable_breakpad_(CommandLine::ForCurrentProcess()->HasSwitch(
130 switches::kDisableBreakpad)),
131 js_flags_(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
132 switches::kJavaScriptFlags)),
133 log_level_(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
134 switches::kLoggingLevel)) {
137 ProxyLauncher::~ProxyLauncher() {}
139 bool ProxyLauncher::WaitForBrowserLaunch(bool wait_for_initial_loads) {
140 AutomationLaunchResult app_launched = automation_proxy_->WaitForAppLaunch();
141 EXPECT_EQ(AUTOMATION_SUCCESS, app_launched)
142 << "Error while awaiting automation ping from browser process";
143 if (app_launched != AUTOMATION_SUCCESS)
144 return false;
146 if (wait_for_initial_loads) {
147 if (!automation_proxy_->WaitForInitialLoads()) {
148 LOG(ERROR) << "WaitForInitialLoads failed.";
149 return false;
151 } else {
152 #if defined(OS_WIN)
153 // TODO(phajdan.jr): Get rid of this Sleep when logging_chrome_uitest
154 // stops "relying" on it.
155 base::PlatformThread::Sleep(TestTimeouts::action_timeout());
156 #endif
159 return true;
162 bool ProxyLauncher::LaunchBrowserAndServer(const LaunchState& state,
163 bool wait_for_initial_loads) {
164 // Set up IPC testing interface as a server.
165 automation_proxy_.reset(CreateAutomationProxy(
166 TestTimeouts::action_max_timeout()));
168 if (!LaunchBrowser(state))
169 return false;
171 if (!WaitForBrowserLaunch(wait_for_initial_loads))
172 return false;
174 return true;
177 bool ProxyLauncher::ConnectToRunningBrowser(bool wait_for_initial_loads) {
178 // Set up IPC testing interface as a client.
179 automation_proxy_.reset(CreateAutomationProxy(
180 TestTimeouts::action_max_timeout()));
182 return WaitForBrowserLaunch(wait_for_initial_loads);
185 void ProxyLauncher::CloseBrowserAndServer() {
186 QuitBrowser();
188 // Suppress spammy failures that seem to be occurring when running
189 // the UI tests in single-process mode.
190 // TODO(jhughes): figure out why this is necessary at all, and fix it
191 AssertAppNotRunning(
192 base::StringPrintf(
193 "Unable to quit all browser processes. Original PID %d",
194 process_id_));
196 DisconnectFromRunningBrowser();
199 void ProxyLauncher::DisconnectFromRunningBrowser() {
200 automation_proxy_.reset(); // Shut down IPC testing interface.
203 bool ProxyLauncher::LaunchBrowser(const LaunchState& state) {
204 if (state.clear_profile || !temp_profile_dir_.IsValid()) {
205 if (temp_profile_dir_.IsValid() && !temp_profile_dir_.Delete()) {
206 LOG(ERROR) << "Failed to delete temporary directory.";
207 return false;
210 if (!temp_profile_dir_.CreateUniqueTempDir()) {
211 LOG(ERROR) << "Failed to create temporary directory.";
212 return false;
215 if (!test_launcher_utils::OverrideUserDataDir(user_data_dir())) {
216 LOG(ERROR) << "Failed to override user data directory.";
217 return false;
221 if (!state.template_user_data.empty()) {
222 // Recursively copy the template directory to the user_data_dir.
223 if (!CopyDirectoryContentsNoCache(state.template_user_data,
224 user_data_dir())) {
225 LOG(ERROR) << "Failed to copy user data directory template.";
226 return false;
229 // Update the history file to include recent dates.
230 UpdateHistoryDates(user_data_dir());
233 // Optionally do any final setup of the test environment.
234 if (!state.setup_profile_callback.is_null())
235 state.setup_profile_callback.Run();
237 if (!LaunchBrowserHelper(state, true, false, &process_)) {
238 LOG(ERROR) << "LaunchBrowserHelper failed.";
239 return false;
241 process_id_ = base::GetProcId(process_);
243 return true;
246 void ProxyLauncher::QuitBrowser() {
247 // If we have already finished waiting for the browser to exit
248 // (or it hasn't launched at all), there's nothing to do here.
249 if (process_ == base::kNullProcessHandle || !automation_proxy_.get())
250 return;
252 if (SESSION_ENDING == shutdown_type_) {
253 TerminateBrowser();
254 return;
257 base::TimeTicks quit_start = base::TimeTicks::Now();
259 if (WINDOW_CLOSE == shutdown_type_) {
260 int window_count = 0;
261 EXPECT_TRUE(automation()->GetBrowserWindowCount(&window_count));
263 // Synchronously close all but the last browser window. Closing them
264 // one-by-one may help with stability.
265 while (window_count > 1) {
266 scoped_refptr<BrowserProxy> browser_proxy =
267 automation()->GetBrowserWindow(0);
268 EXPECT_TRUE(browser_proxy.get());
269 if (browser_proxy.get()) {
270 EXPECT_TRUE(browser_proxy->RunCommand(IDC_CLOSE_WINDOW));
271 EXPECT_TRUE(automation()->GetBrowserWindowCount(&window_count));
272 } else {
273 break;
277 // Close the last window asynchronously, because the browser may
278 // shutdown faster than it will be able to send a synchronous response
279 // to our message.
280 scoped_refptr<BrowserProxy> browser_proxy =
281 automation()->GetBrowserWindow(0);
282 EXPECT_TRUE(browser_proxy.get());
283 if (browser_proxy.get()) {
284 EXPECT_TRUE(browser_proxy->is_valid());
285 EXPECT_TRUE(browser_proxy->ApplyAccelerator(IDC_CLOSE_WINDOW));
286 browser_proxy = NULL;
288 } else if (USER_QUIT == shutdown_type_) {
289 scoped_refptr<BrowserProxy> browser_proxy =
290 automation()->GetBrowserWindow(0);
291 EXPECT_TRUE(browser_proxy.get());
292 if (browser_proxy.get()) {
293 EXPECT_TRUE(browser_proxy->RunCommandAsync(IDC_EXIT));
295 } else {
296 NOTREACHED() << "Invalid shutdown type " << shutdown_type_;
299 ChromeProcessList processes = GetRunningChromeProcesses(process_id_);
301 // Now, drop the automation IPC channel so that the automation provider in
302 // the browser notices and drops its reference to the browser process.
303 if (automation_proxy_.get())
304 automation_proxy_->Disconnect();
306 // Wait for the browser process to quit. It should quit once all tabs have
307 // been closed.
308 int exit_code = -1;
309 EXPECT_TRUE(WaitForBrowserProcessToQuit(
310 TestTimeouts::action_max_timeout(), &exit_code));
311 EXPECT_EQ(0, exit_code); // Expect a clean shutdown.
313 browser_quit_time_ = base::TimeTicks::Now() - quit_start;
315 // Ensure no child processes are left dangling.
316 TerminateAllChromeProcesses(processes);
319 void ProxyLauncher::TerminateBrowser() {
320 // If we have already finished waiting for the browser to exit
321 // (or it hasn't launched at all), there's nothing to do here.
322 if (process_ == base::kNullProcessHandle || !automation_proxy_.get())
323 return;
325 base::TimeTicks quit_start = base::TimeTicks::Now();
327 ChromeProcessList processes = GetRunningChromeProcesses(process_id_);
329 // Now, drop the automation IPC channel so that the automation provider in
330 // the browser notices and drops its reference to the browser process.
331 if (automation_proxy_.get())
332 automation_proxy_->Disconnect();
334 #if defined(OS_POSIX)
335 EXPECT_EQ(kill(process_, SIGTERM), 0);
336 #endif // OS_POSIX
338 int exit_code = -1;
339 EXPECT_TRUE(WaitForBrowserProcessToQuit(
340 TestTimeouts::action_max_timeout(), &exit_code));
341 EXPECT_EQ(0, exit_code); // Expect a clean shutdown.
343 browser_quit_time_ = base::TimeTicks::Now() - quit_start;
345 // Ensure no child processes are left dangling.
346 TerminateAllChromeProcesses(processes);
349 void ProxyLauncher::AssertAppNotRunning(const std::string& error_message) {
350 std::string final_error_message(error_message);
352 ChromeProcessList processes = GetRunningChromeProcesses(process_id_);
353 if (!processes.empty()) {
354 final_error_message += " Leftover PIDs: [";
355 for (ChromeProcessList::const_iterator it = processes.begin();
356 it != processes.end(); ++it) {
357 final_error_message += base::StringPrintf(" %d", *it);
359 final_error_message += " ]";
361 ASSERT_TRUE(processes.empty()) << final_error_message;
364 bool ProxyLauncher::WaitForBrowserProcessToQuit(
365 base::TimeDelta timeout,
366 int* exit_code) {
367 #ifdef WAIT_FOR_DEBUGGER_ON_OPEN
368 timeout = base::TimeDelta::FromSeconds(500);
369 #endif
370 bool success = false;
372 // Only wait for exit if the "browser, please terminate" message had a
373 // chance of making it through.
374 if (!automation_proxy_->channel_disconnected_on_failure())
375 success = base::WaitForExitCodeWithTimeout(process_, exit_code, timeout);
377 base::CloseProcessHandle(process_);
378 process_ = base::kNullProcessHandle;
379 process_id_ = -1;
381 return success;
384 void ProxyLauncher::PrepareTestCommandline(CommandLine* command_line,
385 bool include_testing_id) {
386 // Add any explicit command line flags passed to the process.
387 CommandLine::StringType extra_chrome_flags =
388 CommandLine::ForCurrentProcess()->GetSwitchValueNative(
389 switches::kExtraChromeFlags);
390 if (!extra_chrome_flags.empty()) {
391 // Split by spaces and append to command line.
392 std::vector<CommandLine::StringType> flags;
393 base::SplitStringAlongWhitespace(extra_chrome_flags, &flags);
394 for (size_t i = 0; i < flags.size(); ++i)
395 command_line->AppendArgNative(flags[i]);
398 // Also look for extra flags in environment.
399 scoped_ptr<base::Environment> env(base::Environment::Create());
400 std::string extra_from_env;
401 if (env->GetVar("EXTRA_CHROME_FLAGS", &extra_from_env)) {
402 std::vector<std::string> flags;
403 base::SplitStringAlongWhitespace(extra_from_env, &flags);
404 for (size_t i = 0; i < flags.size(); ++i)
405 command_line->AppendArg(flags[i]);
408 // No default browser check, it would create an info-bar (if we are not the
409 // default browser) that could conflicts with some tests expectations.
410 command_line->AppendSwitch(switches::kNoDefaultBrowserCheck);
412 // This is a UI test.
413 command_line->AppendSwitchASCII(switches::kTestType, kUITestType);
415 // Tell the browser to use a temporary directory just for this test
416 // if it is not already set.
417 if (command_line->GetSwitchValuePath(switches::kUserDataDir).empty()) {
418 command_line->AppendSwitchPath(switches::kUserDataDir, user_data_dir());
421 if (include_testing_id)
422 command_line->AppendSwitchASCII(switches::kTestingChannelID,
423 PrefixedChannelID());
425 if (!show_error_dialogs_)
426 command_line->AppendSwitch(switches::kNoErrorDialogs);
427 if (no_sandbox_)
428 command_line->AppendSwitch(switches::kNoSandbox);
429 if (full_memory_dump_)
430 command_line->AppendSwitch(switches::kFullMemoryCrashReport);
431 if (enable_dcheck_)
432 command_line->AppendSwitch(switches::kEnableDCHECK);
433 if (silent_dump_on_dcheck_)
434 command_line->AppendSwitch(switches::kSilentDumpOnDCHECK);
435 if (disable_breakpad_)
436 command_line->AppendSwitch(switches::kDisableBreakpad);
438 if (!js_flags_.empty())
439 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, js_flags_);
440 if (!log_level_.empty())
441 command_line->AppendSwitchASCII(switches::kLoggingLevel, log_level_);
443 command_line->AppendSwitch(switches::kMetricsRecordingOnly);
445 if (!CommandLine::ForCurrentProcess()->HasSwitch(
446 switches::kEnableErrorDialogs))
447 command_line->AppendSwitch(switches::kEnableLogging);
449 #ifdef WAIT_FOR_DEBUGGER_ON_OPEN
450 command_line->AppendSwitch(switches::kDebugOnStart);
451 #endif
453 // Force the app to always exit when the last browser window is closed.
454 command_line->AppendSwitch(switches::kDisableZeroBrowsersOpenForTests);
456 // Allow file:// access on ChromeOS.
457 command_line->AppendSwitch(switches::kAllowFileAccess);
459 // The tests assume that file:// URIs can freely access other file:// URIs.
460 command_line->AppendSwitch(switches::kAllowFileAccessFromFiles);
463 bool ProxyLauncher::LaunchBrowserHelper(const LaunchState& state,
464 bool main_launch,
465 bool wait,
466 base::ProcessHandle* process) {
467 CommandLine command_line(state.command);
469 // Add command line arguments that should be applied to all UI tests.
470 PrepareTestCommandline(&command_line, state.include_testing_id);
472 // Sometimes one needs to run the browser under a special environment
473 // (e.g. valgrind) without also running the test harness (e.g. python)
474 // under the special environment. Provide a way to wrap the browser
475 // commandline with a special prefix to invoke the special environment.
476 const char* browser_wrapper = getenv("BROWSER_WRAPPER");
477 if (browser_wrapper) {
478 #if defined(OS_WIN)
479 command_line.PrependWrapper(base::ASCIIToWide(browser_wrapper));
480 #elif defined(OS_POSIX)
481 command_line.PrependWrapper(browser_wrapper);
482 #endif
483 VLOG(1) << "BROWSER_WRAPPER was set, prefixing command_line with "
484 << browser_wrapper;
487 if (main_launch)
488 browser_launch_time_ = base::TimeTicks::Now();
490 base::LaunchOptions options;
491 options.wait = wait;
493 #if defined(OS_WIN)
494 options.start_hidden = !state.show_window;
495 #elif defined(OS_POSIX)
496 int ipcfd = -1;
497 file_util::ScopedFD ipcfd_closer(&ipcfd);
498 base::FileHandleMappingVector fds;
499 if (main_launch && automation_proxy_.get()) {
500 ipcfd = automation_proxy_->channel()->TakeClientFileDescriptor();
501 fds.push_back(std::make_pair(ipcfd,
502 kPrimaryIPCChannel + base::GlobalDescriptors::kBaseDescriptor));
503 options.fds_to_remap = &fds;
505 #endif
507 return base::LaunchProcess(command_line, options, process);
510 AutomationProxy* ProxyLauncher::automation() const {
511 EXPECT_TRUE(automation_proxy_.get());
512 return automation_proxy_.get();
515 base::FilePath ProxyLauncher::user_data_dir() const {
516 EXPECT_TRUE(temp_profile_dir_.IsValid());
517 return temp_profile_dir_.path();
520 base::ProcessHandle ProxyLauncher::process() const {
521 return process_;
524 base::ProcessId ProxyLauncher::process_id() const {
525 return process_id_;
528 base::TimeTicks ProxyLauncher::browser_launch_time() const {
529 return browser_launch_time_;
532 base::TimeDelta ProxyLauncher::browser_quit_time() const {
533 return browser_quit_time_;
536 // NamedProxyLauncher functions
538 NamedProxyLauncher::NamedProxyLauncher(const std::string& channel_id,
539 bool launch_browser,
540 bool disconnect_on_failure)
541 : channel_id_(channel_id),
542 launch_browser_(launch_browser),
543 disconnect_on_failure_(disconnect_on_failure) {
546 AutomationProxy* NamedProxyLauncher::CreateAutomationProxy(
547 base::TimeDelta execution_timeout) {
548 AutomationProxy* proxy = new AutomationProxy(execution_timeout,
549 disconnect_on_failure_);
550 proxy->InitializeChannel(channel_id_, true);
551 return proxy;
554 bool NamedProxyLauncher::InitializeConnection(const LaunchState& state,
555 bool wait_for_initial_loads) {
556 if (launch_browser_) {
557 #if defined(OS_POSIX)
558 // Because we are waiting on the existence of the testing file below,
559 // make sure there isn't one already there before browser launch.
560 if (!base::DeleteFile(base::FilePath(channel_id_), false)) {
561 LOG(ERROR) << "Failed to delete " << channel_id_;
562 return false;
564 #endif
566 if (!LaunchBrowser(state)) {
567 LOG(ERROR) << "Failed to LaunchBrowser";
568 return false;
572 // Wait for browser to be ready for connections.
573 bool channel_initialized = false;
574 base::TimeDelta sleep_time = base::TimeDelta::FromMilliseconds(
575 automation::kSleepTime);
576 for (base::TimeDelta wait_time = base::TimeDelta();
577 wait_time < TestTimeouts::action_max_timeout();
578 wait_time += sleep_time) {
579 channel_initialized = IPC::Channel::IsNamedServerInitialized(channel_id_);
580 if (channel_initialized)
581 break;
582 base::PlatformThread::Sleep(sleep_time);
584 if (!channel_initialized) {
585 LOG(ERROR) << "Failed to wait for testing channel presence.";
586 return false;
589 if (!ConnectToRunningBrowser(wait_for_initial_loads)) {
590 LOG(ERROR) << "Failed to ConnectToRunningBrowser";
591 return false;
593 return true;
596 void NamedProxyLauncher::TerminateConnection() {
597 if (launch_browser_)
598 CloseBrowserAndServer();
599 else
600 DisconnectFromRunningBrowser();
603 std::string NamedProxyLauncher::PrefixedChannelID() const {
604 std::string channel_id;
605 channel_id.append(automation::kNamedInterfacePrefix).append(channel_id_);
606 return channel_id;
609 // AnonymousProxyLauncher functions
611 AnonymousProxyLauncher::AnonymousProxyLauncher(bool disconnect_on_failure)
612 : disconnect_on_failure_(disconnect_on_failure) {
613 channel_id_ = AutomationProxy::GenerateChannelID();
616 AutomationProxy* AnonymousProxyLauncher::CreateAutomationProxy(
617 base::TimeDelta execution_timeout) {
618 AutomationProxy* proxy = new AutomationProxy(execution_timeout,
619 disconnect_on_failure_);
620 proxy->InitializeChannel(channel_id_, false);
621 return proxy;
624 bool AnonymousProxyLauncher::InitializeConnection(const LaunchState& state,
625 bool wait_for_initial_loads) {
626 return LaunchBrowserAndServer(state, wait_for_initial_loads);
629 void AnonymousProxyLauncher::TerminateConnection() {
630 CloseBrowserAndServer();
633 std::string AnonymousProxyLauncher::PrefixedChannelID() const {
634 return channel_id_;